Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - mike_g

Pages: [1] 2 3
1
C / C++ /C# / Trying to optimise fadeout
« on: July 18, 2008 »
Hi, I have a fadeout effect, but its too slow o my eeepc. I get 15fps :/ I have a fast version ,commented out, but it fades out too quickly. The bottleneck seems to be the floating point maths and the type casting. Heres the code:

Code: [Select]
void blit_and_fade(float mod)
{
int i, size = GRAPHICS_W*GRAPHICS_H;
Uint32 *pix = (Uint32*)screen->pixels;
Uint32 *buf = buffer;
Uint32 r_b, g;

for(i=0; i<size; i++, pix++, buf++)
{
*pix = *buf;
r_b = (Uint32)((*buf & 0x00FF00FF) * mod) & 0x00FF00FF;
g   = (Uint32)((*buf & 0x0000FF00) * mod) & 0x0000FF00;
*buf = r_b | g;
//*buf = (*buf >> 1) & 8355711; // <- FAST FADE LOOKS CRAP
}
}
Any suggestion how I coud do this w/o the floating point? Cheers.

2
Hi, I'm having some problems zooming and rotating splines.

Basically there are two types of points thats easiest to show this pic:


Even numbered, red, points are for the start and end of a line.
Odd numbered, blue, points set the angle of the curve.

The way I am going about it is to first move all the red points (which I can do). Once the red points have been moved I run through the blue points. I need to place them at the location at which the light blue lines in the pic intersect. I'm a bit confused with the maths tho. Does anyone know how I should go about this?

Heres an example of my unfinished function in Blitz code:
Code: [Select]
Function ZoomSpline(s.spline, zoom)

For i=0 To s\size-1
cx = cx + s\pts[i]\x
cy = cy + s\pts[i]\y
Next
cx = cx / s\size
cy = cy / s\size

