diff --git a/include/resources/font.hpp b/include/resources/font.hpp index fd115ac..59553a9 100644 --- a/include/resources/font.hpp +++ b/include/resources/font.hpp @@ -1,11 +1,11 @@ #pragma once -#if 0 - #include "engine_api.h" #include "resource.hpp" +#include "gfx.hpp" + #include #include @@ -17,7 +17,7 @@ class ENGINE_API Font : public Resource { public: struct Character { - unsigned int textureID; // openGL texture handle + gfx::Texture* texture; glm::ivec2 size; glm::ivec2 bearing; // offset from baseline to top-left of glyph long advance; // offset to the next glyph @@ -33,4 +33,3 @@ public: }; } -#endif \ No newline at end of file diff --git a/include/util/files.hpp b/include/util/files.hpp index a1cac8d..ed24609 100644 --- a/include/util/files.hpp +++ b/include/util/files.hpp @@ -7,5 +7,6 @@ namespace engine::util { std::unique_ptr> readTextFile(const std::string& path); + std::unique_ptr> readBinaryFile(const std::string& path); } diff --git a/include/window.hpp b/include/window.hpp index 544a458..78ac5e2 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -22,7 +22,7 @@ namespace engine { class ENGINE_API Window { public: - Window(const std::string& title, bool resizable = true); + Window(const std::string& title, bool resizable = true, bool fullscreen = true); Window(const Window&) = delete; Window& operator=(const Window&) = delete; ~Window(); @@ -203,4 +203,4 @@ namespace engine { void onMouseWheelEvent(SDL_MouseWheelEvent& e); }; -} \ No newline at end of file +} diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..ceec117 --- /dev/null +++ b/release.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cd "Release/test" +./enginetest diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..f1b72b7 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cd "Debug/test" +./enginetest diff --git a/src/engine.cpp b/src/engine.cpp index 0c19992..226dc9c 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -16,7 +16,7 @@ namespace engine { Application::Application(const char* appName, const char* appVersion) { - m_win = new Window(appName, true); + m_win = new Window(appName, true, true); gfxdev = new GFXDevice(appName, appVersion, m_win->getHandle()); diff --git a/src/resources/font.cpp b/src/resources/font.cpp index c905437..2c80c59 100644 --- a/src/resources/font.cpp +++ b/src/resources/font.cpp @@ -1,87 +1,58 @@ -#if 0 #include "resources/font.hpp" -#include -#include FT_FREETYPE_H +#define STB_TRUETYPE_IMPLEMENTATION +#include + +#include "util/files.hpp" +#include "gfx_device.hpp" namespace engine::resources { Font::Font(const std::filesystem::path& resPath) : Resource(resPath, "font") { - FT_Library library; - FT_Face face; - int err; + // TODO: load font + auto fontBuffer = util::readBinaryFile(resPath); - err = FT_Init_FreeType(&library); - if (err) { - throw std::runtime_error("Failed to initialise freetype library"); + stbtt_fontinfo info{}; + int res = stbtt_InitFont(&info, fontBuffer->data(), 0); + if (res != 0) { + throw std::runtime_error("Failed to read font file: " + resPath.string()); } - err = FT_New_Face(library, resPath.string().c_str(), 0, &face); - if (err == FT_Err_Unknown_File_Format) { - FT_Done_FreeType(library); - throw std::runtime_error("Unknown file format for font '" + resPath.string() + "'"); - } - else if (err != 0) { - FT_Done_FreeType(library); - throw std::runtime_error("Unable to open font '" + resPath.string() + "'"); - } - - err = FT_Set_Pixel_Sizes(face, 0, 64); - if (err) { - FT_Done_Face(face); - FT_Done_FreeType(library); - throw std::runtime_error("Attempt to set pixel size to one unavailable in the font"); - } - - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + float scale = stbtt_ScaleForPixelHeight(&info, 64); for (unsigned char c = 0; c < 128; c++) { - err = FT_Load_Char(face, c, FT_LOAD_RENDER); - if (err) { - FT_Done_Face(face); - FT_Done_FreeType(library); - throw std::runtime_error("Unable to load char glyph"); + // TODO: get character bitmap buffer, size, offsets, advance + int32_t advance = 0, xoff = 0; + stbtt_GetCodepointHMetrics(&info, c, &advance, &xoff); + + int32_t w, h, yoff; + const uint8_t* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, c, &w, &h, &xoff, &yoff); + + auto colorBuffer = std::make_unique>(w * h); + int i = 0; + for (uint32_t& col : *colorBuffer) { + col = bitmap[i]; + i++; } // generate texture - unsigned int texture; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RED, - face->glyph->bitmap.width, - face->glyph->bitmap.rows, - 0, - GL_RED, - GL_UNSIGNED_BYTE, - face->glyph->bitmap.buffer - ); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gfx::Texture* texture = gfxdev->createTexture(colorBuffer->data(), w, h, gfx::TextureFilter::LINEAR, gfx::TextureFilter::LINEAR); Character character = { texture, - glm::ivec2{face->glyph->bitmap.width, face->glyph->bitmap.rows}, // Size of Glyph - glm::ivec2{face->glyph->bitmap_left, face->glyph->bitmap_top}, // Offset from baseline (bottom-left) to top-left of glyph - face->glyph->advance.x + glm::ivec2{w, h}, // Size of Glyph + glm::ivec2{xoff, yoff}, // Offset from baseline (bottom-left) to top-left of glyph + advance }; m_characters.insert(std::make_pair(c, character)); } - glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // reset alignment settings - - FT_Done_Face(face); - FT_Done_FreeType(library); + // TODO clean up resources } @@ -95,4 +66,3 @@ Font::Character Font::getChar(char c) } } -#endif \ No newline at end of file diff --git a/src/util/files.cpp b/src/util/files.cpp index aee6fcf..ad9356a 100644 --- a/src/util/files.cpp +++ b/src/util/files.cpp @@ -30,4 +30,23 @@ namespace engine::util { return buffer; } + std::unique_ptr> readBinaryFile(const std::string& path) + { + std::ifstream file(path, std::ios::ate | std::ios::binary); + if (file.is_open() == false) { + throw std::runtime_error("Unable to open file " + path); + } + + auto buffer = std::make_unique>(file.tellg()); + + file.seekg(0); + + file.read((char*)buffer->data(), buffer->size()); + + file.close(); + + return buffer; + } + } + diff --git a/src/window.cpp b/src/window.cpp index b455747..4b475b3 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -9,7 +9,7 @@ const uint64_t BILLION = 1000000000; namespace engine { - Window::Window(const std::string& title, bool resizable) : m_title(title), m_resizable(resizable) + Window::Window(const std::string& title, bool resizable, bool fullscreen) : m_title(title), m_resizable(resizable), m_fullscreen(fullscreen) { // init SDL @@ -39,6 +39,10 @@ namespace engine { windowFlags |= SDL_WINDOW_RESIZABLE; } + if (m_fullscreen) { + windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; + } + // create the window m_handle = SDL_CreateWindow( m_title.c_str(), @@ -471,4 +475,4 @@ namespace engine { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Game Error", message.c_str(), NULL); } -} \ No newline at end of file +}