From 4f584174a687ab4f3b9737411219b775ff072b1c Mon Sep 17 00:00:00 2001 From: squishy Date: Sun, 31 Mar 2024 01:56:47 +0000 Subject: [PATCH] added tileset fonts back in --- blooblib/include/tileset.h | 7 ++++- blooblib/src/font.cpp | 52 +++++++++++++++++++++----------------- blooblib/src/tileset.cpp | 16 ++++++++++++ test/assets/assets.ini | 4 +++ test/test.cpp | 15 +++++++---- 5 files changed, 65 insertions(+), 29 deletions(-) diff --git a/blooblib/include/tileset.h b/blooblib/include/tileset.h index 234a331..7425993 100644 --- a/blooblib/include/tileset.h +++ b/blooblib/include/tileset.h @@ -3,11 +3,16 @@ #include #include #include "image.h" +#include "resource_manager.h" #include "vec2.h" -struct tileset { +struct font; + +struct tileset : resource { + friend struct font; tileset(image const* image, vec2i tile_size); //tileset(std::string path, vec2i tile_size); + tileset(resource_manager& rm, ini_category const* ini, std::string const& path, std::vector const& data); image& operator[](int index); diff --git a/blooblib/src/font.cpp b/blooblib/src/font.cpp index 8373211..1031a13 100644 --- a/blooblib/src/font.cpp +++ b/blooblib/src/font.cpp @@ -1,35 +1,41 @@ #include "font.h" #include "image.h" +#include "tileset.h" font::font(resource_manager& rm, ini_category const* ini, std::string const& path, std::vector const& data) { if(!ini) throw std::runtime_error("font needs assets.ini"); - if(!ini->get("proportional")) - throw std::runtime_error("not yet implemented"); + if(ini->get("proportional")) { + auto image = rm.get<::image>(path); + auto x = 0; + std::vector slices; + for(int i = 0; i < image->size().x; ++i) { + if(image->get(vec2i(i, 0)) & 0xff000000) + slices.push_back(i); + } + int count = slices.size(); + slices.push_back(image->size().x); + _lineheight = image->size().y - 1; - - auto image = rm.get<::image>(path); - auto x = 0; - std::vector slices; - for(int i = 0; i < image->size().x; ++i) { - if(image->get(vec2i(i, 0)) & 0xff000000) - slices.push_back(i); + auto charset = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~"; + for(int i = 0; i < count; ++i) { + auto r = recti(vec2i(slices[i], 1), vec2i(slices[i + 1] - slices[i], _lineheight)); + //auto img = ::image(r.size); + auto ch = charset[i]; + _glyphs.emplace(ch, r.size); + _glyphs.at(ch).clear(0); + _glyphs.at(ch).draw(image, vec2i::zero, r); + } + } else { + int width = ini->get("width"); + int height = ini->get("height"); + auto ts = rm.get(path); + _lineheight = ts->_tile_size.y; + for(int i = 0; i < 256; ++i) { + _glyphs.insert({ i, ts->_tiles[i] }); + } } - int count = slices.size(); - slices.push_back(image->size().x); - _lineheight = image->size().y - 1; - - auto charset = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~"; - for(int i = 0; i < count; ++i) { - auto r = recti(vec2i(slices[i], 1), vec2i(slices[i + 1] - slices[i], _lineheight)); - //auto img = ::image(r.size); - auto ch = charset[i]; - _glyphs.emplace(ch, r.size); - _glyphs.at(ch).clear(0); - _glyphs.at(ch).draw(image, vec2i::zero, r); - } - } image const* font::get(char ch) const { diff --git a/blooblib/src/tileset.cpp b/blooblib/src/tileset.cpp index 6b240b1..2885e98 100644 --- a/blooblib/src/tileset.cpp +++ b/blooblib/src/tileset.cpp @@ -1,4 +1,5 @@ #include "tileset.h" +#include "image.h" tileset::tileset(image const* image, vec2i tile_size) { auto tile_count = image->size() / tile_size; @@ -11,6 +12,21 @@ tileset::tileset(image const* image, vec2i tile_size) { } } } + +tileset::tileset(resource_manager& rm, ini_category const* ini, std::string const& path, std::vector const& data) { + auto tile_size = vec2i(ini->get("tile_width"), ini->get("tile_height")); + _tile_size = tile_size; + auto image = rm.get<::image>(path); + auto tile_count = image->size() / tile_size; + for(int y = 0; y < tile_count.y; ++y) { + for(int x = 0; x < tile_count.x; ++x) { + auto tile = ::image(tile_size); + tile.clear(0); + tile.draw(image, vec2i::zero, recti(tile_size * vec2i(x, y), tile_size)); + _tiles.push_back(tile); + } + } +} /* tileset::tileset(std::string path, vec2i tile_size) : tileset(new image(path), tile_size) diff --git a/test/assets/assets.ini b/test/assets/assets.ini index 65c4b87..25a741b 100644 --- a/test/assets/assets.ini +++ b/test/assets/assets.ini @@ -1,3 +1,7 @@ [habbo.png] proportional = true +[font.png] +proportional = false +tile_width = 8 +tile_height = 14 \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index daf280c..3dbecea 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -12,22 +12,24 @@ struct my_game : game { //tileset tileset; - font const* font; + font const* font1; + font const* font2; double time; }; void my_game::init(settings& settings) { settings.target_fps = 6000; time = 0.0; - settings.scale = 3; - settings.size = vec2i(1920/3, 1080/3); + settings.scale = 1; + settings.size = vec2i(1920, 1080); //img = get("test.png"); //slime = get("slime.png"); //slime = slime->upscale_2x(); //slime = slime->upscale_2x(); //slime = slime->upscale_2x(); //ini = get<::ini>("test.ini"); - font = get<::font>("_/default-font-prop.png"); + font1 = get<::font>("_/default-font-prop.png"); + font2 = get<::font>("font.png"); //cursor(&font['\\']); } @@ -47,7 +49,10 @@ void my_game::render(image& target) { //target.draw(tileset[0xda], vec2i(320, 180) + pos, 0xff00ff); auto str = "Can only be played if\nthere are no card in\nyour draw pile.\nDeal 50 damage to ALL\nenemies."; //target.draw(str, vec2i(100, 100) + pos, font, 0xffffff, 0); - target.draw(std::format("FPS: {}", fps()), vec2i(10, 10), font, 0xffffff, 0); + target.draw(std::format("FPS: {}", fps()), vec2i(10, 10), font1, 0xffffff, 0); + + for(int i = 0; i < 64; ++i) + target.draw("C-4", vec2i(100, i * 14), font2, 0xffffff, 0); //target.draw("\\", mouse_pos(), font, 0xffffff, 0); }