Struggling with classes / types

General FreeBASIC programming questions.
paul doe
Posts: 916
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Struggling with classes / types

Postby paul doe » Sep 06, 2018 14:41

dafhi wrote:the animation and shaders demos crash. i redownloaded animation and same.

Ok, let me check this issue.

<checks>

I reuploaded everything (in case I forgot to check some changes and/or bugfixes). Everything works here. Which compiler/compiler switches are you using?

dafhi wrote:i can still look at the source. really dont want to use globals and was wondering if there was another option besides adding stuff to MyGameLoop (cause i dont want to mess up your class with unintentional noobie stuff)

i guess i'll just mess it up

That's exactly the point of the code: you can mess it up all your want, it's there for that specific purpose (learning, fiddling, etc). If you want to learn OOP, as I starting point, I would strongly recommend that you read this. Those principles (they are not called 'rules' because, as you'll soon see, sometimes you just have to break them) will help you understand a great deal of what OOP is about, and why you would want to use it. You can open another thread to discuss this if you want (so as to not pollute this one, unless the OP doesn't mind)
dafhi
Posts: 1241
Joined: Jun 04, 2005 9:51

Re: Struggling with classes / types

Postby dafhi » Sep 06, 2018 16:42

okay, just realized why the two weren't working. i need to write "Chdir exepath" to precede loading of bitmaps

alright I'll check out this SOLID info
badidea
Posts: 1417
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Struggling with classes / types

Postby badidea » Sep 06, 2018 19:17

paul doe wrote:The best advice I could give you is to try other languages (besides BASIC/C/C++ lookalikes). Try ones that are really different from BASIC, like LISP, Clojure, Forth, Ruby, and (why not?), Brainf*uck. Learn how those languages perform computation. It'll do wonders for your thought processes. Learn several paradigms. You don't need to be fluent in those languages/paradigms, just competent enough to allow you to understand the implications of using them. OOP can be a lot of fun, when you get past patterns, principles, practices and all that crap, and you're finally able to appreciate the gem behind the glitter ;)

I know, I am waiting for Object-Oriented Brainf*ck, but then I read this: Goodbye, Object Oriented Programming :-)
Last edited by badidea on Sep 06, 2018 19:28, edited 1 time in total.
paul doe
Posts: 916
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Struggling with classes / types

Postby paul doe » Sep 06, 2018 19:19

dafhi wrote:okay, just realized why the two weren't working. i need to write "Chdir exepath" to precede loading of bitmaps

What it strikes me as strange, is why? I don't need that here, so why would you? Does anybody has the same problem?
dafhi
Posts: 1241
Joined: Jun 04, 2005 9:51

Re: Struggling with classes / types

Postby dafhi » Sep 06, 2018 19:30

ive never seen any1 else mention this issue.
paul doe
Posts: 916
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Struggling with classes / types

Postby paul doe » Sep 06, 2018 19:35

badidea wrote:... but then I read this: Goodbye, Object Oriented Programming :-)

How could I have forgotten Forth? I even implemented a compiler (or two =D) for this one, so it's rather strange (it must be that I've been fairly stressed lately). Now that language is the work of a genius. I always admired LISP -another superlative language- but Forth is, to me, the epitome of good computer language design. How can something so simple, so straightforward, be so utterly powerful? Would you believe me if I told you that, on the old manuals that came with some of the implementations for microcomputers (I had a MSX-2 Turbo as my first computer, and boy do I loved her) there were instructions about how to write a compiler for it, in machine code?

If humanity made any sense, it would have adopted Forth (or indeed, LISP) as the de-facto language instead of abominations like C++ or BASIC; there would be no concept of 'portability': you write most of the code in Forth itself, and only 'port' the core of the language (usually less than 2000 LOC unless you get fancy), which is the only architecture dependent part.

So, sorry for the late rambling, mate, but I absolutely had to recommend you Forth (and, did I mentioned LISP, before?) =D

