From 4f4e67dcb8a790ded4bdd4c0fd84f328a53baeab Mon Sep 17 00:00:00 2001 From: shru Date: Mon, 2 Oct 2017 07:35:38 -0700 Subject: [PATCH] Version 0.4.0 - Support high DPI mode. --- README.md | 12 +++++--- rockspecs/terebi-0.4.0-1.rockspec | 22 ++++++++++++++ spec.lua | 48 +++++++++++++++++++++++++++++-- terebi.lua | 13 +++++---- 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 rockspecs/terebi-0.4.0-1.rockspec diff --git a/README.md b/README.md index 3d43a9a..40c8d93 100644 --- a/README.md +++ b/README.md @@ -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 ------- @@ -74,7 +79,7 @@ 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 ``` @@ -82,7 +87,6 @@ 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." diff --git a/rockspecs/terebi-0.4.0-1.rockspec b/rockspecs/terebi-0.4.0-1.rockspec new file mode 100644 index 0000000..c728cdc --- /dev/null +++ b/rockspecs/terebi-0.4.0-1.rockspec @@ -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" + } +} diff --git a/spec.lua b/spec.lua index 975ab07..0ba0af3 100644 --- a/spec.lua +++ b/spec.lua @@ -7,7 +7,10 @@ local noop = function() end describe('Terebi:', function() + local pixelScale + before_each(function() + pixelScale = 1 _G.love = { graphics = {}, mouse = {}, @@ -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() @@ -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) @@ -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) @@ -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 () diff --git a/terebi.lua b/terebi.lua index 911867d..8a0a4a1 100644 --- a/terebi.lua +++ b/terebi.lua @@ -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 = [[ @@ -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 @@ -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) @@ -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) @@ -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))