1
C / C++ /C# / Re: Trying to optimise fadeout
« on: July 18, 2008 »
Cool. I have wanted to learn how to use the MMX registers sometime. Its just something I havent got around to doing. I'll come back to this later on
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.
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.
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 FunctionCheers.
;******************************************************************;
;----------------------- DATA TYPES REQUIRED ----------------------;
;******************************************************************;
Const MAX_SPLINE_SIZE = 1000
Const CURVE_DETAIL = 50
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 -------------------------;
;******************************************************************;
Graphics 800,600,32, 1
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Read size
For i=0 To size
Read x
Read y
temp.point(i) = NewPoint(x, y)
Next
s.spline = NewSpline()
For a = 0 To 9
f.runner = CreateRunner(s)
f\loc = a*4
Next
Global hold = -1 ;indicates which point is held
Global scroll_x=0, scroll_y=0
While Not KeyHit(1)
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)
RunnerDot(r.runner)
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)
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
EndIf
Else
If r\loc+4 >= r\s\size ;End of spline
r\loc=0
Return
EndIf
ptx1 = r\s\pts[r\loc+2]\x: pty1 = r\s\pts[r\loc+2]\y
ptx2 = r\s\pts[r\loc+3]\x: pty2 = r\s\pts[r\loc+3]\y
ptx3 = r\s\pts[r\loc+4]\x: pty3 = r\s\pts[r\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)
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
Function RunnerDot(r.runner)
overlap = False
For i=0 To RUNNER_SIZE-1
If r\pos[i] > r\pos[i+1] Then overlap = True
Next
If overlap = False
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
Else If r\loc+4 < r\s\size
ptx1 = r\s\pts[r\loc+2]\x: pty1 = r\s\pts[r\loc+2]\y
ptx2 = r\s\pts[r\loc+3]\x: pty2 = r\s\pts[r\loc+3]\y
ptx3 = r\s\pts[r\loc+4]\x: pty3 = r\s\pts[r\loc+4]\y
EndIf
x2 = QuadraticBezier(r\pos[RUNNER_SIZE] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[RUNNER_SIZE] , pty1, pty2, pty3)
Color 255, 0, 0
Oval x2-1, y2-1, 3, 3, 1
End Function
Data 52, 100, 180, 10, 120, 60, 90, 40, 120, 100, 180, 60, 120, 80, 90, 70, 120, 100, 180, 130, 120, 120, 90, 140, 120, 100, 180, 160, 120, 140, 90, 190, 120, 100, 180, 20, 140, 30, 180, 37, 209, 70, 210, 30, 190, 60, 180, 80, 180, 100, 190, 120, 180, 150, 180, 180, 190, 130, 210, 170, 210, 180, 180, 189, 151, 100, 180, 110, 260, 90, 290, 20, 420, 50, 480, 130, 330, 190, 350, 218, 359, 160, 410, 110, 450, 220, 370, 244, 352, 170, 320, 142, 308, 50, 480, 20, 430, 50, 340, 59, 311, 20, 270, 60, 270, -21, 226
;******************************************************************;
;----------------------- 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
Would have been nice to see it animated.
It's a very important subject, especially when you try to apply it to internet traffic routing.Yeah, I heard that a lot of routing protocols use it.
A* can also find items without knowing exactly where to look much the same as Dijkstra's meathod. I forget the actual way its done but I think you only have to consider how far you have travelled (since you dont know how far away from the destination you are) and keep going until you find what you are looking. Its not as efficient but when you are using the same code to do different jobs its easy to work with.Yeah I guess you could find it without a target just ignoring the heuristic. Then it would pretty much be dijkstra's algo.