EDIT: Ohhh! I made a mistake and edited the previous post, instead of publishing a new one. Oh well...
Last edited by paul doe on Sep 07, 2018 0:25, edited 3 times in total.
badidea
Posts: 1417
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Struggling with classes / types

Postby badidea » Sep 06, 2018 19:37

paul doe wrote:
dafhi wrote:okay, just realized why the two weren't working. i need to write "Chdir exepath" to precede loading of bitmaps

What it strikes me as strange, is why? I don't need that here, so why would you? Does anybody has the same problem?

The fb-animation-demo works fine here. Which one is the shaders demo? The black space ships and ugly monsters?
paul doe
Posts: 916
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Struggling with classes / types

Postby paul doe » Sep 06, 2018 20:06

badidea wrote:Which one is the shaders demo? The black space ships and ugly monsters?

The shader demo can be found here. I don't remember if I published it in the forum before. I definitely didn't published the other one ;)
badidea
Posts: 1417
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Struggling with classes / types

Postby badidea » Sep 06, 2018 21:02

fb-shaders-demo does work here for other reasons:

Code: Select all

inc/glextensions.bi(90) error 4:
Duplicated definition, glActiveTexture in 'glBindProc( glActiveTexture )'
inc/glextensions.bi(90) error 316:
Function result assignment outside of the function, found '=' in 'glBindProc( glActiveTexture )'
inc/glextensions.bi(90) error 1:
Argument count mismatch, found '=' in 'glBindProc( glActiveTexture )'

When I comment out glBindProc( glActiveTexture ) in glextensions.bi, I get:
"... failed to compile vertex shader ... GLSL 4.0 is not supported ..."
paul doe
Posts: 916
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Struggling with classes / types

Postby paul doe » Sep 06, 2018 21:25

badidea wrote:When I comment out glBindProc( glActiveTexture ) in glextensions.bi, I get:
"... failed to compile vertex shader ... GLSL 4.0 is not supported ..."

I remember correctly there was something about that here in the forum. What are your GPU specifications? Try with this vertex shader instead (save it with the same name or pass it to the shader loader code):

Code: Select all

#version 330 core
/*
   Input data for the shaders like positions, UVs and other vertex related
   stuff are called 'attributes', and are of course different for each
   instance of the shader
*/
layout( location = 0 ) in vec3 vertexPosition ;
layout( location = 1 ) in vec2 vertexUV ;

// the UV coordinates are forwarded to the fragment shader
out vec2 UV ;

/*
   Uniforms stay constant across all shader instances, and are read-only by definition.
   Think of them as 'global variables' that can be accessed by each instance
*/
uniform mat3 projection ;
uniform mat3 rotation ;
uniform mat3 scale ;

void main()
{         
   gl_Position.xyz = projection * rotation * vertexPosition ;
   gl_Position.w = 1.0 ;
   
   UV = vertexUV ;
}
Last edited by paul doe on Sep 06, 2018 23:30, edited 1 time in total.
dafhi
Posts: 1241
Joined: Jun 04, 2005 9:51

Re: Struggling with classes / types

Postby dafhi » Sep 06, 2018 21:42

Here's my "starfield." I need to optimize the draw so it only updates the edges. This uses my pm_wave instead of RNG.

Code: Select all

type myint as integer
type float as single

#Ifndef floor   '' http://www.freebasic.net/forum/viewtopic.php?p=118633
#Define floor(x) (((x)*2.0-0.5)shr 1)
#define ceil(x) (-((-(x)*2.0-0.5)shr 1))
  #EndIf

#ifndef pi
const   TwoPi = 8*atn(1)
const   Pi = 4*atn(1)
  #EndIf

function modu(in as double, m as double=1) as double
  return in - m * floor(in / m)
End Function

#define wfor(v, initial, cond, ste)  v=initial-ste: while cond: v+=ste

