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() function Camera.make(cls,pos,zoom) pos = pos or coords.Pos:make(0,0) zoom = zoom or 30 return setmetatable({pos=pos,zoom=zoom},cls) end function Camera.apply_trans(self) love.graphics.origin() -- centre (0,0) in the middle of the screen love.graphics.translate(screen_width/2,screen_height/2) -- 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 end function Camera.world_to_screen(self,pos) return (pos-self.pos) * self.zoom + screen_offset end function Camera.extents(self) -- 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)) end return {Camera=Camera}