Selfcalling Loop

Advanced game related topics
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

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?
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: Selfcalling Loop

Post by graph100 »

this is somehow way out of the topic ^^

anyway, there is no C++, or C inline support, and I don't think there would be a way, because the purebasic is converted to asm, and not C or C++.
But, I have converted come code found in C easily, even if I don't know this language.
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Okey, back to the topic. As i used the suggested method to create a new sprite for the walls, i now fail to create the collision detection, i tried two methods: detecting the collision of the whole block, but this way, it would get stuck in the whole map, and i tried to use the actual x, and y coordinates of the whole map sprite, but it generates the same problem.
So any suggestions how to make the collision detection?

Eager for some answers. :D
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Should i try to save the actual map data? So write a file and then re-build it after every buffer flipped?

The code atm looks like this:

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
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: Selfcalling Loop

Post by graph100 »

First I recommend you not to place the wall 'with the hands'.
Use a list of wall, it will be so simply then to test each wall and find if you are into one of them, ok ?

do something like this :

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

;}
I even do the detection thing. You should be able to use that code to advance ^^
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Thank you for your answer, i'm studying your code.. seems complicated :) . I will get back here if i have any problems.
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: Selfcalling Loop

Post by graph100 »

well the complicated part is to store the wall position and read them.
You can also enter them manually like :

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....
But I think it is pretty not efficient.

To read data from a data section you can also use the following : (documented in the pb manual)

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

;}
Many different manners do achieve the same result.
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

I see, but this method seems to be creating static walls, not random ones right?
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: Selfcalling Loop

Post by graph100 »

I just wanted to have the same result as your code.
Now you just have to put random values in the list to have random walls !! that's the positive thing about using a list !
You can even create moving wall. by modifying the wall position into the list

I showed you a method to build level. Because usually the level are all the same each time you play.
You can store the levels data into the executable, or into external files, as you like.
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
sprenio
User
User
Posts: 25
Joined: Sat Jul 16, 2011 1:06 am

Re: Selfcalling Loop

Post by sprenio »

Okey, it gets clearer now, thanks :)
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: Selfcalling Loop

Post by AndyMK »

Hey sprenio, isn't this community the best? Trust me when i say you will not find this kind of support from users anywhere else :D I love the way people are so willing to help here and the knowledge is amazing! Makes me feel good when i see topics like this :mrgreen:
Post Reply