Scrolling demo for an RTS game attempt...

Advanced game related topics
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Scrolling demo for an RTS game attempt...

Post 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
r_hyde
Enthusiast
Enthusiast
Posts: 155
Joined: Wed Jul 05, 2006 12:40 am

Post 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)
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post 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
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post 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)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Nice!
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

* echo *

Nice!
Dare2 cut down to size
SCRJ
User
User
Posts: 93
Joined: Sun Jan 15, 2006 1:36 pm

Post by SCRJ »

*echo*
echo
Nice! :lol:

:P
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

thats bad as$ .. map is like a map from a game i used to play

- np

except for the dead cows
PureLust
Enthusiast
Enthusiast
Posts: 486
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Post 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."
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post 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
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post 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.
PureLust
Enthusiast
Enthusiast
Posts: 486
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Post 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.
wolfwood2x
User
User
Posts: 76
Joined: Sun Oct 02, 2005 5:08 am

Post 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.
Post Reply