Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the code to work with the newest version of LÖVE2d #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 83 additions & 44 deletions lib/push.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
-- push.lua v0.2
-- push.lua v0.3

-- Copyright (c) 2017 Ulysse Ramage
-- Copyright (c) 2018 Ulysse Ramage
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

local love11 = love.getVersion() == 11
local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale

local push = {

defaults = {
fullscreen = false,
resizable = false,
pixelperfect = false,
highdpi = true,
canvas = true
}

}
setmetatable(push, push)

--TODO: rendering resolution?
--TODO: clean up code

function push:applySettings(settings)
for k, v in pairs(settings) do
self["_" .. k] = v
Expand All @@ -38,12 +38,12 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)

self:applySettings(self.defaults) --set defaults first
self:applySettings(settings) --then fill with custom settings
love.window.setMode( self._RWIDTH, self._RHEIGHT, {

love.window.setMode(self._RWIDTH, self._RHEIGHT, {
fullscreen = self._fullscreen,
resizable = self._resizable,
highdpi = self._highdpi
} )
})

self:initValues()

Expand All @@ -62,25 +62,29 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
end

function push:setupCanvas(canvases)
table.insert(canvases, { name = "_render" }) --final render
table.insert(canvases, { name = "_render", private = true }) --final render

self._canvas = true
self.canvases = {}

for i = 1, #canvases do
self.canvases[i] = {
name = canvases[i].name,
shader = canvases[i].shader,
canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT)
}
push:addCanvas(canvases[i])
end

return self
end
function push:addCanvas(params)
table.insert(self.canvases, {
name = params.name,
private = params.private,
shader = params.shader,
canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT)
})
end

function push:setCanvas(name)
if not self._canvas then return true end
return love.graphics.setCanvas( self:getCanvasTable(name).canvas )
return love.graphics.setCanvas(self:getCanvasTable(name).canvas)
end
function push:getCanvasTable(name)
for i = 1, #self.canvases do
Expand All @@ -98,34 +102,29 @@ function push:setShader(name, shader)
end

function push:initValues()
self._PSCALE = self._highdpi and love.window.getPixelScale() or 1
self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1

self._SCALE = {
x = self._RWIDTH/self._WWIDTH * self._PSCALE,
y = self._RHEIGHT/self._WHEIGHT * self._PSCALE
}

if self._stretched then --if stretched, no need to apply offset
self._OFFSET = {x = 0, y = 0}
else
local scale = math.min(self._SCALE.x, self._SCALE.y)
if self._pixelperfect then scale = math.floor(scale) end

self._OFFSET = {x = (self._SCALE.x - scale) * (self._WWIDTH/2), y = (self._SCALE.y - scale) * (self._WHEIGHT/2)}
self._SCALE.x, self._SCALE.y = scale, scale --apply same scale to X and Y
end

self._GWIDTH = self._RWIDTH * self._PSCALE - self._OFFSET.x * 2
self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2
end

--[[ DEPRECATED ]]--
function push:apply(operation, shader)
if operation == "start" then
self:start()
elseif operation == "finish" or operation == "end" then
self:finish(shader)
end
self._drawFunctions[operation](self, shader)
end

function push:start()
Expand All @@ -140,32 +139,73 @@ function push:start()
end
end

function push:applyShaders(canvas, shaders)
local _shader = love.graphics.getShader()
if #shaders <= 1 then
love.graphics.setShader(shaders[1])
love.graphics.draw(canvas)
else
local _canvas = love.graphics.getCanvas()

local _tmp = self:getCanvasTable("_tmp")
if not _tmp then --create temp canvas only if needed
self:addCanvas({ name = "_tmp", private = true, shader = nil })
_tmp = self:getCanvasTable("_tmp")
end

love.graphics.push()
love.graphics.origin()
local outputCanvas
for i = 1, #shaders do
local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas
outputCanvas = i % 2 == 0 and canvas or _tmp.canvas
love.graphics.setCanvas(outputCanvas)
love.graphics.clear()
love.graphics.setShader(shaders[i])
love.graphics.draw(inputCanvas)
love.graphics.setCanvas(inputCanvas)
end
love.graphics.pop()

