Skip to content

Commit

Permalink
Image data is now managed without needing to serialize it out
Browse files Browse the repository at this point in the history
Games load and save again, though not fully successfully.
Refactor for image management is much cleaner than previous
implementations.

Closes #109
  • Loading branch information
tredfern committed Jun 24, 2021
1 parent bd3366c commit e3c6e3b
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 284 deletions.
21 changes: 10 additions & 11 deletions assets/bestiary/alien.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
-- https://opensource.org/licenses/MIT

local animation = require "game.rules.graphics.animation"
local sprite = require "game.rules.graphics.sprite"
local imageMgr = require "moonpie.graphics.image"
local Settings = require "game.settings"
local ImageManager = require "game.rules.graphics.image_manager"
local fileName = Settings.assetPath("bestiary/alien_idle.png")

local imageData = imageMgr.load(Settings.assetPath("bestiary/alien_idle.png"))
local frame1 = sprite.fromAtlas(imageData, 0, 0, 32, 32)
local frame2 = sprite.fromAtlas(imageData, 32, 0, 32, 32)
local frame3 = sprite.fromAtlas(imageData, 64, 0, 32, 32)
local frame4 = sprite.fromAtlas(imageData, 96, 0, 32, 32)
ImageManager.load("ALIEN_IDLE_FRAME_1", fileName, { 0, 0, 32, 32 })
ImageManager.load("ALIEN_IDLE_FRAME_2", fileName, { 32, 0, 32, 32 })
ImageManager.load("ALIEN_IDLE_FRAME_3", fileName, { 64, 0, 32, 32 })
ImageManager.load("ALIEN_IDLE_FRAME_4", fileName, { 96, 0, 32, 32 })

local alienIdle = animation:new()
alienIdle:addFrame(frame1, 0.3)
alienIdle:addFrame(frame2, 0.3)
alienIdle:addFrame(frame3, 0.3)
alienIdle:addFrame(frame4, 0.3)
alienIdle:addFrame(ImageManager.getDrawable("ALIEN_IDLE_FRAME_1"), 0.3)
alienIdle:addFrame(ImageManager.getDrawable("ALIEN_IDLE_FRAME_2"), 0.3)
alienIdle:addFrame(ImageManager.getDrawable("ALIEN_IDLE_FRAME_3"), 0.3)
alienIdle:addFrame(ImageManager.getDrawable("ALIEN_IDLE_FRAME_4"), 0.3)

return alienIdle
21 changes: 10 additions & 11 deletions assets/characters/character_idle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
-- https://opensource.org/licenses/MIT

local animation = require "game.rules.graphics.animation"
local sprite = require "game.rules.graphics.sprite"
local imageMgr = require "moonpie.graphics.image"
local Settings = require "game.settings"
local ImageManager = require "game.rules.graphics.image_manager"
local fileName = Settings.assetPath("characters/character_idle.png")

local imageData = imageMgr.load(Settings.assetPath("characters/character_idle.png"))
local frame1 = sprite.fromAtlas(imageData, 0, 0, 32, 32)
local frame2 = sprite.fromAtlas(imageData, 32, 0, 32, 32)
local frame3 = sprite.fromAtlas(imageData, 64, 0, 32, 32)
local frame4 = sprite.fromAtlas(imageData, 96, 0, 32, 32)
ImageManager.load("CHARACTER_IDLE_FRAME_1", fileName, { 0, 0, 32, 32 })
ImageManager.load("CHARACTER_IDLE_FRAME_2", fileName, { 32, 0, 32, 32 })
ImageManager.load("CHARACTER_IDLE_FRAME_3", fileName, { 64, 0, 32, 32 })
ImageManager.load("CHARACTER_IDLE_FRAME_4", fileName, { 96, 0, 32, 32 })

local character_idle = animation:new()
character_idle:addFrame(frame1, 0.3)
character_idle:addFrame(frame2, 0.3)
character_idle:addFrame(frame3, 0.3)
character_idle:addFrame(frame4, 0.3)
character_idle:addFrame(ImageManager.getDrawable("CHARACTER_IDLE_FRAME_1"), 0.3)
character_idle:addFrame(ImageManager.getDrawable("CHARACTER_IDLE_FRAME_2"), 0.3)
character_idle:addFrame(ImageManager.getDrawable("CHARACTER_IDLE_FRAME_3"), 0.3)
character_idle:addFrame(ImageManager.getDrawable("CHARACTER_IDLE_FRAME_4"), 0.3)

return character_idle
50 changes: 0 additions & 50 deletions assets/graphics/simple-character-1.json

This file was deleted.

