From 867980c3f9772a01b793f5842d7d698aa31cf61c Mon Sep 17 00:00:00 2001 From: Timothy O'Barr Date: Tue, 29 Sep 2026 16:31:02 -0500 Subject: [PATCH] Wip: Add basic vector math --- Common/MathCommon.h | 82 +++++++++++++++++++ .../android/gradle/libs.versions.toml | 8 ++ main.cpp | 7 +- ui/Text.cpp | 14 ++-- ui/Text.h | 10 +-- 5 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 Common/MathCommon.h create mode 100644 Extern/imgui-1.92.9b/examples/example_android_opengl3/android/gradle/libs.versions.toml diff --git a/Common/MathCommon.h b/Common/MathCommon.h new file mode 100644 index 0000000..c4636ae --- /dev/null +++ b/Common/MathCommon.h @@ -0,0 +1,82 @@ +#pragma once +#include +#include + +class Vector3 { +private: + float x; + float y; + float z; + +public: + float X() const { return x; } + float Y() const { return y; } + float Z() const { return z; } + + + Vector3() { + x = 0.0f; + y = 0.0f; + z = 0.0f; + } + + Vector3(float xDat, float yDat, float zDat) { + x = xDat; + y = yDat; + z = zDat; + } + + Vector3 Zero() { return Vector3(); } + Vector3 operator+(const Vector3 &rhs) { return Vector3(x + rhs.x, y + rhs.y, z + rhs.z); } + + Vector3 &operator=(Vector3 &rhs) { + swap(*this, rhs); + return *this; + } + + inline friend void swap(Vector3 &lhs, Vector3 &rhs) noexcept { + using std::swap; + swap(lhs.x, rhs.x); + swap(lhs.y, rhs.y); + swap(lhs.z, rhs.z); + } +}; + +class Vector3I { +private: + int x; + int y; + int z; + +public: + int X() const { return x; } + int Y() const { return y; } + int Z() const { return z; } + + + Vector3I() { + x = 0; + y = 0; + z = 0; + } + + Vector3I(int xDat, int yDat, int zDat) { + x = xDat; + y = yDat; + z = zDat; + } + + Vector3I Zero() { return Vector3I(); } + Vector3I operator+(const Vector3I &rhs) { return Vector3I(x + rhs.x, y + rhs.y, z + rhs.z); } + Vector3I &operator=(Vector3I &rhs) { + swap(*this, rhs); + return *this; + } + + inline friend void swap(Vector3I &lhs, Vector3I &rhs) noexcept { + using std::swap; + swap(lhs.x, rhs.x); + swap(lhs.y, rhs.y); + swap(lhs.z, rhs.z); + } +}; diff --git a/Extern/imgui-1.92.9b/examples/example_android_opengl3/android/gradle/libs.versions.toml b/Extern/imgui-1.92.9b/examples/example_android_opengl3/android/gradle/libs.versions.toml new file mode 100644 index 0000000..43d9021 --- /dev/null +++ b/Extern/imgui-1.92.9b/examples/example_android_opengl3/android/gradle/libs.versions.toml @@ -0,0 +1,8 @@ +[versions] +agp = "9.2.0" +kotlin = "2.3.21" + +[plugins] +android-application = { id = "com.android.application", version.ref = "agp" } +kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } + diff --git a/main.cpp b/main.cpp index a3dad46..3b40a64 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ #include @@ -10,11 +11,13 @@ #include #include #include +#include "MathCommon.h" #include "Text.h" #include "fileCommon.h" #include "game.h" #include "window.h" + Game game; Window window; @@ -65,6 +68,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { SDL_Log("SDL could not initialize! SDL error: %s\n", SDL_GetError()); } + Vector3 vector = Vector3(0, 255, 0); + SDL_AppResult result = window.CreateWindow(600, 800); if (result != SDL_APP_CONTINUE) { @@ -87,7 +92,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { return SDL_APP_FAILURE; } - text.SetText("Hello There", &window); + text.SetText("Hello There", &window, {100, 100, 100, 500}); return SDL_APP_CONTINUE; } diff --git a/ui/Text.cpp b/ui/Text.cpp index 88d05f2..0c70fc3 100644 --- a/ui/Text.cpp +++ b/ui/Text.cpp @@ -3,31 +3,29 @@ #include -SDL_AppResult Text::OpenFont(fs::path fontPath) { - +SDL_AppResult Text::OpenFont(const fs::path &font) { if (!TTF_Init()) { SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError()); return SDL_APP_FAILURE; } - loadedFont = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f); + loadedFont = TTF_OpenFont(fs::absolute(font).c_str(), 12.0f); if (!loadedFont) { printf("Font not found! \n"); return SDL_APP_FAILURE; } - color = WHITE; return SDL_APP_CONTINUE; } -bool Text::SetText(std::string text, Window *window) { +bool Text::SetText(const std::string &text, const Window *win, SDL_FRect rect) { buffer += text; SDL_DestroySurface(textRenderingSurface); textRenderingSurface = TTF_RenderText_Blended(loadedFont, buffer.c_str(), 0, color); if (textRenderingSurface) { - font_texture = SDL_CreateTextureFromSurface(window->GetRenderer(), textRenderingSurface); + font_texture = SDL_CreateTextureFromSurface(win->GetRenderer(), textRenderingSurface); SDL_DestroySurface(textRenderingSurface); } if (!font_texture) { @@ -38,7 +36,7 @@ bool Text::SetText(std::string text, Window *window) { return true; } -void Text::Render() { +void Text::Render() const { int w = 0, h = 0; SDL_FRect dst; const float scale = 4.0f; @@ -49,5 +47,5 @@ void Text::Render() { SDL_GetTextureSize(font_texture, &dst.w, &dst.h); dst.x = ((w / scale) - dst.w) / 2; dst.y = ((h / scale) - dst.h) / 2; - SDL_RenderTexture(window.GetRenderer(), font_texture, NULL, &dst); + SDL_RenderTexture(window.GetRenderer(), font_texture, nullptr, &dst); } diff --git a/ui/Text.h b/ui/Text.h index ebf1f4e..0e25f14 100644 --- a/ui/Text.h +++ b/ui/Text.h @@ -19,7 +19,7 @@ private: TTF_Font *loadedFont = nullptr; SDL_Surface *textRenderingSurface = nullptr; - SDL_Color color; + SDL_Color color = {}; SDL_Texture *font_texture = nullptr; @@ -28,8 +28,8 @@ private: public: - SDL_AppResult OpenFont(fs::path fontPath); - bool SetText(std::string text, Window *window); - void Render(); - void CloseFont() { TTF_CloseFont(loadedFont); } + SDL_AppResult OpenFont(const fs::path &font); + bool SetText(const std::string &text, const Window *win, SDL_FRect rect); + void Render() const; + void CloseFont() const { TTF_CloseFont(loadedFont); } };