I'm developing my own game for a while now, and it's time for me to face some challenges I had a very hard time working with. They always say that you need to be good at math to code ; well, I have a pretty decent result with a very poor knowledge in math, but when it comes to camera... Ouch!
The problem is I need something specific : a scrolling camera (through a mouse button) that can zoom (in/out on cursor) and support multiple "layers" and multiple resolutions.
The idea is to have a 2D space game with a grid (layer 1), planets (layer 2), nebulae (layer 3), stars (layer 4, which remains still).
In a nutshell, something that looks professional... And for now... :
Code: Select all
Procedure.b CheckBoundaries()
Define mapW.l, mapH.l, returnFalse.b
If currentSector <> ""
mapW = grid\width
mapH = grid\height
Else
mapW = #GalaxyBackgroundSize * 3
mapH = #GalaxyBackgroundSize * 1.5
EndIf
If currentSector = ""
If diffX > mapW - ScreenWidth() ; right
diffX = mapW - ScreenWidth()
returnFalse = 1
ElseIf diffX < ScreenWidth() - mapW ; left
diffX = ScreenWidth() - mapW
returnFalse = 1
EndIf
If diffY > mapH - ScreenHeight() ; down
diffY = mapH - ScreenHeight()
returnFalse = 1
ElseIf diffY < ScreenHeight() - mapH ; up
diffY = ScreenHeight() - mapH
returnFalse = 1
EndIf
Else
If diffX > 0
diffX = 0
returnFalse = 1
ElseIf diffX < ScreenWidth() - mapW
diffX = ScreenWidth() - mapW
returnFalse = 1
EndIf
If diffY > 0
diffY = 0
returnFalse = 1
ElseIf diffY < ScreenHeight() - mapH
diffY = ScreenHeight() - mapH
returnFalse = 1
EndIf
EndIf
DoIf(returnFalse, ProcedureReturn #False)
ProcedureReturn #True
EndProcedure
Procedure Scrolling(force.b = #False)
Static cursorX.l, cursorY.l, originX.l, originY.l
If force
cursorX = mouseX
cursorY = mouseY
originX = diffX
originY = diffY
diffX = originX - (cursorX - mouseX)
diffY = originY - (cursorY - mouseY)
CheckBoundaries()
ElseIf rmbClicked
cursorX = mouseX
cursorY = mouseY
originX = diffX
originY = diffY
ElseIf (rmbPushed And (cursorX <> mouseX Or cursorY <> mouseY))
UI::blocked = #True
diffX = originX - (cursorX - mouseX)
diffY = originY - (cursorY - mouseY)
DoIf(CheckBoundaries() = #False, Scrolling(#True)) ; avoid increasing offset when scrolling out of screen
ElseIf rmbReleased
If currentSector = ""
UI::RegisterPlanetTooltips() ; register new diffX/diffY
EndIf
UI::blocked = #False
EndIf
EndProcedure
Procedure ConvertZoomDiffXY(oldZoomRate.f, newZoomRate.f)
diffX = ((diffX - mouseX) * newZoomRate / oldZoomRate) + mouseX
diffY = ((diffY - mouseY) * newZoomRate / oldZoomRate) + mouseY
EndProcedure
Procedure CenterCamera(onGalaxy.b = #True)
If onGalaxy
diffX = 0
diffY = 0
Else
zoomInfo\current = zoomInfo\max
FixZooming(#True)
diffX = (-allSectors()\statics\width + ScreenWidth()) / 2
diffY = (-allSectors()\statics\height + ScreenHeight()) / 2
diffX = (-grid\width + ScreenWidth()) / 2
diffY = (-grid\height + ScreenHeight()) / 2
EndIf
EndProcedure
It's a very old piece of code and I don't even understand half of it (it's useless to copy/paste the other procedures, it's even more of a mess).
So... Is there a library or something PB-friendly somewhere ? Or should I pay people who knows what they're doing instead ?