21 changes: 10 additions & 11 deletions assets/maps/features/doors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local Sprite = require "game.rules.graphics.sprite"
local ImageMgr = require "moonpie.graphics.image"
local ImageManager = require "game.rules.graphics.image_manager"
local Settings = require "game.settings"
local Animation = require "game.rules.graphics.animation"
local Orientation = require "game.rules.world.orientation"

local doorAtlas = ImageMgr.load(Settings.assetPath("maps/features/doors.png"))
local closed = Sprite.fromAtlas(doorAtlas, 0, 0, 32, 32)
local opening1 = Sprite.fromAtlas(doorAtlas, 32, 0, 32, 32)
local opening2 = Sprite.fromAtlas(doorAtlas, 0, 32, 32, 32)
local opened = Sprite.fromAtlas(doorAtlas, 32, 32, 32, 32)
local filename = Settings.assetPath("maps/features/doors.png")
local closed = ImageManager.load("DOOR_CLOSED_NS", filename, { 0, 0, 32, 32 })
local opening1 = ImageManager.load("DOOR_OPENING_NS_1", filename, { 32, 0, 32, 32 })
local opening2 = ImageManager.load("DOOR_OPENING_NS_2", filename, { 0, 32, 32, 32 })
local opened = ImageManager.load("DOOR_OPENED_NS", filename, { 32, 32, 32, 32 })

local opening = Animation:new()
opening:addFrame(opening1, 0.2)
opening:addFrame(opening2, 0.2)
opening:addFrame(opened, 0.2)

local closedEW = Sprite.fromAtlas(doorAtlas, 64, 0, 32, 32)
local openingEW1 = Sprite.fromAtlas(doorAtlas, 96, 0, 32, 32)
local openingEW2 = Sprite.fromAtlas(doorAtlas, 64, 32, 32, 32)
local openedEW = Sprite.fromAtlas(doorAtlas, 96, 32, 32, 32)
local closedEW = ImageManager.load("DOOR_CLOSED_EW", filename, { 64, 0, 32, 32 })
local openingEW1 = ImageManager.load("DOOR_OPENING_EW_1", filename, { 96, 0, 32, 32 })
local openingEW2 = ImageManager.load("DOOR_OPENING_EW_2", filename, { 64, 32, 32, 32 })
local openedEW = ImageManager.load("DOOR_OPENED_EW", filename, { 96, 32, 32, 32 })

local openingEW = Animation:new()
openingEW:addFrame(openingEW1, 0.2)
Expand Down
10 changes: 5 additions & 5 deletions assets/maps/features/ladders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local Sprite = require "game.rules.graphics.sprite"
local ImageMgr = require "moonpie.graphics.image"
local ImageManager = require "game.rules.graphics.image_manager"
local Settings = require "game.settings"

local ladderAtlas = ImageMgr.load(Settings.assetPath("maps/features/ladders.png"))
local filename = Settings.assetPath("maps/features/ladders.png")

return {
down = Sprite.fromAtlas(ladderAtlas, 0, 0, 32, 32),
up = Sprite.fromAtlas(ladderAtlas, 32, 0, 32, 32)
down = ImageManager.load("LADDER_DOWN", filename, { 0, 0, 32, 32 }),
up = ImageManager.load("LADDER_UP", filename, { 32, 0, 32, 32 })
}
36 changes: 0 additions & 36 deletions assets/maps/terrains.lua

This file was deleted.

35 changes: 17 additions & 18 deletions assets/maps/walls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local imageMgr = require "moonpie.graphics.image"
local Sprite = require "game.rules.graphics.sprite"
local ImageManager = require "game.rules.graphics.image_manager"

local wallAtlas = imageMgr.load("assets/maps/spaceship-walls.png")
local filename = "assets/maps/spaceship-walls.png"

