04-05-2005, 03:45 PM
I just made this example so that newer FB users don't get the problem we have at using a lot of select cases. Enjoy!
Code:
' Using sub/function pointers
' to make objects easier to handle
' I've made this example because I am
' amazed by the fact that not a lot of you
' are exploiting this fine FB capability
' to make games a lot easier to code.
' ie:
' Instead of a bunch of select case statements
' and deciding object movement via an ID system,
' function/sub pointers w/in TYPES should be used.
' Pros:
' 1. code can be easily modularized
' 2. object AI can be easily debugged because it resides
' in a single sub/funk
' 3. less cluttered code
' 4. object behavior can be easily modified or changed
' 5. code is not as error prone as a buch of select cases
'
' cons: Nothing I can think of ;*)
'
' Relsoft 2005
' Rel.Betterwebber.com
'
'
defint a-z
option explicit
'screen dimensions for screen 14
#define SCREEN_W 320
#define SCREEN_H 240
#define SCREEN_W_MID SCREEN_W shr 1
#define SCREEN_H_MID SCREEN_H shr 1
'misc defines
#define PI 3.141593
#define NUM_PARTS 60
'our particle structure
Type ParticleType
x as single 'bunch of public data
y as single
xv as single
yv as single
angle as integer
radius as integer
color as integer
move_sub as sub (byref pt as ParticleType) 'sub pointer for object behavior
End Type
'declare before use
declare sub move_circle_right (byref pt as ParticleType)
declare sub move_circle_left (byref pt as ParticleType)
'our particles
dim Parts(NUM_PARTS - 1) as ParticleType
'320*240, 32 bit, 2 pages
SCREEN 14, 32, 2
'misc counter
dim i as integer
'put values to each particle
for i = 0 to NUM_PARTS - 1
Parts(i).x = 0
Parts(i).y = 0
Parts(i).xv = 0
Parts(i).yv = 0
Parts(i).angle = int(i * 360* 3 / NUM_PARTS)
Parts(i).Radius = int(i * NUM_PARTS/25)
Parts(i).color = cint(rnd * 255) shl 16 or cint(rnd * 255) shl 8 or cint(rnd * 255)
'lazy way to assign sub pointers :*)
'if odd then rotate right, left otherwise
if i and 1 then
Parts(i).move_sub = @move_circle_right
else
Parts(i).move_sub = @move_circle_left
end if
next i
do
SCREENSET 1, 0 'set work page for double buffering
cls 'clear the screen
'do the objects
'notice how I only call the sub pointer
'and it does everything for me? ;*)
for i = 0 to NUM_PARTS - 1
Parts(i).move_sub(Parts(i))
circle (Parts(i).x, Parts(i).y), 5, Parts(i).color
next i
wait &h3da, 8 'vsynch
SCREENCOPY 'flip
loop until inkey$<>""
end
'only 2 subs for clarity
'rotates right
sub move_circle_right (byref pt as ParticleType)
dim a as single
pt.angle = (pt.angle + 1)
if pt.angle > 359 then pt.angle = 360 - pt.angle
a = pt.angle * PI /180
'add some wavy animation to the circles
'displacement = angle * 2PI / steps
pt.radius = pt.radius + (5) * sin((pt.angle) * 2 * PI/30)
pt.x = SCREEN_W_MID + cos(a) * pt.radius
pt.y = SCREEN_H_MID + sin(a) * pt.radius
end sub
'rotates left
sub move_circle_left (byref pt as ParticleType)
dim a as single
pt.angle = (pt.angle - 1)
if pt.angle < 0 then pt.angle = pt.angle + 360
a = pt.angle * PI /180
pt.radius = pt.radius + (5) * sin((pt.angle) * 2 * PI/30)
pt.x = SCREEN_W_MID + cos(a) * pt.radius
pt.y = SCREEN_H_MID + sin(a) * pt.radius
end sub