diff --git a/moonpie/graphics/camera.lua b/moonpie/graphics/camera.lua new file mode 100644 index 0000000..87a2f91 --- /dev/null +++ b/moonpie/graphics/camera.lua @@ -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 \ No newline at end of file diff --git a/moonpie/graphics/camera_spec.lua b/moonpie/graphics/camera_spec.lua new file mode 100644 index 0000000..0b572d8 --- /dev/null +++ b/moonpie/graphics/camera_spec.lua @@ -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) \ No newline at end of file diff --git a/moonpie/test_helpers/mock_love.lua b/moonpie/test_helpers/mock_love.lua index e2b248c..6f8aa62 100644 --- a/moonpie/test_helpers/mock_love.lua +++ b/moonpie/test_helpers/mock_love.lua @@ -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,