Skip to content

Commit

Permalink
Version 0.4.0 - Support high DPI mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
oniietzschan committed Oct 2, 2017
1 parent 9b8b92d commit 4f4e67d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ terebi
[![Codecov](https://codecov.io/gh/oniietzschan/terebi/branch/master/graph/badge.svg)](https://codecov.io/gh/oniietzschan/terebi)
[![Alex](https://img.shields.io/badge/alex-never_racist-brightgreen.svg)](http://alexjs.com/)

A simple library to handle pixel-perfect scaling of window content in Love2D.
A simple library to handle pixel-perfect scaling of window content in Love2D. Its features are:

* Simple interface for switching between fullscreen and windowed modes.
* Centers and letterboxes the view when scaled content does not exactly fit fullscreen resolution, with configurable letterbox color.
* HighDPI support with no additional code changes.
* A handful of utility functions for converting window coordinates to game coordinates, and vice versa.

Example
-------
Expand Down Expand Up @@ -74,15 +79,14 @@ Advanced users may wish to install terebi using [loverocks](https://github.com/A
```lua
function love.conf(t)
t.dependencies = {
'terebi >= 0.3.0, < 2',
'terebi >= 0.4.0, < 2',
}
end
```

Todo
----

* Works with window resizing.
* High DPI scaling.
* Support rescaling after manual window resize.
* 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.4.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package = "terebi"
version = "0.4.0-1"
source = {
url = "git://github.com/oniietzschan/terebi",
tag = "0.4.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"
}
}
48 changes: 46 additions & 2 deletions spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ local noop = function()
end

describe('Terebi:', function()
local pixelScale

before_each(function()
pixelScale = 1
_G.love = {
graphics = {},
mouse = {},
Expand All @@ -25,6 +28,20 @@ describe('Terebi:', function()
_G.love.window.getMode = spy.new(function()
return 640, 480, {'flags'}
end)
_G.love.window.fromPixels = spy.new(function(x, y)
if y then
return x / pixelScale, y / pixelScale
else
return x / pixelScale
end
end)
_G.love.window.toPixels = spy.new(function(x, y)
if y then
return x * pixelScale, y * pixelScale
else
return x * pixelScale
end
end)
end)

describe('When calling initializeLoveDefaults:', function()
Expand Down Expand Up @@ -118,7 +135,7 @@ describe('Terebi:', function()
end)

describe('When calling increaseScale:', function ()
it('increaseScale should set the scale', function()
it('increaseScale should increase the scale', function()
screen:increaseScale()

assert.are.same(3, screen._scale)
Expand All @@ -136,7 +153,7 @@ describe('Terebi:', function()
end)

describe('When calling decreaseScale:', function ()
it('decreaseScale should set the scale', function()
it('decreaseScale should decrease the scale', function()
screen:decreaseScale()

assert.are.same(1, screen._scale)
Expand Down Expand Up @@ -198,6 +215,33 @@ describe('Terebi:', function()
assert.spy(love.window.setFullscreen).was.called(2)
end)
end)

describe('When high density pixel scale:', function ()
before_each(function ()
pixelScale = 2
end)

it('increaseScale should increase the scale', function()
screen:increaseScale()

assert.are.same(3, screen._scale)
assert.spy(love.window.setMode).was.called_with(480, 360, {'flags'})
end)

it('decreaseScale should set the scale', function()
screen:decreaseScale()

assert.are.same(1, screen._scale)
assert.spy(love.window.setMode).was.called_with(160, 120, {'flags'})
end)

it('setMaxScale should set the scale', function()
screen:setMaxScale()

assert.are.same(10, screen._scale)
assert.spy(love.window.setMode).was.called_with(1600, 1200, {'flags'})
end)
end)
end)

describe('When calling draw:', function ()
Expand Down
13 changes: 8 additions & 5 deletions terebi.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Terebi = {
_VERSION = 'terebi v0.3.0',
_VERSION = 'terebi v0.4.0',
_URL = 'https://github.com/oniietzschan/terebi',
_DESCRIPTION = 'Graphics scaling library for Love2D.',
_LICENSE = [[
Expand Down Expand Up @@ -85,8 +85,7 @@ end

function Screen:_resizeWindow()
local currentW, currentH, flags = love.window.getMode()
local newW = self._scale * self._width
local newH = self._scale * self._height
local newW, newH = love.window.fromPixels(self._width * self._scale, self._height * self._scale)
if not love.window.getFullscreen() and (currentW ~= newW or currentH ~= newH) then
love.window.setMode(newW, newH, flags)
end
Expand Down Expand Up @@ -137,7 +136,7 @@ function Screen:setMaxScale()
end

function Screen:_getMaxScale()
local desktopW, desktopH = love.window.getDesktopDimensions()
local desktopW, desktopH = self:_getDesktopDimensions()
local maxScaleX = math.floor(desktopW / self._width)
local maxScaleY = math.floor(desktopH / self._height)

Expand All @@ -147,7 +146,7 @@ end
function Screen:_updateDrawOffset()
if love.window.getFullscreen() then
-- When fullscreen, center screen on monitor
local desktopW, desktopH = love.window.getDesktopDimensions()
local desktopW, desktopH = self:_getDesktopDimensions()
local scaledWidth = self._width * self._scale
local scaledHeight = self._height * self._scale
self._drawOffsetX = math.floor((desktopW - scaledWidth) / 2)
Expand All @@ -161,6 +160,10 @@ function Screen:_updateDrawOffset()
return self
end

function Screen:_getDesktopDimensions()
return love.window.toPixels(love.window.getDesktopDimensions())
end

function Screen:draw(drawFunc, ...)
assert(type(drawFunc) == 'function', type(drawFunc))

Expand Down

0 comments on commit 4f4e67d

Please sign in to comment.