Page 1 of 1

Scrolling demo for an RTS game attempt...

Posted: Wed Jul 18, 2007 12:59 am
by byo
Ok, so this is pretty basic and if you guys can help I'd appreciate.
Right now, there are no units on the map, so there's not much to do.

But it's a start. Here it is:
http://rapidshare.com/files/43518186/byoGame.zip.html

Code: Select all

;Little scrolling engine by Andre Guerreiro (byo)
;It's very simple and the code is a mess so if you can
;give me hints on easier approaches, I'd gladly appreciate it. :)

;Oh, and Purebasic rules!
;==========================================================


;Declaration of the things (procedures) to come!
Declare Init()
Declare DeInit()
Declare DrawMini()

;Some constants for the basic stuff as ScreenWidth and ScreenHeight for speed
#sW = 640
#sH = 480

;Enumerating images
Enumeration
	#map
	#mini
EndEnumeration

;Let's include images in the executables the PB traditional way
DataSection
	map:
		IncludeBinary "map.jpg"
	mini:
		IncludeBinary "mini.jpg"
EndDataSection

;Variables that will be there til the end
Global mouseX1, mouseX2, mouseY1, mouseY2 
Global map, mini, mapX, mapY, scrollSpeed, miniX, miniY, miniW, miniH
Global MiniSelectable, miniMouseX1, miniMouseY1
Global mapW, mapH

;Every initialization before the loop is done inside this procedure
;for easy reading.
Init()