love.graphics.setCanvas(_canvas)
love.graphics.draw(outputCanvas)
end
love.graphics.setShader(_shader)
end

function push:finish(shader)
love.graphics.setBackgroundColor(unpack(self._borderColor))
if self._canvas then
local _render = self:getCanvasTable("_render")

love.graphics.pop()

love.graphics.setColor(255, 255, 255)
local white = love11 and 1 or 255
love.graphics.setColor(white, white, white)

--draw canvas
love.graphics.setCanvas(_render.canvas)
for i = 1, #self.canvases - 1 do --do not draw _render yet
for i = 1, #self.canvases do --do not draw _render yet
local _table = self.canvases[i]
love.graphics.setShader(_table.shader)
love.graphics.draw(_table.canvas)
if not _table.private then
local _canvas = _table.canvas
local _shader = _table.shader
self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader })
end
end
love.graphics.setCanvas()

--draw render
love.graphics.translate(self._OFFSET.x, self._OFFSET.y)
love.graphics.setShader(shader or self:getCanvasTable("_render").shader)
love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y)
local shader = shader or _render.shader
love.graphics.push()
love.graphics.scale(self._SCALE.x, self._SCALE.y)
self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader })
love.graphics.pop()

--clear canvas
for i = 1, #self.canvases do
love.graphics.setCanvas( self.canvases[i].canvas )
love.graphics.setCanvas(self.canvases[i].canvas)
love.graphics.clear()
end

Expand All @@ -184,42 +224,41 @@ end
function push:toGame(x, y)
x, y = x - self._OFFSET.x, y - self._OFFSET.y
local normalX, normalY = x / self._GWIDTH, y / self._GHEIGHT

x = (x >= 0 and x <= self._WWIDTH * self._SCALE.x) and normalX * self._WWIDTH or nil
y = (y >= 0 and y <= self._WHEIGHT * self._SCALE.y) and normalY * self._WHEIGHT or nil

return x, y
end

--doesn't work - TODO
function push:toReal(x, y)
return x+self._OFFSET.x, y+self._OFFSET.y
return x + self._OFFSET.x, y + self._OFFSET.y
end

function push:switchFullscreen(winw, winh)
self._fullscreen = not self._fullscreen
local windowWidth, windowHeight = love.window.getDesktopDimensions()

if self._fullscreen then --save windowed dimensions for later
self._WINWIDTH, self._WINHEIGHT = self._RWIDTH, self._RHEIGHT
elseif not self._WINWIDTH or not self._WINHEIGHT then
self._WINWIDTH, self._WINHEIGHT = windowWidth * .5, windowHeight * .5
end

self._RWIDTH = self._fullscreen and windowWidth or winw or self._WINWIDTH
self._RHEIGHT = self._fullscreen and windowHeight or winh or self._WINHEIGHT

self:initValues()

love.window.setFullscreen(self._fullscreen, "desktop")
if not self._fullscreen and (winw or winh) then
love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions
end
end

function push:resize(w, h)
local pixelScale = love.window.getPixelScale()
if self._highdpi then w, h = w / pixelScale, h / pixelScale end
if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end
self._RWIDTH = w
self._RHEIGHT = h
self:initValues()
Expand Down
7 changes: 6 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@

require 'src/Dependencies'


function rgba (r, g, b, a)
return {r / 255, g / 255, b / 255, a / 255}
end

function love.load()
love.window.setTitle('Poke50')
love.graphics.setDefaultFilter('nearest', 'nearest')
Expand Down Expand Up @@ -91,4 +96,4 @@ function love.draw()
push:start()
gStateStack:render()
push:finish()
end
end
24 changes: 12 additions & 12 deletions src/Dependencies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ gFonts = {
}

