From 7c03b1b81dcb150902031e6a7b03c51adb2c83b2 Mon Sep 17 00:00:00 2001 From: vekidev Date: Fri, 13 Feb 2026 22:53:09 +0100 Subject: [PATCH] a bunch of shit --- include/entity.hpp | 1 - include/globals.hpp | 6 +- include/math.hpp | 6 ++ include/player.hpp | 1 - include/renderer/calls.hpp | 4 +- make.bat | 5 ++ src/main.cpp | 62 ++++++++++---- src/math.cpp | 6 +- src/player.cpp | 7 +- src/renderer/calls.cpp | 160 ++++++++++++++++++++++++++++++++++++- src/renderer/camera.cpp | 24 +++--- 11 files changed, 242 insertions(+), 40 deletions(-) create mode 100644 make.bat diff --git a/include/entity.hpp b/include/entity.hpp index c603a3e..0984921 100644 --- a/include/entity.hpp +++ b/include/entity.hpp @@ -2,7 +2,6 @@ #define DELTA_ENTITY #include -#include #include "renderer/camera.hpp" #include "math.hpp" diff --git a/include/globals.hpp b/include/globals.hpp index 7d3888c..8b27000 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,8 +1,8 @@ #ifndef DELTA_GLOBALS #define DELTA_GLOBALS -const int SIMULATED_WIDTH = 1200; -const int SIMULATED_HEIGHT = 800; -const int FOCAL = 200; +const int SIMULATED_WIDTH = 1920; +const int SIMULATED_HEIGHT = 1080; +const int FOCAL = 300; #endif \ No newline at end of file diff --git a/include/math.hpp b/include/math.hpp index f6111ed..45ba9db 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -37,4 +37,10 @@ class DeltaVector3 { float getDeterminant(const DeltaVector2 &pointA, const DeltaVector2 &pointB, const DeltaVector2 &candidate); +struct DeltaTriangle { + DeltaVector3 a, b, c; +}; + +uint32_t SDLColorToUint32(SDL_Color color); + #endif \ No newline at end of file diff --git a/include/player.hpp b/include/player.hpp index 206eff0..5a066cc 100644 --- a/include/player.hpp +++ b/include/player.hpp @@ -2,7 +2,6 @@ #define DELTA_PLAYER #include -#include #include "renderer/camera.hpp" #include "entity.hpp" diff --git a/include/renderer/calls.hpp b/include/renderer/calls.hpp index a478525..7172cbd 100644 --- a/include/renderer/calls.hpp +++ b/include/renderer/calls.hpp @@ -6,5 +6,7 @@ #include "math.hpp" void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c); - +void rasterizeTriangleFast(SDL_Surface* surface, const DeltaVector2& v0, const DeltaVector2& v1, const DeltaVector2& v2, uint32_t color); +void rasterizeTriangleSurface(SDL_Surface* surface, const DeltaVector2& a, const DeltaVector2& b, const DeltaVector2& c, uint32_t color); +void setPixel(SDL_Surface* surface, int x, int y, uint32_t color); #endif \ No newline at end of file diff --git a/make.bat b/make.bat new file mode 100644 index 0000000..dc47efa --- /dev/null +++ b/make.bat @@ -0,0 +1,5 @@ +g++ -c src/*.cpp -IC:\\common\include -Iinclude/ +g++ -c src/renderer/*.cpp -IC:\\common\include -Iinclude/ +g++ *.o -LC:\\common\lib -lSDL3 -o out\debug.exe +del .\*.o +.\out\debug.exe \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 21ac5ed..e13dd62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "globals.hpp" #include "math.hpp" @@ -19,11 +20,15 @@ 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_Surface* backbuffer = NULL; + +std::vector triangles; +SDL_Texture* gpuTexture = NULL; SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { + SDL_HideCursor(); SDL_SetAppMetadata("delta engine game", "0.0", "com.miletici.game"); if (!SDL_Init(SDL_INIT_VIDEO)) { @@ -31,11 +36,24 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) return SDL_APP_FAILURE; } - if (!SDL_CreateWindowAndRenderer("game", SIMULATED_WIDTH, SIMULATED_HEIGHT, 0, &window, &renderer)) { + if (!SDL_CreateWindowAndRenderer("game", SIMULATED_WIDTH, SIMULATED_HEIGHT, SDL_WINDOW_FULLSCREEN, &window, &renderer)) { SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); return SDL_APP_FAILURE; } - SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); + + SDL_SetWindowRelativeMouseMode(window, true); + + SDL_SetRenderLogicalPresentation(renderer, SIMULATED_WIDTH, SIMULATED_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); + + triangles.push_back({ {40, 10, 0}, {40, 210, 0}, {40, 10, 200} }); + backbuffer = SDL_CreateSurface(SIMULATED_WIDTH, SIMULATED_HEIGHT, SDL_PIXELFORMAT_ARGB8888); + gpuTexture = SDL_CreateTexture( + renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + SIMULATED_WIDTH, + SIMULATED_HEIGHT + ); return SDL_APP_CONTINUE; /* carry on with the program! */ } @@ -46,6 +64,11 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) return SDL_APP_SUCCESS; } + if (event->type == SDL_EVENT_MOUSE_MOTION) { + player.angle.x += event->motion.xrel / 100; + player.angle.y += event->motion.yrel / 100; + } + if (event->type == SDL_EVENT_KEY_DOWN) { if (event->key.scancode == SDL_SCANCODE_ESCAPE) return SDL_APP_SUCCESS; } @@ -64,20 +87,31 @@ SDL_AppResult SDL_AppIterate(void *appstate) // drawing starts here - SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT); - SDL_RenderClear(renderer); + SDL_FillSurfaceRect(backbuffer, NULL, 0xFF000000); - SDL_SetRenderDrawColor(renderer, 255, 0, 255, SDL_ALPHA_OPAQUE); + // SDL_SetRenderDrawColor(renderer, 255, 0, 255, SDL_ALPHA_OPAQUE); // 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); + // 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); + + for (auto triangle : triangles) { + DeltaVector2 pointA = player.camera.projectPointVec(triangle.a), pointB = player.camera.projectPointVec(triangle.b), pointC = player.camera.projectPointVec(triangle.c); + + rasterizeTriangleFast(backbuffer, pointA, pointB, pointC, SDLColorToUint32({ 255, 0, 255, 255 })); + } + + SDL_UpdateTexture(gpuTexture, NULL, backbuffer->pixels, backbuffer->pitch); + + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderClear(renderer); + SDL_RenderTexture(renderer, gpuTexture, NULL, NULL); SDL_RenderPresent(renderer); diff --git a/src/math.cpp b/src/math.cpp index ae5da35..b9eb8e1 100644 --- a/src/math.cpp +++ b/src/math.cpp @@ -1,5 +1,4 @@ #include -#include #include #include "math.hpp" @@ -111,4 +110,9 @@ float getDeterminant(const DeltaVector2 &pointA, const DeltaVector2 &pointB, con DeltaVector2 ac = candidate - pointA; return (ab.y * ac.x) - (ab.x * ac.y); +} + +uint32_t SDLColorToUint32(SDL_Color color) { + // Layout: AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB + return (uint32_t)((color.a << 24) | (color.r << 16) | (color.g << 8) | (color.b << 0)); } \ No newline at end of file diff --git a/src/player.cpp b/src/player.cpp index 526db19..dd0e513 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -34,9 +33,9 @@ void Player::update(float deltaTime) { 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()); + // 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; diff --git a/src/renderer/calls.cpp b/src/renderer/calls.cpp index 23a4a1b..f3606c3 100644 --- a/src/renderer/calls.cpp +++ b/src/renderer/calls.cpp @@ -1,10 +1,30 @@ #include -#include #include +#include +#include "globals.hpp" #include "renderer/calls.hpp" -void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c) { +void setPixel(SDL_Surface* surface, int x, int y, uint32_t color) { + // 1. Boundary check to prevent memory corruption (Crashes) + if (x < 0 || x >= surface->w || y < 0 || y >= surface->h) return; + + // 2. Lock the surface if it requires it (usually for hardware surfaces) + if (SDL_MUSTLOCK(surface)) { + SDL_LockSurface(surface); + } + + // 3. Calculate the pointer to the specific pixel + // We cast to uint8_t* to do byte-math, then to uint32_t* to write the color + uint8_t* pixel_ptr = (uint8_t*)surface->pixels + (y * surface->pitch) + (x * sizeof(uint32_t)); + *(uint32_t*)pixel_ptr = color; + + if (SDL_MUSTLOCK(surface)) { + SDL_UnlockSurface(surface); + } +} + +void rasterizeTriangleSurface(SDL_Surface* surface, const DeltaVector2& a, const DeltaVector2& b, const DeltaVector2& c, uint32_t color) { DeltaVector2 begin, end, p; begin.x = std::min(a.x, std::min(b.x, c.x)); begin.y = std::min(a.y, std::min(b.y, c.y)); @@ -16,9 +36,141 @@ void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &a, const Delt p.x = x; p.y = y; if (getDeterminant(a, b, p) >= 0 && getDeterminant(b, c, p) >= 0 && getDeterminant(c, a, p) >= 0) { - SDL_RenderPoint(renderer, p.x, p.y); + setPixel(surface, x, y, color); } } } -} \ No newline at end of file +} + +void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &v0, const DeltaVector2 &v1, const DeltaVector2 &v2) { + int minX = std::max(0, (int)std::min({v0.x, v1.x, v2.x})); + int maxX = std::min((int)SIMULATED_WIDTH - 1, (int)std::max({v0.x, v1.x, v2.x})); + int minY = std::max(0, (int)std::min({v0.y, v1.y, v2.y})); + int maxY = std::min((int)SIMULATED_HEIGHT - 1, (int)std::max({v0.y, v1.y, v2.y})); + + float a0 = v1.y - v2.y, b0 = v2.x - v1.x, c0 = v1.x * v2.y - v2.x * v1.y; + float a1 = v2.y - v0.y, b1 = v0.x - v2.x, c1 = v2.x * v0.y - v0.x * v2.y; + float a2 = v0.y - v1.y, b2 = v1.x - v0.x, c2 = v0.x * v1.y - v1.x * v0.y; + + __m128 stepX = _mm_set_ps(3.0f, 2.0f, 1.0f, 0.0f); + __m128 a0_4 = _mm_set1_ps(a0); + __m128 a1_4 = _mm_set1_ps(a1); + __m128 a2_4 = _mm_set1_ps(a2); + + for (int y = minY; y <= maxY; y++) { + float fy = (float)y; + + __m128 e0 = _mm_add_ps(_mm_set1_ps(a0 * minX + b0 * fy + c0), _mm_mul_ps(a0_4, stepX)); + __m128 e1 = _mm_add_ps(_mm_set1_ps(a1 * minX + b1 * fy + c1), _mm_mul_ps(a1_4, stepX)); + __m128 e2 = _mm_add_ps(_mm_set1_ps(a2 * minX + b2 * fy + c2), _mm_mul_ps(a2_4, stepX)); + + __m128 e0_step = _mm_set1_ps(a0 * 4.0f); + __m128 e1_step = _mm_set1_ps(a1 * 4.0f); + __m128 e2_step = _mm_set1_ps(a2 * 4.0f); + + for (int x = minX; x <= maxX; x += 4) { + __m128 mask = _mm_and_ps(_mm_cmpge_ps(e0, _mm_setzero_ps()), + _mm_and_ps(_mm_cmpge_ps(e1, _mm_setzero_ps()), + _mm_cmpge_ps(e2, _mm_setzero_ps()))); + + int bitmask = _mm_movemask_ps(mask); + + if (bitmask != 0) { + for (int i = 0; i < 4; i++) { + if ((bitmask >> i) & 1) { + if (x + i <= maxX) + SDL_RenderPoint(renderer, (float)(x + i), fy); + } + } + } + + e0 = _mm_add_ps(e0, e0_step); + e1 = _mm_add_ps(e1, e1_step); + e2 = _mm_add_ps(e2, e2_step); + } + } +} + +void rasterizeTriangleFast(SDL_Surface* surface, const DeltaVector2& v0, const DeltaVector2& v1, const DeltaVector2& v2, uint32_t color) { + int minX = std::max(0, (int)std::min({v0.x, v1.x, v2.x})); + int maxX = std::min((int)surface->w - 1, (int)std::max({v0.x, v1.x, v2.x})); + int minY = std::max(0, (int)std::min({v0.y, v1.y, v2.y})); + int maxY = std::min((int)surface->h - 1, (int)std::max({v0.y, v1.y, v2.y})); + + float a0 = v1.y - v2.y, b0 = v2.x - v1.x, c0 = v1.x * v2.y - v2.x * v1.y; + float a1 = v2.y - v0.y, b1 = v0.x - v2.x, c1 = v2.x * v0.y - v0.x * v2.y; + float a2 = v0.y - v1.y, b2 = v1.x - v0.x, c2 = v0.x * v1.y - v1.x * v0.y; + + __m128 a0_4 = _mm_set1_ps(a0), a1_4 = _mm_set1_ps(a1), a2_4 = _mm_set1_ps(a2); + __m128 stepX = _mm_set_ps(3.0f, 2.0f, 1.0f, 0.0f); + + uint32_t* pixels = (uint32_t*)surface->pixels; + int pitch = surface->pitch / sizeof(uint32_t); + + for (int y = minY; y <= maxY; y++) { + float fy = (float)y; + uint32_t* rowPtr = &pixels[y * pitch]; + + __m128 e0 = _mm_add_ps(_mm_set1_ps(a0 * minX + b0 * fy + c0), _mm_mul_ps(a0_4, stepX)); + __m128 e1 = _mm_add_ps(_mm_set1_ps(a1 * minX + b1 * fy + c1), _mm_mul_ps(a1_4, stepX)); + __m128 e2 = _mm_add_ps(_mm_set1_ps(a2 * minX + b2 * fy + c2), _mm_mul_ps(a2_4, stepX)); + + __m128 e0_step = _mm_set1_ps(a0 * 4.0f); + __m128 e1_step = _mm_set1_ps(a1 * 4.0f); + __m128 e2_step = _mm_set1_ps(a2 * 4.0f); + + for (int x = minX; x <= maxX; x += 4) { + __m128 mask = _mm_and_ps(_mm_cmpge_ps(e0, _mm_setzero_ps()), + _mm_and_ps(_mm_cmpge_ps(e1, _mm_setzero_ps()), + _mm_cmpge_ps(e2, _mm_setzero_ps()))); + + int bitmask = _mm_movemask_ps(mask); + + if (bitmask != 0) { + if (bitmask == 0xF && (x + 3 <= maxX)) { + _mm_storeu_si128((__m128i*)&rowPtr[x], _mm_set1_epi32(color)); + } else { + for (int i = 0; i < 4; i++) { + if ((bitmask >> i) & 1 && (x + i <= maxX)) { + rowPtr[x + i] = color; + } + } + } + } + e0 = _mm_add_ps(e0, e0_step); + e1 = _mm_add_ps(e1, e1_step); + e2 = _mm_add_ps(e2, e2_step); + } + } +} + +// void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c) { +// DeltaVector2 begin, end, p; +// begin.x = std::min(a.x, std::min(b.x, c.x)); +// begin.y = std::min(a.y, std::min(b.y, c.y)); +// end.x = std::max(a.x, std::max(b.x, c.x)); +// end.y = std::max(a.y, std::max(b.y, c.y)); + +// int entrance, exit; + +// for (int y = std::max(1.0f, begin.y); y < std::min(end.y, SIMULATED_HEIGHT + 0.0f); y++) { +// entrance = -1; +// exit = -1; +// for (int x = std::max(1.0f, begin.x); x < std::min(end.x, SIMULATED_WIDTH + 0.0f); x++) { +// p.x = x; p.y = y; + +// if (getDeterminant(a, b, p) >= 0 && getDeterminant(b, c, p) >= 0 && getDeterminant(c, a, p) >= 0 && entrance == -1) { +// SDL_RenderPoint(renderer, p.x, p.y); +// // entrance = x; +// } +// else if (!(getDeterminant(a, b, p) >= 0 && getDeterminant(b, c, p) >= 0 && getDeterminant(c, a, p) >= 0) && entrance != -1) { +// exit = x; +// break; +// } + +// } + +// // SDL_RenderLine(renderer, entrance, y, exit, y); +// } +// } \ No newline at end of file diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index 1b215db..689bb80 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -3,10 +3,9 @@ // please, dont. if you do continue, edit the value below // as a warning for future you. -// hoursWasted = 2 +// hoursWasted = 5 #include -#include #include #include "renderer/camera.hpp" @@ -27,20 +26,23 @@ DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) { // yaw translate - calculatedPos.x = (worldPos.x - pos.x) * cosineX - (worldPos.y - pos.y) * sineX; - calculatedPos.y = (worldPos.y - pos.y) * cosineX + (worldPos.x - pos.x) * sineX; - calculatedPos.z = (worldPos.z - pos.z); + DeltaVector3 t = worldPos - pos; + + float rx = t.x * cosineX - t.y * sineX; + float ry = t.y * cosineX + t.x * sineX; + float rz = (worldPos.z - pos.z); // pitch translate - float tempY = calculatedPos.y, tempZ = calculatedPos.z; - - calculatedPos.y = tempY * cosineY - tempZ * sineY; - calculatedPos.z = tempY * sineY + tempZ * cosineY; + calculatedPos.x = rx; + calculatedPos.y = ry * cosineY - t.z * sineY; + calculatedPos.z = t.z * cosineY + ry * sineY; // dont touch calculatedPos.x - projected.x = calculatedPos.x * FOCAL / calculatedPos.y + SIMULATED_WIDTH / 2; - projected.y = calculatedPos.z * FOCAL / calculatedPos.y + SIMULATED_HEIGHT / 2; + // calculatedPos.y = calculatedPos.y * cosineY; + + projected.x = calculatedPos.x * FOCAL / calculatedPos.y + SIMULATED_WIDTH / 2.0f; + projected.y = calculatedPos.z * FOCAL / calculatedPos.y + SIMULATED_HEIGHT / 2.0f; // now gotta convert y to our y projected.y = SIMULATED_HEIGHT - projected.y;