From 1c8c2d6937b2dea434e92aad4029d514f7430151 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 15:51:58 +0200 Subject: [PATCH 01/16] feat(screen): add a way to get coordinate and scale --- CHANGELOG.md | 2 ++ gamecore/libs/cscreen.lua | 8 ++++++++ gamecore/screen.lua | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7497702..22007f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **examples:** Test all player number and camera types +- **core.screen:** Add a way to get coordinate and scale + ### Changed - **world2D:** Use a list for bodies (hitboxes, etc) and one other for actors diff --git a/gamecore/libs/cscreen.lua b/gamecore/libs/cscreen.lua index 579ea0d..e8c3243 100644 --- a/gamecore/libs/cscreen.lua +++ b/gamecore/libs/cscreen.lua @@ -87,6 +87,14 @@ function CScreen.project(x, y) return math.floor((x - tx) / fsv), math.floor((y - ty) / fsv) end +function CScreen.getScale() + return fsv +end + +function CScreen.getScreenCoordinate(x, y) + return math.floor((x + tx) * fsv), math.floor((y + ty) * fsv) +end + -- Change letterbox color function CScreen.setColor(r, g, b, a) cr = r diff --git a/gamecore/screen.lua b/gamecore/screen.lua index ec3e6e0..fe23c7c 100644 --- a/gamecore/screen.lua +++ b/gamecore/screen.lua @@ -66,6 +66,14 @@ function ScreenManager:getMousePosition() return CScreen.project(love.mouse.getX(), love.mouse.getY()) end +function ScreenManager:getScale() + return CScreen.getScale() +end + +function ScreenManager:getScreenCoordinate(x, y) + return CScreen.getScreenCoordinate(x, y) +end + -- INFO FUNCTIONS -- Get screen informations From e4b89ddd3186dc8332aa240e2c798fec907eacde Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 18:28:29 +0200 Subject: [PATCH 02/16] chore(camera): split camera into a specific folder --- gamecore/modules/world/{camera.lua => camera/init.lua} | 2 +- gamecore/modules/world/{ => camera}/libs/hump/camera.lua | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename gamecore/modules/world/{camera.lua => camera/init.lua} (99%) rename gamecore/modules/world/{ => camera}/libs/hump/camera.lua (100%) diff --git a/gamecore/modules/world/camera.lua b/gamecore/modules/world/camera/init.lua similarity index 99% rename from gamecore/modules/world/camera.lua rename to gamecore/modules/world/camera/init.lua index 464bb07..c04ada6 100644 --- a/gamecore/modules/world/camera.lua +++ b/gamecore/modules/world/camera/init.lua @@ -22,7 +22,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] -local cwd = (...):gsub('%.camera$', '') .. "." +local cwd = (...):gsub('%.init$', '') .. "." local CameraSystem = Object:extend() local View = require(cwd .. "libs.hump.camera") diff --git a/gamecore/modules/world/libs/hump/camera.lua b/gamecore/modules/world/camera/libs/hump/camera.lua similarity index 100% rename from gamecore/modules/world/libs/hump/camera.lua rename to gamecore/modules/world/camera/libs/hump/camera.lua From fb428b45c3a29c93612dc666e496b6bdfaae8be0 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 18:35:33 +0200 Subject: [PATCH 03/16] chore(camera): put the view algorithm in a "utils" file --- gamecore/modules/world/camera/init.lua | 40 ++------------------- gamecore/modules/world/camera/utils.lua | 46 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 37 deletions(-) create mode 100644 gamecore/modules/world/camera/utils.lua diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index c04ada6..49ff883 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -27,6 +27,8 @@ local cwd = (...):gsub('%.init$', '') .. "." local CameraSystem = Object:extend() local View = require(cwd .. "libs.hump.camera") +local camutils = require(cwd .. "utils") + local SPLITSCREEN_ISVERTICAL = false local SCREEN_LIMIT = 4 @@ -57,43 +59,7 @@ function CameraSystem:initViews() self.views.basewidth, self.views.baseheight = core.screen:getDimensions() self.views.width, self.views.height = self:getViewsDimensions() - self.views.posList = {} - self.views.posList.dual = {} - self.views.posList.multi = {} - - if (self.verticalSplit) then - self.views.posList.dual[1] = {} - self.views.posList.dual[1].x = 0 - self.views.posList.dual[1].y = (self.views.baseheight/4) - - self.views.posList.dual[2] = {} - self.views.posList.dual[2].x = 0 - self.views.posList.dual[2].y = -(self.views.baseheight/4) - else - self.views.posList.dual[1] = {} - self.views.posList.dual[1].x = -(self.views.basewidth/4) - self.views.posList.dual[1].y = 0 - - self.views.posList.dual[2] = {} - self.views.posList.dual[2].x = (self.views.basewidth/4) - self.views.posList.dual[2].y = 0 - end - - self.views.posList.multi[1] = {} - self.views.posList.multi[1].x = -(self.views.basewidth /4) - self.views.posList.multi[1].y = -(self.views.baseheight/4) - - self.views.posList.multi[2] = {} - self.views.posList.multi[2].x = (self.views.basewidth /4) - self.views.posList.multi[2].y = -(self.views.baseheight/4) - - self.views.posList.multi[3] = {} - self.views.posList.multi[3].x = -(self.views.basewidth /4) - self.views.posList.multi[3].y = (self.views.baseheight/4) - - self.views.posList.multi[4] = {} - self.views.posList.multi[4].x = (self.views.basewidth /4) - self.views.posList.multi[4].y = (self.views.baseheight/4) + self.views.posList = camutils.getViewsPositions(self.views.basewidth, self.views.baseheight, self.verticalSplit) end -- INFO FUNCTIONS diff --git a/gamecore/modules/world/camera/utils.lua b/gamecore/modules/world/camera/utils.lua new file mode 100644 index 0000000..6d47831 --- /dev/null +++ b/gamecore/modules/world/camera/utils.lua @@ -0,0 +1,46 @@ +local camutils = {} + +function camutils.getViewsPositions(basewidth, baseheight, verticalSplit) + local posList = {} + + posList.dual = {} + posList.multi = {} + + if (verticalSplit) then + posList.dual[1] = {} + posList.dual[1].x = 0 + posList.dual[1].y = (baseheight/4) + + posList.dual[2] = {} + posList.dual[2].x = 0 + posList.dual[2].y = -(baseheight/4) + else + posList.dual[1] = {} + posList.dual[1].x = -(basewidth/4) + posList.dual[1].y = 0 + + posList.dual[2] = {} + posList.dual[2].x = (basewidth/4) + posList.dual[2].y = 0 + end + + posList.multi[1] = {} + posList.multi[1].x = -(basewidth /4) + posList.multi[1].y = -(baseheight/4) + + posList.multi[2] = {} + posList.multi[2].x = (basewidth /4) + posList.multi[2].y = -(baseheight/4) + + posList.multi[3] = {} + posList.multi[3].x = -(basewidth /4) + posList.multi[3].y = (baseheight/4) + + posList.multi[4] = {} + posList.multi[4].x = (basewidth /4) + posList.multi[4].y = (baseheight/4) + + return posList +end + +return camutils From 6947d2f3f22c0d9111aaa208d97cf174fca4b125 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 18:41:13 +0200 Subject: [PATCH 04/16] fix(camera): fix onscreen coordinates by using new core.screen func --- CHANGELOG.md | 2 ++ gamecore/modules/world/camera/init.lua | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22007f5..e3350ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **examples:** Add missing translations +- **camera:** Fix onscreen coordinates by using new core.screen func + ### Removed - **actor:** Remove all function related to XGravity diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index 49ff883..02019d3 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -232,8 +232,10 @@ function CameraSystem:getOnScreenViewCoordinate(id) viewx = (basex) + view.pos.onScreen.x - (self.views.width / 2) viewy = (basey) + view.pos.onScreen.y - (self.views.height / 2) - vieww = self.views.width - viewh = self.views.height + viewx, viewy = core.screen:getScreenCoordinate(viewx, viewy) + + vieww = self.views.width * core.screen:getScale() + viewh = self.views.height * core.screen:getScale() return viewx, viewy, vieww, viewh end @@ -245,7 +247,7 @@ function CameraSystem:getOnScreenViewRelativePosition(id) viewx = view.pos.onScreen.x viewy = view.pos.onScreen.y - return viewx, viewy + return core.screen:getScreenCoordinate(viewx, viewy) end function CameraSystem:getOnScreenViewCenter(id) @@ -256,13 +258,13 @@ function CameraSystem:getOnScreenViewCenter(id) viewx = (basex) + view.pos.onScreen.x viewy = (basey) + view.pos.onScreen.y - return viewx, viewy + return core.screen:getScreenCoordinate(viewx, viewy) end function CameraSystem:getViewScale(id) local cam = self:getViewCam(id) - return cam.scale + return cam.scale * core.screen:getScale() end function CameraSystem:limitView(id) From f48de0e9bdaa96453e001cd834ed4de45cbeff2a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 18:48:25 +0200 Subject: [PATCH 05/16] improvement(camera): manually attach the view instead of using hump func --- gamecore/modules/world/camera/init.lua | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index 02019d3..6ac4ae6 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -185,7 +185,25 @@ function CameraSystem:attachView(id) if (id ~= nil) then local cam = self:getViewCam(id) - cam:attach() + if id ~= nil then + -- Du à la manière dont fonctionne STI, on est obligé de récupérer les info + -- de position de camera pour afficher la carte par rapport à ces infos + tx, ty = self:getInternalCamCoordinate(id) + tx, ty = core.screen:getScreenCoordinate(tx, ty) + scale = self:getViewScale(id) or 2 + local vx, vy = self:getOnScreenViewRelativePosition(id) + tx = math.floor(tx - math.abs(vx)) * -1 + ty = math.floor(ty - math.abs(vy)) * -1 + + local w, h = core.screen:getDimensions() + end + + print(tx, ty, scale) + + love.graphics.push() + love.graphics.origin() + love.graphics.translate(math.floor(tx), math.floor(ty)) + love.graphics.scale(scale, scale) end end @@ -193,7 +211,7 @@ function CameraSystem:detachView(id) if (id ~= nil) then local cam = self:getViewCam(id) - cam:detach() + love.graphics.pop() end end From c7e2f2dacab267f5f99d85c6b252b02fd310239a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 18:51:00 +0200 Subject: [PATCH 06/16] fix(baseworld): draw all map layer instead of using map:draw() It allow us to use the exact same transform for map and actors --- gamecore/modules/world/baseworld.lua | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index 35f7529..1636854 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -432,8 +432,8 @@ function BaseWorld:draw(dt) else for i=1, camNumber do self.cameras:setScissor(i) - self:drawMap(i) self.cameras:attachView(i) + self:drawMap(i) self:drawActors(i) self.cameras:detachView(i) self.cameras:drawHUD(i) @@ -451,21 +451,12 @@ function BaseWorld:drawActors(id) end function BaseWorld:drawMap(id) - local tx, ty, scale = 0, 0, 1 - if id ~= nil then - -- Du à la manière dont fonctionne STI, on est obligé de récupérer les info - -- de position de camera pour afficher la carte par rapport à ces infos - tx, ty = self.cameras:getInternalCamCoordinate(id) - scale = self.cameras:getViewScale(id) or 2 - local vx, vy = self.cameras:getOnScreenViewRelativePosition(id) - tx = math.floor(tx - math.abs(vx)) - ty = math.floor(ty - math.abs(vy)) - - local w, h = core.screen:getDimensions() - end - if self.haveMap then - self.map:draw(-tx, -ty, scale, scale) + for _, layer in ipairs(self.map.layers) do + if layer.visible and layer.opacity > 0 then + self.map:drawLayer(layer) + end + end end end From 35433b273d41cb4f3e30bd5c401577435e1d00d8 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 18:57:50 +0200 Subject: [PATCH 07/16] chore(camera): remove hump.camera --- CHANGELOG.md | 2 + gamecore/modules/world/camera/init.lua | 7 +- .../modules/world/camera/libs/hump/camera.lua | 212 ------------------ 3 files changed, 7 insertions(+), 214 deletions(-) delete mode 100644 gamecore/modules/world/camera/libs/hump/camera.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index e3350ac..ddd450b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **examples:** Remove basic examples +- **libs:** Remove hump.camera + ## 0.5.1 - 2019-06-21 ### Fixed diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index 6ac4ae6..b582b04 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -25,7 +25,7 @@ local cwd = (...):gsub('%.init$', '') .. "." local CameraSystem = Object:extend() -local View = require(cwd .. "libs.hump.camera") +--local View = require(cwd .. "libs.hump.camera") local camutils = require(cwd .. "utils") @@ -139,7 +139,10 @@ function CameraSystem:addView(x, y, target) view.pos.y = y or 0 view.pos.onScreen = {} - view.cam = View(view.pos.x, view.pos.y, 1, 0, true) + view.cam = {} + view.cam.scale = 1 + view.cam.x = x + view.cam.y = y -- TODO: add a target system in order to make a camera able -- to target a specific object view.target = target diff --git a/gamecore/modules/world/camera/libs/hump/camera.lua b/gamecore/modules/world/camera/libs/hump/camera.lua deleted file mode 100644 index 34e7aef..0000000 --- a/gamecore/modules/world/camera/libs/hump/camera.lua +++ /dev/null @@ -1,212 +0,0 @@ ---[[ -Copyright (c) 2010-2015 Matthias Richter - -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. - -Except as contained in this notice, the name(s) of the above copyright holders -shall not be used in advertising or otherwise to promote the sale, use or -other dealings in this Software without prior written authorization. - -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 _PATH = (...):match('^(.*[%./])[^%.%/]+$') or '' -local cos, sin = math.cos, math.sin - -local camera = {} -camera.__index = camera - --- Movement interpolators (for camera locking/windowing) -camera.smooth = {} - -function camera.smooth.none() - return function(dx,dy) return dx,dy end -end - -function camera.smooth.linear(speed) - assert(type(speed) == "number", "Invalid parameter: speed = "..tostring(speed)) - return function(dx,dy, s) - -- normalize direction - local d = math.sqrt(dx*dx+dy*dy) - local dts = math.min((s or speed) * love.timer.getDelta(), d) -- prevent overshooting the goal - if d > 0 then - dx,dy = dx/d, dy/d - end - - return dx*dts, dy*dts - end -end - -function camera.smooth.damped(stiffness) - assert(type(stiffness) == "number", "Invalid parameter: stiffness = "..tostring(stiffness)) - return function(dx,dy, s) - local dts = love.timer.getDelta() * (s or stiffness) - return dx*dts, dy*dts - end -end - - -local function new(x,y, zoom, rot, smoother) - x,y = x or love.graphics.getWidth()/2, y or love.graphics.getHeight()/2 - zoom = zoom or 1 - rot = rot or 0 - smoother = smoother or camera.smooth.none() -- for locking, see below - return setmetatable({x = x, y = y, scale = zoom, rot = rot, smoother = smoother}, camera) -end - -function camera:lookAt(x,y) - self.x, self.y = x, y - return self -end - -function camera:move(dx,dy) - self.x, self.y = self.x + dx, self.y + dy - return self -end - -function camera:position() - return self.x, self.y -end - -function camera:rotate(phi) - self.rot = self.rot + phi - return self -end - -function camera:rotateTo(phi) - self.rot = phi - return self -end - -function camera:zoom(mul) - self.scale = self.scale * mul - return self -end - -function camera:zoomTo(zoom) - self.scale = zoom - return self -end - -function camera:attach(x,y,w,h, noclip) - x,y = x or 0, y or 0 - w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight() - - self._sx,self._sy,self._sw,self._sh = love.graphics.getScissor() - - local cx,cy = x+w/2, y+h/2 - love.graphics.push() - love.graphics.translate(cx, cy) - love.graphics.scale(self.scale) - love.graphics.rotate(self.rot) - love.graphics.translate(-self.x, -self.y) -end - -function camera:detach() - love.graphics.pop() -end - -function camera:draw(...) - local x,y,w,h,noclip,func - local nargs = select("#", ...) - if nargs == 1 then - func = ... - elseif nargs == 5 then - x,y,w,h,func = ... - elseif nargs == 6 then - x,y,w,h,noclip,func = ... - else - error("Invalid arguments to camera:draw()") - end - - self:attach(x,y,w,h,noclip) - func() - self:detach() -end - --- world coordinates to camera coordinates -function camera:cameraCoords(x,y, ox,oy,w,h) - ox, oy = ox or 0, oy or 0 - w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight() - - -- x,y = ((x,y) - (self.x, self.y)):rotated(self.rot) * self.scale + center - local c,s = cos(self.rot), sin(self.rot) - x,y = x - self.x, y - self.y - x,y = c*x - s*y, s*x + c*y - return x*self.scale + w/2 + ox, y*self.scale + h/2 + oy -end - --- camera coordinates to world coordinates -function camera:worldCoords(x,y, ox,oy,w,h) - ox, oy = ox or 0, oy or 0 - w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight() - - -- x,y = (((x,y) - center) / self.scale):rotated(-self.rot) + (self.x,self.y) - local c,s = cos(-self.rot), sin(-self.rot) - x,y = (x - w/2 - ox) / self.scale, (y - h/2 - oy) / self.scale - x,y = c*x - s*y, s*x + c*y - return x+self.x, y+self.y -end - -function camera:mousePosition(ox,oy,w,h) - local mx,my = love.mouse.getPosition() - return self:worldCoords(mx,my, ox,oy,w,h) -end - --- camera scrolling utilities -function camera:lockX(x, smoother, ...) - local dx, dy = (smoother or self.smoother)(x - self.x, self.y, ...) - self.x = self.x + dx - return self -end - -function camera:lockY(y, smoother, ...) - local dx, dy = (smoother or self.smoother)(self.x, y - self.y, ...) - self.y = self.y + dy - return self -end - -function camera:lockPosition(x,y, smoother, ...) - return self:move((smoother or self.smoother)(x - self.x, y - self.y, ...)) -end - -function camera:lockWindow(x, y, x_min, x_max, y_min, y_max, smoother, ...) - -- figure out displacement in camera coordinates - x,y = self:cameraCoords(x,y) - local dx, dy = 0,0 - if x < x_min then - dx = x - x_min - elseif x > x_max then - dx = x - x_max - end - if y < y_min then - dy = y - y_min - elseif y > y_max then - dy = y - y_max - end - - -- transform displacement to movement in world coordinates - local c,s = cos(-self.rot), sin(-self.rot) - dx,dy = (c*dx - s*dy) / self.scale, (s*dx + c*dy) / self.scale - - -- move - self:move((smoother or self.smoother)(dx,dy,...)) -end - --- the module -return setmetatable({new = new, smooth = camera.smooth}, - {__call = function(_, ...) return new(...) end}) From 81dd584e115c5578c4eb3ae4915f95ac2efe7553 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 22:49:27 +0200 Subject: [PATCH 08/16] improvement(camera): use a new canvas-based view system --- gamecore/modules/world/baseworld.lua | 3 -- gamecore/modules/world/camera/init.lua | 59 ++++++++++++++++---------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index 1636854..348ae0a 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -431,13 +431,10 @@ function BaseWorld:draw(dt) self:drawActors() else for i=1, camNumber do - self.cameras:setScissor(i) self.cameras:attachView(i) self:drawMap(i) self:drawActors(i) self.cameras:detachView(i) - self.cameras:drawHUD(i) - self.cameras:resetScissor( ) end end end diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index b582b04..74175c0 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -149,10 +149,17 @@ function CameraSystem:addView(x, y, target) table.insert(self.views.list, view) self.views.width, self.views.height = self:getViewsDimensions() + self:regenerateCanvases() self:recalculateViewsPositions() end end +function CameraSystem:regenerateCanvases() + for i, view in ipairs(self.views.list) do + view.canvas = love.graphics.newCanvas(self.views.width, self.views.height) + end +end + function CameraSystem:getView(id) return self.views.list[id] end @@ -186,34 +193,45 @@ end function CameraSystem:attachView(id) if (id ~= nil) then - local cam = self:getViewCam(id) + local view = self:getView(id) + + self.current_canvas = love.graphics.getCanvas() + love.graphics.setCanvas(view.canvas) + love.graphics.clear() if id ~= nil then -- Du à la manière dont fonctionne STI, on est obligé de récupérer les info -- de position de camera pour afficher la carte par rapport à ces infos - tx, ty = self:getInternalCamCoordinate(id) - tx, ty = core.screen:getScreenCoordinate(tx, ty) - scale = self:getViewScale(id) or 2 - local vx, vy = self:getOnScreenViewRelativePosition(id) - tx = math.floor(tx - math.abs(vx)) * -1 - ty = math.floor(ty - math.abs(vy)) * -1 + tx, ty = self:getViewCoordinate(id) + scale = self:getViewScale(id) or 1 + tx = math.floor(tx) * -1 * scale + ty = math.floor(ty) * -1 * scale local w, h = core.screen:getDimensions() end - print(tx, ty, scale) - love.graphics.push() love.graphics.origin() love.graphics.translate(math.floor(tx), math.floor(ty)) - love.graphics.scale(scale, scale) + love.graphics.scale(view.cam.scale, view.cam.scale) end end function CameraSystem:detachView(id) if (id ~= nil) then - local cam = self:getViewCam(id) + local view = self:getView(id) + love.graphics.pop() + love.graphics.setCanvas(self.current_canvas) + local tx, ty = self:getOnScreenViewCoordinate(id) + local scale = core.screen:getScale() + love.graphics.push() + love.graphics.origin() + love.graphics.translate(math.floor(tx), math.floor(ty)) + love.graphics.scale(scale, scale) + + love.graphics.draw(view.canvas) + self:drawHUD(id) love.graphics.pop() end end @@ -223,11 +241,13 @@ function CameraSystem:getViewCoordinate(id) local cam = self:getViewCam(id) local viewx, viewy, vieww, viewh - viewx = view.pos.x - ((self.views.width/2) / cam.scale) - viewy = view.pos.y - ((self.views.height/2) / cam.scale) - vieww = self.views.width / cam.scale + vieww = self.views.width / cam.scale viewh = self.views.height / cam.scale + + viewx = view.pos.x - (vieww / 2) + viewy = view.pos.y - (viewh / 2) + return viewx, viewy, vieww, viewh end @@ -285,7 +305,7 @@ end function CameraSystem:getViewScale(id) local cam = self:getViewCam(id) - return cam.scale * core.screen:getScale() + return cam.scale end function CameraSystem:limitView(id) @@ -300,8 +320,6 @@ function CameraSystem:limitView(id) self.views.list[id].pos.x = utils.math.between(posx, minx, maxx) self.views.list[id].pos.y = utils.math.between(posy, miny, maxy) - - self:computeCamPosition(id) end -- UPDATE and MOVE functions @@ -321,7 +339,6 @@ function CameraSystem:moveView(id, x, y) self.views.list[id].pos.x = x self.views.list[id].pos.y = y - self:computeCamPosition(id) self:limitView(id) end @@ -412,14 +429,10 @@ function CameraSystem:drawDebugViewBox(id) end function CameraSystem:drawHUD(id) - local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id) local view = self:getView(id) - local string2 = id .. " (" .. viewx .. ":" .. (viewh-viewy) .. ") " + local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id) - love.graphics.translate(viewx, viewy) view.target:drawHUD(id, vieww, viewh) - - love.graphics.translate(-viewx, -(viewy)) end return CameraSystem From 7ec715850c97612f7825dd5aa0fea4db8832c66b Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 22:53:55 +0200 Subject: [PATCH 09/16] chore(camera): remove unused functions --- gamecore/modules/world/camera/init.lua | 59 -------------------------- 1 file changed, 59 deletions(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index 74175c0..b13604f 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -170,27 +170,6 @@ function CameraSystem:getViewCam(id) return view.cam end -function CameraSystem:setScissor(id) - local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id) - - if (self:getViewNumber() > 2) or (self:getViewNumber() == 2 and SPLITSCREEN_ISVERTICAL) then - -- FIXME: it's an ugly workaround that need to be fixed. For an unkown - -- reason, the scissoring is wrong in and only in this function and only - -- when we have vertical split. Thus, we need to substract viewy to viewy - viewy = viewh - viewy - end - - if (self.mode == "split") then - love.graphics.setScissor(viewx, viewy, vieww, viewh) - end -end - -function CameraSystem:resetScissor( ) - if (self.mode == "split") then - love.graphics.setScissor( ) - end -end - function CameraSystem:attachView(id) if (id ~= nil) then local view = self:getView(id) @@ -251,20 +230,6 @@ function CameraSystem:getViewCoordinate(id) return viewx, viewy, vieww, viewh end -function CameraSystem:getInternalCamCoordinate(id) - local view = self:getView(id) - local cam = self:getViewCam(id) - - local viewx, viewy, vieww, viewh - viewx = cam.x - ((self.views.width/2) / cam.scale) - viewy = cam.y - ((self.views.height/2) / cam.scale) - - vieww, viewh = core.screen:getDimensions() - vieww = vieww / cam.scale - viewh = viewh / cam.scale - return viewx, viewy, vieww, viewh -end - function CameraSystem:getOnScreenViewCoordinate(id) local view = self:getView(id) @@ -342,17 +307,6 @@ function CameraSystem:moveView(id, x, y) self:limitView(id) end -function CameraSystem:computeCamPosition(id) - local decalx = self.views.list[id].pos.onScreen.x - local decaly = self.views.list[id].pos.onScreen.y - - local realx = self.views.list[id].pos.x - local realy = self.views.list[id].pos.y - - self.views.list[id].cam.x = realx - decalx - self.views.list[id].cam.y = realy - decaly -end - function CameraSystem:followActor(id) local view = self:getView(id) @@ -415,19 +369,6 @@ end -- DRAW FUNCTIONS -- Basic callback to draw stuff -function CameraSystem:drawDebugViewBox(id) - local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id) - utils.graphics.box(viewx, viewy, vieww, viewh) - - local xx, yy = self:getOnScreenViewCenter(id) - love.graphics.line(xx-3, yy, xx+3, yy) - love.graphics.line(xx, yy-3, xx, yy+3) - - local xx, yy = self:getInternalCamCoordinate(id) - local string = id .. " x:" .. xx .. " y:" .. yy - love.graphics.print(string, viewx + 4, viewy + 4) -end - function CameraSystem:drawHUD(id) local view = self:getView(id) local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id) From 7376d75aeb816eb9d1b2937ff86fa4deaff8e64a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 22:59:58 +0200 Subject: [PATCH 10/16] chore(camera): simplify view system Remove view.cam and view.pos --- gamecore/modules/world/camera/init.lua | 67 +++++++++++--------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index b13604f..2bdeab1 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -92,13 +92,13 @@ end function CameraSystem:recalculateViewsPositions() if #self.views.list == 1 then - self.views.list[1].pos.onScreen.x = 0 - self.views.list[1].pos.onScreen.y = 0 + self.views.list[1].onScreen.x = 0 + self.views.list[1].onScreen.y = 0 else for i,v in ipairs(self.views.list) do local x, y = self:getViewPositions(i) - self.views.list[i].pos.onScreen.x = x - self.views.list[i].pos.onScreen.y = y + self.views.list[i].onScreen.x = x + self.views.list[i].onScreen.y = y end end end @@ -135,14 +135,12 @@ function CameraSystem:addView(x, y, target) local view = {} view.pos = {} - view.pos.x = x or 0 - view.pos.y = y or 0 - view.pos.onScreen = {} + view.x = x or 0 + view.y = y or 0 + view.scale = 1 + + view.onScreen = {} - view.cam = {} - view.cam.scale = 1 - view.cam.x = x - view.cam.y = y -- TODO: add a target system in order to make a camera able -- to target a specific object view.target = target @@ -164,12 +162,6 @@ function CameraSystem:getView(id) return self.views.list[id] end -function CameraSystem:getViewCam(id) - local view = self:getView(id) - - return view.cam -end - function CameraSystem:attachView(id) if (id ~= nil) then local view = self:getView(id) @@ -192,7 +184,7 @@ function CameraSystem:attachView(id) love.graphics.push() love.graphics.origin() love.graphics.translate(math.floor(tx), math.floor(ty)) - love.graphics.scale(view.cam.scale, view.cam.scale) + love.graphics.scale(view.scale, view.scale) end end @@ -217,15 +209,14 @@ end function CameraSystem:getViewCoordinate(id) local view = self:getView(id) - local cam = self:getViewCam(id) local viewx, viewy, vieww, viewh - vieww = self.views.width / cam.scale - viewh = self.views.height / cam.scale + vieww = self.views.width / view.scale + viewh = self.views.height / view.scale - viewx = view.pos.x - (vieww / 2) - viewy = view.pos.y - (viewh / 2) + viewx = view.x - (vieww / 2) + viewy = view.y - (viewh / 2) return viewx, viewy, vieww, viewh end @@ -235,8 +226,8 @@ function CameraSystem:getOnScreenViewCoordinate(id) local viewx, viewy, vieww, viewh local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2) - viewx = (basex) + view.pos.onScreen.x - (self.views.width / 2) - viewy = (basey) + view.pos.onScreen.y - (self.views.height / 2) + viewx = (basex) + view.onScreen.x - (self.views.width / 2) + viewy = (basey) + view.onScreen.y - (self.views.height / 2) viewx, viewy = core.screen:getScreenCoordinate(viewx, viewy) @@ -250,8 +241,8 @@ function CameraSystem:getOnScreenViewRelativePosition(id) local viewx, viewy local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2) - viewx = view.pos.onScreen.x - viewy = view.pos.onScreen.y + viewx = view.onScreen.x + viewy = view.onScreen.y return core.screen:getScreenCoordinate(viewx, viewy) end @@ -261,30 +252,30 @@ function CameraSystem:getOnScreenViewCenter(id) local viewx, viewy local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2) - viewx = (basex) + view.pos.onScreen.x - viewy = (basey) + view.pos.onScreen.y + viewx = (basex) + view.onScreen.x + viewy = (basey) + view.onScreen.y return core.screen:getScreenCoordinate(viewx, viewy) end function CameraSystem:getViewScale(id) - local cam = self:getViewCam(id) + local view = self:getView(id) - return cam.scale + return view.scale end function CameraSystem:limitView(id) local viewx, viewy, vieww, viewh = self:getViewCoordinate(id) local worldw, worldh = self.world:getDimensions() - local posx = self.views.list[id].pos.x - local posy = self.views.list[id].pos.y + local posx = self.views.list[id].x + local posy = self.views.list[id].y local minx = self.views.width / 2 local miny = self.views.height / 2 local maxx = worldw - minx local maxy = worldh - miny - self.views.list[id].pos.x = utils.math.between(posx, minx, maxx) - self.views.list[id].pos.y = utils.math.between(posy, miny, maxy) + self.views.list[id].x = utils.math.between(posx, minx, maxx) + self.views.list[id].y = utils.math.between(posy, miny, maxy) end -- UPDATE and MOVE functions @@ -301,8 +292,8 @@ function CameraSystem:update(dt) end function CameraSystem:moveView(id, x, y) - self.views.list[id].pos.x = x - self.views.list[id].pos.y = y + self.views.list[id].x = x + self.views.list[id].y = y self:limitView(id) end @@ -357,7 +348,7 @@ function CameraSystem:followAllActors(id) local scalex = (maxX-minX)/ww local scaley = (maxY-minY)/hh scale = math.max(scale, scalex, scaley) - view.cam.scale = 1/scale + view.scale = 1/scale self.world:resizeMap(ww * 3, hh * 3) end From f9a268a51a0141a09addf367ed910fac605c6018 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 23:12:22 +0200 Subject: [PATCH 11/16] improvement(camera): make getViewsDimensions a wrapper around a camutil --- gamecore/modules/world/camera/init.lua | 16 +++------------- gamecore/modules/world/camera/utils.lua | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index 2bdeab1..fdff2ee 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -73,21 +73,11 @@ function CameraSystem:haveView() return (self:getViewNumber() == 0) end -function CameraSystem:getViewsDimensions(viewNumber) +function CameraSystem:getViewsDimensions() local basewidth, baseheight = self.views.basewidth, self.views.baseheight - local viewnumber = viewNumber or self:getViewNumber() + local viewnumber = self:getViewNumber() - if (viewnumber <= 1) then - return basewidth, baseheight - elseif (viewnumber == 2) then - if (self.verticalSplit) then - return (basewidth), (baseheight/2) - else - return (basewidth/2), (baseheight) - end - else - return (basewidth/2), (baseheight/2) - end + return camutils.getViewsDimensions(viewnumber, basewidth, baseheight, self.verticalSplit) end function CameraSystem:recalculateViewsPositions() diff --git a/gamecore/modules/world/camera/utils.lua b/gamecore/modules/world/camera/utils.lua index 6d47831..94d2306 100644 --- a/gamecore/modules/world/camera/utils.lua +++ b/gamecore/modules/world/camera/utils.lua @@ -43,4 +43,18 @@ function camutils.getViewsPositions(basewidth, baseheight, verticalSplit) return posList end +function camutils.getViewsDimensions(viewnumber, basewidth, baseheight, verticalSplit) + if (viewnumber <= 1) then + return basewidth, baseheight + elseif (viewnumber == 2) then + if (verticalSplit) then + return (basewidth), (baseheight/2) + else + return (basewidth/2), (baseheight) + end + else + return (basewidth/2), (baseheight/2) + end +end + return camutils From f2cabff81c663fc372ffb5a38559cd42b1150a11 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Jul 2019 23:15:30 +0200 Subject: [PATCH 12/16] chore(camera): improve comments --- gamecore/modules/world/camera/init.lua | 1 - gamecore/modules/world/camera/utils.lua | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index fdff2ee..a6f9576 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -1,5 +1,4 @@ -- camera.lua :: a basic camera adapted to the asset/world system. --- Use hump.camera as the view backend. --[[ Copyright © 2019 Kazhnuz diff --git a/gamecore/modules/world/camera/utils.lua b/gamecore/modules/world/camera/utils.lua index 94d2306..251b9aa 100644 --- a/gamecore/modules/world/camera/utils.lua +++ b/gamecore/modules/world/camera/utils.lua @@ -1,3 +1,26 @@ +-- camutils.lua :: some camera utilities + +--[[ + Copyright © 2019 Kazhnuz + + 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 camutils = {} function camutils.getViewsPositions(basewidth, baseheight, verticalSplit) From 2f7d313ab9c3410b94c49a2f6bc33504b13574ab Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 15 Jul 2019 10:04:06 +0200 Subject: [PATCH 13/16] fix(camera): fix tearing and heavy pixelisation in zoom mode Currently, the canvas was created at render size and elements where resized inside. Work a bit differenlty, by creating a canvas that have the view "true" size (meaning the scaled size), and draw everything at 1:1 size inside. Then, when drawing the view canvas, resize the whole canvas at the multiplication of both scaling. --- gamecore/modules/world/camera/init.lua | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index a6f9576..add1031 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -164,8 +164,8 @@ function CameraSystem:attachView(id) -- de position de camera pour afficher la carte par rapport à ces infos tx, ty = self:getViewCoordinate(id) scale = self:getViewScale(id) or 1 - tx = math.floor(tx) * -1 * scale - ty = math.floor(ty) * -1 * scale + tx = math.floor(tx) * -1 + ty = math.floor(ty) * -1 local w, h = core.screen:getDimensions() end @@ -173,25 +173,30 @@ function CameraSystem:attachView(id) love.graphics.push() love.graphics.origin() love.graphics.translate(math.floor(tx), math.floor(ty)) - love.graphics.scale(view.scale, view.scale) end end function CameraSystem:detachView(id) if (id ~= nil) then local view = self:getView(id) + love.graphics.rectangle("fill", view.x-8, view.y-8, 16, 16) love.graphics.pop() love.graphics.setCanvas(self.current_canvas) local tx, ty = self:getOnScreenViewCoordinate(id) - local scale = core.screen:getScale() + local scale = core.screen:getScale() * view.scale + love.graphics.push() love.graphics.origin() love.graphics.translate(math.floor(tx), math.floor(ty)) love.graphics.scale(scale, scale) love.graphics.draw(view.canvas) + + local unscale = 1 / view.scale + love.graphics.scale(unscale, unscale) self:drawHUD(id) + love.graphics.pop() end end @@ -247,6 +252,16 @@ function CameraSystem:getOnScreenViewCenter(id) return core.screen:getScreenCoordinate(viewx, viewy) end +function CameraSystem:setViewScale(id, scale) + local view = self:getView(id) + + view.scale = scale + + local _, _, w, h = self:getViewCoordinate(id) + + view.canvas = love.graphics.newCanvas(math.ceil(w), math.ceil(h)) +end + function CameraSystem:getViewScale(id) local view = self:getView(id) @@ -337,7 +352,7 @@ function CameraSystem:followAllActors(id) local scalex = (maxX-minX)/ww local scaley = (maxY-minY)/hh scale = math.max(scale, scalex, scaley) - view.scale = 1/scale + self:setViewScale(id, 1/scale) self.world:resizeMap(ww * 3, hh * 3) end From 79aff589b924073cd1fa534d99564c94f62a99d7 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 15 Jul 2019 10:09:54 +0200 Subject: [PATCH 14/16] fix(camera): remove test reticule --- gamecore/modules/world/camera/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index add1031..edf6823 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -179,7 +179,6 @@ end function CameraSystem:detachView(id) if (id ~= nil) then local view = self:getView(id) - love.graphics.rectangle("fill", view.x-8, view.y-8, 16, 16) love.graphics.pop() love.graphics.setCanvas(self.current_canvas) From 932ec262838bef36e0d739fcccc34a90d6f49491 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 15 Jul 2019 10:11:11 +0200 Subject: [PATCH 15/16] improvement(boxes): don't export as a texture the shadow canvas As now the canvas drawing isn't affected by the transform, it should be safe to not export it as a texture anymore. --- gamecore/modules/world/actors/utils/boxes/parent.lua | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/gamecore/modules/world/actors/utils/boxes/parent.lua b/gamecore/modules/world/actors/utils/boxes/parent.lua index d8538c6..9ae975a 100644 --- a/gamecore/modules/world/actors/utils/boxes/parent.lua +++ b/gamecore/modules/world/actors/utils/boxes/parent.lua @@ -40,7 +40,7 @@ function Box3D:new(owner, w, h, d) self.texture = {} self:setTopTexture() self:setBottomTexture() - self.texture.shadows = nil + self.texture.shadows = love.graphics.newCanvas(self.w, self.h) self:register() end @@ -123,19 +123,14 @@ end function Box3D:redrawShadowCanvas() if (self.needRedraw) then - local canvas = love.graphics.newCanvas(self.w, self.h) - love.graphics.setCanvas( canvas ) + love.graphics.setCanvas( self.texture.shadows ) + love.graphics.clear() for i,v in ipairs(self.shadowSources) do v.actor:drawShadow(v.x, v.y) end love.graphics.setCanvas( ) - local imagedata = canvas:newImageData() - self.texture.shadows = love.graphics.newImage( imagedata ) - imagedata:release() - canvas:release() - self.needRedraw = false end end From a48308ae40d5cdc31afb541131b74128eb1691bc Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 15 Jul 2019 10:13:51 +0200 Subject: [PATCH 16/16] meta: update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddd450b..223ab16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **examples:** Rename "movable" to "topdown" +- **camera:** Brand new camera system based on canvases instead of just transforms + ### Fixed - **world:** Remove a forgotten camera debug function @@ -57,6 +59,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **camera:** Fix onscreen coordinates by using new core.screen func +- **camera:** Fix camera's drawing in other resolution mode + ### Removed - **actor:** Remove all function related to XGravity