Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: neumanix on January 03, 2007
-
Hi everyone! I just started trying to get into this freebasic stuff, and it looks quite interesting!
I haven't done much coding for a long while so I'm a little rusty I might add.
I have some questions... What is the best way to clear the screenbuffer when you use tinyptc?
I have tried the redim array with option dynamic and that works well, but then I understood that one can use erase buffer on a static buffer as well. Here below is a simple 2d starscroller I just made. It works like this, but when I try the erase buffer method it crashes.
Also, is this the correct way to use types. In blitz there was the for...each method, but no such thing seems to exist in FB.
This is not much, but maybe it can be helpful to other beginners like myself.
P.S. When i installed the new Vsync tinyptc, it turn smooth as silk!!! Great work guys!
'#define ptc_win
#define mycls redim as integer gfxbuffer (gfx_w * gfx_h)
#define writepixel(x,y,col) gfxbuffer(x + y*gfx_w)=col
#include once "tinyptc.bi"
option dynamic
option explicit
const gfx_w = 640
const gfx_h = 480
const gfx_size = gfx_w * gfx_h
const numstars = 1000
dim n
randomize timer
if (ptc_open("horizontal starscroller",gfx_w,gfx_h)=0) then
end -1
end if
dim as integer gfxbuffer(gfx_size)
type star
x as integer
y as integer
speed as ubyte
col as integer
end type
dim s(numstars) as star
' setup start positions and speeds
for n=0 to numstars
s(n).x=(rnd*gfx_w)
s(n).y=(rnd*gfx_h)
s(n).speed=(rnd*3)+1
if s(n).speed = 1 then
s(n).col = &h101050
end if
if s(n).speed = 2 then
s(n).col = &h303070
end if
if s(n).speed = 3 then
s(n).col = &h707090
end if
if s(n).speed = 4 then
s(n).col = &hd0d0f0
end if
writepixel(s(n).x, s(n).y, s(n).col)
next
while inkey$<>chr$(27)
for n=0 to numstars
s(n).x = s(n).x + s(n).speed
if s(n).x > gfx_w then
s(n).x = 0
s(n).y = (rnd*gfx_h)
end if
writepixel(s(n).x, s(n).y, s(n).col)
next
ptc_update @gfxbuffer(0)
mycls
wend
ptc_close()
end
-
This will work for you, if you want to use erase, you need static arrays. so I changed option static.. Also the array that you are erasing needs to be global, so dim shared. That's about all I changed, nice first prog :)
You could also use inline asm to clear the screen or memcopy but erase seems to be just as fast and it's what I use.
#define writepixel(x,y,col) gfxbuffer(x + y*gfx_w)=col
#include once "tinyptc.bi"
option static
option explicit
const gfx_w = 640
const gfx_h = 480
const gfx_size = gfx_w * gfx_h
const numstars = 1000
dim n
randomize timer
if (ptc_open("horizontal starscroller",gfx_w,gfx_h)=0) then
end -1
end if
dim shared as integer gfxbuffer(gfx_size)
type star
x as integer
y as integer
speed as ubyte
col as integer
end type
dim s(numstars) as star
' setup start positions and speeds
for n=0 to numstars
s(n).x=(rnd*gfx_w)
s(n).y=(rnd*gfx_h)
s(n).speed=(rnd*3)+1
if s(n).speed = 1 then
s(n).col = &h101050
end if
if s(n).speed = 2 then
s(n).col = &h303070
end if
if s(n).speed = 3 then
s(n).col = &h707090
end if
if s(n).speed = 4 then
s(n).col = &hd0d0f0
end if
writepixel(s(n).x, s(n).y, s(n).col)
next
while inkey$<>chr$(27)
for n=0 to numstars
s(n).x = s(n).x + s(n).speed
if s(n).x > gfx_w then
s(n).x = 0
s(n).y = (rnd*gfx_h)
end if
writepixel(s(n).x, s(n).y, s(n).col)
next
ptc_update @gfxbuffer(0)
erase gfxbuffer
wend
ptc_close()
end
-
Good one shockwave . . . hope this also helps though . . .
Don't know if it helps but have you had a look at my starfield, its in here somewhere in the freebasic pages.
There are two ways that i use, one is just to use the erase buffer and another way is this:-
for a=0 to gfx_w*gfx_h-1
writepixel(a,rgb(0,0,0)) 'ie 0,0,0 is black - could clear it with another colour . . .
next a
I hope this makes sense - it works for me - if you want me to post my starfield using this please ask . . .
Drew
-
Yes I tried that but it still crashes here. It runs for maybe a second, then crash.
Did the modification you made work on your computer? If so, it's a bit strange.
Thanks btw for your nice comment. I'm looking forward to learn more and maybe produce my first ever 64k intro :)
I know i'm a bit like you when it comes to those small exes :)
-
neumanix, here is my code with my subroutine for clearing the screen - not sure if it will help?
Regards
Drew
option explicit
#include once "tinyptc.bi"
const xres = 800
const yres = 600
Dim shared as Integer NS
NS=512
Dim shared as Integer x(ns),y(ns)
dim shared as integer r,g,b
Dim Shared as Integer x1(ns),y1(ns)
dim shared as integer r1,g1,b1
Dim Shared as Integer x2(ns),y2(ns)
dim shared as integer r2,g2,b2
Dim Shared as Integer x3(ns),y3(ns)
dim shared as integer r3,g3,b3
Dim shared as Integer a
Dim shared as String key
Declare Sub DrawStars1
Declare Sub MoveStars1
Declare Sub Cler
for a=0 to ns-1
x(a)=int(rnd(1)*xres)
y(a)=int(rnd(1)*yres)
x1(a)=int(rnd(1)*xres)
y1(a)=int(rnd(1)*yres)
x2(a)=int(rnd(1)*xres)
y2(a)=int(rnd(1)*yres)
x3(a)=int(rnd(1)*xres)
y3(a)=int(rnd(1)*yres)
next
r=255:g=255:b=255
r1=192:g1=192:b1=192
r2=128:g2=129:b2=128
r3=64:g3=64:b3=64
If( ptc_open( "Starfield", XRES, YRES ) = 0 ) Then
End -1
End If
Dim Shared sb(xres*yres)
#define PP(x,y,argb) sb(y*XRES+x)=argb
while key<>chr$(27)
key = inkey$()
Cler
DrawStars1
MoveStars1
ptc_update @sb(0)
Wend
Sub DrawStars1
for a=0 to ns-1
pp(x(a),y(a),rgb(r,g,b))
pp(x1(a),y1(a),rgb(r1,g1,b1))
pp(x2(a),y2(a),rgb(r2,g2,b2))
pp(x3(a),y3(a),rgb(r3,g3,b3))
next a
End Sub
Sub MoveStars1
for a=0 to ns-1
x(a)=x(a)+4
x1(a)=x1(a)+3
x2(a)=x2(a)+2
x3(a)=x3(a)+1
if x(a)>=xres then x(a)=0
if x1(a)>=xres then x1(a)=0
if x2(a)>=xres then x2(a)=0
if x3(a)>=xres then x3(a)=0
next a
End Sub
Sub Cler
for a=0 to xres*yres-1
pp(a,0,0)
next a
End Sub
-
Just use the "erase" command to clear the buffer Drew :) It's quicker. Btw, your starfield is great too!!
-
@Shockwave, I don't know if you missed my reply, but the mod you made didn't work. It runs for a second then crashes.
Did it work for you DrewPee?
-
That's the correct way to use types. It works the same in C too.
The quickest way I know to clear the buffer is to use memset from the crt.bi library.
Something like
#include "crt.bi"
dim screen(640*480) as integer
...
memset(@screen(0),0,640*480*4)
Jim
-
yeah as jim says the mem functions in the crt lib are the fastest functions there are for moving large chunk of memory around and also if you move on to c or c++ its a bonus to know the crt lib.
-
I'll second the use of Erase Screenbuffer, as thats the method I also use.
It can be used for all arrays too. And no need to use the brackets, just the name will suffice.
I never knew how to use memset, so will give that ago and cheers Jimbo.
-
@nuemanix - yep it worked for me fine . . . looked cool too!
@Jim - thanks for that one matey - just changed my code to use it - and yes it defintely is smoother! Nice One!
@Shockwave - thanks for the comments on my starfield!
:goodpost:
Drew
-
Mmm. I hadn't used memset before, I'll try that. Thanks Jim.