From 1d8083af86995c8d2714ee4dd647864ac6a0eb97 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 4 Jan 2019 18:09:26 +0100 Subject: [PATCH] engine: get rid of the Dict class (WIP). This was not a very smart class. We replace it with a bidirectional map. --- doc/tutorial/06_sprite.cpp | 6 +- src/Makefile.am | 11 ++- src/audio/sample.cpp | 21 +++++- src/audio/sampler.cpp | 84 ---------------------- src/engine/entity.h | 29 +++++++- src/font.cpp | 21 +++++- src/font.h | 7 +- src/forge.cpp | 61 ---------------- src/forge.h | 38 ---------- src/lol-core.vcxproj | 8 --- src/lol-core.vcxproj.filter | 24 ------- src/lol/audio/all.h | 3 +- src/lol/audio/sample.h | 9 ++- src/lol/audio/sampler.h | 41 ----------- src/lol/extras.h | 7 +- src/text.cpp | 18 +++-- src/tiler.cpp | 103 --------------------------- src/tiler.h | 42 ----------- src/tileset.cpp | 135 +++++++++++++++++++++++------------- src/tileset.h | 18 +++-- 20 files changed, 191 insertions(+), 495 deletions(-) delete mode 100644 src/audio/sampler.cpp delete mode 100644 src/forge.cpp delete mode 100644 src/forge.h delete mode 100644 src/lol/audio/sampler.h delete mode 100644 src/tiler.cpp delete mode 100644 src/tiler.h diff --git a/doc/tutorial/06_sprite.cpp b/doc/tutorial/06_sprite.cpp index 69d45197..c4257fc7 100644 --- a/doc/tutorial/06_sprite.cpp +++ b/doc/tutorial/06_sprite.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Sprite tutorial // -// Copyright © 2011—2015 Sam Hocevar +// Copyright © 2011—2019 Sam Hocevar // © 2012 Daniel Stephens (artwork) // // Lol Engine is free software. It comes without any warranty, to @@ -31,7 +31,7 @@ public: scene.PushCamera(m_camera); Ticker::Ref(m_camera); - m_tileset = Tiler::Register("06_sprite.png"); + m_tileset = TileSet::create("06_sprite.png"); for (int i = 0; i < FRAME_COUNT; ++i) m_tileset->define_tile(ibox2(i * 24, 376, 24 + i * 24, 24 + 376)); @@ -46,7 +46,7 @@ public: ~SpriteTutorial() { - Tiler::Deregister(m_tileset); + TileSet::destroy(m_tileset); Scene& scene = Scene::GetScene(); scene.PopCamera(m_camera); diff --git a/src/Makefile.am b/src/Makefile.am index f348e1ce..5c4a1b2d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,10 +8,9 @@ noinst_LIBRARIES = liblol-core.a EXTRA_DIST += lol-core.vcxproj lol-core.vcxproj.filter liblol_core_a_SOURCES = \ - tiler.cpp tiler.h dict.cpp dict.h lolgl.h \ - scene.cpp scene.h font.cpp font.h \ + lolgl.h scene.cpp scene.h font.cpp font.h \ textureimage.cpp textureimage.h textureimage-private.h \ - tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h \ + tileset.cpp tileset.h video.cpp video.h \ profiler.cpp profiler.h text.cpp text.h emitter.cpp emitter.h \ numeric.h utils.h messageservice.cpp messageservice.h \ gradient.cpp gradient.h gradient.lolfx \ @@ -52,7 +51,7 @@ liblol_core_headers = \ lol/algorithm/sort.h lol/algorithm/portal.h lol/algorithm/aabb_tree.h \ \ lol/audio/all.h \ - lol/audio/audio.h lol/audio/sampler.h lol/audio/sample.h \ + lol/audio/audio.h lol/audio/sample.h \ \ lol/sys/all.h \ lol/sys/init.h lol/sys/file.h lol/sys/getopt.h lol/sys/thread.h \ @@ -99,7 +98,7 @@ liblol_core_sources = \ gpu/framebuffer.cpp gpu/texture.cpp gpu/renderer.cpp \ gpu/rendercontext.cpp \ \ - audio/audio.cpp audio/sampler.cpp audio/sample.cpp \ + audio/audio.cpp audio/sample.cpp \ \ input/input.cpp input/input.h input/input_internal.h input/keys.h \ input/controller.cpp input/controller.h \ @@ -119,7 +118,7 @@ liblol_core_sources = \ sys/init.cpp sys/file.cpp sys/hacks.cpp \ sys/thread.cpp sys/threadtypes.cpp sys/getopt.cpp \ \ - image/resource.cpp image/resource-private.h \ + image/resource.cpp image/resource-private.h \ image/image.cpp image/image-private.h image/kernel.cpp image/pixel.cpp \ image/crop.cpp image/resample.cpp image/noise.cpp image/combine.cpp \ image/codec/gdiplus-image.cpp image/codec/imlib2-image.cpp \ diff --git a/src/audio/sample.cpp b/src/audio/sample.cpp index 1f855b7b..2b42ac28 100644 --- a/src/audio/sample.cpp +++ b/src/audio/sample.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -32,6 +32,9 @@ namespace lol { +/* The sample cache */ +static entity_dict sample_cache; + /* * sample implementation class */ @@ -52,7 +55,19 @@ private: * Public sample class */ -sample::sample(char const *path) +sample *sample::create(std::string const &path) +{ + auto ret = sample_cache.get(path); + return ret ? ret : sample_cache.set(path, new sample(path)); +} + +void sample::destroy(sample *s) +{ + // FIXME: decrement! + sample_cache.erase(s); +} + +sample::sample(std::string const &path) : data(new sample_data()) { data->m_name = std::string(" ") + path; @@ -66,7 +81,7 @@ sample::sample(char const *path) } if (!data->m_chunk) { - msg::error("could not load sample %s: %s\n", path, Mix_GetError()); + msg::error("could not load sample %s: %s\n", path.c_str(), Mix_GetError()); } data->m_channel = -1; #endif diff --git a/src/audio/sampler.cpp b/src/audio/sampler.cpp deleted file mode 100644 index 55360ad1..00000000 --- a/src/audio/sampler.cpp +++ /dev/null @@ -1,84 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2016 Sam Hocevar -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#include - -namespace lol -{ - -/* - * sampler implementation class - */ - -static class sampler_data -{ - friend class sampler; - -public: - Dict samples; -} -samplerdata; - -static sampler_data * const data = &samplerdata; - -/* - * Public sampler class - */ - -int sampler::load_sample(char const *path) -{ - int id = data->samples.MakeSlot(path); - - if (!data->samples.GetEntity(id)) - { - sample *s = new sample(path); - data->samples.SetEntity(id, s); - } - - return id + 1; /* ID 0 is for the empty sample */ -} - -void sampler::unload_sample(int id) -{ - if (id > 0) - data->samples.RemoveSlot(id - 1); /* ID 0 is for the empty sample */ -} - -void sampler::play_sample(int id) -{ - if (id > 0) - { - sample *s = (sample *)data->samples.GetEntity(id - 1); - s->play(); - } -} - -void sampler::loop_sample(int id) -{ - if (id > 0) - { - sample *s = (sample *)data->samples.GetEntity(id - 1); - s->loop(); - } -} - -void sampler::stop_sample(int id) -{ - if (id > 0) - { - sample *s = (sample *)data->samples.GetEntity(id - 1); - s->stop(); - } -} - -} /* namespace lol */ - diff --git a/src/engine/entity.h b/src/engine/entity.h index b2aa1a5d..d284fcb2 100644 --- a/src/engine/entity.h +++ b/src/engine/entity.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -46,7 +46,6 @@ class Entity friend class Scene; friend class Ticker; friend class TickerData; - friend class Dict; friend class Emcee; public: @@ -150,5 +149,31 @@ private: uint64_t m_scene_mask = 0; }; +template struct entity_dict +{ + T *get(std::string const &key) + { + auto it = m_cache1.find(key); + return it != m_cache1.end() ? it->second : nullptr; + } + + T *set(std::string const &key, T *entity) + { + m_cache1[key] = entity; + m_cache2[entity] = key; + return entity; + } + + void erase(T *entity) + { + // FIXME: temporary; we need Ticker::Ref etc. + m_cache1.erase(m_cache2[entity]); + m_cache2.erase(entity); + } + + std::map m_cache1; + std::map m_cache2; +}; + } /* namespace lol */ diff --git a/src/font.cpp b/src/font.cpp index cf5f5ccc..6733b5cb 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // 2013 Jean-Yves Lamoureux // // Lol Engine is free software. It comes without any warranty, to @@ -19,6 +19,9 @@ namespace lol { +/* The font cache */ +static entity_dict font_cache; + /* * Font implementation class */ @@ -37,12 +40,24 @@ private: * Public Font class */ +Font *Font::create(std::string const &path) +{ + auto ret = font_cache.get(path); + return ret ? ret : font_cache.set(path, new Font(path)); +} + +void Font::destroy(Font *f) +{ + // FIXME: decrement! + font_cache.erase(f); +} + Font::Font(std::string const &path) : data(new FontData()) { data->m_name = " " + path; - data->tileset = Tiler::Register(path, ivec2::zero, ivec2(16)); + data->tileset = TileSet::create(path, ivec2::zero, ivec2(16)); data->size = data->tileset->GetTileSize(0); m_drawgroup = DRAWGROUP_TEXTURE; @@ -50,7 +65,7 @@ Font::Font(std::string const &path) Font::~Font() { - Tiler::Deregister(data->tileset); + TileSet::destroy(data->tileset); delete data; } diff --git a/src/font.h b/src/font.h index 95113200..5d47dfb9 100644 --- a/src/font.h +++ b/src/font.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -27,10 +27,13 @@ class FontData; class Font : public Entity { public: + static Font *create(std::string const &path); + static void destroy(Font *); + +protected: Font(std::string const &path); ~Font(); -protected: /* Inherited from Entity */ virtual std::string GetName() const; virtual void tick_draw(float seconds, Scene &scene); diff --git a/src/forge.cpp b/src/forge.cpp deleted file mode 100644 index 3aef045f..00000000 --- a/src/forge.cpp +++ /dev/null @@ -1,61 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2018 Sam Hocevar -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#include - -namespace lol -{ - -/* - * Forge implementation class - */ - -static class ForgeData -{ - friend class Forge; - -public: - Dict fonts; -} -forgedata; - -static ForgeData * const data = &forgedata; - -/* - * Public Forge class - */ - -int Forge::Register(std::string const &path) -{ - int id = data->fonts.MakeSlot(path); - - if (!data->fonts.GetEntity(id)) - { - Font *font = new Font(path); - data->fonts.SetEntity(id, font); - } - - return id; -} - -void Forge::Deregister(int id) -{ - data->fonts.RemoveSlot(id); -} - -Font *Forge::GetFont(int id) -{ - return (Font *)data->fonts.GetEntity(id); -} - -} /* namespace lol */ - diff --git a/src/forge.h b/src/forge.h deleted file mode 100644 index fa11815a..00000000 --- a/src/forge.h +++ /dev/null @@ -1,38 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2018 Sam Hocevar -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#pragma once - -// -// The Forge class -// --------------- -// The Forge is a static class that manages fonts. -// - -#include "font.h" - -namespace lol -{ - -class Forge -{ -public: - static int Register(std::string const &path); - static void Deregister(int id); - static Font *GetFont(int id); - -private: - Forge() {} -}; - -} /* namespace lol */ - diff --git a/src/lol-core.vcxproj b/src/lol-core.vcxproj index 79004934..432a0410 100644 --- a/src/lol-core.vcxproj +++ b/src/lol-core.vcxproj @@ -83,7 +83,6 @@ - @@ -93,7 +92,6 @@ - @@ -111,7 +109,6 @@ - @@ -216,7 +213,6 @@ - @@ -227,7 +223,6 @@ - @@ -240,7 +235,6 @@ - @@ -260,7 +254,6 @@ - @@ -368,7 +361,6 @@ - diff --git a/src/lol-core.vcxproj.filter b/src/lol-core.vcxproj.filter index e77d9353..55fda846 100644 --- a/src/lol-core.vcxproj.filter +++ b/src/lol-core.vcxproj.filter @@ -204,15 +204,9 @@ audio - - audio - ... - - ... - ... @@ -222,9 +216,6 @@ ... - - ... - ... @@ -391,9 +382,6 @@ sys - - tileset - tileset @@ -532,9 +520,6 @@ ... - - ... - ... @@ -544,9 +529,6 @@ ... - - ... - ... @@ -592,9 +574,6 @@ lol\audio - - lol\audio - lol\base @@ -761,9 +740,6 @@ lol\sys - - tileset - tileset diff --git a/src/lol/audio/all.h b/src/lol/audio/all.h index 68f2947b..15503fa8 100644 --- a/src/lol/audio/all.h +++ b/src/lol/audio/all.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -13,6 +13,5 @@ #pragma once #include -#include #include diff --git a/src/lol/audio/sample.h b/src/lol/audio/sample.h index b6a96428..42381417 100644 --- a/src/lol/audio/sample.h +++ b/src/lol/audio/sample.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -30,10 +30,13 @@ class sample_data; class sample : public Entity { public: - sample(char const *path); - virtual ~sample(); + static sample *create(std::string const &path); + static void destroy(sample *s); protected: + sample(std::string const &path); + virtual ~sample(); + /* Inherited from Entity */ virtual std::string GetName() const; virtual void tick_game(float seconds); diff --git a/src/lol/audio/sampler.h b/src/lol/audio/sampler.h deleted file mode 100644 index 1a24f359..00000000 --- a/src/lol/audio/sampler.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2016 Sam Hocevar -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#pragma once - -// -// The sampler class -// ----------------- -// The sampler is a static class that manages samples. -// - -#include - -namespace lol -{ - -class sampler -{ -public: - static int load_sample(char const *path); - static void unload_sample(int id); - - static void play_sample(int id); - static void loop_sample(int id); - static void stop_sample(int id); - -private: - sampler() {} -}; - -} /* namespace lol */ - diff --git a/src/lol/extras.h b/src/lol/extras.h index 3fd90528..4ac35e83 100644 --- a/src/lol/extras.h +++ b/src/lol/extras.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -50,14 +50,9 @@ #include // Other objects -#include #include #include #include #include #include -// Managers -#include -#include - diff --git a/src/text.cpp b/src/text.cpp index 07473d2c..3c61aa38 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -28,7 +28,7 @@ class TextData friend class Text; private: - int m_font; + Font *m_font; TextAlign m_align; std::string m_text; vec3 m_pos; @@ -43,7 +43,7 @@ private: Text::Text(std::string const &text, char const *font) : data(new TextData()) { - data->m_font = Forge::Register(font); + data->m_font = Font::create(font); data->m_align = TextAlign::Left; data->m_text = text; data->m_pos = vec3(0, 0, 0); @@ -90,8 +90,7 @@ vec3 Text::GetPos() ivec2 Text::GetFontSize() { - Font *font = Forge::GetFont(data->m_font); - return font->GetSize(); + return data->m_font->GetSize(); } void Text::tick_draw(float seconds, Scene &scene) @@ -100,24 +99,23 @@ void Text::tick_draw(float seconds, Scene &scene) if (auto length = data->m_text.length()) { - Font *font = Forge::GetFont(data->m_font); vec3 delta(0.0f); float text_width = ((length - 0.5f) + (length - 1) * data->m_spacing) - * font->GetSize().x; + * data->m_font->GetSize().x; if (data->m_align == TextAlign::Right) delta.x -= text_width * data->m_scale.x; else if (data->m_align == TextAlign::Center) delta.x -= 0.5f * text_width * data->m_scale.x; - font->Print(scene, data->m_pos + delta, data->m_text, - data->m_scale, data->m_spacing); + data->m_font->Print(scene, data->m_pos + delta, data->m_text, + data->m_scale, data->m_spacing); } } Text::~Text() { - Forge::Deregister(data->m_font); + Font::destroy(data->m_font); delete data; } diff --git a/src/tiler.cpp b/src/tiler.cpp deleted file mode 100644 index 6bc7c1f9..00000000 --- a/src/tiler.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2018 Sam Hocevar -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#include - -namespace lol -{ - -/* - * Tiler implementation class - */ - -static class TilerData -{ - friend class Tiler; - -public: - TilerData() - { } - -private: - Dict tilesets; -} -tilerdata; - -static TilerData * const data = &tilerdata; - -/* - * Public Tiler class - */ - -TileSet *Tiler::Register(std::string const &path, ivec2 size, ivec2 count) -{ - int id = data->tilesets.MakeSlot(path); - TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id); - - if (!tileset) - { - tileset = new TileSet(path, size, count); - data->tilesets.SetEntity(id, tileset); - } - - return tileset; -} - -TileSet *Tiler::Register(std::string const &path) -{ - int id = data->tilesets.MakeSlot(path); - TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id); - - if (!tileset) - { - tileset = new TileSet(path); - data->tilesets.SetEntity(id, tileset); - } - - return tileset; -} - -TileSet *Tiler::Register(std::string const &path, Image* image, ivec2 size, ivec2 count) -{ - int id = data->tilesets.MakeSlot(path); - TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id); - - if (!tileset) - { - tileset = new TileSet(path, image, size, count); - data->tilesets.SetEntity(id, tileset); - } - - return tileset; -} - -TileSet *Tiler::Register(std::string const &path, Image* image) -{ - int id = data->tilesets.MakeSlot(path); - TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id); - - if (!tileset) - { - tileset = new TileSet(path, image); - data->tilesets.SetEntity(id, tileset); - } - - return tileset; -} - -void Tiler::Deregister(TileSet *tileset) -{ - data->tilesets.RemoveSlot(tileset); -} - -} /* namespace lol */ - diff --git a/src/tiler.h b/src/tiler.h deleted file mode 100644 index 69383e94..00000000 --- a/src/tiler.h +++ /dev/null @@ -1,42 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2018 Sam Hocevar -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#pragma once - -// -// The Tiler class -// --------------- -// The Tiler is a static class that manages tilesets. -// - -#include - -#include "tileset.h" - -namespace lol -{ - -class Tiler -{ -public: - static TileSet *Register(std::string const &path, ivec2 size, ivec2 count); - static TileSet *Register(std::string const &path); - static TileSet *Register(std::string const &path, image* img, ivec2 size, ivec2 count); - static TileSet *Register(std::string const &path, image* img); - static void Deregister(TileSet *); - -private: - Tiler() {} -}; - -} /* namespace lol */ - diff --git a/src/tileset.cpp b/src/tileset.cpp index 70eaa09f..a7c72967 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,6 +12,7 @@ #include +#include #include #include #include @@ -27,6 +28,9 @@ namespace lol { +/* The tileset cache */ +static entity_dict tileset_cache; + /* * TileSet implementation class */ @@ -45,72 +49,107 @@ protected: * Public TileSet class */ -TileSet::TileSet(std::string const &path) - : TextureImage(path), - m_tileset_data(new TileSetData()), - m_palette(nullptr) +TileSet *TileSet::create(std::string const &path) { + auto ret = tileset_cache.get(path); + return ret ? ret : tileset_cache.set(path, new TileSet(path)); } -TileSet::TileSet(std::string const &path, Image* image) - : TextureImage(path, image), - m_tileset_data(new TileSetData()), - m_palette(nullptr) +TileSet *TileSet::create(std::string const &path, image* img) { + auto ret = tileset_cache.get(path); + return ret ? ret : tileset_cache.set(path, new TileSet(path, img)); } -TileSet::TileSet(std::string const &path, Image* image, array& tiles) - : TextureImage(path, image), - m_tileset_data(new TileSetData()), - m_palette(nullptr) +TileSet *TileSet::create(std::string const &path, image* img, array& tiles) { - define_tile(tiles); + auto ret = tileset_cache.get(path); + if (!ret) + { + ret = tileset_cache.set(path, new TileSet(path, img)); + ret->define_tile(tiles); + } + return ret; } -TileSet::TileSet(std::string const &path, ivec2 size, ivec2 count) - : TileSet(path) +TileSet *TileSet::create(std::string const &path, ivec2 size, ivec2 count) { - /* If count is valid, fix size; otherwise, fix count. */ - if (count.x > 0 && count.y > 0) + auto ret = tileset_cache.get(path); + if (!ret) { - size = m_data->m_image_size / count; - } - else - { - if (size.x <= 0 || size.y <= 0) - size = ivec2(32, 32); - count = max(ivec2(1, 1), m_data->m_image_size / size); + ret = tileset_cache.set(path, new TileSet(path)); + + /* If count is valid, fix size; otherwise, fix count. */ + if (count.x > 0 && count.y > 0) + { + size = ret->m_data->m_image_size / count; + } + else + { + if (size.x <= 0 || size.y <= 0) + size = ivec2(32, 32); + count = max(ivec2(1, 1), ret->m_data->m_image_size / size); + } + + for (int j = 0; j < count.y; ++j) + for (int i = 0; i < count.x; ++i) + { + ret->define_tile(ibox2(size * ivec2(i, j), + size * ivec2(i + 1, j + 1))); + } } - for (int j = 0; j < count.y; ++j) - for (int i = 0; i < count.x; ++i) - { - define_tile(ibox2(size * ivec2(i, j), - size * ivec2(i + 1, j + 1))); - } + return ret; } -TileSet::TileSet(std::string const &path, Image* image, ivec2 size, ivec2 count) - : TileSet(path, image) +TileSet *TileSet::create(std::string const &path, image* img, ivec2 size, ivec2 count) { - /* If count is valid, fix size; otherwise, fix count. */ - if (count.x > 0 && count.y > 0) - { - size = m_data->m_image_size / count; - } - else + auto ret = tileset_cache.get(path); + if (!ret) { - if (size.x <= 0 || size.y <= 0) - size = ivec2(32, 32); - count = max(ivec2(1, 1), m_data->m_image_size / size); + ret = tileset_cache.set(path, new TileSet(path, img)); + + /* If count is valid, fix size; otherwise, fix count. */ + if (count.x > 0 && count.y > 0) + { + size = ret->m_data->m_image_size / count; + } + else + { + if (size.x <= 0 || size.y <= 0) + size = ivec2(32, 32); + count = max(ivec2(1, 1), ret->m_data->m_image_size / size); + } + + for (int j = 0; j < count.y; ++j) + for (int i = 0; i < count.x; ++i) + { + ret->define_tile(ibox2(size * ivec2(i, j), + size * ivec2(i + 1, j + 1))); + } } - for (int j = 0; j < count.y; ++j) - for (int i = 0; i < count.x; ++i) - { - define_tile(ibox2(size * ivec2(i, j), - size * ivec2(i + 1, j + 1))); - } + return ret; +} + +void TileSet::destroy(TileSet *tileset) +{ + // FIXME: decrement! + tileset_cache.erase(tileset); +} + +TileSet::TileSet(std::string const &path) + : TextureImage(path), + m_tileset_data(new TileSetData()), + m_palette(nullptr) +{ +} + +TileSet::TileSet(std::string const &path, image *img) + : TextureImage(path, img), + m_tileset_data(new TileSetData()), + m_palette(nullptr) +{ } TileSet::~TileSet() diff --git a/src/tileset.h b/src/tileset.h index ca91ef1b..0b1b3479 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -41,16 +41,22 @@ class TileSet : public TextureImage typedef TextureImage super; public: - TileSet(std::string const &path); - TileSet(std::string const &path, image* img); - TileSet(std::string const &path, image* img, array& tiles); + static TileSet *create(std::string const &path); + static TileSet *create(std::string const &path, image* img); + static TileSet *create(std::string const &path, image* img, array& tiles); /* Old style: path to PNG file */ - TileSet(std::string const &path, ivec2 size, ivec2 count); - TileSet(std::string const &path, image* img, ivec2 size, ivec2 count); + static TileSet *create(std::string const &path, ivec2 size, ivec2 count); + static TileSet *create(std::string const &path, image* img, ivec2 size, ivec2 count); + + static void destroy(TileSet *); virtual ~TileSet(); +private: + TileSet(std::string const &path); + TileSet(std::string const &path, image *img); + protected: virtual void Init(std::string const &path, ResourceCodecData* loaded_data); virtual void Init(std::string const &path, image* img);