Re: You could write 2D games on the CanvasGadget
Posted: Mon Jul 18, 2011 6:42 pm
iirc I never said you should use a canvas for 2D games, only that its graphics performance is good enough that you could. If you did you'd also have to write your own collision routines. Anyway, that said, here's a small demo moving thousands of objects on a canvas gadget. You be the judge if it would support a 2D game or not. Turn the debugger off for smoothest results:
Code: Select all
Structure shrapnel
x.f
y.f
u.f
v.f
life.f
s.f
c.l
EndStructure
Global NewList dot.shrapnel()
CreateImage(0, 64, 64, 32|#PB_Image_Transparent)
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_Gradient|#PB_2DDrawing_AllChannels)
BackColor (RGBA(0,0,255,255))
FrontColor(RGBA(0,0,0,0))
EllipticalGradient(20, 20, 64, 64)
Circle(31, 31, 31)
StopDrawing()
CreateImage(1, 64, 64, 32|#PB_Image_Transparent)
StartDrawing(ImageOutput(1))
DrawingMode(#PB_2DDrawing_Gradient|#PB_2DDrawing_AllChannels)
BackColor (RGBA(255,0,0,255))
FrontColor(RGBA(0,0,0,0))
EllipticalGradient(20, 20, 64, 64)
Circle(31, 31, 31)
StopDrawing()
OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(0,0,0,640,480)
Procedure BlowemUp(spotx,spoty)
For g=0 To 900
AddElement(dot())
angle.f=Random(359)*3.141592/180
dot()\life=1+Random(254)
dot()\u=Cos(angle)*Random(128)/64
dot()\v=Sin(angle)*Random(128)/64
dot()\v=dot()\v-2
dot()\x=spotx
dot()\y=spoty
dot()\s=Random(15)/10
dot()\c = Random($FFFFFF)
Next g
EndProcedure
x=0:y=0:quit=0:f=1:d=1:rt=576:bt=416:x1=350:y1=100:f1=1:d1=1
Repeat
ev=WindowEvent()
While ev
If ev=#PB_Event_CloseWindow:quit=1:EndIf
ev=WindowEvent()
Wend
If f:x+1:If x>=rt:f=0:EndIf:Else:x-1:If x<=0:f=1:EndIf:EndIf
If d:y+1:If y>=bt:d=0:EndIf:Else:y-1:If y<=0:d=1:EndIf:EndIf
If f1:x1+1:If x1>=rt:f1=0:EndIf:Else:x1-1:If x1<=0:f1=1:EndIf:EndIf
If d1:y1+1:If y1>=bt:d1=0:EndIf:Else:y1-1:If y1<=0:d1=1:EndIf:EndIf
StartDrawing(CanvasOutput(0))
Box(0,0,640,480,$000000)
DrawAlphaImage(ImageID(0),x,y)
DrawAlphaImage(ImageID(1),x1,y1)
If ListSize(dot())<400
BlowemUp(100+Random(480),100+Random(280))
EndIf
ForEach dot()
dot()\x=dot()\x+dot()\u
dot()\y=dot()\y+dot()\v
dot()\v=dot()\v+0.05
If dot()\x=>(0) And dot()\x<=(639) And dot()\y=>(0) And dot()\y<=(479)
c=dot()\life
c=c+c<<8+c<<16
Circle(dot()\x,dot()\y,1,dot()\c)
EndIf
dot()\life=dot()\life-1:If dot()\life<=0:DeleteElement(dot()):EndIf
Next
StopDrawing()
Delay(5)
Until quit
End