diff options
Diffstat (limited to 'client/camera.lua')
| -rw-r--r-- | client/camera.lua | 23 | 
1 files changed, 15 insertions, 8 deletions
diff --git a/client/camera.lua b/client/camera.lua index bb1d8a6..d4d7901 100644 --- a/client/camera.lua +++ b/client/camera.lua @@ -2,7 +2,6 @@ local coords = require"common.coords"  local class = require"common.class"  -- in screen units -local screen_width, screen_height = love.graphics.getDimensions()  -- zoom is screen pixels per world unit  local Camera = class() @@ -11,28 +10,36 @@ function Camera.make(cls,pos,zoom)  	zoom = zoom or 30  	return setmetatable({pos=pos,zoom=zoom},cls)  end +local function screen_offset() +	-- in screen units, not in world units ! +	local W,H = love.graphics.getDimensions() +	return coords.Pos:make(W/2,H/2) +end  function Camera.apply_trans(self) -	-- love.graphics.origin() +	local so = screen_offset() +  	-- centre (0,0) in the middle of the screen -	love.graphics.translate(screen_width/2,screen_height/2) -	-- apply camera transformations +	love.graphics.translate(so.x,so.y) +	-- apply camera transformations  	love.graphics.scale(self.zoom)  	love.graphics.translate(-self.pos.x,-self.pos.y)  end -local screen_offset = coords.Pos:make(screen_width/2,screen_height/2)  function Camera.screen_to_world(self,pos) -	return (pos-screen_offset)/self.zoom + self.pos +	local so = screen_offset() +	return (pos-so)/self.zoom + self.pos  end  function Camera.world_to_screen(self,pos) -	return (pos-self.pos) * self.zoom + screen_offset +	local so = screen_offset() +	return (pos-self.pos) * self.zoom + so  end  function Camera.extents(self) +	local W,H = love.graphics.getDimensions()  	-- returns top left and bottom right pos's in world coords  	return self:screen_to_world(coords.Pos:make(0,0)), -			self:screen_to_world(coords.Pos:make(screen_width,screen_height)) +			self:screen_to_world(coords.Pos:make(W,H))  end  | 
