2D Cameras

BlitzMax Forums/BlitzMax Programming/2D Cameras

Twinprogrammer(Posted 2012) [#1]
Hello,

I was wondering how you would make a 2d camera(like is it something to do with the viewport).


GW(Posted 2012) [#2]
veiwport is the easiest way to do it, assuming your using a fixed map. You can keep a few global variables for xOffset and yOffset and 'SetOrigin' with these values. If you want mouse style scrolling just check for the mouse at the borders of the screen and add subtract a fixed step from the offset globals for the right and bottom edges. add the step value for the top and left edge.


Twinprogrammer(Posted 2012) [#3]
Could you give an example ?


GW(Posted 2012) [#4]
Here is a quick-n-dirty example.
SuperStrict
Framework brl.retro
Import brl.glmax2d

Graphics 800,600

'// draw many rects to simulate many images drawn per frame
For Local I% = 0 Until 20
	DrawRect(Rand(700),Rand(500),Rand(20),Rand(40))
Next

Global image:TImage = LoadImage(GrabPixmap(0,0,800,600))
Global xOff%, yOff%

While Not KeyHit(key_escape)
	Cls
		xoff :+ (KeyDown(key_left)*5)
		xoff :- (KeyDown(key_right)*5)
		
		SetOrigin xOff,yOff
		
		DrawImage(image,0,0)
		DrawRect(1200,300,200,200)
	Flip
Wend



Twinprogrammer(Posted 2012) [#5]
Would I be able to make multiple cameras with this example ?


Derron(Posted 2012) [#6]
Store individual xoff/yoff fields for each camera type.

and then set the usedCamera-Variable to the one you want.



bye
Ron


Twinprogrammer(Posted 2012) [#7]
What I meant to say is would I be able to have multiple cameras on screen at once(Like split screen)?

Last edited 2012


Derron(Posted 2012) [#8]
Just limit the Draw-functions to the visible area of each camera.

if camera1 is set to display 0,0-400,600 and camera2 for 401,0-800,600
then you have to call your draw-functions with that borders.

something like this
Function CameraDraw(x:int,y:int,w:int,h:int)
	SetViewPort(x,y,w,h) 'or use a drawcommand with a similar function
	For local obj:TSprites = eachin MySpriteList
		if obj.x+obj.w >= x or obj.x < x+w or obj.y+obj.h >= y or obj.y < y+h
			obj.Draw(...)
		endif
	Next
End Function


bye
Ron