From 6eacf393b370a7586276108705b805e222a069a5 Mon Sep 17 00:00:00 2001 From: cursed22bc Date: Sat, 28 Feb 2026 12:38:40 +0200 Subject: tile mapping and drawing --- main.lua | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 15 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 66ef333..e44d670 100644 --- a/main.lua +++ b/main.lua @@ -11,13 +11,19 @@ local dpiScale = 1 local canvas = nil local smoothCameraShader = nil ---TODO: separate to components local cameraModule = require("camera") local camera = nil --- end - local fonts = require("fonts") +local currentState = "game" +local currentLevel = "assets/maps/tilemap1.lua" +local world = nil + +local states = { + game = {}, + menu = {}, + settings = {} +} local function recalcScale(w, h) dpiScale = (love.window.getDPIScale and love.window.getDPIScale()) or 1 @@ -27,8 +33,49 @@ local function recalcScale(w, h) offsetY = math.floor((h - VIRTUAL_HEIGHT * finalScale) / 2) end +function states.game.load() + local World = require("world") + world = World:new() + world:load(currentLevel) + local target = world:getPlayer() or { x = 0, y = 0, width = 16, height = 16 } + camera = cameraModule:new(target, VIRTUAL_WIDTH/3, VIRTUAL_HEIGHT/3, true, WORLD_TO_CANVAS) + world:setCamera(camera) +end + +function states.game.update(dt) + if camera then camera:update(dt) end + if world then world:update(dt) end +end + +function states.game.draw() + if camera then camera:set() end + if world then world:draw() end + if camera then camera:unset() end +end + +function states.menu.load() + camera = nil + world = nil +end + +function states.menu.update(dt) end + +function states.menu.draw() + love.graphics.print("Menu (state machine ready)", 10, 10) +end + +function states.settings.load() + camera = nil + world = nil +end + +function states.settings.update(dt) end + +function states.settings.draw() + love.graphics.print("Settings", 10, 10) +end + function love.load() - camera = cameraModule:new({x = 0, y = 0, width = VIRTUAL_WIDTH, height = VIRTUAL_HEIGHT}, VIRTUAL_WIDTH, VIRTUAL_HEIGHT, true, WORLD_TO_CANVAS) love.graphics.setDefaultFilter("nearest", "nearest") love.window.setTitle("Openformer") fonts.load() @@ -46,6 +93,8 @@ function love.load() local w, h = love.graphics.getWidth(), love.graphics.getHeight() recalcScale(w, h) + local state = states[currentState] + if state and state.load then state.load() end end function love.resize(w, h) @@ -55,10 +104,9 @@ function love.resize(w, h) canvas:setFilter("nearest", "nearest") end - function love.update(dt) - if not camera then return end - camera:update(dt) + local state = states[currentState] + if state and state.update then state.update(dt) end end function love.keypressed(key, scancode, isrepeat) @@ -71,20 +119,33 @@ function love.draw() love.graphics.setCanvas(canvas) love.graphics.clear() love.graphics.push() - if camera then - camera:set() - love.graphics.print("FPS: " .. love.timer.getFPS(), 10, 10) + local state = states[currentState] + if state and state.draw then state.draw() end love.graphics.pop() - - camera:unset() - end - love.graphics.setCanvas() love.graphics.clear() local drawX = offsetX - (CANVAS_PADDING * finalScale) / 2 local drawY = offsetY - (CANVAS_PADDING * finalScale) / 2 + + local subDx, subDy = 0, 0 + if world and world.camera and world.camera.getSubPixelOffset then + subDx, subDy = world.camera:getSubPixelOffset() + end + + local uvOffsetX = subDx * WORLD_TO_CANVAS / CANVAS_WIDTH + local uvOffsetY = subDy * WORLD_TO_CANVAS / CANVAS_HEIGHT + + if smoothCameraShader and (subDx ~= 0 or subDy ~= 0) then + smoothCameraShader:send("offset", { uvOffsetX, uvOffsetY }) + love.graphics.setShader(smoothCameraShader) + end + love.graphics.draw(canvas, math.floor(drawX), math.floor(drawY), 0, finalScale, finalScale) + + if smoothCameraShader then + love.graphics.setShader() + end end -return nil \ No newline at end of file +return nil -- cgit v1.2.3