Browse Source

engine: get rid of the Dict class (WIP).

This was not a very smart class. We replace it with a bidirectional map.
legacy
Sam Hocevar 6 years ago
parent
commit
1d8083af86
20 changed files with 191 additions and 495 deletions
  1. +3
    -3
      doc/tutorial/06_sprite.cpp
  2. +5
    -6
      src/Makefile.am
  3. +18
    -3
      src/audio/sample.cpp
  4. +0
    -84
      src/audio/sampler.cpp
  5. +27
    -2
      src/engine/entity.h
  6. +18
    -3
      src/font.cpp
  7. +5
    -2
      src/font.h
  8. +0
    -61
      src/forge.cpp
  9. +0
    -38
      src/forge.h
  10. +0
    -8
      src/lol-core.vcxproj
  11. +0
    -24
      src/lol-core.vcxproj.filter
  12. +1
    -2
      src/lol/audio/all.h
  13. +6
    -3
      src/lol/audio/sample.h
  14. +0
    -41
      src/lol/audio/sampler.h
  15. +1
    -6
      src/lol/extras.h
  16. +8
    -10
      src/text.cpp
  17. +0
    -103
      src/tiler.cpp
  18. +0
    -42
      src/tiler.h
  19. +87
    -48
      src/tileset.cpp
  20. +12
    -6
      src/tileset.h

+ 3
- 3
doc/tutorial/06_sprite.cpp View File

@@ -1,7 +1,7 @@
//
// Lol Engine — Sprite tutorial
//
// Copyright © 2011—2015 Sam Hocevar <sam@hocevar.net>
// Copyright © 2011—2019 Sam Hocevar <sam@hocevar.net>
// © 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);


+ 5
- 6
src/Makefile.am View File

@@ -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 \


+ 18
- 3
src/audio/sample.cpp View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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> 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("<sample> ") + 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


+ 0
- 84
src/audio/sampler.cpp View File

@@ -1,84 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2016 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/engine-internal.h>

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 */


+ 27
- 2
src/engine/entity.h View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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<typename T> 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<std::string, T*> m_cache1;
std::map<T*, std::string> m_cache2;
};

} /* namespace lol */


+ 18
- 3
src/font.cpp View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
// 2013 Jean-Yves Lamoureux <jylam@lnxscene.org>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -19,6 +19,9 @@
namespace lol
{

/* The font cache */
static entity_dict<Font> 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 = "<font> " + 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;
}



+ 5
- 2
src/font.h View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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);


+ 0
- 61
src/forge.cpp View File

@@ -1,61 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/engine-internal.h>

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 */


+ 0
- 38
src/forge.h View File

@@ -1,38 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
//
// 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 */


+ 0
- 8
src/lol-core.vcxproj View File

@@ -83,7 +83,6 @@
<ClCompile Include="application\application.cpp" />
<ClCompile Include="audio\audio.cpp" />
<ClCompile Include="audio\sample.cpp" />
<ClCompile Include="audio\sampler.cpp" />
<ClCompile Include="camera.cpp" />
<ClCompile Include="base\assert.cpp" />
<ClCompile Include="base\enum.cpp" />
@@ -93,7 +92,6 @@
<ClCompile Include="debug\lines.cpp" />
<ClCompile Include="debug\record.cpp" />
<ClCompile Include="debug\stats.cpp" />
<ClCompile Include="dict.cpp" />
<ClCompile Include="easymesh\csgbsp.cpp" />
<ClCompile Include="easymesh\easymesh.cpp" />
<ClCompile Include="easymesh\easymeshbuild.cpp" />
@@ -111,7 +109,6 @@
<ClCompile Include="engine\worldentity.cpp" />
<ClCompile Include="emitter.cpp" />
<ClCompile Include="font.cpp" />
<ClCompile Include="forge.cpp" />
<ClCompile Include="gpu\framebuffer.cpp" />
<ClCompile Include="gpu\indexbuffer.cpp" />
<ClCompile Include="gpu\lolfx.cpp" />
@@ -216,7 +213,6 @@
<ClCompile Include="sys\threadtypes.cpp" />
<ClCompile Include="text.cpp" />
<ClCompile Include="textureimage.cpp" />
<ClCompile Include="tiler.cpp" />
<ClCompile Include="tileset.cpp" />
<ClCompile Include="video.cpp" />
</ItemGroup>
@@ -227,7 +223,6 @@
<ClInclude Include="debug\fps.h" />
<ClInclude Include="debug\record.h" />
<ClInclude Include="debug\stats.h" />
<ClInclude Include="dict.h" />
<ClInclude Include="easymesh\csgbsp.h" />
<ClInclude Include="easymesh\easymesh.h" />
<ClInclude Include="easymesh\easymeshbuild.h" />
@@ -240,7 +235,6 @@
<ClInclude Include="engine\worldentity.h" />
<ClInclude Include="engine\world.h" />
<ClInclude Include="font.h" />
<ClInclude Include="forge.h" />
<ClInclude Include="gradient.h" />
<ClInclude Include="image\image-private.h" />
<ClInclude Include="image\resource-private.h" />
@@ -260,7 +254,6 @@
<ClInclude Include="lol\audio\all.h" />
<ClInclude Include="lol\audio\audio.h" />
<ClInclude Include="lol\audio\sample.h" />
<ClInclude Include="lol\audio\sampler.h" />
<ClInclude Include="lol\base\all.h" />
<ClInclude Include="lol\base\array.h" />
<ClInclude Include="lol\base\assert.h" />
@@ -368,7 +361,6 @@
<ClInclude Include="text.h" />
<ClInclude Include="textureimage-private.h" />
<ClInclude Include="textureimage.h" />
<ClInclude Include="tiler.h" />
<ClInclude Include="tileset.h" />
<ClInclude Include="utils.h" />
<ClInclude Include="video.h" />


