Re: Selfcalling Loop
Posted: Thu Jul 28, 2011 4:21 pm
Hey, i dont want to create a new topic only for this... So i know pure basic has inline asm support but has it got also inline c++ support? Or is there any way to make it support it?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
; 2D Top-Down "RPG"
;------------------
Macro EventSink()
ev = WindowEvent():While ev:If ev=#PB_Event_CloseWindow:End:EndIf:ev=WindowEvent():Wend
EndMacro
Declare gameUp()
Declare moveEngine()
Declare drawEngine()
Declare mapCreate()
Declare spriteCreate()
Global char_x = 4
Global char_y = 4
Enumeration
#MainWindow
#Walls
#Player
#Monsters
#Map
EndEnumeration
gameUp()
Repeat
EventSink()
moveEngine()
drawEngine()
ForEver
Procedure gameUp()
OpenWindow(#MainWindow, 0, 0, 384, 384, "TechDemo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
InitSprite()
InitKeyboard()
OpenWindowedScreen(WindowID(#MainWindow), 0, 0, 384, 384, 1, 0, 0)
ExamineKeyboard()
spriteCreate()
mapCreate()
EndProcedure
Procedure spriteCreate()
CreateSprite(#Walls, 16, 16, 0)
CreateSprite(#Player, 8, 8, 0)
CreateSprite(#Monsters, 8, 8, 0)
; ------------------------------
StartDrawing(SpriteOutput(#Player))
Box(0, 0, SpriteWidth(#Player), SpriteHeight(#Player), #Yellow)
StopDrawing()
EndProcedure
Procedure mapCreate()
; map_height = 24
; map_width = 24
; the actual creation of the map:
x=0
StartDrawing(SpriteOutput(#Walls))
Box(x, y, SpriteWidth(#Walls), SpriteHeight(#Walls), #White)
StopDrawing()
Repeat
y=0
Repeat
If 0 = Random(5) And x > 15 And y > 15
DisplaySprite(#Walls, x, y)
EndIf
y = y + 16
Until y = 384
x = x + 16
Until x = 384
; PSEUDORANDOM GENERATION OF WALLS (THESE ARE STATIC)
DisplaySprite(#Walls, 0, 272)
DisplaySprite(#Walls, 0, 144)
DisplaySprite(#Walls, 224, 0)
DisplaySprite(#Walls, 320, 0)
GrabSprite(#Map, 0, 0, WindowWidth(#MainWindow), WindowHeight(#MainWindow))
EndProcedure
Procedure moveEngine()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_W)
char_y = char_y - 1
ElseIf KeyboardPushed(#PB_Key_S)
char_y = char_y + 1
ElseIf KeyboardPushed(#PB_Key_A)
char_x = char_x - 1
ElseIf KeyboardPushed(#PB_Key_D)
char_x = char_x + 1
EndIf
EndProcedure
Procedure drawEngine()
DisplaySprite(#Map, 0, 0)
DisplaySprite(#Player, char_x, char_y)
FlipBuffers()
EndProcedure
Code: Select all
Structure Wall_info
x.w
y.w
w.w ; width
h.w ; height
EndStructure
;{ creation of the wall list
NewList Wall.Wall_info()
; here you read the wall data
*wall_adresse = ?wall_position ; ? + wall_position give the memory adresse of the label 'wall_position:' in the data. It serves to point the data correctly
n = PeekW(*wall_adresse) ; we read the numbre of wall in order to loop correctly
*wall_adresse + 2
For a = 1 To n
AddElement(Wall())
Wall()\x = PeekW(*wall_adresse)
Wall()\y = PeekW(*wall_adresse + 2)
Wall()\w = PeekW(*wall_adresse + 4)
Wall()\h = PeekW(*wall_adresse + 6)
*wall_adresse + 8 ; on déplace le curseur de lecture
Next
;}
;{ code principal
; here we create a point which will be the thing walking in your game :
thing.POINT\x = 55
thing\y = 8
; then we test if the thing is in a wall ^^ :
ForEach Wall()
If thing\x > Wall()\x And thing\x < Wall()\x + Wall()\w And thing\y > Wall()\y And thing\y < Wall()\y + Wall()\h
Debug "Thing is in the wall index " + Str(ListIndex(Wall()))
EndIf
Next
;}
End
;{ Here you stock the wall data
DataSection
wall_position:
Data.w 4 ; number of wall
Data.w 0, 0, 10, 20 ; coordinate x, y, w, h d'un mur
Data.w 50, 0, 10, 20
Data.w 0, 50, 10, 20
Data.w 50, 50, 10, 20
EndDataSection
;}
Code: Select all
AddElement(Wall())
Wall()\x = 0
Wall()\y = 2
wall()\w = 36
wall()h = 9
AddElement(Wall())
Wall()\x = 10
Wall()\y = 12
wall()\w = 36
wall()h = 19
etc....
Code: Select all
;{ creation of the wall list
NewList Wall.Wall_info()
; here you read the wall data
Restore wall_position
Read.w n ; we read the number of wall in order to loop correctly
For a = 1 To n
AddElement(Wall())
Read.w = Wall()\x
Read.w Wall()\y
Read.w Wall()\w
Read.w Wall()\h
Next
;}