type imvars32
   
    /' -- a class created for
      .seamless screen & image interaction
      .encapsulate standard metrics
      .convenient additional vars, subs and functions
      .quick reference for ScreenInfo & ImageInfo
     
    -- 2018 Sep 5, by dafhi - '/
   
    as myint              w,h,bpp,bypp,pitch
    as myint              rate, num_pages, flags
    as any ptr            im, pixels, p_page(Any) '2018 Sep 5
    as string             driver_name
   
    declare constructor   (as any ptr=0)
    declare destructor
   
    declare property      page(as myint)
    declare sub           get_info(as any ptr=0)
    as single             wh, hh, diagonal, scale 'helpers
    as myint              wm, hm, pitchBy, ub     '
    as ulong ptr          p32                     '
   private:
    declare sub           release
end type
Destructor.imvars32:  release
End Destructor

property imvars32.page(i as myint)
    p32 = p_page(i):  pixels = p32
End Property

Sub imvars32.release
  If ImageInfo(im) = 0 Then ImageDestroy im
  im = 0
End Sub

constructor.imvars32(im as any ptr) ' 2018 Aug 22
  if im=0 then exit constructor
  if ImageInfo(im) = 0 then get_info im
end constructor

sub imvars32.get_info(im as any ptr)
    release
    if im=0 then:  pixels=screenptr
      ScreenInfo w,h, bpp,num_pages, pitch, rate, driver_name:  bypp=bpp\8 '2018 Jan 9
    elseif Imageinfo(im)=0 then
      ImageInfo im, w, h, bypp, pitch, pixels
      bpp = bypp * 8:  this.im = im
    endif:  pitchBy=pitch\bypp:  p32=pixels:  ub = w*h-1
    wm=w-1: wh=w/2
    hm=h-1: hh=h/2:  diagonal = sqr(wm*wm+hm*hm)
    scale = diagonal / sqr(.5) / 2
    redim p_page(num_pages-1)                      '2018 Sep 5
    for i as integer=0 to num_pages-1
      ScreenSet i:  p_page(i) = ScreenPtr
    next: if num_Pages > 1 then screenset 0,1 'work_page, visible_page
end sub

' -----------

function rotr32(x as ulong, r as ulong) as ulong
  return (x shr r) or (x shl (-r and 31))
end function

type tRCSG_32
   
    /' -- reversible Complex Sequence Generator - 2018 Sep 4 - by dafhi
 
        32 bit sequence generator similar to LCG.
        The difference is a bit rotation before the add.
       
        Wanted to make a reversible RNG for large textures,
        now I have one :D
     
      usage:
     
      dim as tRCSG_32 rng
     
      ? rng '' or rng.f(rng.state)
      ? rng.r(rng.state)
     
    '/
   
    as long           state
   
    declare operator  cast as ulong
    declare function  r(as long=0)as ulong
   
    declare property  mul(as long)
   
    as long           mul_  = 5 ' https://www.johndcook.com/blog/2017/07/05/simple-random-number-generator/
    as long           ro    = 10
    as long           add   = 2
   
    declare constructor
    declare function  f(as long=0)as ulong
    declare function  modinv(u as integer, v as ulongint)as uinteger
    as long           invMul
End Type

constructor tRCSG_32
    mul = 3
End Constructor

function tRCSG_32.modinv(u as integer, v as ulongint)as uinteger
    '
    ' https://www.di-mgt.com.au/euclidean.html#extendedeuclidean
   
    dim as longint inv, u1, u3, v1, v3, t1, t3, q
    dim as integer iter
    u1 = 1
    u3 = u
    v1 = 0
    v3 = v
    iter = 1
    while (v3 <> 0)
        q = u3 \ v3 '' changed to truncate divide
        t3 = u3 mod v3
        t1 = u1 + q * v1
        u1 = v1: v1 = t1: u3 = v3: v3 = t3
        iter = -iter
    wend
    if (u3 <> 1)then return 0
    if (iter < 0)then return v - u1
    return u1