local wallSprite = { }
wallSprite.se = Sprite.fromAtlas(wallAtlas, 0, 0, 32, 32)
wallSprite.nsew = Sprite.fromAtlas(wallAtlas, 32, 0, 32, 32)
wallSprite.ew = Sprite.fromAtlas(wallAtlas, 64, 0, 32, 32)
wallSprite.sw = Sprite.fromAtlas(wallAtlas, 96, 0, 32, 32)
wallSprite.nse = Sprite.fromAtlas(wallAtlas, 0, 32, 32, 32)
wallSprite.w = Sprite.fromAtlas(wallAtlas, 32, 32, 32, 32)
wallSprite.e = Sprite.fromAtlas(wallAtlas, 64, 32, 32, 32)
wallSprite.nsw = Sprite.fromAtlas(wallAtlas, 96, 32, 32, 32)
wallSprite.ns = Sprite.fromAtlas(wallAtlas, 0, 64, 32, 32)
wallSprite.s = Sprite.fromAtlas(wallAtlas, 32, 64, 32, 32)
wallSprite.sew = Sprite.fromAtlas(wallAtlas, 64, 64, 32, 32)
wallSprite.ne = Sprite.fromAtlas(wallAtlas, 0, 96, 32, 32)
wallSprite.new = Sprite.fromAtlas(wallAtlas, 32, 96, 32, 32)
wallSprite.n = Sprite.fromAtlas(wallAtlas, 64, 96, 32, 32)
wallSprite.nw = Sprite.fromAtlas(wallAtlas, 96, 96, 32, 32)
wallSprite.se = ImageManager.load("SPACESHIP_WALL_SE", filename, { 0, 0, 32, 32 })
wallSprite.nsew = ImageManager.load("SPACESHIP_WALL_NSEW", filename, { 32, 0, 32, 32 })
wallSprite.ew = ImageManager.load("SPACESHIP_WALL_EW", filename, { 64, 0, 32, 32 })
wallSprite.sw = ImageManager.load("SPACESHIP_WALL_SW", filename, { 96, 0, 32, 32 })
wallSprite.nse = ImageManager.load("SPACESHIP_WALL_NSE", filename, { 0, 32, 32, 32 })
wallSprite.w = ImageManager.load("SPACESHIP_WALL_W", filename, { 32, 32, 32, 32 })
wallSprite.e = ImageManager.load("SPACESHIP_WALL_E", filename, { 64, 32, 32, 32 })
wallSprite.nsw = ImageManager.load("SPACESHIP_WALL_NSW", filename, { 96, 32, 32, 32 })
wallSprite.ns = ImageManager.load("SPACESHIP_WALL_NS", filename, { 0, 64, 32, 32 })
wallSprite.s = ImageManager.load("SPACESHIP_WALL_S", filename, { 32, 64, 32, 32 })
wallSprite.sew = ImageManager.load("SPACESHIP_WALL_SEW", filename, { 64, 64, 32, 32 })
wallSprite.ne = ImageManager.load("SPACESHIP_WALL_NE", filename, { 0, 96, 32, 32 })
wallSprite.new = ImageManager.load("SPACESHIP_WALL_NEW", filename, { 32, 96, 32, 32 })
wallSprite.n = ImageManager.load("SPACESHIP_WALL_N", filename, { 64, 96, 32, 32 })
wallSprite.nw = ImageManager.load("SPACESHIP_WALL_NW", filename, { 96, 96, 32, 32 })

return wallSprite
2 changes: 1 addition & 1 deletion ext/moonpie
4 changes: 2 additions & 2 deletions game/rules/aliens/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


local tables = require "moonpie.tables"
local sprite = require "game.rules.graphics.sprite"
local ImageManager = require "game.rules.graphics.image_manager"
local Animator = require "game.rules.graphics.animator"
local World = require "game.rules.world"
local Behaviors = require "game.rules.npcs.behaviors"
Expand Down Expand Up @@ -33,7 +33,7 @@ function Actions.addSpawner(position)
return World.actions.addEntity {
position = position,
spawner = true,
sprite = sprite.fromFile("assets/graphics/spawner.png")
sprite = ImageManager.load("ALIEN_SPAWNER", "assets/graphics/spawner.png")
}
end

Expand Down
63 changes: 63 additions & 0 deletions game/rules/graphics/image_manager.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local tables = require "moonpie.tables"
local class = require "moonpie.class"
local Colors = require "game.rules.graphics.colors"
local Wrapper = class({})
local ImageManager = {}
local store = {}
local imageData = {}


function Wrapper:constructor(imageKey)
self.imageKey = imageKey
self.color = Colors.drawDefault
end

function Wrapper:draw(sx, sy)
-- This works around getting access back to the image manager on reloading
local mgr = require "game.rules.graphics.image_manager"
local i, q = mgr.get(self.imageKey)
love.graphics.setColor(self.color)
if q then
love.graphics.draw(i, q, sx, sy)
else
love.graphics.draw(i, sx, sy)
end
end

function Wrapper:setColor(c)
self.color = c
end

function ImageManager.set(key, image, quad)
store[key] = tables.pack(image, quad)
end

function ImageManager.get(key)
if store[key] then
return tables.unpack(store[key])
end
end

function ImageManager.load(key, filename, quadData)
local q = nil
imageData[filename] = imageData[filename] or love.graphics.newImage(filename)
if quadData then
local x, y, w, h = tables.unpack(quadData)
q = love.graphics.newQuad(x, y, w, h, imageData[filename])
end

ImageManager.set(key, imageData[filename], q)
return ImageManager.getDrawable(key)
end

function ImageManager.getDrawable(key)
return Wrapper:new(key)
end


return ImageManager
Loading

0 comments on commit e3c6e3b

Please sign in to comment.