I saw "Life", the latest showroom entry and I thought it was pretty cool.. :good:
http://www.glbasic.com/showroom.php?site=games&game=life&lang=en (http://www.glbasic.com/showroom.php?site=games&game=life&lang=en)
Unfortunately, it always starts with a random pattern, so I've written a little function to read in *.lif files. These are relatively simple files (plain-text) used to describe and exchange Life pattern. You can download these kind of files from many websites, for example here (http://www.radicaleye.com/lifepage/patterns/contents.html). I've also created a glbasic.lif, it display the GLBasic logo. 8) (see attachment)
FUNCTION lifload: filename$
// load and interpret a Game of Life pattern file (*.lif)
LOCAL str$, px$, py$
LOCAL i
LOCAL x%, y%, scrx%, scry%
// clear
FOR y%=0 TO heigt%-1
FOR x%=0 TO width%-1
pl%[x%][y%] = 0
NEXT
NEXT
// get middle of screen
GETSCREENSIZE scrx%, scry%
generatie%=0
// open and read file
IF OPENFILE(1, filename$, 1)
WHILE (ENDOFFILE(1) = FALSE)
// read next line
READLINE 1, str$
str$ = str$ + "\n"
// command
IF (MID$(str$, 0, 1) = "#")
// P = position command, for example "#P -3 -1"
IF (MID$(str$, 1, 1) = "P")
str$ = MID$(str$, 3)
i = INSTR(str$, " ")
IF (i <> -1)
// get positions
px$ = MID$(str$, 0, i)
py$ = MID$(str$, i+1)
// string to int
x% = INTEGER(px$) + (scrx% / 2)
y% = INTEGER(py$) + (scry% / 2)
ENDIF
ENDIF
// TODO: display comment lines on screen? "#D blah bla"
//IF (MID$(str$, 1, 1) = "D")
//ENDIF
ELSE
// cells, for example "*...***"
FOR i = 0 TO LEN(str$)
IF (MID$(str$, i, 1) = "*")
// not out of bounds
IF (x%+i >= 0) AND (y% >= 0) AND (x%+i < scrx%) AND (y% < scry%)
pl%[x%+i][y%] = 1
ENDIF
ENDIF
NEXT //i
y% = y% + 1
ENDIF
WEND
ENDIF
ENDFUNCTION
and in the sourcecode (provided in showroom download) you can replace "GOSUB rrand" with "lifload("glbasic.lif")"
It was written by someone only known as Goos (who is dutch I think, looking at some of the code), no e-mail or anything, so I hope he will read this topic.
[attachment deleted by admin]
Hello BdR,
I was not aware of any .LIF files are out there. But i like the concept and in one evening i trew up this code.
I love to use your lifload code and i made one to read a *.LIF-file from the folder ypu start LIFE from.
I have upgraded then program to the showroom
I am indeed Dutch.
Greetings from Goos :)
Great to hear you liked the lifload function and upgraded the program. :) But I redownloaded the file from the showroom, and I don't know how to load a lif-file with the windows executable (I don't have a GP2x) and the source files seems to be missing too.
Also, all files are in the same folder. I think it would be better to put the font.png in the folder "media" and put all *.lif files in a separate folder.
I am sory my mistake.
I now have updated the folder. It should have the GLBasic program and a GP2X-WIZ and a WIN32 EXE.
I also put de *.LIF files in the <Media> folder.
Thanks for your input.
Goos
Here's the program too.
// --------------------------------- //
// Project: life
// Start: Saturday, August 29, 2009
// IDE Version: 7.088
// lKvfKn
LOADFONT "font.png",1
SETFONT 1
SETCURRENTDIR("Media")
info$=PLATFORMINFO$("")
GOSUB menu
GETSCREENSIZE xs%,ys%
LIMITFPS 11
generatie%=0
fps%=11
width%=xs%
heigt%=ys%
small%=0
A$=""
DIM pl%[width%][heigt%]
DIM pld%[width%][heigt%]
GOSUB rrand
WHILE KEY(29)=0
GOSUB nextgen
GOSUB showplay
dtime = GETTIMER()
Tfps% = ((1000/dtime)+Tfps%)/2
PRINT "TrueFPS="+Tfps%,80,30
SHOWSCREEN
GOSUB letters
WEND
END
SUB showplay:
life%=0
FOR y%=0 TO heigt%-1
FOR x%=0 TO width%-1
IF pl%[x%][y%]=1
IF small%=0
SETPIXEL x+small*40,y+small*40,RGB(255,255,255)
ELSE
vx%=small%*2
DRAWRECT x%*vx%,y%*vx%,vx%,vx%,RGB(255,255,255)
ENDIF
life%=life%+1
ENDIF
NEXT
NEXT
generatie%=generatie%+1
PRINT "Aantal levende cellen="+life%,0,10
SELECT small%
CASE 1
PRINT "BIG",0,ys%-30
CASE 0
PRINT "SMALL",0,ys%-30
CASE 2
PRINT "EXTRA",0,ys%-30
ENDSELECT
PRINT " Generatie="+generatie%,0,20
PRINT "fps="+fps%,0,30
ENDSUB
SUB nextgen:
FOR y%=0 TO heigt%-1
FOR x%=0 TO width%-1
x1%=x%-1
x2%=x%+1
y1%=y%-1
y2%=y%+1
IF x%=0 THEN x1%=width%-1
IF x%=width%-1 THEN x2%=0
IF y%=0 THEN y1%=heigt%-1
IF y%=heigt%-1 THEN y2%=0
numbers%=pl%[x1%][y1%]+pl%[x%][y1%]+pl%[x2%][y1%]+pl%[x%][y2%]+pl%[x2%][y2%]+pl%[x1%][y2%]+pl%[x1%][y%]+pl%[x2%][y%]
IF (numbers%<2) OR (numbers%>3)
pld%[x%][y%]=0
ELSEIF numbers%=2 AND pl%[x%][y%]=1
pld[x%][y%]=1
ENDIF
IF numbers%=3 THEN pld%[x%][y%]=1
NEXT
NEXT
pl%=pld%
ENDSUB
SUB rrand:
generatie%=0
FOR y%=0 TO heigt%-1
FOR x%=0 TO width%-1
pl%[x%][y%]=RND(1)
NEXT
NEXT
ENDSUB
SUB letters:
IF KEY(28)
WHILE KEY(28)
WEND
LIFMENU()
ENDIF
IF KEY(200)
fps%=fps%+1
LIMITFPS fps%
ENDIF
IF KEY(208)
fps%=fps%-1
IF fps%<1 THEN fps%=1
LIMITFPS fps%
ENDIF
IF KEY(205)
IF small%=0
small%=1
width%=width%/2
heigt%=heigt%/2
ELSEIF small%=1
small%=2
width%=width%/2
heigt%=heigt%/2
ELSEIF small%=2
small%=0
width%=width%*4
heigt%=heigt%*4
ENDIF
loos:
IF KEY(205) THEN GOTO loos
ENDIF
IF KEY(15) THEN GOSUB menu
pause:
IF KEY(203) THEN GOTO pause
ENDSUB
SUB menu:
IF info$="WIN32"
PRINT "Press <ENTER> to select a *.LIF file",0,30
PRINT "Press Up or Down to set the fps",0,60
PRINT "Press <ESC> to stop the program",0,90
PRINT "Press left to Pause",0,120
PRINT "Press right to Toggle Big or Small",0,150
PRINT "Push a key to begin",0,190
ELSE
PRINT "Press Menu to select a *.LIF file",0,30
PRINT "Press Up or Down to set the fps",0,60
PRINT "Press X to stop the program",0,90
PRINT "Press left to Pause",0,120
PRINT "Press right to Toggle Big or Small",0,150
PRINT "Push a key to begin",0,190
ENDIF
SHOWSCREEN
lp:
IF KEY(15) THEN GOTO lp
KEYWAIT
ENDSUB
FUNCTION lifload: filename$
// load and interpret a Game of Life pattern file (*.lif)
LOCAL str$, px$, py$
LOCAL i
LOCAL x%, y%, scrx%, scry%
// clear
FOR y%=0 TO heigt%-1
FOR x%=0 TO width%-1
pl%[x%][y%] = 0
NEXT
NEXT
// get middle of screen
scrx%=width%
scry%=heigt%
generatie%=0
// open and read file
IF OPENFILE(1, filename$, 1)
WHILE (ENDOFFILE(1) = FALSE)
// read next line
READLINE 1, str$
str$ = str$ + "\n"
// command
IF (MID$(str$, 0, 1) = "#")
// P = position command, for example "#P -3 -1"
IF (MID$(str$, 1, 1) = "P")
str$ = MID$(str$, 3)
i = INSTR(str$, " ")
IF (i <> -1)
// get positions
px$ = MID$(str$, 0, i)
py$ = MID$(str$, i+1)
// string to int
x% = INTEGER(px$) + (scrx% / 2)
y% = INTEGER(py$) + (scry% / 2)
ENDIF
ENDIF
// TODO: display comment lines on screen? "#D blah bla"
//IF (MID$(str$, 1, 1) = "D")
//ENDIF
ELSE
// cells, for example "*...***"
FOR i = 0 TO LEN(str$)
IF (MID$(str$, i, 1) = "*")
// not out of bounds
IF (x%+i >= 0) AND (y% >= 0) AND (x%+i < scrx%) AND (y% < scry%)
pl%[x%+i][y%] = 1
ENDIF
ENDIF
NEXT //i
y% = y% + 1
ENDIF
WEND
ENDIF
ENDFUNCTION
FUNCTION LIFMENU:
LIMITFPS 10
LOCAL xz%,yz%
GETFONTSIZE font_x, font_y
GETSCREENSIZE xz%,yz%
iopen=0
ifile=2
infile=2
num = GETFILELIST("*.LIF", files$[])
WHILE TRUE
tprint=0
IF infile*font_y<(yz%-10)
ifile=infile
ENDIF
DRAWRECT 0,(ifile*font_y),320,font_y,RGB(255,255,255)
PRINT ifile*font_y,100,0
PRINT ifile*font_y+font_y,100,font_y
FOR i=iopen TO BOUNDS(files$[], 0)-1 // BOUNDS(files$[], 0)-1 = num = num_dir+num_file
IF tprint*font_y<(yz%-10)
PRINT files$[i], 0, tprint*font_y
ENDIF
tprint=tprint+1
NEXT
SHOWSCREEN
IF KEY(208)
infile=infile+1
IF infile*font_y>(yz%-10) THEN iopen=iopen+1
ENDIF
IF KEY(200)
IF infile>3
infile=infile-1
IF infile*font_y>(yz%-10) THEN iopen=iopen-1
ENDIF
ENDIF
IF KEY(28)
WHILE KEY(28)=TRUE
WEND
lifload(files$[infile])
LIMITFPS fps%
BREAK
ENDIF
WEND
ENDFUNCTION