end function

property tRCSG_32.mul(in as long)
    invMul = modinv(in, 2^32)
    mul_ = in
End Property

function tRCSG_32.r(i as long) as ulong
    i = (i * invMul - add)
    i = rotr32(i, 32-ro)
    state = i:  return i
End function

function tRCSG_32.f(i as long) as ulong
    i = rotr32(i, ro)
    state = (i + add) * mul_
    return state
End function

operator tRCSG_32.cast as ulong
    return f(state)
End Operator

' --------------

function tri_wave(i as float, m as float = 4)as float
    i = modu(i, m)
    if i > .75*m then
      return i - m
    elseif i < m/4 then
      return i
    EndIf:  return m/2-i
End Function

function pm_wave(i as float, i_scalar as float = 1, m_offset as float = 0, m as float = 3, mod_static as float = 4)as float
    m += tri_wave(i * i_scalar + m_offset, mod_static)
    return tri_wave(i, m) / m
End Function


/' -- [under construction] infinite 2d starfield - by dafhi -- '/

type sf_glide_xy
   
    declare property        size(as myint)
   
    declare sub             calc(as float)
   
    as float                offset
 
    'declare function        pixel(as myint)as myint
 
    '' complex sequence gen ''
    declare property        mul(as myint)
    declare property        add(as myint)
    declare property        ror(as myint) 
   
    as ulong                valu(any)
    as tRCSG_32             csg
  private:
    as ulong                vals(any)
    declare sub             fill()
    as myint                u, c, delta, i, j, j_end, ii, ii_prev
    as myint                block, block_p, state
End Type

property sf_glide_xy.size(in as myint)
    redim valu(in - 1)
    c = in
    u = in \ 32
    redim vals(u)
    i = 0
    block_p = 0
    calc 1
End Property

property sf_glide_xy.mul(i as myint)
    csg.mul = i
End Property
property sf_glide_xy.add(i as myint)
    csg.add = i
End Property
property sf_glide_xy.ror(i as myint)
    csg.ro = i
End Property

'function sf_glide_xy.pixel(as myint)as myint
 
'End Function

sub sf_glide_xy.fill
    /'
    state = csg.state
    for i as myint = 0 to u
      vals(i) = csg
    Next
    csg.state = state
 
    for j as myint = i to i + ubound(valu)
      valu(j - i) = (( vals(j\32) shr (j and 31) ))and 1
    Next
    '/
    for j as myint = ii to ii + ubound(valu)
      valu(j-ii) = pm_wave(j, .1, offset, .5, 2) + .5
    Next
End Sub

sub sf_glide_xy.calc(f as float)
 
    #define completion _' wend: _
      fill: ii_prev = ii: block_p = block
   
    ii = floor(f+.5)
    var st = sgn(ii - ii_prev)
   
    i = ii mod 32
    if st > 0 then
      block = ii \ 32:  j_end = block-st
      'wfor(j, block_p, j<j_end, st): _
      '  csg.state = csg.f(csg.state): _
      completion
    elseif st < 0 then
      block = ii \ 32:  j_end = block-st
      'wfor(j, block_p, j>j_end, st): _
      '  csg.state = csg.r(csg.state): _
      completion
    EndIf

End Sub

type starfield
   
    declare sub         render_target(byref as imvars32)
    declare sub         draw(as float=0, as float=0, as float=1)
    const               COLOR_OFF = rgb(0,0,255)
    const               COLOR_ON  = rgb(230,230,220)

  private:
    as myint            w, h
    as sf_glide_xy      x, y
    as imvars32 ptr    pim
End Type

sub starfield.render_target(byref imv as imvars32)
    pim = @imv
    x.offset = 12000
    y.add = 23456
    'x.offset = 1000
    y.offset = -100
End Sub