gSounds = {
['field-music'] = love.audio.newSource('sounds/field_music.wav'),
['battle-music'] = love.audio.newSource('sounds/battle_music.mp3'),
['blip'] = love.audio.newSource('sounds/blip.wav'),
['powerup'] = love.audio.newSource('sounds/powerup.wav'),
['hit'] = love.audio.newSource('sounds/hit.wav'),
['run'] = love.audio.newSource('sounds/run.wav'),
['heal'] = love.audio.newSource('sounds/heal.wav'),
['exp'] = love.audio.newSource('sounds/exp.wav'),
['levelup'] = love.audio.newSource('sounds/levelup.wav'),
['victory-music'] = love.audio.newSource('sounds/victory.wav'),
['intro-music'] = love.audio.newSource('sounds/intro.mp3')
}
['field-music'] = love.audio.newSource('sounds/field_music.wav', "stream"),
['battle-music'] = love.audio.newSource('sounds/battle_music.mp3', "stream"),
['blip'] = love.audio.newSource('sounds/blip.wav', "static"),
['powerup'] = love.audio.newSource('sounds/powerup.wav', "static"),
['hit'] = love.audio.newSource('sounds/hit.wav', "static"),
['run'] = love.audio.newSource('sounds/run.wav', "static"),
['heal'] = love.audio.newSource('sounds/heal.wav', "static"),
['exp'] = love.audio.newSource('sounds/exp.wav', "static"),
['levelup'] = love.audio.newSource('sounds/levelup.wav', "static"),
['victory-music'] = love.audio.newSource('sounds/victory.wav', "stream"),
['intro-music'] = love.audio.newSource('sounds/intro.mp3', "stream")
}
4 changes: 2 additions & 2 deletions src/battle/BattleSprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function BattleSprite:update(dt)
end

function BattleSprite:render()
love.graphics.setColor(255, 255, 255, self.opacity)
love.graphics.setColor(rgba(255, 255, 255, self.opacity))

-- if blinking is set to true, we'll send 1 to the white shader, which will
-- convert every pixel of the sprite to pure white
Expand All @@ -46,4 +46,4 @@ function BattleSprite:render()

-- reset shader
love.graphics.setShader()
end
end
2 changes: 1 addition & 1 deletion src/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ VIRTUAL_HEIGHT = 216
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720

TILE_SIZE = 16
TILE_SIZE = 16
8 changes: 4 additions & 4 deletions src/gui/Panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ end

function Panel:render()
if self.visible then
love.graphics.setColor(255, 255, 255, 255)
love.graphics.setColor(rgba(255, 255, 255, 255))
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height, 3)
love.graphics.setColor(56, 56, 56, 255)
love.graphics.setColor(rgba(56, 56, 56, 255))
love.graphics.rectangle('fill', self.x + 2, self.y + 2, self.width - 4, self.height - 4, 3)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.setColor(rgba(255, 255, 255, 255))
end
end

function Panel:toggle()
self.visible = not self.visible
end
end
14 changes: 7 additions & 7 deletions src/gui/ProgressBar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ ProgressBar = Class{}
function ProgressBar:init(def)
self.x = def.x
self.y = def.y

self.width = def.width
self.height = def.height

self.color = def.color

self.value = def.value
Expand All @@ -38,14 +38,14 @@ function ProgressBar:render()
local renderWidth = (self.value / self.max) * self.width

-- draw main bar, with calculated width based on value / max
love.graphics.setColor(self.color.r, self.color.g, self.color.b, 255)
love.graphics.setColor(rgba(self.color.r, self.color.g, self.color.b, 255))

if self.value > 0 then
love.graphics.rectangle('fill', self.x, self.y, renderWidth, self.height, 3)
end

-- draw outline around actual bar
love.graphics.setColor(0, 0, 0, 255)
love.graphics.setColor(rgba(0, 0, 0, 255))
love.graphics.rectangle('line', self.x, self.y, self.width, self.height, 3)
love.graphics.setColor(255, 255, 255, 255)
end
love.graphics.setColor(rgba(255, 255, 255, 255))
end
Loading