randomly generated graphics demo

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

randomly generated graphics demo

Post by DamageX »

I came up with this algorithm a while back when I was thinking about possibilities for the 4KB demo category. Of course, FB executable itself is much too big. I could have made it a Sega Saturn or DOS program... but I never did.

Most of the parameters are randomly chosen but some are tunable by hand within the source code.

Code: Select all

dim as integer ptr scrp,framep
dim as integer x,y,z,zz,h,v,c,x2,y2,f1,f2,f3
dim as integer x3,y3,xt,yt
dim as integer xx,xy,yx,yy,xx2,xy2,yx2,yy2
dim as integer xxx,yxx,xxy,xyy,yxy,yyy
dim as integer xxxt,yxxt,xxyt,xyyt,yxyt,yyyt

f3=2
f2=0
f1=9

screen 18,32
dim shared framebuf(0 to 479,0 to 639) as integer

randomize

do

for zz=1 to 4

xxxt=int(rnd*128)-64
yxxt=int(rnd*128)-64
xxyt=int(rnd*128)-64
xyyt=int(rnd*128)-64
yxyt=int(rnd*128)-64
yyyt=int(rnd*128)-64
xt=int(rnd*16)-8
yt=int(rnd*16)-8

for z=1 to 64

if inkey$="q" then end

x=0:y=0
xx=256:xy=0:yx=0:yy=256

for v=0 to 239
x2=x:y2=y
xx2=xx:yx2=yx

for h=0 to 255

c=(((y2+y3) and 65280) shl 8)+(((x2+x3) shr 8) and 255)

' to clip or not to clip?? ;)
'if y2<0 then c=0
if y2>65535 then c=0
'if x2<0 then c=0
if x2>65535 then c=0

framebuf(240+v,320+h)=c
framebuf(240+v,319-h)=c
framebuf(239-v,320+h)=c
framebuf(239-v,319-h)=c

x2+=(xx2 shr f1)
y2+=(yx2 shr f1)
xx2+=(xxx shr f2)
yx2+=(yxx shr f2)

next h

x+=(xy shr f1)
y+=(yy shr f1)
xx+=(xxy shr f2)
xy+=(xyy shr f2)
yx+=(yxy shr f2)
yy+=(yyy shr f2)

next v

' copy framebuffer to screen
scrp=screenptr:framep=@framebuf(0,0)
screenlock
for h=0 to 307199:*scrp=*framep:scrp+=1:framep+=1:next h
screenunlock

sleep 16

xxx+=xxxt
yxx+=yxxt
xxy+=xxyt
xyy+=xyyt
yxy+=yxyt
yyy+=yyyt
x3+=xt
y3+=yt

next z

next zz

xxx=(xxx shr f3)
yxx=(yxx shr f3)
xxy=(xxy shr f3)
xyy=(xyy shr f3)
yxy=(yxy shr f3)
yyy=(yyy shr f3)

loop
David Watson
Posts: 56
Joined: May 15, 2013 16:48
Location: England

Re: randomly generated graphics demo

Post by David Watson »

I got a seg fault initially on my x86-64 linux system.
Changing all the dim as integer lines to dim as long fixed it.
After that it looked pretty good.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: randomly generated graphics demo

Post by D.J.Peters »

Yes the problem are ULONG (32-bit color) vs INTEGER 32/64-bit
here are the 64-bit version.
[q]=quit

Joshy

Code: Select all

dim shared as ulong framebuf(0 to 479,0 to 639)
dim as ulong ptr scrp,framep
dim as ulong c

dim as integer x,y,z,zz,h,v,x2,y2,f1,f2,f3
dim as integer x3,y3,xt,yt
dim as integer xx,xy,yx,yy,xx2,xy2,yx2,yy2
dim as integer xxx,yxx,xxy,xyy,yxy,yyy
dim as integer xxxt,yxxt,xxyt,xyyt,yxyt,yyyt

f3=2
f2=0
f1=9

screenres 640,480,32

randomize

