diff --git a/.cache/clangd/index/bitmap.hpp.62887C634633116B.idx b/.cache/clangd/index/bitmap.hpp.62887C634633116B.idx index 9eef607..76c67da 100644 Binary files a/.cache/clangd/index/bitmap.hpp.62887C634633116B.idx and b/.cache/clangd/index/bitmap.hpp.62887C634633116B.idx differ diff --git a/.cache/clangd/index/bitmap_io.cpp.181EA33891BBC2AF.idx b/.cache/clangd/index/bitmap_io.cpp.181EA33891BBC2AF.idx index d70caa9..bf0d132 100644 Binary files a/.cache/clangd/index/bitmap_io.cpp.181EA33891BBC2AF.idx and b/.cache/clangd/index/bitmap_io.cpp.181EA33891BBC2AF.idx differ diff --git a/.cache/clangd/index/bitmap_io.hpp.C3324F345C5DD5D2.idx b/.cache/clangd/index/bitmap_io.hpp.C3324F345C5DD5D2.idx index 9729ed5..0a94598 100644 Binary files a/.cache/clangd/index/bitmap_io.hpp.C3324F345C5DD5D2.idx and b/.cache/clangd/index/bitmap_io.hpp.C3324F345C5DD5D2.idx differ diff --git a/.cache/clangd/index/context.cpp.EECF18BC474ACA88.idx b/.cache/clangd/index/context.cpp.EECF18BC474ACA88.idx index c919b0d..4f207ed 100644 Binary files a/.cache/clangd/index/context.cpp.EECF18BC474ACA88.idx and b/.cache/clangd/index/context.cpp.EECF18BC474ACA88.idx differ diff --git a/.cache/clangd/index/context.hpp.0247079C4DA06A0E.idx b/.cache/clangd/index/context.hpp.0247079C4DA06A0E.idx index 3fe8fed..89bb967 100644 Binary files a/.cache/clangd/index/context.hpp.0247079C4DA06A0E.idx and b/.cache/clangd/index/context.hpp.0247079C4DA06A0E.idx differ diff --git a/.cache/clangd/index/geometry.hpp.5CBEA5776F164981.idx b/.cache/clangd/index/geometry.hpp.5CBEA5776F164981.idx index 490f4fc..c281eae 100644 Binary files a/.cache/clangd/index/geometry.hpp.5CBEA5776F164981.idx and b/.cache/clangd/index/geometry.hpp.5CBEA5776F164981.idx differ diff --git a/.cache/clangd/index/glad.c.DC115C5ADDB0D90B.idx b/.cache/clangd/index/glad.c.DC115C5ADDB0D90B.idx index aae9c95..8ae4809 100644 Binary files a/.cache/clangd/index/glad.c.DC115C5ADDB0D90B.idx and b/.cache/clangd/index/glad.c.DC115C5ADDB0D90B.idx differ diff --git a/.cache/clangd/index/glad.h.76E6696D4B8D4E14.idx b/.cache/clangd/index/glad.h.76E6696D4B8D4E14.idx index b26a411..a901133 100644 Binary files a/.cache/clangd/index/glad.h.76E6696D4B8D4E14.idx and b/.cache/clangd/index/glad.h.76E6696D4B8D4E14.idx differ diff --git a/.cache/clangd/index/globals.hpp.24BCAC15858D1B89.idx b/.cache/clangd/index/globals.hpp.24BCAC15858D1B89.idx index f5a88bd..ca5b57e 100644 Binary files a/.cache/clangd/index/globals.hpp.24BCAC15858D1B89.idx and b/.cache/clangd/index/globals.hpp.24BCAC15858D1B89.idx differ diff --git a/.cache/clangd/index/khrplatform.h.D6CEFEA22454BFC5.idx b/.cache/clangd/index/khrplatform.h.D6CEFEA22454BFC5.idx index 54da499..83a7f49 100644 Binary files a/.cache/clangd/index/khrplatform.h.D6CEFEA22454BFC5.idx and b/.cache/clangd/index/khrplatform.h.D6CEFEA22454BFC5.idx differ diff --git a/.cache/clangd/index/matrix.hpp.82213AF3AAC74723.idx b/.cache/clangd/index/matrix.hpp.82213AF3AAC74723.idx index 4851585..dde4218 100644 Binary files a/.cache/clangd/index/matrix.hpp.82213AF3AAC74723.idx and b/.cache/clangd/index/matrix.hpp.82213AF3AAC74723.idx differ diff --git a/.cache/clangd/index/matrixbase.hpp.F11E20E9DFADCEFF.idx b/.cache/clangd/index/matrixbase.hpp.F11E20E9DFADCEFF.idx index fa4b1b4..09ff181 100644 Binary files a/.cache/clangd/index/matrixbase.hpp.F11E20E9DFADCEFF.idx and b/.cache/clangd/index/matrixbase.hpp.F11E20E9DFADCEFF.idx differ diff --git a/.cache/clangd/index/pong.cpp.0666B09B2C5A6C2D.idx b/.cache/clangd/index/pong.cpp.0666B09B2C5A6C2D.idx index 94c0f53..3ea7a51 100644 Binary files a/.cache/clangd/index/pong.cpp.0666B09B2C5A6C2D.idx and b/.cache/clangd/index/pong.cpp.0666B09B2C5A6C2D.idx differ diff --git a/.cache/clangd/index/quickwings.cpp.85EA7B40D80DA419.idx b/.cache/clangd/index/quickwings.cpp.85EA7B40D80DA419.idx index 4a3cfdd..9919361 100644 Binary files a/.cache/clangd/index/quickwings.cpp.85EA7B40D80DA419.idx and b/.cache/clangd/index/quickwings.cpp.85EA7B40D80DA419.idx differ diff --git a/.cache/clangd/index/renderer.cpp.740BAA1FB38348C8.idx b/.cache/clangd/index/renderer.cpp.740BAA1FB38348C8.idx index 550bb04..e3249ee 100644 Binary files a/.cache/clangd/index/renderer.cpp.740BAA1FB38348C8.idx and b/.cache/clangd/index/renderer.cpp.740BAA1FB38348C8.idx differ diff --git a/.cache/clangd/index/renderer.hpp.926D47CDCF3C1D01.idx b/.cache/clangd/index/renderer.hpp.926D47CDCF3C1D01.idx index 366053f..5f5591a 100644 Binary files a/.cache/clangd/index/renderer.hpp.926D47CDCF3C1D01.idx and b/.cache/clangd/index/renderer.hpp.926D47CDCF3C1D01.idx differ diff --git a/.cache/clangd/index/rgba.hpp.FC678113A439A522.idx b/.cache/clangd/index/rgba.hpp.FC678113A439A522.idx index 995976a..293d99c 100644 Binary files a/.cache/clangd/index/rgba.hpp.FC678113A439A522.idx and b/.cache/clangd/index/rgba.hpp.FC678113A439A522.idx differ diff --git a/.cache/clangd/index/shader.cpp.43E3518708249EAE.idx b/.cache/clangd/index/shader.cpp.43E3518708249EAE.idx index 41f8b36..08f7ece 100644 Binary files a/.cache/clangd/index/shader.cpp.43E3518708249EAE.idx and b/.cache/clangd/index/shader.cpp.43E3518708249EAE.idx differ diff --git a/.cache/clangd/index/shader.hpp.E7CD36D8F71952ED.idx b/.cache/clangd/index/shader.hpp.E7CD36D8F71952ED.idx index 8b09bd6..588a122 100644 Binary files a/.cache/clangd/index/shader.hpp.E7CD36D8F71952ED.idx and b/.cache/clangd/index/shader.hpp.E7CD36D8F71952ED.idx differ diff --git a/.cache/clangd/index/shader_sprite.hpp.493F804E562576C3.idx b/.cache/clangd/index/shader_sprite.hpp.493F804E562576C3.idx index 9cf87aa..98f856b 100644 Binary files a/.cache/clangd/index/shader_sprite.hpp.493F804E562576C3.idx and b/.cache/clangd/index/shader_sprite.hpp.493F804E562576C3.idx differ diff --git a/.cache/clangd/index/simple.cpp.96D892360BBE0299.idx b/.cache/clangd/index/simple.cpp.96D892360BBE0299.idx index 7b2e081..cbd1522 100644 Binary files a/.cache/clangd/index/simple.cpp.96D892360BBE0299.idx and b/.cache/clangd/index/simple.cpp.96D892360BBE0299.idx differ diff --git a/.cache/clangd/index/sprite.hpp.A9098C65589F3A19.idx b/.cache/clangd/index/sprite.hpp.A9098C65589F3A19.idx index 1303344..0abb653 100644 Binary files a/.cache/clangd/index/sprite.hpp.A9098C65589F3A19.idx and b/.cache/clangd/index/sprite.hpp.A9098C65589F3A19.idx differ diff --git a/.cache/clangd/index/stb_image.h.A40CF375DB872323.idx b/.cache/clangd/index/stb_image.h.A40CF375DB872323.idx index 05885de..3865e03 100644 Binary files a/.cache/clangd/index/stb_image.h.A40CF375DB872323.idx and b/.cache/clangd/index/stb_image.h.A40CF375DB872323.idx differ diff --git a/.cache/clangd/index/vector.hpp.E320F2C0D8CBC593.idx b/.cache/clangd/index/vector.hpp.E320F2C0D8CBC593.idx index 1470971..55a3e32 100644 Binary files a/.cache/clangd/index/vector.hpp.E320F2C0D8CBC593.idx and b/.cache/clangd/index/vector.hpp.E320F2C0D8CBC593.idx differ diff --git a/.cache/clangd/index/window.cpp.C49665C3E96358E9.idx b/.cache/clangd/index/window.cpp.C49665C3E96358E9.idx index b4bad8c..f23c8ef 100644 Binary files a/.cache/clangd/index/window.cpp.C49665C3E96358E9.idx and b/.cache/clangd/index/window.cpp.C49665C3E96358E9.idx differ diff --git a/.cache/clangd/index/window.hpp.B8A7D1770B434EEC.idx b/.cache/clangd/index/window.hpp.B8A7D1770B434EEC.idx index c589a3b..a45284a 100644 Binary files a/.cache/clangd/index/window.hpp.B8A7D1770B434EEC.idx and b/.cache/clangd/index/window.hpp.B8A7D1770B434EEC.idx differ diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 3149482..f5d7881 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,6 +1,6 @@ # Contributors - Hartmut Seichter (dev lead) -- Robin Rottstädt (Flappy Bird, image loader) +- Robin Rottstädt (image loader) - Tim Gösselmann (Win32 fixes) -- Hannes Brothuhn (Flappy Bird fixes) +- Hannes Brothuhn (Flappy Bird clone) diff --git a/examples/quickwings/quickwings.cpp b/examples/quickwings/quickwings.cpp index 8564040..dd51112 100644 --- a/examples/quickwings/quickwings.cpp +++ b/examples/quickwings/quickwings.cpp @@ -5,7 +5,6 @@ * */ -#include #include #include #include @@ -17,104 +16,8 @@ #include #include -#include #include -#include -#include -#include #include -#include -#include -#include - -namespace QuickWings { - -using SpriteName = std::tuple; -using SpriteMap = std::unordered_map; - -struct App { - SpriteMap sprites; - - static auto create() -> App { - - paradiso::BitmapIO::get().set_asset_path( - paradiso::get_executable_path().parent_path() / "assets"); - - auto assets = std::array{ - SpriteName{"background.bg", "background-day.png"}, - SpriteName{"background.fg", "base.png"}, - }; - - SpriteMap load_map{}; - - for (const auto& [name, filename] : assets) { - std::print("{} : {}", name, filename); - - if (auto bm = paradiso::BitmapIO::get().load(filename); - bm.has_value()) { - load_map[name] = bm.value(); - std::print(" ok!\n"); - } else { - std::print(" error!\n"); - } - } - return {.sprites{load_map}}; - } - - void run() { - auto window = paradiso::Window(); - - paradiso::Size size {.width = 640, .height = 480}; - - window - .set_size(size) // ... Grösse - .set_position( - paradiso::Point{.x = 1920 / 2 - 500 / 2, - .y = 1080 / 2 - 700 / 2}) // ... Position - .set_title("PardiSO.Quickwings") // ... Titel - .set_visible(true); // ... und jetzt anzeigen! - - // der Fenster Kontext - auto ctx = paradiso::Context{}; - - // ein Shader (Schattierungsprogramm) - auto shader = paradiso::Shader{}; - - // wir nutzen einen vorgefertigten shader - shader.load_preset(paradiso::Shader::Preset::Sprite); - - // ein viewport stellt die Sicht der Kamera dar, d.h. bei quadratischen - // Pixeln sollte hier auch eine dementsprechende Grösse eingestellt - // werden - ctx.set_viewport( - paradiso::Rectangle{.position = paradiso::Point{.x = 0, .y = 0}, - .size = window.size()}); - - // nothing beats a classic look - ctx.set_clearcolor(paradiso::RGBA::from_rgb(0x00, 0x00, 0x00)); - - while ( - window.update([&](paradiso::Window& w) -> bool { - ctx.set_viewport(paradiso::Rectangle{ - .position = paradiso::Point{.x = 0, .y = 0}, - .size = size, - }); - - // Quit - return !(w.keyboard_input().size() && - w.keyboard_input().top().key == 'Q'); - }){}; - - } - - } // namespace QuickWings - - auto main() -> int { - auto app = QuickWings::App::create(); - app.run(); - } - -#if 0 // TODO remove all hard coded 'magic' values! @@ -128,12 +31,7 @@ float risky_pos_top_y; float risky_pos_bottom_max_y; float risky_pos_top_max_y; - - struct Background { - - SpriteMap sprites; - paradiso::Sprite backgroundLeft; paradiso::Sprite backgroundRight; paradiso::Sprite grassLeft; @@ -146,9 +44,7 @@ struct Background { // TODO no constructors in rule of zero Background() { auto backgroundImage = - paradiso::BitmapIO::get().load("background-day.png").has_value() - ? paradiso::BitmapIO::get().load("background-day.png").value() - : paradiso::Bitmap(); + paradiso::BitmapIO::get().load("background-day.png"); backgroundLeft = paradiso::Sprite{ .bitmap = backgroundImage, .pivot = {paradiso::Vector2::make(0.0f, 0.16f)}, @@ -563,7 +459,7 @@ auto main() -> int { .set_size(size) // ... Grösse .set_position(paradiso::Point{.x = 1920 / 2 - 500 / 2, .y = 1080 / 2 - 700 / 2}) // ... Position - .set_title("PardiSO.Quickwings") // ... Titel + .set_title("PardiSO.FlappyBird") // ... Titel .set_visible(true); // ... und jetzt anzeigen! // der Fenster Kontext @@ -586,8 +482,7 @@ auto main() -> int { // Asset loader bekommt den Pfad - paradiso::BitmapIO::get().set_asset_path( - paradiso::get_executable_path().parent_path() / "assets"); + paradiso::BitmapIO::get().set_path("assets"); // Load auto background = Background{}; @@ -649,4 +544,3 @@ auto main() -> int { return 0; } -#endif diff --git a/src/lib/include/paradiso/bitmap_io.hpp b/src/lib/include/paradiso/bitmap_io.hpp index 1dd77b9..1700bc1 100644 --- a/src/lib/include/paradiso/bitmap_io.hpp +++ b/src/lib/include/paradiso/bitmap_io.hpp @@ -23,11 +23,8 @@ #ifndef PARADISO_BITMAP_IO_HPP #define PARADISO_BITMAP_IO_HPP -#include #include -#include -#include #include #include @@ -35,23 +32,13 @@ namespace paradiso { struct BitmapIO { - enum struct LoadError { - FileNotFound, - FileCorrupted, - FileNotBitmap, - }; - - using LoadResult = std::expected; - using path = std::filesystem::path; - static BitmapIO& get(); - auto load(std::string_view filename, bool ignore_cache = false) const - -> LoadResult; + Bitmap load(std::string_view filename, bool ignore_cache = false) const; - void set_asset_path(path path_); + void set_path(std::string_view path); - path asset_path() const; + std::string path() const; private: struct Impl; diff --git a/src/lib/src/bitmap_io.cpp b/src/lib/src/bitmap_io.cpp index 35510e3..b279244 100644 --- a/src/lib/src/bitmap_io.cpp +++ b/src/lib/src/bitmap_io.cpp @@ -26,14 +26,15 @@ #include "paradiso/bitmap_io.hpp" #include "paradiso/bitmap.hpp" -#include -#include #include // STB image loading #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" +#include +#include + namespace paradiso { struct BitmapIO::Impl { @@ -44,7 +45,7 @@ struct BitmapIO::Impl { BitmapCacheType cache_; - static BitmapIO::LoadResult _read_bitmap(const path& filepath) { + static Bitmap read(std::string_view filename) { // Load with stb_image stbi_set_flip_vertically_on_load(true); // flip y axis for OpenGL @@ -53,16 +54,16 @@ struct BitmapIO::Impl { auto stb_free = [](uint8_t* data) { stbi_image_free(data); }; - if (std::unique_ptr image{stbi_load( - filepath.string().c_str(), &width, &height, &channels, 0)}; + if (std::unique_ptr image{ + stbi_load(filename.data(), &width, &height, &channels, 0)}; image.get()) { - const auto area = width * height; + int size = width * height; // Convert to Vector of RGBA std::vector rgba = - std::vector(area); - for (int i = 0; i < area; i++) { + std::vector(size); + for (int i = 0; i < size; i++) { // get rgba values int pos = i * 4; int r = image.get()[pos + 0]; @@ -71,50 +72,47 @@ struct BitmapIO::Impl { int a = image.get()[pos + 3]; // bug in from_rgba. it expects bgra, not rgba - // auto val = paradiso::RGBA::from_rgba(b, g, r, a); - rgba[i] = paradiso::RGBA::from_rgba(r, g, b, a); + auto val = paradiso::RGBA::from_rgba(b, g, r, a); + rgba[i] = val; } + + // rotate image by 90 degrees CW + std::vector rotated = + std::vector(size); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + + int pos = y * width + x; + int pos_rotated = x * height + (height - y - 1); + rotated[pos_rotated] = rgba[pos]; + } + } + return paradiso::Bitmap::from_data( paradiso::Size{static_cast(height), static_cast(width)}, - rgba); - - // rotate image by 90 degrees CW - // std::vector rotated = - // std::vector(size); - // for (int x = 0; x < width; x++) { - // for (int y = 0; y < height; y++) { - - // int pos = y * width + x; - // int pos_rotated = x * height + (height - y - 1); - // rotated[pos_rotated] = rgba[pos]; - // } - // } - - // return paradiso::Bitmap::from_data( - // paradiso::Size{static_cast(height), - // static_cast(width)}, - // rotated); + rotated); } else { - return std::unexpected(BitmapIO::LoadError::FileNotFound); + std::cerr << "cannot load " << filename << '\n'; } return Bitmap(); // well, no exception ... } - auto load(std::string_view filename, bool ignore_cache) -> LoadResult { - // check cache first - if (cache_.find(filename) != cache_.end() && !ignore_cache) { - return cache_[filename.data()]; - } // construct file path - auto asset_filepath = asset_path_ / std::filesystem::path(filename); + Bitmap load(std::string_view filename) { + if (cache_.find(filename) == cache_.end()) { - auto data = _read_bitmap(asset_filepath); + auto asset_filepath = asset_path_ / std::filesystem::path(filename); - if (data.has_value()) { - cache_[filename.data()] = *data; + std::cout << "Loading " << asset_filepath << std::endl; + + // moving to C++23 this should be replaced with + // std::expected + auto data = read(asset_filepath.generic_string()); + + cache_[filename] = data; } - return data; + return cache_[filename.data()]; } }; @@ -125,15 +123,14 @@ BitmapIO& BitmapIO::get() { return instance; } -auto BitmapIO::load(std::string_view filename, bool ignore_cache) const - -> BitmapIO::LoadResult { - return impl_->load(filename, ignore_cache); +Bitmap BitmapIO::load(std::string_view filename, bool ignore_cache) const { + return impl_->load(filename); } -void BitmapIO::set_asset_path(BitmapIO::path path_) { - impl_->asset_path_ = path_; -} +void BitmapIO::set_path(std::string_view path) { impl_->asset_path_ = path; } -BitmapIO::path BitmapIO::asset_path() const { return impl_->asset_path_; } +std::string BitmapIO::path() const { + return impl_->asset_path_.generic_string(); +} } // namespace paradiso diff --git a/src/lib/src/utils.cpp b/src/lib/src/utils.cpp index bfd05d9..f63cc91 100644 --- a/src/lib/src/utils.cpp +++ b/src/lib/src/utils.cpp @@ -37,7 +37,7 @@ path get_executable_path() { #if defined(_WIN32) std::array lpFname{}; DWORD ret = GetModuleFileName(NULL, lpFname.data(), lpFname.size()); - return {std::string{std::from_range, lpFname}}; + return {std::string{std::begin(lpFname), std::end(lpFname)}}; #elif defined(__linux) return std::filesystem::canonical(u8"/proc/self/exe"); #endif