For i=0 To s\size-1  Step 2 ;line terminator points
dx# = s\pts[i]\x - cx
dy# = s\pts[i]\y - cy
angle# = ATan2(dy#, dx#)
s\pts[i]\x = s\pts[i]\x + Cos(angle#)*zoom
s\pts[i]\y = s\pts[i]\y + Sin(angle#)*zoom
Next

For i=1 To s\size-2 Step 2 ;angle points

Next
End Function
Cheers. 

3
Blitz / Messy Spline Thing [BB2D]
« on: July 10, 2008 »
I'm working on a spline editor in Java atm. I made an effect with lines following a load of randomly generated lines. Looks like scribbling at the moment. Hopefully It should look better when the paths are laid out in a nice pattern. Cheers.
Code: [Select]
;******************************************************************;
;----------------------- DATA TYPES REQUIRED ----------------------;
;******************************************************************;
Const MAX_SPLINE_SIZE = 100
Const CURVE_DETAIL = 300

Type point
Field x, y
End Type

Type spline
Field size
Field pts.point[MAX_SPLINE_SIZE]
End Type

;Temp array for holding points used in spline constructor
Dim temp.point(MAX_SPLINE_SIZE)


Const RUNNER_SIZE = 12

Type runner
Field x, y
Field s.spline
Field pos[RUNNER_SIZE]
Field loc
End Type

;******************************************************************;
;------------------------- FUNCTIONS USED -------------------------;
;******************************************************************;

Function NewPoint.point(x, y)
Local p.point = New point
p\x = x
p\y = y
Return p
End Function

Function NewSpline.spline()
Local s.spline = New spline
Local i = 0
While(temp.point(i) <> Null)
s\pts.point[i] = temp.points(i)
i = i + 1
If(i = MAX_SPLINE_SIZE) Then Exit
Wend
s\size = i
Return s
End Function

Function DrawSpline(s.spline)
i=0
While(s\pts.point[i+2] <> Null)
BezierCurve(s\pts.point[i], s\pts.point[i+1], s\pts.point[i+2])
i=i+2
Wend
End Function

Function BezierCurve(p1.point, p2.point, p3.point)
For i = 0 To CURVE_DETAIL
ptx1 = p1\x+scroll_x: pty1 = p1\y+scroll_y
ptx2 = p2\x+scroll_x: pty2 = p2\y+scroll_y
ptx3 = p3\x+scroll_x: pty3 = p3\y+scroll_y
x1 = QuadraticBezier(i-1, ptx1, ptx2, ptx3)
y1 = QuadraticBezier(i-1, pty1, pty2, pty3)
x2 = QuadraticBezier(i  , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(i  , pty1, pty2, pty3)
Line x1, y1, x2, y2
Next
End Function


Function QuadraticBezier(Perc#,p1#,p2#,p3#)
d1#=(((p2-p1)*perc)/CURVE_DETAIL)+p1
d2#=(((p3-p2)*perc)/CURVE_DETAIL)+p2
d3#=(((d2-d1)*perc)/CURVE_DETAIL)+d1
Return d3
End Function


Function CubicBezier(perc#,p1#,p2#,p3#,p4#)
d1#=(((p2-p1)*perc)/CURVE_DETAIL)+p1
d2#=(((p3-p2)*perc)/CURVE_DETAIL)+p2
d3#=(((p4-p3)*perc)/CURVE_DETAIL)+p3
d4#=QuadraticBezier(perc,d1,d2,d3)
Return d4
End Function



;******************************************************************;
;--------------------------- TEST PROGRAM -------------------------;
;******************************************************************;
Const VIDEO_MODE = 2
Graphics 1024,768,32, VIDEO_MODE
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Const PALETTE_SIZE = 512
Dim palette(PALETTE_SIZE)
Local rd#, g#, b#
For i=0 To PALETTE_SIZE
rd#=rd#+0.7: If(rd# > 255)r#=255
g#=g#+1.2: If(g# > 255)g#=255
b#=b#+0.1: If(b# > 255)b#=255
palette(i) = Int(rd) Shl 16 + Int(g) Shl 8 + Int(b)
Next


For a = 1 To 10
For b = 0 To 10
temp.point(b) = NewPoint(Rand(0, 1023), Rand(0, 767))
Next
s.spline = NewSpline()
Next
For s.spline = Each spline
f.runner = CreateRunner(s)
f\loc = Rand(0, 8) Mod 2
Next


Global hold = -1 ;indicates which point is held
Global scroll_x=0, scroll_y=0

While Not KeyHit(1)
;Cls
Runner()
Flip
Wend

;********************************************************************;
;--------------------------- EXTRA FUNCTIONS ------------------------;
;********************************************************************;
Function CreateRunner.runner(s.spline)
Local n.runner = New runner
n\x = s\pts.points[0]\x
n\y = s\pts.points[0]\y
n\s = s
n\loc = 0
For i = 0 To RUNNER_SIZE
n\pos[i]=i
Next
Return n
End Function


Function Runner()
For r.runner = Each runner
DrawRunner(r)
Next
End Function

Function DrawRunner(r.runner)
overlap = False

For i= 0 To RUNNER_SIZE-1
rd = rd+10
gr = gr+8
bl = bl+5
If overlap = False
If r\pos[i] < r\pos[i+1]
ptx1 = r\s\pts[r\loc]\x: pty1 = r\s\pts[r\loc]\y
ptx2 = r\s\pts[r\loc+1]\x: pty2 = r\s\pts[r\loc+1]\y
ptx3 = r\s\pts[r\loc+2]\x: pty3 = r\s\pts[r\loc+2]\y
If i < RUNNER_SIZE-1
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = QuadraticBezier(r\pos[i+1] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[i+1] , pty1, pty2, pty3)
;col = DrawLine(x1, y1, x2, y2, col)

Color rd, gr, bl: Line x1, y1, x2, y2
EndIf

Else ; switchover
overlap = True
If r\loc+4 >= r\s\size  ;End of spline
r\loc=0
Return
EndIf
ptx1 = r\s\pts[r\loc]\x: pty1 = r\s\pts[r\loc]\y
ptx2 = r\s\pts[r\loc+1]\x: pty2 = r\s\pts[r\loc+1]\y
ptx3 = r\s\pts[r\loc+2]\x: pty3 = r\s\pts[r\loc+2]\y
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = r\s\pts[r\loc+2]\x
y2 = r\s\pts[r\loc+2]\y
Color rd, gr, bl: Line x1, y1, x2, y2
;col = DrawLine(x1, y1, x2, y2, col)
EndIf
Else
If r\loc+4 >= r\s\size  ;End of spline
r\loc=0
Return
EndIf

ptx1 = r\s\pts[loc+2]\x: pty1 = r\s\pts[loc+2]\y
ptx2 = r\s\pts[loc+3]\x: pty2 = r\s\pts[loc+3]\y
ptx3 = r\s\pts[loc+4]\x: pty3 = r\s\pts[loc+4]\y
If i < RUNNER_SIZE-1
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = QuadraticBezier(r\pos[i+1] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[i+1] , pty1, pty2, pty3)
;col = DrawLine(x1, y1, x2, y2, col)
Color rd, gr, bl: Line x1, y1, x2, y2
EndIf
EndIf



Next

For i= 0 To RUNNER_SIZE
r\pos[i] = r\pos[i]+1
If r\pos[i] = CURVE_DETAIL Then r\pos[i]=0
Next

If r\pos[0] = 0
r\loc=r\loc+2
EndIf

End Function

4
General chat / Hello again
« on: June 16, 2008 »
Hey guys, I havent been around much recently. Been kind of bogged down with boring college work most of the time so havent been doing any demo coding anyway. Hopefully I'll be able to get back to it over the summer :)

I just made a new site to dump some of my stuff; a lot of it was posted here a while back, but stupid sitesled kept deleting my account for direct linking files. Anyway if anyones interested its here

http://grundez.googlepages.com/Index.html

Cheers  :cheers:

5
Projects / Collision Domain
« on: August 27, 2007 »
This has nothing to do with networking, but it looked kind of cool so I thought I'd try and make a demo out of it.
Download Here

6
Blitz / Fast Colour Blending [BB2D]
« on: August 23, 2007 »
Heres a fast colour blending function, with some optimizations stonemonkey showed me. Thought I'd post it incase anyone else finds it useful. Oh and alpha_1 should be a value between 0 and 256.
Code: [Select]
Function ColourBlend( rgb_1, rgb_2, alpha_1 )
alpha_2 = 256 - alpha_1

rb_1 = ((rgb_1 And $00FF00FF) * alpha_1) And $FF00FF00
g_1  = ((rgb_1 And $0000FF00) * alpha_1) And $00FF0000
rb_2 = ((rgb_2 And $00FF00FF) * alpha_2) And $FF00FF00
g_2  = ((rgb_2 And $0000FF00) * alpha_2) And $00FF0000

rb_1 = rb_1 + rb_2
g_1  = g_1 + g_2

Return (rb_1 Or g_1) Sar 8
End Function
 

7
Projects / Lavapit Demo
« on: August 21, 2007 »
I thought I'd try and finish something for a change. So here it is:



Download link

Its 600 and something K, so I couldent upload it to the site. If Shockwave or someone feels like doing it that would be cool. Its kind of lacking sound, and it might run a little slow for some of you with older computers. Hopefully It wont be too slow tho.

[Link fixed ~ Shockwave]

8
Blitz / Fast Image Scaling [BB2D]
« on: August 20, 2007 »
Heres some code I made today for fast image scaling. Basically each image is a type with a number of frame handles dependant on SCALE_DETAIL. The CreateScaledImage() function then creates and draws an appropriately sized image for each frame.

Heres the code:
Code: [Select]
AppTitle "Fast Image Scaling"
Graphics 640, 480, 32, 2
SetBuffer BackBuffer()

;Each image has an array of handles for scaled versions
;The more handles the greater the scale detail
Const SCALE_DETAIL = 50 ;50 frames
Const SCALE_RANGE = 2   ;between 2 and 0.1

;Each image has a number of frames and a name as an identifier
Type image
Field name$
Field frames[SCALE_DETAIL]
End Type

pic = LoadImage("tree.png")
CreateScaledImage(pic, "tree")
FreeImage(image)



;---------------------------------------------------------------------------------------------------;
;******************************** DEMO PROGRAM *****************************************************;
;---------------------------------------------------------------------------------------------------;
SeedRnd MilliSecs()
;FRAME RATE VARIABLES
Global old_time = MilliSecs()
Global frame_count, fps

Type tree
Field image
Field scale
Field x, y
End Type
InitTrees()

inc=1: i=5
While Not KeyHit(1)
Cls

DrawTrees()

KeyInput()
FPS()
Flip
Wend
ClearScaled() ;free scaled images

;---------------------------------------------------------------------------------------------------;
;******************************** SCALE FUNCTIONS **************************************************;
;---------------------------------------------------------------------------------------------------;

Function CreateScaledImage(image, name$)
im.image = New image
im\name = "tree"


Local src_x, src_y ;co-ords to read from original image
Local src_w, src_h ;width and height of original
Local dst_x, dst_y ;co-ords to draw to scaled image
Local dst_w, dst_h ;width and height of scaled image
Local col, inc#    ;colour to draw , scale size increment

src_w = ImageWidth(image)
src_h = ImageHeight(image)
inc# = Float(SCALE_RANGE) / Float(SCALE_DETAIL)

SetBuffer ImageBuffer(image, mask)

For i = 1 To SCALE_DETAIL
image_size# = image_size# + inc#

dst_w = Floor(src_w * image_size#)
dst_h = Floor(src_h * image_size#)
 
im\frames[i] = CreateImage(dst_w, dst_h)

LockBuffer ImageBuffer(image)
LockBuffer ImageBuffer(im\frames[i])

For dst_y=0 To dst_h
src_y = Floor(dst_y / image_size#)
For dst_x=0 To dst_w
src_x = Floor(dst_x / image_size)
col = ReadPixelFast(src_x, src_y)
WritePixelFast(dst_x, dst_y, col, ImageBuffer(im\frames[i]))
Next
Next

UnlockBuffer ImageBuffer(image)
UnlockBuffer ImageBuffer(im\frames[i])

MaskImage im\frames[i], 255, 0, 255
Next

SetBuffer BackBuffer()
End Function


Function ClearScaled()
For im.image = Each image
For i = 1 To SCALE_DETAIL
FreeImage(im\frames[i])
Next
Next
End Function


;---------------------------------------------------------------------------------------------------;
;******************************** DEMO FUNCTIONS ***************************************************;
;---------------------------------------------------------------------------------------------------;

Function KeyInput()
If KeyDown(200)
ResizeTrees(1)
Else If KeyDown(208)
ResizeTrees(-1)
EndIf
End Function


Function ResizeTrees(inc)
im.image = First image
For t.tree = Each tree
go=0
If t\scale < SCALE_DETAIL And t\scale > 1 Then go =1

t\scale = t\scale +inc
If go = 1
t\image = im\frames[t\scale]
t\y = 240-(ImageHeight(t\image)/2)
EndIf
Next
End Function

Function DrawTrees()
For t.tree = Each tree
DrawImage t\image, t\x, t\y
Next
End Function

Function InitTrees()
;Make trees
im.image = First image
For i = 1 To 100
t.tree = New tree
t\scale = Rand(1, SCALE_DETAIL)
t\image = im\frames[t\scale]
t\x = Rand(0, 600)
t\y = 240-(ImageHeight(t\image)/2)
Next
;Sort trees by distance
a.tree = First tree
flag = True
While flag
flag = False
b.tree = Last tree
While a <> b
c.tree = Before b
If c = Null Then Exit
If b\scale < c\scale
Insert c After b
flag=True
Else
b=c
EndIf
Wend
a = After b
Wend
End Function

Function FPS()
If MilliSecs() > old_time+1000
old_time = MilliSecs()
fps=frame_count
frame_count=0
EndIf
Color 255, 255, 255
Text 0, 0, "FPS: "+fps
frame_count=frame_count+1
End Function
And heres an image for the test program:

9
Blitz / Wavy and Stretchy Text [BB2D]
« on: August 20, 2007 »
Heres some code for a couple of simple bitmap font text effects I made. You can get the font used in it here:
http://dbfinteractive.com/index.php?topic=311.0

It also works with the other bitmap fonts on this site. And heres the code:
Code: [Select]
Const GRAPHICS_W = 800
Const GRAPHICS_H = 600
Const FONT_W = 16
Const FONT_H = 16
Const CHARS$ = " !@#$%+'[]  ,-./0123456789:;(=)? ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const LETTERS = 64
Const FONT_MASK = -16777216

Dim font(LETTERS*FONT_W, FONT_H)

Graphics GRAPHICS_W, GRAPHICS_H, 32, 2
SetBuffer BackBuffer()

BitmapFontLoad("Com.png")

Global old_time = MilliSecs()
Global frame_count, fps

inc=2: y=100: x=100
While Not KeyHit(1)
Cls
WobbleText(x, y, count, 50, "SOMETHING IS GOING ON")
WobbleText(x-20, y+100, count+5, 40, "ITS A WAVY TEXT DEMO !")

StretchyText(x, y+300, FONT_H, 50, "STRETCHY TEXT")

count=count+1
x=x+inc
If x+(20*16) >= GRAPHICS_W Or x < 0 Then inc=-inc
FPS()
Flip 0
Wend

Function StretchyText(x_pos, y_pos, min, max, txt$)
length = Len(txt$)
LockBuffer BackBuffer()
For char=0 To length-1
;GET THE POSITION IN FONT OF THE LETTER
letter$=Mid(txt$, char+1, 1)
For frame = 1 To LETTERS
If letter$ = Mid(CHARS$, frame, 1) Then Exit
Next
frame=frame-1

char_x = x_pos + char*FONT_W
;DRAW LETTER
For x=0 To FONT_W-1
height = (min+max+(Sin(char_x+x)*max)-1)
char_y = y_pos - (height / 2)
y_inc# = Float(FONT_H)/height

font_x = frame*FONT_W+x
font_y#=0
draw_x = char_x+x
For y=0 To height
draw_y = char_y+y
If font(font_x, font_y#) <> FONT_MASK
WritePixel(draw_x, draw_y, font(font_x, font_y#))
EndIf
font_y# = font_y#+y_inc#
Next
Next
Next
UnlockBuffer BackBuffer()
End Function



Function WobbleText(x_pos, y_pos, deg, intensity, txt$)
length = Len(txt$)
LockBuffer BackBuffer()

For char=0 To length-1
letter$=Mid(txt$, char+1, 1)

;GET THE POSITION IN FONT OF THE LETTER
For frame = 1 To LETTERS
If letter$ = Mid(CHARS$, frame, 1) Then Exit
Next
frame=frame-1

x_mod = char*FONT_W

;DRAW LETTER
For x=0 To FONT_W-1
wobble=Sin((char*FONT_W)+x+deg)*intensity
font_x = frame*FONT_W+x

For y=0 To FONT_H-1
draw_y = y_pos+y+wobble
draw_x = x_pos+x_mod+x

If font(font_x, y) <> FONT_MASK
WritePixel(draw_x, draw_y, font(font_x, y))
EndIf
Next
Next
Next
UnlockBuffer BackBuffer()
End Function

Function BitmapFontLoad(filename$)
tmp=LoadImage(filename$)
w=ImageWidth(tmp)
h=ImageHeight(tmp)
SetBuffer ImageBuffer(tmp)
LockBuffer
For y=0 To h-1
y_mod = y Mod FONT_H
For x=0 To w-1
font(((y/FONT_H)*w)+x, y_mod)=ReadPixelFast(x, y)
Next
Next
UnlockBuffer
SetBuffer BackBuffer()
FreeImage(tmp)
End Function


Function FPS()
If MilliSecs() > old_time+1000
old_time = MilliSecs()
fps=frame_count
frame_count=0
EndIf
Color 255, 255, 255
Text 0, 0, "FPS: "+fps
frame_count=frame_count+1
End Function
I'm working on porting these to C, but I am getting segfaults at the moment for poking about in the wrong bits of memory. Hopefully I should have that fixed in a bit tho :)

10
Projects / Raycaster
« on: August 20, 2007 »
Ok, it looks like I have given up on this for the time being at least. So heres what I got of my raycaster. It includes an executable version and Blitz source code. Theres a level editor with it so you can create you own levels and also indoor and outdoor effects. Theres a slight bug with the floor casting, but the horrible clipping I had before problem is fixed.

Download link

11
I'm coding automated movement in my raycasting prog at the moment. It uses A* to find a path, then rotates to face the next tile to move to. I want to find out what direction to turn in will take the least rotation.

So say I know the target facing I want to reach. I have 2 player directions dir_x and dir_y, which range between -1 and 1. How can I find which direction will take the least rotation?

Heres an sketch. Ignore the blue writing its just random crap. The red dot represents the player. Theres an example target of dir_x = 0.5, dir_y = 0.5. Any facing in the green half of the grid would be rotated to the right, whereas anything in the turquoise half would be rotated to the left. 

I hope I explained this in an understandable way. If I dident, let me know. Cheers :)

12
Blitz / Another Raycaster [BB2D]
« on: August 12, 2007 »
Heres a raycaster I have been working on. It needs optimising and the clipping can be a bit dodgy at times. If its too slow you can comment out the DrawFire(), which should speed it up a bit. I seriously dont understand how people make these things in < 5K. Anyway:
Code: [Select]
Const GRAPHICS_W=640
Const GRAPHICS_H=480
Const DRAW_FROM=20
Const MAP_W=30
Const MAP_H=30
Const TEX_W=64
Const TEX_H=64

Graphics GRAPHICS_W, GRAPHICS_H, 32, 0
SetBuffer BackBuffer()
Dim screen(GRAPHICS_W, GRAPHICS_H)


Dim fire(GRAPHICS_W, GRAPHICS_H)
Dim palette(256)
FireInit()


;LOAD TEXTURES
tmp = LoadImage("tex.png");LoadAnimImage("textures.png", 64, 64, 0, 8)
Dim tex(ImageWidth(tmp), ImageHeight(tmp))
LockBuffer ImageBuffer(tmp)
For y=0 To ImageHeight(tmp)-1
For x=0 To ImageWidth(tmp)-1
tex(x, y)=ReadPixelFast(x, y, ImageBuffer(tmp))
Next
Next
UnlockBuffer ImageBuffer(tmp)
FreeImage(tmp)

;INIT MAP
Dim map(MAP_W, MAP_H)
For y=0 To MAP_H-1
For x=0 To MAP_W-1
Read map(x, y)
Next
Next

;PLAYER VARIABLES
Global pos_x#=10, pos_y#=5
Global dir_x#=-1, dir_y#=0
Global cam_x#=0,  cam_y#=0.66 ;camera plane
Global mov_spd#=0.3, rot_spd#=3

;FRAME RATE VARIABLES
Global old_time = MilliSecs()
Global frame_count, fps

;MAIN LOOP
While Not KeyHit(1)
Clear();can go when roof and floor are drawn
DrawFire()
Raycast()
Render()
PlayerMove()
FPS()
Flip
Wend

;---------------------------------------------------------------------------------------------------;
;*************************************** RAYCASTING ************************************************;
;---------------------------------------------------------------------------------------------------;

Function Raycast()
Local ray_x# = pos_x#
Local ray_y# = pos_y#

For x=0 To GRAPHICS_W-1
camera_x# = 2 * x / Float(GRAPHICS_W)-1
ray_dir_x# = dir_x# + cam_x# * camera_x#
ray_dir_y# = dir_y# + cam_y# * camera_x#
map_x = ray_x#
map_y = ray_y#

;DISTANCE TO NEXT SIDE
delta_dist_x# = Sqr(1+(ray_dir_y# * ray_dir_y#)/(ray_dir_x# * ray_dir_x#))
delta_dist_y# = Sqr(1+(ray_dir_x# * ray_dir_x#)/(ray_dir_y# * ray_dir_y#))

;CALCULATE STEP AND INITIAL DISTANCE TO NEAREST SIDE
If ray_dir_x# < 0
step_x = -1
side_dist_x# = (ray_x# - map_x) * delta_dist_x#
Else
step_x = 1
side_dist_x# = (map_x + 1.0 - ray_x#) * delta_dist_x#
EndIf
If ray_dir_y# < 0
step_y = -1
side_dist_y#= (ray_y# - map_y) * delta_dist_y#
Else
step_y = 1
side_dist_y# = (map_y + 1.0 - ray_y#) * delta_dist_y#
EndIf


;PERFORM DDA
hit=0
While hit = 0
;jump To next map square, or in x-direction, or in y-direction
If side_dist_x# < side_dist_y#
side_dist_x# = side_dist_x# + delta_dist_x#
map_x = map_x + step_x
side = 0
Else
side_dist_y# = side_dist_y# + delta_dist_y#
map_y = map_y + step_y
side = 1
EndIf
;Check If ray has hit a wall
If map(map_x, map_y) > 0 hit = 1
Wend


;CALCULATE DISTANCE PROJECTED ON CAMERA DIRECTION
If side = 0
perp_wall_dist# = Abs((map_x - ray_x# + (1-step_x) / 2) / ray_dir_x#)
Else
perp_wall_dist# = Abs((map_y - ray_y# + (1-step_y) / 2) / ray_dir_y#)
EndIf

Local line_height = Abs(Int(GRAPHICS_H / perp_wall_dist#))
Local line_start = (-line_height / 2) + (GRAPHICS_H / 2)
If line_start < 0 Then line_start = 0
Local line_end = (line_height / 2) + (GRAPHICS_H / 2)
If line_end >= GRAPHICS_H Then line_end = GRAPHICS_H-1

;TEXTURE WALLS
tex_num = map(map_x, map_y)-1 ;what texture to use

;GET VALUE OF WALL_X
If side = 1
wall_x# = ray_x# + ((map_y - ray_y# + (1 - step_y) / 2) / ray_dir_y#) * ray_dir_x#
Else
wall_x# = ray_y# + ((map_x - ray_x# + (1 - step_x) / 2) / ray_dir_x#) * ray_dir_y#
EndIf
wall_x# = wall_x# - Floor(wall_x#)

;GET X CO-ORD ON TEXTURE
tex_x = Int(wall_x# * Float(TEX_W))
If side = 0 And ray_dir_x# > 0 Then tex_x = TEX_W - tex_x - 1
If side = 1 And ray_dir_y# < 0 Then tex_x = TEX_W - tex_x - 1
If tex_x >= TEX_W Then tex_x = tex_x - TEX_W
If tex_x < 0 Then tex_x = TEX_W + tex_x


;For y=0 To line_start
; screen(x, y)=255
;Next

;DRAW TO WALLS TO BUFFER
For y=line_start+1 To line_end

d = (y * 256) - ((GRAPHICS_H * 128) + (line_height * 128))
If line_height > 0 ;stops divide by 0
tex_y = (((d * TEX_H) / line_height) / 256)
If tex_y >= TEX_H Then tex_y = tex_y - TEX_H
If tex_y < 0 Then tex_y = TEX_H + tex_y
Else
tex_y=0
EndIf

col = tex((tex_num*TEX_W)+tex_x, tex_y)
If side = 1 Then col = (col Shr 1) And 8355711
screen(x, y)=col
Next

;FLOOR CASTING
If side = 0 And ray_dir_x# >= 0
floor_wall_x# = map_x
floor_wall_y# = map_y + wall_x#
Else If side = 0 And ray_dir_x# < 0
floor_wall_x# = map_x + 1
floor_wall_y# = map_y + wall_x#
Else If side = 1 And ray_dir_y# > 0
floor_wall_x# = map_x + wall_x#
floor_wall_y# = map_y
Else
floor_wall_x# = map_x + wall_x#
floor_wall_y# = map_y + 1
EndIf

wall_dist# = perp_wall_dist#
player_dist# = 0
If line_end < 0 Then line_end =0


For y=line_end+1 To GRAPHICS_H-1
current_dist# = GRAPHICS_H / (2.0 * y-GRAPHICS_H);make a lookup table for this
weight# = (current_dist# - player_dist#) / (wall_dist# - player_dist#)
current_floor_x# = weight * floor_wall_x# +(1-weight) * pos_x#
current_floor_y# = weight * floor_wall_y# +(1-weight) * pos_y#

floor_tex_x = Int(current_floor_x# * TEX_W) Mod TEX_W
floor_tex_y = Int(current_floor_y# * TEX_H) Mod TEX_H
screen(x, y) = tex(floor_tex_x, floor_tex_y)
Next

Next

End Function

;---------------------------------------------------------------------------------------------------;
;************************************* SCREEN DRAWING **********************************************;
;---------------------------------------------------------------------------------------------------;

Function Clear()
For y=0 To GRAPHICS_H-1
For x=0 To GRAPHICS_W-1
screen(x, y)=0
Next
Next
End Function

Function Render()
LockBuffer BackBuffer()
For y=0 To GRAPHICS_H-1
For x=0 To GRAPHICS_W-1
WritePixelFast(x, y, screen(x, y))
Next
Next
UnlockBuffer BackBuffer()
End Function

;---------------------------------------------------------------------------------------------------;
;************************************* PLAYER MOVEMENT *********************************************;
;---------------------------------------------------------------------------------------------------;

Function PlayerMove()
;STRAFE MODE
If KeyDown(42)
If KeyDown(203);left
mov_x# = pos_x# + ((-dir_y#) * mov_spd# *0.5)
mov_y# = pos_y# - ((-dir_x#) * mov_spd# *0.5)
If map(mov_x#, pos_y#) = 0 Then pos_x# = mov_x#
If map(pos_x#, mov_y#) = 0 Then pos_y# = mov_y#
Else If KeyDown(205);right
mov_x# = pos_x# + ((dir_y#) * mov_spd# *0.5)
mov_y# = pos_y# - ((dir_x#) * mov_spd# *0.5)
If map(mov_x#, pos_y#) = 0 Then pos_x# = mov_x#
If map(pos_x#, mov_y#) = 0 Then pos_y# = mov_y#
EndIf
;TURN MODE
Else
;Rotate camera direction and plane
If KeyDown(203);left
old_dir_x# = dir_x#
dir_x# = (dir_x# * Cos(rot_spd#)) - (dir_y# * Sin(rot_spd#))
dir_y# = (old_dir_x# * Sin(rot_spd#)) + (dir_y# * Cos(rot_spd#))
old_cam_x# = cam_x#
cam_x# = (cam_x# * Cos(rot_spd#)) - (cam_y# * Sin(rot_spd#))
cam_y# = (old_cam_x# * Sin(rot_spd#)) + (cam_y# * Cos(rot_spd#))
Else If KeyDown(205);right
old_dir_x# = dir_x#
dir_x# = (dir_x# * Cos(-rot_spd#)) - (dir_y# * Sin(-rot_spd#))
dir_y# = (old_dir_x# * Sin(-rot_spd#)) + (dir_y# * Cos(-rot_spd#))
old_cam_x# = cam_x#
cam_x# = (cam_x# * Cos(-rot_spd#)) - (cam_y# * Sin(-rot_spd#))
cam_y# = (old_cam_x# * Sin(-rot_spd#)) + (cam_y# * Cos(-rot_spd#))
EndIf
EndIf

;MOVE FORWARDS AND BACKWARDS
If KeyDown(200)
mov_x#=pos_x# + (dir_x# * mov_spd#)
mov_y#=pos_y# + (dir_y# * mov_spd#)
If map(mov_x#, pos_y#) = 0 Then pos_x# = mov_x#
If map(pos_x#, mov_y#) = 0 Then pos_y# = mov_y#
Else If KeyDown(208)
mov_x#=pos_x# - (dir_x# * mov_spd# *0.7)
mov_y#=pos_y# - (dir_y# * mov_spd# *0.7)
If map(mov_x#, pos_y#) = 0 Then pos_x# = mov_x#
If map(pos_x#, mov_y#) = 0 Then pos_y# = mov_y#
EndIf
End Function


;---------------------------------------------------------------------------------------------------;
;************************************* EXTRA FUNCTIONS *********************************************;
;---------------------------------------------------------------------------------------------------;


Function FPS()
If MilliSecs() > old_time+1000
old_time = MilliSecs()
fps=frame_count
frame_count=0
EndIf
Color 255, 255, 255
Text 0, 0, "FPS: "+fps
frame_count=frame_count+1
End Function

Function FireInit()
For i = 0 To 255
If r > 255 Then r = 255
If g > 255 Then g = 255
If b > 255 Then b = 255
palette(i)=(r Shl 16)+(g Shl 8)+b
r=r+2: g=g+3: b=b+5
Next
End Function


Function DrawFire()
Local w=GRAPHICS_W-1
Local h=(GRAPHICS_H/2)-1

;CREATE A RANDOM BASE FOR THE FIRE
For x=0 To w
    fire(x, h) = Rand(0, 400)
Next

;DO FIRE
For y=DRAW_FROM To h-1
upper_y = y+1
If y < h-1
lower_y = y+2
Else
lower_y = upper_y
EndIf
left_pix = fire( 0, upper_y )
mid_pix =  fire( 0, upper_y )
right_pix =fire( 1, upper_y)
base_pix = fire( 0, lower_y)

For x=0 To w
left_pix = mid_pix
mid_pix = right_pix

If x < w
right_pix= fire( x+1, upper_y )
Else
right_pix= fire( x, upper_y )
EndIf
base_pix = fire( x, lower_y )

fire(x, y)=((left_pix+mid_pix+right_pix+base_pix)Shl 5)/129

;DRAW FIRE TO SCREEN BUFFER
screen(x, y)=palette( fire(x, y) )
Next
Next


End Function

;---------------------------------------------------------------------------------------------------;
;*************************************** LEVEL DATA ************************************************;
;---------------------------------------------------------------------------------------------------;

Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 3, 5, 5, 3, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Data 1, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 4, 0, 5, 5, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 4, 0, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 4, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 4, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 2, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 5, 5, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 2, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 6, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

And heres the textures for it:

One thing I wanted to do was draw variable height walls. I created this working from a tutorial that doesent explain how to do this, does anyone know how this could be done, or where I can find a tut that explains this? Cheers. 

13
Projects / What should I use
« on: August 10, 2007 »
I got 2 different versions of a demo I'm working and I cant make up my mind which one to go with. you can download it here. The second version looks a bit messy, but it changes more. Could some of you let us know which one you prefer? Thanks.

14
Blitz / Fast Fire [BB2D]
« on: August 09, 2007 »
Heres an old school fire effect I found here and converted to Blitz. I am trying to get it to run as fast as possible. So far I have managed to get the fps up from 16 to 60 on my computer, which I am quite chuffed with, but theres one thing I cant figure out.

I would like to be able to get the Y position at the top of the fire (where its no longer black), then start drawing from that position. It would be much better when drawing on higher resolutions. I figured maybe some of you guys have done this stuff before and know how this could be done.

Anyway, heres the code:
Code: [Select]
Const GRAPHICS_WIDTH = 400
Const GRAPHICS_HEIGHT = 300

Graphics GRAPHICS_WIDTH, GRAPHICS_HEIGHT, 32, 2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Dim fire(GRAPHICS_WIDTH, GRAPHICS_HEIGHT)
Dim screen_buffer(GRAPHICS_WIDTH, GRAPHICS_HEIGHT)
Dim palette(256)

;LOAD COLOUR PALETTE
For i = 0 To 255
If r > 255 Then r = 255
If g > 255 Then g = 255
If b > 255 Then b = 255
palette(i)=(r Shl 16)+(g Shl 8)+b
r=r+5: g=g+3: b=b+2
Next

;DEMO LOOP
old_time=MilliSecs()
While Not KeyHit(1)
Cls
If MilliSecs() > old_time+1000
old_time=MilliSecs()
fps=frame_count
frame_count=0
EndIf

DrawFire()

Color 255, 255, 255
Text 2, 2, fps
frame_count=frame_count+1
Flip
Wend


Function DrawFire()
Local w=GRAPHICS_WIDTH-1
Local h=GRAPHICS_HEIGHT-1

;CREATE A RANDOM BASE FOR THE FIRE
For x=0 To w
    fire(x, h) = Rand(0, 255)
Next

;DO FIRE
For y=50 To h-1 ;how do i get the top of the fire?
upper_y = y+1
If y < h-1
lower_y = y+2
Else
lower_y = upper_y
EndIf
left_pix = fire( 0, upper_y )
mid_pix =  fire( 0, upper_y )
right_pix =fire( 1, upper_y)
base_pix = fire( 0, lower_y)

For x=0 To w
left_pix = mid_pix
mid_pix = right_pix

If x < w
right_pix= fire( x+1, upper_y )
Else
right_pix= fire( x, upper_y )
EndIf

base_pix = fire( x, lower_y )
fire(x, y)=((left_pix+mid_pix+right_pix+base_pix)Shl 5) /129

;DRAW FIRE TO SCREEN BUFFER
screen_buffer(x, y)=palette( fire(x, y) )
Next
Next

;DRAW TO SCREEN
LockBuffer BackBuffer()
For y=50 To h
For x=0 To w
    WritePixelFast(x, y, screen_buffer(x, y))
Next
Next
UnlockBuffer BackBuffer()
End Function

15
C / C++ /C# / Image Not Drawiing
« on: August 09, 2007 »
Ok, I'm pretty sure this is a pointer another problem  :-\

I'm coding a demo using SDL. For the text each letter is going to be an object:
Code: [Select]
struct letter
{
    float x, y;
    char ch;
    struct letter *next;
}; typedef struct letter letter;

letter *NewLetter(letter **head)
{
    letter *newnode;

    if(!(newnode = malloc(sizeof(*newnode))))
        return NULL;
   
    newnode->next=*head;
    *head = newnode;   
    return newnode;
}
When the program starts I create a set of letters from main:
Code: [Select]
   letter *letterhead=NULL;
   
   InitLetters(30, 100, 4, 3, "SOMETHING IS GOING ON", letterhead);
Heres the function where they are created:
Code: [Select]
void InitLetters(int x, int y, int space, int randy, char* txt, letter *lh)
{
    int pos=0;
    while(*txt)
    {
        lh=NewLetter(&lh);
        lh->x=x+pos+space+Rand(-randy, randy);
        lh->y=y+Rand(-randy, randy)*3;
        lh->ch=*txt;
        pos+=FONT_WIDTH;
        txt++;
    }           
}
 
In my main loop I then draw each letter:
Code: [Select]
void DrawLetters(letter *lh)
{
    src.w=FONT_WIDTH;
    src.h=FONT_HEIGHT;
    while(lh != NULL)
    {
        exit(1);
        if(lh->ch > ' ' && lh->ch < '[')
        {
            dest.x=lh->x;
            dest.y=lh->y;
            src.x=((lh->ch-' ')%FONTSET_WIDTH)*FONT_WIDTH;
            src.y=((lh->ch-' ')/FONTSET_WIDTH)*FONT_HEIGHT;         
            SDL_BlitSurface(font, &src, screen, &dest);
        }         
        lh=lh->next;
    }         
}
Now I added 'exit(1)' to the loop in this function so the prog should quit at the first letter, but it dosent. This should mean that none of the letters were actually created in the InitLetters function.
Someone know whats wrong here?

16
Blitz / Saving Alpha Info [BB2D]
« on: August 08, 2007 »
Ok I'm working on a prog that creates a greyscale bumpmap of an image (that part works). Now i want to save it to the high 8 bits of an image, but it dosent work. For some reason it seems as if its not saving the info in the high 8 bits, or the bits are being reverted to 'all on'.

To run the code you will need an image in the same directory called: 'test.bmp'. Best keep it small, this prog is sloooow. Its meant to output the bumpmap on the left - image on the right. Anyway heres the code:

Code: [Select]
;-------------- GLOBALS ------------------;
Const FILTER_W = 3
Const FILTER_H = 3
Const FACTOR# = 1.0
Const BIAS# = 128.0
Dim filter(FILTER_W, FILTER_H)
;------------- INIT FILTER ---------------;
For y = 0 To FILTER_H-1
For x = 0 To FILTER_W-1
Read filter(x, y)
Next
Next
Data -1, -1,  0
Data -1,  0,  1
Data  0,  1,  1
;-------------- TEST PROG ----------------;
Graphics 640, 480, 32, 2
SetBuffer BackBuffer()
image = LoadImage("test.bmp")
BumpMap(image)

DrawImage image, 256, 0
DrawBump(image, 0, 0)

WaitKey
;-----------------------------------------;
Function BumpMap(image)

hx = FILTER_W / 2
hy = FILTER_H / 2

w = ImageWidth(image)
h = ImageHeight(image)

SetBuffer ImageBuffer(image)
LockBuffer

For y=0 To h-1
For x=0 To w-1

r#=0: g#=0: b#=0

For fy=0 To FILTER_H-1
    For fx=0 To FILTER_W-1
ix = (x-hx+fx+w) Mod w
iy = (y-hy+fy+h) Mod h
col = ReadPixel(ix, iy)
r# = r# + GetGElement(col) * filter(fx, fy)
g# = g# + GetGElement(col) * filter(fx, fy)
b# = b# + GetBElement(col) * filter(fx, fy)
Next
Next

r# = r# * FACTOR + BIAS
g# = g# * FACTOR + BIAS
b# = b# * FACTOR + BIAS

If r# < 0 Then r# = 0
If r# > 255 Then r# = 255
If g# < 0 Then g# = 0
If g# > 255 Then g# = 255
If b# < 0 Then b# = 0
If b# > 255 Then b# = 255

col = col And 16777215 ;& out alpha bits
alpha = ((r# * 61)+(g# * 174)+(b# * 21)) / 256 ;greyscale bumpmap
alpha = alpha Shl 24
;WRITE PIXEL BACK WITH NEW ALPHA INFO
WritePixel(x, y, col+alpha)
Next
Next

UnlockBuffer
SetBuffer BackBuffer()

End Function

;----------- RGB CONVERSION ----------------;
Function GetRElement(col)
Return (col Shr 16) And 255
End Function

Function GetGElement(col)
Return (col Shr 8)  And 255
End Function

Function GetBElement(col)
Return col And 255
End Function

Function ToRGB(r, g, b)
Return (r Shl 16) + (g Shl 8) + b
End Function
;------------- TEST FUNCTION ---------------;
Function DrawBump(image, x_pos, y_pos)
w = ImageWidth(image)
h = ImageHeight(image)
For y=0 To h-1
For x=0 To w-1
col = ReadPixel(x, y, ImageBuffer(image))
col = col Shr 24
col = ToRGB(col, col, col)
WritePixel(x_pos+x, y_pos+y, col)
Next
Next
End Function

Does anyone know how to fix this?

17
Blitz / Fractal Effect[BB2D]
« on: August 06, 2007 »
An animated fractal effect, I kind of made by accident.
Code: [Select]
AppTitle "Fractals"
Graphics 640, 480, 32, 2
SetBuffer BackBuffer()


Const recursions = 8
Const base_length = 100
Const base_lines = 3
Const ox = 320
Const oy = 240
Const spawn = 2
Const shrink# = 1.6

Type lines
Field x1, y1
Field x2, y2
Field length
Field angle#
End Type




Global off
;deg_off = 360/spawn

While Not KeyHit(1)
    Cls
off=off+1
Fractal()

Flip
Delay(1)
Wend

Function Fractal()
length = base_length
num_lines = base_lines
deg_off = (360/base_lines)+off

For i = 1 To base_lines
l.lines = New lines
l\x1=ox: l\y1=oy
l\angle# = i * deg_off
l\length = base_length
l\x2 = l\x1+Sin(l\angle)*l\length
l\y2 = l\y1+Cos(l\angle)*l\length
Next

For r = 1 To recursions
re=200: gr=100: bl=100
Color re-(r*16), gr-(r*8), bl-(r*8)

For l.lines = Each lines
Line l\x1, l\y1, l\x2, l\y2
Next

count = 0

For l.lines = Each lines
count = count + 1

If r < recursions
For i = 1 To spawn
l2.lines = New lines
l2\x1 = l\x2: l2\y1 = l\y2
l2\length = l\length/shrink#
l2\angle = l\angle - (((spawn+1)*deg_off)/2)
l2\angle = l2\angle + (i * deg_off)
l2\x2 = l2\x1+Sin(l2\angle)*l2\length
l2\y2 = l2\y1+Cos(l2\angle)*l2\length
Next

EndIf
Delete l.lines ;delete the old line

If r = 1 And count >= base_lines Then Exit
If r > 1 And count >= base_lines*(spawn ^ (r-1)) Then Exit

Next
Next
End Function
It could really do with some optimasation in that is doesent create then destroy each line each time the function is called.

18
Blitz / A-Star Pathfinding [BB2D]
« on: August 05, 2007 »
I don't know if there would be any use for this kind of thing in a demo, but heres a set of pathfinding functions I have been working on:
Code: [Select]
Graphics 640, 480, 32, 2
Global font = LoadFont(arial, 10)
SetFont font

;------------Globals----------------;
Const x_cells=20
Const y_cells=16
Const all_cells = x_cells * y_cells
Const PATH_MAX=100
Dim grid(all_cells)

Type open
    Field grid_pos
Field parent
Field score
Field move_cost
End Type

Type closed
    Field grid_pos
Field parent
Field score
Field move_cost
End Type

Dim path(PATH_MAX)
;------------Init Grid--------------;
For i = 0 To all_cells-1
    Read grid(i)
Next

DrawGrid()
PathFind(141, 186)
DrawPath()
WaitKey

;-----------------------------------------------------------------------------------------------;
;***************************** PATH FINDING FUNCTIONS ******************************************;
;-----------------------------------------------------------------------------------------------;
Function PathFind(orig, dest)

;CREATE START POINT
this.open = New open
this\grid_pos = orig
this\parent=-1
this\score=0
this\move_cost=0

Repeat
;FIND LOWEST SCORE
lowest.open = First open
For this.open = Each open
If this\score < lowest\score Then lowest = this
Next
 
;IF TARGET REACHED
If lowest\grid_pos = dest
GetPath(lowest.open)
Return True
EndIf

;CHECK EACH ADJACENT SQUARE TO TILE
For y=-x_cells To x_cells Step x_cells
    For x=-1 To 1
    new_pos = lowest\grid_pos+x+y
If(PathIsValid(lowest\grid_pos, x, y, new_pos)=True)

;GET MOVE COST TO ADJACENT TILE
If(y <> 0 And x <> 0)
move_cost = grid(new_pos)+(grid(new_pos)*0.4)
Else
move_cost = grid(new_pos)
EndIf

;GUESS MOVE COST FROM TILE TO TARGET
;need a better method
dx#=Abs((dest Mod x_cells)-(new_pos Mod x_cells)-1)
dy#=Abs((dest / x_cells)-(new_pos / x_cells))
distance = Sqr((dx*dx)+(dy*dy))*10

goodness = distance + move_cost +lowest\move_cost

;CHECK IF DESTINATION IS ON OPEN LIST
check=0
For test.open = Each open
If test\grid_pos = new_pos
If test\score < goodness
check =1
Else
Delete test.open
EndIf
Exit
EndIf
Next
;IF NOT ADD TO OPEN LIST
If check = 0
create.open = New open
create\parent = lowest\grid_pos
create\move_cost = lowest\move_cost + move_cost
create\grid_pos = new_pos
create\score = goodness
EndIf

;DEBUG TEXT - DEMO STUFF-------------------------------------------------,
Color 200, 0, 0 ;
Text(((new_pos Mod x_cells)*32)+22, ((new_pos / x_cells)*32)+2, move_cost)
Color 0, 200, 0
Text(((new_pos Mod x_cells)*32)+18, ((new_pos / x_cells)*32)+23, distance)
Color 0, 0, 200
If check = 0
Text(((new_pos Mod x_cells)*32)+2, ((new_pos / x_cells)*32)+23, create\score)
EndIf
Delay(50) ;
;------------------------------------------------------------------------'
EndIf
Next
Next

;MOVE CURRENT TILE FROM OPEN LIST TO CLOSED LIST
cl.closed = New closed
cl\grid_pos = lowest\grid_pos
cl\parent = lowest\parent
cl\score = lowest\score 
Delete lowest.open

Forever
End Function
;-----------------------------------------------------;
Function GetPath(lowest.open)
waypoint = lowest\parent
count = 0
While waypoint <> -1
For c.closed = Each closed
If c\grid_pos = waypoint Then Exit
Next
path(count)=c\grid_pos
waypoint=c\parent
count=count+1
Wend
End Function
;-----------------------------------------------------;
Function PathIsValid(this_pos, x, y, pos)
If x=0 And y=0 Then Return False
If pos < 0 Or pos >= all_cells Then Return False
If this_pos Mod x_cells = 0 And pos Mod x_cells = x_cells-1 Then Return False
If this_pos Mod x_cells = x_cells-1 And pos Mod x_cells = 0 Then Return False
If(PathIsBlocked(pos)=True) Then Return False
For cl.closed = Each closed
If cl\grid_pos = pos Return False
Next
Return True
End Function

;--------OBSTRUCTING TILE REFERENCES GO IN HERE-------;
Function PathIsBlocked(pos)
    If grid(pos) = -1 Return True
Return False
End Function

;-----------------------------------------------------------------------------------------------;
;***************************** DEMO FUNCTIONS **************************************************;
;-----------------------------------------------------------------------------------------------;
Function DrawGrid()
For x=0 To 640 Step 32
    For y=0 To 480 Step 32
    Color 255, 255, 255
Line x, 0, x, 480
    Line 0, y, 640, y
   
tile=grid(((y/32)*x_cells)+(x/32))
    If tile = -1
Color 80, 80, 80
Rect x, y, 32, 32, 1
    Else If tile = -2
Color 120, 30, 30
Rect x, y, 32, 32, 1
    Else If tile = -3
Color 30, 120, 30
Rect x, y, 32, 32, 1
    Else
    Text x+3, y+2, tile
    EndIf
Next
Next 
End Function

Function DrawPath()
While path(i) <> 0
Color 200,200, 50
Oval (path(i) Mod x_cells)*32+12, (path(i) / x_cells)*32+12, 8, 8, 1
Color 20,20, 100
Text (path(i) Mod x_cells)*32+14, (path(i) / x_cells)*32+11, i
i=i+1
Wend
End Function
;-----------------------------------------------------------------------------------------------;
;***********************************************************************************************;
;-----------------------------------------------------------------------------------------------;

Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, -1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, -2, 10, 10, -1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, -1, -1, -1, -1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, -3, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
Data 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
I should be converting this C once I got my code for linked lists sorted out.

19
C / C++ /C# / Plasma effect in SDL
« on: June 22, 2007 »
I'm learning how to use SDL at the moment. So far it has been quite fun. Heres a plasma effect you might recognize:
Code: [Select]
#include <SDL\SDL.h>
#include <math.h>

#define GRAPHICS_WIDTH 640
#define GRAPHICS_HEIGHT 480
#define DEG 0.017453292519943296

SDL_Surface *screen=NULL;
SDL_Rect src, dest;
SDL_Event event;   

int genadd1 = 0, genadd2 = 0;
float sintable[2880];
float costable[2880];
Uint32 colourtable[1024];

void Plasma()
{
    int loopx,loopy;// Loop variables.
    int cv1,cv2,ca1,ca2;// Used in calculations.
    int draw_point=0;
    genadd1+=4;// Change these additions
    genadd2+=3;// For different speed X+Y.
    Uint32 *pix;
   
    if(genadd1>360) genadd1=genadd1-360;
    if(genadd2>360) genadd2=genadd2-360;

    ca1 = 50+49*costable[genadd1];// You can play with these vars.
    ca2 = 50+49*sintable[genadd2];// Strange things may happen though.

    for(loopy=0; loopy<GRAPHICS_HEIGHT; loopy++)
    {
        cv1 = 205+(ca1*sintable[genadd2+loopy]);     
        for(loopx=0; loopx<GRAPHICS_WIDTH; loopx++)
        {
            cv2 = 400+(ca2*costable[genadd1+loopx]);
            pix=screen->pixels+draw_point;
            *pix=colourtable[cv1+cv2];
            draw_point+=4;
        }
    }
}

void ColourGen()
{
     int colourload;
     int basered=0, basegreen=0, baseblue=255;
     int redbounce=0, greenbounce=0, bluebounce=0;
     
     for(colourload=0; colourload<1024; colourload++)
     {
         if(redbounce==0)
         {
             basered++;
             if(basered>100)
             { 
                 basered=255;
                 redbounce=1;
             }
         }
         if(greenbounce==0 && colourload>512)
         {
             basegreen+=2;
             if(basegreen>255)
             {
                 basegreen= 255;
                 greenbounce=1;
             }
         }
         if(bluebounce==0)
         {
             baseblue--;
             if(baseblue<0)
             {
                 baseblue = 255;
                 bluebounce=1;
             }
         }
         if(redbounce==1)
         {
             basered-=3;
             if(basered<0)
             {
                 basered = 0;
                 redbounce=0;
             }
         }
         if(greenbounce==1)
         {   
             basegreen-=1;
             if(basegreen<0)
             {
                 basegreen=0;
                 greenbounce=0;
             }
         }
         if(bluebounce==1)
         {   
             baseblue++;
             if(baseblue>255)
             {
                 baseblue = 0;
                 bluebounce=0;
             }
         }

         colourtable[colourload]=((basered << 8)+basegreen << 8)+baseblue;
    }
}

int main(int argc, char* argv[])
{
   if(SDL_Init(SDL_INIT_VIDEO)!=0) return 1;
   screen = SDL_SetVideoMode(GRAPHICS_WIDTH, GRAPHICS_HEIGHT, 32, 0);
   if(screen == NULL) return 1;

   int a;       
   for(a=0; a<2880; a++){sintable[a]=sin(a*DEG); costable[a]=cos(a*DEG);}

   ColourGen();   
   while(1)
   {
       Plasma();           
       while(SDL_PollEvent(&event))
           if(event.type==SDL_KEYDOWN)
               return 0;
       if(SDL_Flip(screen)==-1) return 1;
   }
}
 
Its basically the example from the tutorial off the old DBF board rewritten in C. I havent been able to add a frame rate to this as SDL dosent come with built in text functions, tho I'm working on a crappy little bitmap font loader. Even without a frame timer I the difference in speed is certainly noticeable because it is possible to do the pixel writing within the plasma function, without having to send the coords into a another function then work out the memory address for each pixel. Anyway, maybe I'll have a go at drawing something on top of this.

20
General coding questions / Reflection in 2D
« on: June 22, 2007 »
If something moving at angle_a hits a flat surface that runs along angle_b how could I work out the angle at which the thing moving along angle_a bounces off the surface? I did trigonometry once but forgot it all :(

If someone could give me a little explanation, about how I could do this it would be very helpful :)

Pages: [1] 2 3