From 0c1541e98c53ec3ec0e9756c13173cbad171c7c8 Mon Sep 17 00:00:00 2001 From: velimir Date: Fri, 13 Feb 2026 13:00:22 +0100 Subject: [PATCH] up down is broken --- include/entity.hpp | 9 +++++-- include/math.hpp | 5 ++++ include/player.hpp | 23 ++++++++++++++++++ include/renderer/camera.hpp | 1 + src/main.cpp | 33 +++++++++++++++++++++++-- src/math.cpp | 27 +++++++++++++++++++++ src/player.cpp | 48 +++++++++++++++++++++++++++++++++++++ src/renderer/camera.cpp | 8 ++++--- 8 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 include/player.hpp create mode 100644 src/player.cpp diff --git a/include/entity.hpp b/include/entity.hpp index 6e1fe7f..c603a3e 100644 --- a/include/entity.hpp +++ b/include/entity.hpp @@ -1,16 +1,21 @@ #ifndef DELTA_ENTITY #define DELTA_ENTITY +#include +#include + #include "renderer/camera.hpp" #include "math.hpp" class DeltaEntity { public: DeltaVector3 pos; + DeltaVector3 velocity; DeltaVector2 angle; - DeltaEntity(float x, float y, float z); - void draw(DeltaCamera camera); + DeltaEntity() {pos.x = 0; pos.y = 0; pos.z = 0; velocity = DeltaVector3(); angle = DeltaVector2();}; + DeltaEntity(float x, float y, float z) {pos.x = x; pos.y = y; pos.z = z; velocity = DeltaVector3(); angle = DeltaVector2(); }; + void draw(SDL_Renderer *renderer); void update(float deltaTime); }; diff --git a/include/math.hpp b/include/math.hpp index 05f3ea3..f6111ed 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -23,6 +23,11 @@ class DeltaVector3 { float x, y, z; DeltaVector3(float xval, float yval, float zval); DeltaVector3(); + DeltaVector3 operator+(const DeltaVector3& other) const; + DeltaVector3 operator-(const DeltaVector3& other) const; + DeltaVector3 operator*(float a) const; + DeltaVector3& operator+=(const DeltaVector3& other); + DeltaVector3& operator-=(const DeltaVector3& other); float distanceToVector(const DeltaVector3 &vec) const; float distanceToPos(float x, float y) const; float dotProduct(const DeltaVector3 &vec) const; diff --git a/include/player.hpp b/include/player.hpp new file mode 100644 index 0000000..206eff0 --- /dev/null +++ b/include/player.hpp @@ -0,0 +1,23 @@ +#ifndef DELTA_PLAYER +#define DELTA_PLAYER + +#include +#include + +#include "renderer/camera.hpp" +#include "entity.hpp" +#include "math.hpp" + +class Player: public DeltaEntity { + public: + DeltaCamera camera; + DeltaVector2 direction; + + Player() {pos.x = 0; pos.y = 0; pos.z = 0; velocity = DeltaVector3(); angle = DeltaVector2(); camera = DeltaCamera(0, 0, 0);}; + Player(float x, float y, float z) {pos.x = x; pos.y = y; pos.z = z; velocity = DeltaVector3(); angle = DeltaVector2(); camera = DeltaCamera(0,0,0);}; + + void draw(SDL_Renderer *renderer); + void update(float deltaTime); +}; + +#endif \ No newline at end of file diff --git a/include/renderer/camera.hpp b/include/renderer/camera.hpp index 26643f4..3b3176c 100644 --- a/include/renderer/camera.hpp +++ b/include/renderer/camera.hpp @@ -8,6 +8,7 @@ class DeltaCamera { DeltaVector3 pos; DeltaVector2 angle; + DeltaCamera() {pos = DeltaVector3(); angle = DeltaVector2();}; DeltaCamera(float x, float y, float z); DeltaVector2 projectPointVec(const DeltaVector3 &worldPos); DeltaVector2 projectPoint(float worldX, float worldY, float worldZ); diff --git a/src/main.cpp b/src/main.cpp index 2399d88..21ac5ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,12 +3,14 @@ #include #include #include +#include #include #include "globals.hpp" #include "math.hpp" #include "renderer/camera.hpp" #include "renderer/calls.hpp" +#include "player.hpp" static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; @@ -16,6 +18,10 @@ static SDL_Renderer *renderer = NULL; DeltaVector2 temp1(125, 150), temp2(150, 120), temp3(100, 100); DeltaVector2 topleft, bottomright, p; +Player player = Player(70, 110, 20); + +double prev = 0, now = 0, deltaTime; + SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { SDL_SetAppMetadata("delta engine game", "0.0", "com.miletici.game"); @@ -39,21 +45,44 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) if (event->type == SDL_EVENT_QUIT) { return SDL_APP_SUCCESS; } + + if (event->type == SDL_EVENT_KEY_DOWN) { + if (event->key.scancode == SDL_SCANCODE_ESCAPE) return SDL_APP_SUCCESS; + } + return SDL_APP_CONTINUE; } SDL_AppResult SDL_AppIterate(void *appstate) { - const double now = ((double)SDL_GetTicks()) / 1000.0; + now = ((double)SDL_GetTicks()) / 1000.0; + deltaTime = now - prev; + + SDL_SetWindowTitle(window, std::to_string((int)(1 / deltaTime)).c_str()); + player.update(deltaTime); + + // drawing starts here + SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 255, 0, 255, SDL_ALPHA_OPAQUE); - rasterizeTriangle(renderer, temp1, temp2, temp3); + // rasterizeTriangle(renderer, temp1, temp2, temp3); + + DeltaVector2 rendered = player.camera.projectPointVec({ 40, 10, 30 }); + SDL_RenderPoint(renderer, rendered.x, rendered.y); + rendered = player.camera.projectPointVec({ 40, 210, 30 }); + SDL_RenderPoint(renderer, rendered.x, rendered.y); + rendered = player.camera.projectPointVec({ 40, 10, -170 }); + SDL_RenderPoint(renderer, rendered.x, rendered.y); + rendered = player.camera.projectPointVec({ 40, 210, -170 }); + SDL_RenderPoint(renderer, rendered.x, rendered.y); SDL_RenderPresent(renderer); + prev = now; + return SDL_APP_CONTINUE; } diff --git a/src/math.cpp b/src/math.cpp index 5d3441c..ae5da35 100644 --- a/src/math.cpp +++ b/src/math.cpp @@ -67,6 +67,33 @@ DeltaVector3::DeltaVector3(float xval, float yval, float zval) { z = zval; } +DeltaVector3 DeltaVector3::operator+(const DeltaVector3& other) const { + return DeltaVector3(x + other.x, y + other.y, z + other.y); +} + +DeltaVector3 DeltaVector3::operator-(const DeltaVector3& other) const { + return DeltaVector3(x - other.x, y - other.y, z - other.z); +} + +DeltaVector3 DeltaVector3::operator*(float a) const { + return DeltaVector3(x * a, y * a, z * a); +} + +DeltaVector3& DeltaVector3::operator+=(const DeltaVector3& other) { + x += other.x; + y += other.y; + z += other.z; + + return *this; +} +DeltaVector3& DeltaVector3::operator-=(const DeltaVector3& other) { + x -= other.x; + y -= other.y; + z -= other.z; + + return *this; +} + float DeltaVector3::length() const { return std::sqrt(x * x + y * y + z * z); } diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..526db19 --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +#include "player.hpp" + +void Player::draw(SDL_Renderer *renderer) { + +} + +void Player::update(float deltaTime) { + const bool *key_states = SDL_GetKeyboardState(NULL); + direction = {(float)(key_states[SDL_SCANCODE_D] - key_states[SDL_SCANCODE_A]), (float)(key_states[SDL_SCANCODE_W] - key_states[SDL_SCANCODE_S])}; + + DeltaVector2 angleChange; + angleChange.x = (float)(key_states[SDL_SCANCODE_RIGHT] - key_states[SDL_SCANCODE_LEFT]); + angleChange.y = (float)(key_states[SDL_SCANCODE_UP] - key_states[SDL_SCANCODE_DOWN]); + + float dx = std::sin(angle.x), dy = std::cos(angle.x); + + // if (key_states[SDL_SCANCODE_W]) + velocity = DeltaVector3(); + if (direction.y == 1) { velocity.x += dx * 200; velocity.y += dy * 200; } + if (direction.y == -1) { velocity.x -= dx * 200; velocity.y -= dy * 200; } + + if (direction.x == 1) { velocity.x += dy * 200; velocity.y -= dx * 200; } + if (direction.x == -1) { velocity.x -= dy * 200; velocity.y += dx * 200; } + + if (key_states[SDL_SCANCODE_SPACE]) { + velocity.z = 200; + } + if (key_states[SDL_SCANCODE_LSHIFT]) { + velocity.z = -200; + } + + SDL_Log(std::to_string(pos.x).c_str()); + SDL_Log(std::to_string(pos.y).c_str()); + SDL_Log(std::to_string(pos.z).c_str()); + + angle.x += angleChange.x * 3.14 * deltaTime; + angle.y += angleChange.y * 3.14 * deltaTime; + + pos += velocity * deltaTime; + + camera.pos = pos; + camera.angle = angle; +} \ No newline at end of file diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index 7bb50d3..1b215db 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -33,12 +33,14 @@ DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) { // pitch translate - calculatedPos.y = calculatedPos.y * cosineY - calculatedPos.z * sineY; - calculatedPos.z = calculatedPos.y * sineY + calculatedPos.z * cosineY; + float tempY = calculatedPos.y, tempZ = calculatedPos.z; + + calculatedPos.y = tempY * cosineY - tempZ * sineY; + calculatedPos.z = tempY * sineY + tempZ * cosineY; // dont touch calculatedPos.x projected.x = calculatedPos.x * FOCAL / calculatedPos.y + SIMULATED_WIDTH / 2; - projected.y = calculatedPos.y * FOCAL / calculatedPos.y + SIMULATED_HEIGHT / 2; + projected.y = calculatedPos.z * FOCAL / calculatedPos.y + SIMULATED_HEIGHT / 2; // now gotta convert y to our y projected.y = SIMULATED_HEIGHT - projected.y;