do
  for zz=1 to 4
    xxxt=int(rnd*128)-64
    yxxt=int(rnd*128)-64
    xxyt=int(rnd*128)-64
    xyyt=int(rnd*128)-64
    yxyt=int(rnd*128)-64
    yyyt=int(rnd*128)-64
    xt=int(rnd*16)-8
    yt=int(rnd*16)-8
    for z=1 to 64
      if inkey$="q" then end
      x=0:y=0:xx=256:xy=0:yx=0:yy=256
      for v=0 to 239
        x2=x:y2=y:xx2=xx:yx2=yx
        for h=0 to 255
          c=(((y2+y3) and 65280) shl 8)+(((x2+x3) shr 8) and 255)
          ' to clip or not to clip?? ;)
          'if y2<0 then c=0
          if y2>65535 then c=0
          'if x2<0 then c=0
          if x2>65535 then c=0
          framebuf(240+v,320+h)=c
          framebuf(240+v,319-h)=c
          framebuf(239-v,320+h)=c
          framebuf(239-v,319-h)=c
          x2+=(xx2 shr f1)
          y2+=(yx2 shr f1)
          xx2+=(xxx shr f2)
          yx2+=(yxx shr f2)
        next h
        x+=(xy shr f1)
        y+=(yy shr f1)
        xx+=(xxy shr f2)
        xy+=(xyy shr f2)
        yx+=(yxy shr f2)
        yy+=(yyy shr f2)
      next v
      ' copy framebuffer to screen
      screenlock
        scrp=screenptr
        framep=@framebuf(0,0)
        for h=0 to 640*480-1
          scrp[h]=framep[h]
        next h
      screenunlock
      sleep 16
      xxx+=xxxt
      yxx+=yxxt       
      xxy+=xxyt
      xyy+=xyyt
      yxy+=yxyt
      yyy+=yyyt
      x3+=xt
      y3+=yt
    next z
  next zz
  xxx=(xxx shr f3)
  yxx=(yxx shr f3)  
  xxy=(xxy shr f3)
  xyy=(xyy shr f3)  
  yxy=(yxy shr f3)
  yyy=(yyy shr f3)
loop
bplus
Posts: 56
Joined: May 01, 2017 15:57

Re: randomly generated graphics demo

Post by bplus »

DamageX wrote:I came up with this algorithm a while back when I was thinking about possibilities for the 4KB demo category. Of course, FB executable itself is much too big. I could have made it a Sega Saturn or DOS program... but I never did.

Most of the parameters are randomly chosen but some are tunable by hand within the source code.

Code: Select all

dim as integer ptr scrp,framep
dim as integer x,y,z,zz,h,v,c,x2,y2,f1,f2,f3
dim as integer x3,y3,xt,yt
dim as integer xx,xy,yx,yy,xx2,xy2,yx2,yy2
dim as integer xxx,yxx,xxy,xyy,yxy,yyy
dim as integer xxxt,yxxt,xxyt,xyyt,yxyt,yyyt

f3=2
f2=0
f1=9

screen 18,32
dim shared framebuf(0 to 479,0 to 639) as integer

randomize

do

for zz=1 to 4

xxxt=int(rnd*128)-64
yxxt=int(rnd*128)-64
xxyt=int(rnd*128)-64
xyyt=int(rnd*128)-64
yxyt=int(rnd*128)-64
yyyt=int(rnd*128)-64
xt=int(rnd*16)-8
yt=int(rnd*16)-8

for z=1 to 64

if inkey$="q" then end

x=0:y=0
xx=256:xy=0:yx=0:yy=256

for v=0 to 239
x2=x:y2=y
xx2=xx:yx2=yx

for h=0 to 255

c=(((y2+y3) and 65280) shl 8)+(((x2+x3) shr 8) and 255)

' to clip or not to clip?? ;)
'if y2<0 then c=0
if y2>65535 then c=0
'if x2<0 then c=0
if x2>65535 then c=0

framebuf(240+v,320+h)=c
framebuf(240+v,319-h)=c
framebuf(239-v,320+h)=c
framebuf(239-v,319-h)=c

x2+=(xx2 shr f1)
y2+=(yx2 shr f1)
xx2+=(xxx shr f2)
yx2+=(yxx shr f2)

next h