+ 0
- 24
src/lol-core.vcxproj.filter View File

@@ -204,15 +204,9 @@
<ClCompile Include="audio\sample.cpp">
<Filter>audio</Filter>
</ClCompile>
<ClCompile Include="audio\sampler.cpp">
<Filter>audio</Filter>
</ClCompile>
<ClCompile Include="camera.cpp">
<Filter>...</Filter>
</ClCompile>
<ClCompile Include="dict.cpp">
<Filter>...</Filter>
</ClCompile>
<ClCompile Include="eglapp.cpp">
<Filter>...</Filter>
</ClCompile>
@@ -222,9 +216,6 @@
<ClCompile Include="font.cpp">
<Filter>...</Filter>
</ClCompile>
<ClCompile Include="forge.cpp">
<Filter>...</Filter>
</ClCompile>
<ClCompile Include="gradient.cpp">
<Filter>...</Filter>
</ClCompile>
@@ -391,9 +382,6 @@
<ClCompile Include="sys\threadtypes.cpp">
<Filter>sys</Filter>
</ClCompile>
<ClCompile Include="tiler.cpp">
<Filter>tileset</Filter>
</ClCompile>
<ClCompile Include="tileset.cpp">
<Filter>tileset</Filter>
</ClCompile>
@@ -532,9 +520,6 @@
<ClInclude Include="camera.h">
<Filter>...</Filter>
</ClInclude>
<ClInclude Include="dict.h">
<Filter>...</Filter>
</ClInclude>
<ClInclude Include="eglapp.h">
<Filter>...</Filter>
</ClInclude>
@@ -544,9 +529,6 @@
<ClInclude Include="font.h">
<Filter>...</Filter>
</ClInclude>
<ClInclude Include="forge.h">
<Filter>...</Filter>
</ClInclude>
<ClInclude Include="gradient.h">
<Filter>...</Filter>
</ClInclude>
@@ -592,9 +574,6 @@
<ClInclude Include="lol\audio\sample.h">
<Filter>lol\audio</Filter>
</ClInclude>
<ClInclude Include="lol\audio\sampler.h">
<Filter>lol\audio</Filter>
</ClInclude>
<ClInclude Include="lol\base\array.h">
<Filter>lol\base</Filter>
</ClInclude>
@@ -761,9 +740,6 @@
<ClInclude Include="lol\sys\threadtypes.h">
<Filter>lol\sys</Filter>
</ClInclude>
<ClInclude Include="tiler.h">
<Filter>tileset</Filter>
</ClInclude>
<ClInclude Include="tileset.h">
<Filter>tileset</Filter>
</ClInclude>


+ 1
- 2
src/lol/audio/all.h View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2016 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/audio/audio.h>
#include <lol/audio/sampler.h>
#include <lol/audio/sample.h>


+ 6
- 3
src/lol/audio/sample.h View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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);


+ 0
- 41
src/lol/audio/sampler.h View File

@@ -1,41 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2016 Sam Hocevar <sam@hocevar.net>
//
// 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 <stdint.h>

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 */


+ 1
- 6
src/lol/extras.h View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2016 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/../lolimgui.h>

// Other objects
#include <lol/../dict.h>
#include <lol/../mesh/mesh.h>
#include <lol/../mesh/primitivemesh.h>
#include <lol/../application/application.h>
#include <lol/../easymesh/csgbsp.h>
#include <lol/../easymesh/easymesh.h>

// Managers
#include <lol/../forge.h>
#include <lol/../tiler.h>


+ 8
- 10
src/text.cpp View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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;
}



+ 0
- 103
src/tiler.cpp View File

@@ -1,103 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/engine-internal.h>

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 */


+ 0
- 42
src/tiler.h View File

@@ -1,42 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
//
// 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 <stdint.h>

#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 */


+ 87
- 48
src/tileset.cpp View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/engine-internal.h>

#include <map>
#include <cstdlib>
#include <cstdio>
#include <cstring>
@@ -27,6 +28,9 @@
namespace lol
{

/* The tileset cache */
static entity_dict<TileSet> 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<ivec2, ivec2>& tiles)
: TextureImage(path, image),
m_tileset_data(new TileSetData()),
m_palette(nullptr)
TileSet *TileSet::create(std::string const &path, image* img, array<ivec2, ivec2>& 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()


+ 12
- 6
src/tileset.h View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// 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<ivec2, ivec2>& 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<ivec2, ivec2>& 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);


Loading…
Cancel
Save