[SOLVED] Doubt with STI and Camera libraries

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

[SOLVED] Doubt with STI and Camera libraries

Post by AdrianN »

Hi guys, I had a doubt with the integration of the map using STI and a camera.
I tried with some libraries, like Gamera, Stalker-X and Cam11, I usually use Gamera in my projects, but I have problem with the scale (when scale is less that 0.5) and anothers mistakes, and the other libs doesn't work well with tilemap, or maybe is a error of my code.

I made a project to test the Cameras libraries but only with Gamera I can move in the tilemap, with trick.

I'm looking for way to scale the tilemap and camera together, and use other functionalities like angle in my futures projects.

Then, what is the best way to integrate Sti and these cameras libraries?

For example: STI and Gamera

Code: Select all

function camera_gamera:draw(map)
    local cx,cy,_,_=cam:getVisible()
    
    map:draw(-cx,-cy,scale,scale)
    cam:draw(function(l,t,w,h)
        --map:draw(-cx,-cy,scale,scale)
       
        love.graphics.print("hello world", 100,100)
    end)
    
end 
I always use this code in my projects but the tilemap is out of the camera. also I can't set angle and I had problem with the scale when I put map:draw in the cam:draw function.
Attachments
CameraTest.love
(902.09 KiB) Downloaded 126 times
Last edited by AdrianN on Fri Jun 26, 2020 7:34 am, edited 2 times in total.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Doubt with STI and Camera libraries

Post by Andlac028 »

I looked into STI library, and found, that draw function resets coordinates (inside of love.graphics.push and pop = it's not affected by transformation of camera), so you have to pass tx,ty - translateX,Y to map:draw (it's line 813 of libs/STI/sti/init.lua)
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: Doubt with STI and Camera libraries

Post by AdrianN »

Andlac028 wrote: Wed Jun 24, 2020 2:20 pm I looked into STI library, and found, that draw function resets coordinates (inside of love.graphics.push and pop = it's not affected by transformation of camera), so you have to pass tx,ty - translateX,Y to map:draw (it's line 813 of libs/STI/sti/init.lua)
Hello, Yes I use the tx,ty in draw function.

Code: Select all

-- tx ty sx sy
map:draw(-cx,-cy,scale,scale)
But exist another way to syncronize the camera with the map?

I had this, when I set scale in 0.5 with gamera.

Image

Then I need to add this:

Code: Select all

function camera_gamera:draw(map)
    local cx,cy,_,_=cam:getVisible()
    
    --map:draw(-cx,-cy,1/scale,1/scale)
    cam:draw(function(l,t,w,h)
        map:draw(-cx,-cy,1/scale,1/scale)
       
        love.graphics.print("hello world", 100,100)
    end)
    
end 

But for example if I setAngle to the camera, the map no change it's angle.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Doubt with STI and Camera libraries

Post by Andlac028 »

AdrianN wrote: Wed Jun 24, 2020 9:08 pm But exist another way to syncronize the camera with the map?
Yes, comment out lines 820-822 and line 830 (lg.push, origin, translate and pop functions are here) in libs/STI/sti/init.lua
AdrianN wrote: Wed Jun 24, 2020 9:08 pm I had this, when I set scale in 0.5 with gamera.
Comment out line 836 (lg.scale) in libs/STI/sti/init.lua

After all, it looks like that:

Code: Select all

--- Draw every Layer
-- @param tx Translate on X
-- @param ty Translate on Y
-- @param sx Scale on X
-- @param sy Scale on Y
function Map:draw(tx, ty, sx, sy)
	local current_canvas = lg.getCanvas()
	lg.setCanvas(self.canvas)
	lg.clear()

	-- Scale map to 1.0 to draw onto canvas, this fixes tearing issues
	-- Map is translated to correct position so the right section is drawn
	--lg.push() -- **not needed anymore when 2 lines bellow are removed**
	--lg.origin() -- **COMMENT OUT - resets position**
	--lg.translate(math.floor(tx or 0), math.floor(ty or 0)) -- **COMMENT OUT - sets position to suplied tx and ty arguments**

	for _, layer in ipairs(self.layers) do
		if layer.visible and layer.opacity > 0 then
			self:drawLayer(layer)
		end
	end

	--lg.pop() -- not needed anymore

	-- Draw canvas at 0,0; this fixes scissoring issues
	-- Map is scaled to correct scale so the right section is shown
	lg.push()
	lg.origin()
	--lg.scale(sx or 1, sy or sx or 1) -- **COMMENT OUT - sets scale**

	lg.setCanvas(current_canvas)
	lg.draw(self.canvas)

	lg.pop()
end
That's because STI tries to not affect global transformation
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Doubt with STI and Camera libraries

Post by pgimeno »

Arbitrary transformations get ugly with spritebatches (tiled maps). The seams between tiles usually become visible. To avoid this, the spritebatch can be drawn to a canvas without any transform, and then that canvas can be transformed. It can become difficult to handle.

I guess the STI creators restricted the transformations to scaling and moving for simplicity. To work with a camera and support rotation, you will need to modify the rendering so it goes to a sufficiently large canvas that can fit the diagonals of the visible window at any angle. Not easy.
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: Duda con las bibliotecas de ITS y Cámara

Post by AdrianN »

Sure, I'm understanding.
Then, I need to use my custom TileMap Renderer to use transformation in my game.
I had the doubth month ago, at the moment I only scale the tilemap but I had these mistakes that I named previously when I deploy my game in an android device.

Thanks guys.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests