Wip: Add basic vector math

This commit is contained in:
Timothy O'Barr
2026-09-29 16:31:02 -05:00
parent 1a69261952
commit 867980c3f9
5 changed files with 107 additions and 14 deletions
+82
View File
@@ -0,0 +1,82 @@
#pragma once
#include <math.h>
#include <utility>
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);
}
};
@@ -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" }
+6 -1
View File
@@ -1,3 +1,4 @@
#include <SDL3/SDL_rect.h>
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
@@ -10,11 +11,13 @@
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
#include <SDL3_ttf/SDL_ttf.h>
#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;
}
+6 -8
View File
@@ -3,31 +3,29 @@
#include <cstdio>
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);
}
+5 -5
View File
@@ -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); }
};