sub starfield.draw(xf as float, yf as float, _
                   scale as float) '' future implementatino
                   
    if pim=0 orelse pim->driver_name = "" then exit sub
   
    if w<>pim->w then w=pim->w: x.size = w: w=pim->w
    if h<>pim->h then h=pim->h: y.size = h: h=pim->h
    var ixf = floor(xf+.5)
    var iyf = floor(yf+.5)
    scale = .05
    for iy as myint = 0 to pim->hm
      dim as ulong yy = pm_wave(iyf + iy, scale, y.offset, .25, 2) + .5
      y.csg.add = x.csg.f(iyf + iy + yy * 4) / 5000
      var p = @pim->p32[iy*pim->pitchBy]
      for ix as myint = 0 to pim->wm
        dim as ulong xx = pm_wave(ixf + ix + y.csg.add, scale, x.offset, .25, 2) + .5
        p[ix] = iif(xx, COLOR_ON, COLOR_OFF)
      Next
    Next
End Sub


type tAnimFPS     ' framerate limiter
    as float            ifps = 1 / 60
    as float            fps_report
    as double           t, tNext, tNext_report
    as double           tp, dt, t0
    as myint            frame, ret
    declare function    update(as double=0) as boolean ' 2018 Aug 29
    declare constructor
   private:
End Type
constructor.tAnimFPS
    t=timer: tp=t: tNext=t: tNext_report = t: t0=t
End Constructor
function tAnimFPS.update(_t as double) as boolean ' 2018 Aug 29
    tp = t:  if _t=0 then _t=timer
    t=_t: dt=t-tp:  ret=false
    if t >= tNext_report then fps_report=frame: tNext_report = t+1: frame=0
    if t >= tNext then while tNext<t:tnext+=ifps:wend: frame+=1: ret = true
    return ret
End function


' -- sinewave vars / helper
'
type sinevars
  as single             a=rnd*twopi, ia = .0003 * (.05 + rnd)
  as single             ret, bas = 0.5, scale = .5
  declare operator      cast as single
  declare operator      cast as string
end type
operator sinevars.cast as single
  a -= twopi*int(a/twopi)
  ret = bas + scale * sin(a):  return ret
end operator
operator sinevars.cast as string
  return str(csng(this))
End Operator


sub Main
 
    dim as tAnimFps   anim
    anim.ifps = 1/41
   
   
    dim as imvars32   buf
    dim as starfield  sf
    sf.render_target buf
   
    screenres 640,480,32,2
    buf.get_info

    dim as sinevars   x, y
    y.ia = 1.5
    x.ia = .002
    x.scale = buf.w * 1

    var               iphys_fps = 1/67, phys_t = 0f
   
    var               page = 1, kstr = ""
   
    while kstr = ""
     
      if anim.update then
        cls
        buf.page = page
        sf.draw x, y.a
        ? str(x.a), y.a
        ? "fps: "; anim.fps_report
        Screenset 1 - page, page
        page = 1 - page
      endif
     
      kstr = inkey
      sleep 1
     
      phys_t += anim.dt

      var frames=0
      while phys_t>0
        frames += 1
        phys_t -= iphys_fps
      wend
      var speed = frames * 1 / 2
      x.a += x.ia * speed
      y.a += y.ia * speed * 1
     
    WEND
 
END SUB

Main

[edit] Just noticed that -O 3 changes the pattern
paul doe
Posts: 916
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Struggling with classes / types

Postby paul doe » Sep 07, 2018 0:08

dafhi wrote:Here's my "starfield."

Nice. The reversible PRNG is particularly useful. Did you tested how it fares? Ask deltarho[1859] about this one ;)
dafhi
Posts: 1241
Joined: Jun 04, 2005 9:51

Re: Struggling with classes / types

Postby dafhi » Sep 07, 2018 0:46

Glad you like it! :D It's basically an LCG. I imagine that the rotate doesn't increase the period but don't quote me on that

Return to “General”

Who is online

Users browsing this forum: No registered users and 15 guests