;The famous loop
Repeat
	ExamineKeyboard()
	ExamineMouse()
	
	;I'm not sure if this is working or not. Tell me if it's not, please.
	ReleaseMouse(1-IsScreenActive())
	
	;Our way out of the program by pressing ESC
	If KeyboardPushed(#PB_Key_Escape)
		Exit = #True
	EndIf
	
	;Captures the mouse inside the minimap and moves the map according to its location.
	;My math is ugly so please help me improve, commenting what you're doing cause I
	;suck at math. It's just about proportion and location.
	If MouseButton(#PB_MouseButton_Left)
		If (MouseX() >= miniX) And (MouseX() < miniX+miniW) And (MouseY() >= miniY) And (MouseY() < miniY+miniH)
			If MB_Left <> 2
				mapX = -((MouseX()-#sW/20)-miniX)*20-#sW/2
				mapY = -((MouseY()-#sH/20)-miniY)*20-#sH/2
				If mapX > 0 : mapX = 0 : EndIf
				If mapY > 0 : mapY = 0 : EndIf
				If mapX < -ImageWidth(#map)+#sW : mapX = -ImageWidth(#map)+#sW : EndIf
				If mapY < -ImageHeight(#map)+#sH : mapY = -ImageHeight(#map)+#sH : EndIf
			EndIf
		Else
			;Thanks to Joakim Christiansen for this next little workaround routine
			;while there is no MouseDown() native event.
			If MB_Left = 0 
				MB_Left = 1 
			Else 
				MB_Left = 2 
			EndIf
		EndIf
	Else
		If MB_Left = 3 
			MB_Left = 0 
		ElseIf MB_Left = 2 
			MB_Left = 3 
		EndIf 
	EndIf
	
	;This is where we draw everything.
	StartDrawing(ScreenOutput())
	DrawImage(ImageID(#map), mapX, mapY)
	DrawMini()
	DrawingMode(#PB_2DDrawing_Transparent)
	DrawText(miniX+miniW+5, miniY+5, "This is a little scrolling demo with minimap made by byo (Andre Guerreiro)", #White)
	DrawText(miniX+miniW+5, miniY+18, "Use the arrow keys or the mouse to scroll. You can drag a selectable region", #White)
	DrawText(miniX+miniW+5, miniY+31, "by holding the left mouse button. Sorry for the messy code.", #White)
	DrawText(miniX+miniW+5, miniY+60, "Click on the minimap and you'll get to the point selected by the mouse", #White) 
	StopDrawing()
	
	;Let's capture the left mouse button and create a selecion region if it's down.
	;0 is UP, 1 is PRESSED, 2 is DOWN, 3 is RELEASED
	Select MB_left
		Case 0			
			FlipBuffers()	
		Case 1
			mouseX1 = MouseX()
			mouseY1 = MouseY()
			miniMouseX1 = mouseX1
			miniMouseY1 = mouseY1
		Case 2
			mouseX2 = MouseX() - mouseX1
			mouseY2 = MouseY() - mouseY1
			
			StartDrawing(ScreenOutput())
			Line(mouseX1, mouseY1, mouseX2, 0, #Green)
			Line(mouseX1, mouseY1, 0, mouseY2, #Green)
			Line(MouseX(), mouseY1, 0, mouseY2, #Green)
			Line(mouseX1, mouseY1+mouseY2, mouseX2, 0, #Green)				
			DrawMini()			
			StopDrawing()
			
			FlipBuffers()
		Case 3
			MB_Left = 0
	EndSelect
	
	;Scrolling the map if the mouse it's on the 4 edges of the screen or if the
	;player touches the arrow keys.
	If (MouseX() = 0) Or (KeyboardPushed(#PB_Key_Left))
		mapX + scrollSpeed		
		If mapX > 0
			mapX = 0
		Else
			mouseX1 + scrollSpeed
		EndIf
	EndIf
	If (MouseY() = 0) Or (KeyboardPushed(#PB_Key_Up))
		mapY + scrollSpeed		
		If mapY > 0
			mapY = 0
		Else
			mouseY1 + scrollSpeed	
		EndIf
	EndIf
	If (MouseX() = #sW-1) Or (KeyboardPushed(#PB_Key_Right))
		mapX - scrollSpeed		
		If mapX < -ImageWidth(#map)+#sW
			mapX = -ImageWidth(#map)+#sW
		Else
			mouseX1 - scrollSpeed	
		EndIf
	EndIf
	If (MouseY() = #sH-1) Or (KeyboardPushed(#PB_Key_Down))
		mapY - scrollSpeed		
		If mapY < -ImageHeight(#map)+#sH
			mapY = -ImageHeight(#map)+#sH
		Else
			mouseY1 - scrollSpeed	
		EndIf
	EndIf			
	
	;This is required for dealing with the window events behind the screen
	;Thanks to netmaestro.
	While WindowEvent() : Wend
Until Exit
;End of our confused loop.

;A little routine for freeing stuff inside variables.
DeInit()



Procedure Init()
	InitSprite()
	
	OpenWindow(0, #PB_Ignore, #PB_Ignore, #sW, #sH, "byo game", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
	OpenWindowedScreen(WindowID(0), 0, 0, #sW, #sH, #False, 0, 0)
	
	ClearScreen(#Black)
	StartDrawing(ScreenOutput())
	DrawingMode(#PB_2DDrawing_Transparent)
	DrawText(#sW-100, #sH-40, "LOADING...", #White)
	StopDrawing()
	
	FlipBuffers()
	
	InitKeyboard()
	InitMouse()
	
	ShowCursor_(#True)
	MouseLocate(#sW/2, #sH/2)
	
	UseJPEGImageDecoder()
	
	
	scrollSpeed = 10	
	mapX = 0 : mapY = 0
	mapW = 2400 : mapW = 2760
	miniX = 4 : miniY = #sH-142
	miniW = 120 : miniH = 138
	
	CatchImage(#map, ?map)
	CatchImage(#mini, ?mini)	
	
EndProcedure

Procedure DeInit()
	FreeImage(#map)
	FreeImage(#mini)	
EndProcedure

Procedure DrawMini()
	posX = miniX-mapX/20
	posY = miniY-mapY/20
	posW = (#sW/20)-1
	posH = (#sH/20)-1

	DrawImage(ImageID(#mini), miniX, miniY)
	Line(posX, posY, posW, 0, #White)
	Line(posX, posY, 0, posH, #White)
	Line(posX, posY+(posH), posW, 0, #White)
	Line(posX+(posW), posY, 0, posH+1, #White)	
EndProcedure

Posted: Thu Jul 19, 2007 2:15 am
by r_hyde
Impressive graphics, and it has a good feel to it! If possible, it would be nice to have the mouse-based scrolling work a little different, by having the scrolling start as the mouse nears the edge of the screen, with the scrolling speed increasing the closer you get to the edge. But this isn't bad...nope! Not bad at all 8)

Posted: Thu Jul 19, 2007 3:34 am
by byo
Hi, r_hyde.

Thanks for the comments.
The graphics aren't mine. They're from Close Combat.
It's not actually a map yet, but a scrolling image.
I should have a map data structure.

:D

Posted: Fri Jul 20, 2007 4:20 am
by Rook Zimbabwe
Byo! Great job!!!
I had been hatching a plan like this myself...

I was going to do the buildings as small 3D models (simple cubes and prisms for the roofs etc.) I had abandoned the idea because work does get in the way!!!

Your idea has me thinking again... Keep up the good work!!! 8)

Posted: Fri Jul 20, 2007 9:09 pm
by blueznl
Nice!

Posted: Sat Jul 21, 2007 2:20 am
by Dare
* echo *

Nice!

Posted: Sat Jul 21, 2007 2:22 am
by SCRJ
*echo*
echo
Nice! :lol:

:P

Posted: Sat Jul 21, 2007 4:25 am
by NoahPhense
thats bad as$ .. map is like a map from a game i used to play

- np

except for the dead cows

Posted: Sat Jul 21, 2007 2:59 pm
by PureLust
NoahPhense wrote:thats bad as$ .. map is like a map from a game i used to play
As byo already said: "The graphics aren't mine. They're from Close Combat."

Posted: Sat Jul 21, 2007 3:35 pm
by NoahPhense
PureLust wrote:
NoahPhense wrote:thats bad as$ .. map is like a map from a game i used to play
As byo already said: "The graphics aren't mine. They're from Close Combat."
I should have specified, I meant the badas$ part about the code.

But yes, now that you mention is, that's the game I used to play.

- np

Posted: Sun Jul 22, 2007 10:20 pm
by byo
Thanks. I actually think the code is a bit of a mess. :lol:

For placing units on the map, I'll need a map structure instead of simple global variables holding mapX, mapY, scrollX and a structure for the units.

Do you guys have any code or demo to post regarding RTS games engines made with PB? I'd love to see them.

Posted: Sun Jul 22, 2007 11:32 pm
by PureLust
byo wrote:Do you guys have any code or demo to post regarding RTS games engines made with PB? I'd love to see them.
Just search for "Tile engine" and you will find something which could be interesting for you. E.g.:

>Isometric demo code< by aaron
>2d map editor< by shopro or
>Isometric Tile-Engine< by HeXOR.

Posted: Mon Aug 20, 2007 9:54 pm
by wolfwood2x
It wedged on my XP box. It started and scrolled a tiny bit before freezing up. Not sure what caused it. I tried it a few times and each time I got "Not responding" Still the little bit I saw working is impressive. The scrolling functionality tied to the mini-map is quite nice.