Skip to content

Commit

Permalink
version 0.3.0 - Added getMousePosition(), windowToScreen(), screenToW…
Browse files Browse the repository at this point in the history
…indow().
  • Loading branch information
oniietzschan committed Sep 21, 2017
1 parent e3f6cfe commit 9b8b92d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 13 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end
```

Additional Functionality
-------
------------------------

```lua
-- Sets the scale factor.
Expand All @@ -53,10 +53,36 @@ screen:getScale()

-- Sets scale to the largest factor which can fit on the current monitor.
screen:setMaxScale()

-- Gets the position of the mouse cursor in virtual screen (game) coordinates.
local mouseX, mouseY = screen:getMousePosition()

-- Converts window coordinates to virtual screen (game) coordinates.
local gameX, gameY = screen:windowToScreen(windowX, windowY)

-- Converts virtual screen (game) coordinates to window coordinates.
local windowX, windowY = screen:screenToWindow(gameX, gameY)
```

Installation
------------

The most simple way to install terebi is to simply copy `terebi.lua` into your game directory and `require 'terebi'`.

Advanced users may wish to install terebi using [loverocks](https://github.com/Alloyed/loverocks), by adding something like the following to their `conf.lua`:

```lua
function love.conf(t)
t.dependencies = {
'terebi >= 0.3.0, < 2',
}
end
```

Todo
----

* Works with window resizing.
* High DPI scaling.
* Support the ability to start the game at the highest scale window which will fit on the screen.
* "You can also prevent the window from being created before main.lua is loaded, by doing t.window = false in love.conf. You will need to call love.window.setMode before calling any love.graphics functions though."
22 changes: 22 additions & 0 deletions rockspecs/terebi-0.3.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package = "terebi"
version = "0.3.0-1"
source = {
url = "git://github.com/oniietzschan/terebi",
tag = "0.3.0",
}
description = {
summary = "Graphics scaling library for Love2D.",
detailed = "A simple library to handle pixel-perfect scaling of window content in Love2D.",
homepage = "https://github.com/oniietzschan/terebi",
license = "MIT"
}
dependencies = {
"lua >= 5.1",
"love >= 0.10, < 0.11"
}
build = {
type = "builtin",
modules = {
terebi = "terebi.lua"
}
}
65 changes: 57 additions & 8 deletions spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('Terebi:', function()
before_each(function()
_G.love = {
graphics = {},
mouse = {},
window = {},
}
_G.love.graphics.newCanvas = spy.new(function(w, h)
Expand Down Expand Up @@ -206,13 +207,11 @@ describe('Terebi:', function()
local drawFunc

before_each(function()
originalCanvas = {id = 'originalCanvas'}
terebiCanvas = screen._canvas

_G.love.graphics.getCanvas = spy.new(function()
return originalCanvas
end)
_G.love.graphics.push = noop()
_G.love.graphics.setCanvas = noop()
_G.love.graphics.pop = noop()
_G.love.graphics.clear = noop()
_G.love.graphics.draw = noop()

Expand All @@ -223,11 +222,11 @@ describe('Terebi:', function()
it('should draw to canvas', function()
screen:draw(drawFunc, 'arg1', 'arg2')

assert.spy(love.graphics.setCanvas).was.called(2)
assert.spy(love.graphics.push).was.called_with('all')
assert.spy(love.graphics.setCanvas).was.called_with(terebiCanvas)
assert.spy(love.graphics.clear).was.called()
assert.spy(drawSpy).was.called_with('arg1', 'arg2')
assert.spy(love.graphics.setCanvas).was.called_with(originalCanvas)
assert.spy(love.graphics.pop).was.called()
assert.spy(love.graphics.draw).was.called_with(terebiCanvas, 0, 0, 0, 2, 2)
end)

Expand All @@ -246,11 +245,11 @@ describe('Terebi:', function()
screen._drawOffsetY = 120
screen:draw(drawFunc, 'arg1', 'arg2')

assert.spy(love.graphics.setCanvas).was.called(2)
assert.spy(love.graphics.push).was.called_with('all')
assert.spy(love.graphics.setCanvas).was.called_with(terebiCanvas)
assert.spy(love.graphics.clear).was.called()
assert.spy(drawSpy).was.called_with('arg1', 'arg2')
assert.spy(love.graphics.setCanvas).was.called_with(originalCanvas)
assert.spy(love.graphics.pop).was.called()
assert.spy(love.graphics.getColor).was.called()
assert.spy(love.graphics.getDimensions).was.called()
assert.spy(love.graphics.rectangle).was.called_with('fill', 0, 0, 640, 480)
Expand All @@ -262,5 +261,55 @@ describe('Terebi:', function()
assert.error(function() screen:draw() end)
end)
end)

describe('When calling getMousePosition', function ()
it('should convert mouse coordinates on window to screen coordinates', function ()
_G.love.mouse.getPosition = spy.new(function()
return 0, 0
end)
assert.same({0, 0}, {screen:getMousePosition()})
assert.spy(love.mouse.getPosition).was.called()

_G.love.mouse.getPosition = spy.new(function()
return 100, 256
end)
assert.same({50, 128}, {screen:getMousePosition()})
assert.spy(love.mouse.getPosition).was.called()
end)
end)

describe('When calling windowToScreen', function ()
it('should convert window coordinates to screen coordinates', function ()
assert.same({0, 0}, {screen:windowToScreen(0, 0)})
assert.same({50, 128}, {screen:windowToScreen(100, 256)})
end)

describe('When screen has a draw offset', function ()
it('should convert window coordinates to screen coordinates', function ()
screen._drawOffsetX = 30
screen._drawOffsetY = 20

assert.same({0, 0}, {screen:windowToScreen(30, 20)})
assert.same({35, 118}, {screen:windowToScreen(100, 256)})
end)
end)
end)

describe('When calling screenToWindow', function ()
it('should convert screen coordinates to window coordinates', function ()
assert.same({0, 0}, {screen:screenToWindow(0, 0)})
assert.same({100, 256}, {screen:screenToWindow(50, 128)})
end)

describe('When screen has a draw offset', function ()
it('should convert screen coordinates to window coordinates', function ()
screen._drawOffsetX = 30
screen._drawOffsetY = 20

assert.same({30, 20}, {screen:screenToWindow(0, 0)})
assert.same({130, 276}, {screen:screenToWindow(50, 128)})
end)
end)
end)
end)
end)
27 changes: 23 additions & 4 deletions terebi.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Terebi = {
_VERSION = 'terebi v0.2.0',
_VERSION = 'terebi v0.3.0',
_URL = 'https://github.com/oniietzschan/terebi',
_DESCRIPTION = 'Graphics scaling library for Love2D.',
_LICENSE = [[
Expand Down Expand Up @@ -164,13 +164,12 @@ end
function Screen:draw(drawFunc, ...)
assert(type(drawFunc) == 'function', type(drawFunc))

local previousCanvas = love.graphics.getCanvas()

love.graphics.push('all')
love.graphics.setCanvas(self._canvas)
love.graphics.clear()
drawFunc(...)
love.graphics.pop()

love.graphics.setCanvas(previousCanvas)
-- Draw background if it would be visible
if self._drawOffsetX ~= 0 or self._drawOffsetY ~= 0 then
local r, g, b = love.graphics.getColor()
Expand All @@ -184,6 +183,26 @@ function Screen:draw(drawFunc, ...)
return self
end

function Screen:getMousePosition()
return self:windowToScreen(love.mouse.getPosition())
end

function Screen:windowToScreen(x, y)
assert(type(x) == 'number')
assert(type(y) == 'number')

return (x - self._drawOffsetX) / self._scale,
(y - self._drawOffsetY) / self._scale
end

function Screen:screenToWindow(x, y)
assert(type(x) == 'number')
assert(type(y) == 'number')

return x * self._scale + self._drawOffsetX,
y * self._scale + self._drawOffsetY
end



return Terebi

0 comments on commit 9b8b92d

Please sign in to comment.