x+=(xy shr f1)
y+=(yy shr f1)
xx+=(xxy shr f2)
xy+=(xyy shr f2)
yx+=(yxy shr f2)
yy+=(yyy shr f2)

next v

' copy framebuffer to screen
scrp=screenptr:framep=@framebuf(0,0)
screenlock
for h=0 to 307199:*scrp=*framep:scrp+=1:framep+=1:next h
screenunlock

sleep 16

xxx+=xxxt
yxx+=yxxt
xxy+=xxyt
xyy+=xyyt
yxy+=yxyt
yyy+=yyyt
x3+=xt
y3+=yt

next z

next zz

xxx=(xxx shr f3)
yxx=(yxx shr f3)
xxy=(xxy shr f3)
xyy=(xyy shr f3)
yxy=(yxy shr f3)
yyy=(yyy shr f3)

loop
No errors flagged but program stopped working. Windows is checking into the problem. Ran in FbEdit default config, Windows 10 laptop.

Append: Oh dang! I thought I had Peters, sorry.
bplus
Posts: 56
Joined: May 01, 2017 15:57

Re: randomly generated graphics demo

Post by bplus »

D.J.Peters wrote:Yes the problem are ULONG (32-bit color) vs INTEGER 32/64-bit
here are the 64-bit version.
[q]=quit

Joshy

Code: Select all

dim shared as ulong framebuf(0 to 479,0 to 639)
dim as ulong ptr scrp,framep
dim as ulong c

dim as integer x,y,z,zz,h,v,x2,y2,f1,f2,f3
dim as integer x3,y3,xt,yt
dim as integer xx,xy,yx,yy,xx2,xy2,yx2,yy2
dim as integer xxx,yxx,xxy,xyy,yxy,yyy
dim as integer xxxt,yxxt,xxyt,xyyt,yxyt,yyyt

f3=2
f2=0
f1=9

screenres 640,480,32

randomize

do
  for zz=1 to 4
    xxxt=int(rnd*128)-64
    yxxt=int(rnd*128)-64
    xxyt=int(rnd*128)-64
    xyyt=int(rnd*128)-64
    yxyt=int(rnd*128)-64
    yyyt=int(rnd*128)-64
    xt=int(rnd*16)-8
    yt=int(rnd*16)-8
    for z=1 to 64
      if inkey$="q" then end
      x=0:y=0:xx=256:xy=0:yx=0:yy=256
      for v=0 to 239
        x2=x:y2=y:xx2=xx:yx2=yx
        for h=0 to 255
          c=(((y2+y3) and 65280) shl 8)+(((x2+x3) shr 8) and 255)
          ' to clip or not to clip?? ;)
          'if y2<0 then c=0
          if y2>65535 then c=0
          'if x2<0 then c=0
          if x2>65535 then c=0
          framebuf(240+v,320+h)=c
          framebuf(240+v,319-h)=c
          framebuf(239-v,320+h)=c
          framebuf(239-v,319-h)=c
          x2+=(xx2 shr f1)
          y2+=(yx2 shr f1)
          xx2+=(xxx shr f2)
          yx2+=(yxx shr f2)
        next h
        x+=(xy shr f1)
        y+=(yy shr f1)
        xx+=(xxy shr f2)
        xy+=(xyy shr f2)
        yx+=(yxy shr f2)
        yy+=(yyy shr f2)
      next v
      ' copy framebuffer to screen
      screenlock
        scrp=screenptr
        framep=@framebuf(0,0)
        for h=0 to 640*480-1
          scrp[h]=framep[h]
        next h
      screenunlock
      sleep 16
      xxx+=xxxt
      yxx+=yxxt       
      xxy+=xxyt
      xyy+=xyyt
      yxy+=yxyt
      yyy+=yyyt
      x3+=xt
      y3+=yt
    next z
  next zz
  xxx=(xxx shr f3)
  yxx=(yxx shr f3)  
  xxy=(xxy shr f3)
  xyy=(xyy shr f3)  
  yxy=(yxy shr f3)
  yyy=(yyy shr f3)
loop
OH COOL! Thanks guys! ;)
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: randomly generated graphics demo

Post by sancho2 »

Cool program.
Its 'Q' to quit in case anyone else didn't realize.
Post Reply