Skip to content

Commit

Permalink
Set up basic camera functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tredfern committed Jan 28, 2020
1 parent 618f4f2 commit 45fbc85
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
37 changes: 37 additions & 0 deletions moonpie/graphics/camera.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Copyright (c) 2020 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local Camera = {}

function Camera:new()
local c = setmetatable({}, { __index = Camera })
c.x = 0
c.y = 0
c.scale_x = 1
c.scale_y = 1
return c
end

function Camera:set_position(x, y)
self.x = x
self.y = y
end

function Camera:activate()
love.graphics.push()
love.graphics.translate(self.x, self.y)
love.graphics.scale(self.scale_x, self.scale_y)
end

function Camera:deactivate()
love.graphics.pop()
end

function Camera:scale(sx, sy)
self.scale_x = sx
self.scale_y = sy
end

return Camera
48 changes: 48 additions & 0 deletions moonpie/graphics/camera_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Copyright (c) 2020 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("Camera", function()
local Camera = require "moonpie.graphics.camera"
local mock_love = require "moonpie.test_helpers.mock_love"

it("initializes to an inconsequential camera", function()
local c = Camera:new()
assert.equals(0, c.x)
assert.equals(0, c.y)
assert.equals(1, c.scale_x)
assert.equals(1, c.scale_y)
end)

it("pushes on the translation stack on activate", function()
mock_love.override_graphics("push", spy.new(function() end))
local c = Camera:new()
c:activate()
assert.spy(love.graphics.push).was.called()
end)

it("pops the stack when the camera is deactivated", function()
mock_love.override_graphics("pop", spy.new(function() end))
local c = Camera:new()
c:activate()
c:deactivate()
assert.spy(love.graphics.pop).was.called()
end)

it("translates based on the position of the camera", function()
mock_love.override_graphics("translate", spy.new(function() end))
local c = Camera:new()
c:set_position(43, 29)
c:activate()
assert.spy(love.graphics.translate).was.called_with(43, 29)
end)

it("scales the camera", function()
mock_love.override_graphics("scale", spy.new(function() end))
local c = Camera:new()
c:scale(10, 11)
c:activate()
assert.spy(love.graphics.scale).was.called_with(10, 11)
end)
end)
1 change: 1 addition & 0 deletions moonpie/test_helpers/mock_love.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ love = {
push = function() end,
rectangle = function(mode, x, y, w, h) end,
reset = function() end,
scale = function() end,
setCanvas = function() end,
setColor = function() end,
setFont = function() end,
Expand Down

0 comments on commit 45fbc85

Please sign in to comment.