initial commit

This commit is contained in:
2026-02-12 19:55:21 +01:00
commit 5cd6abdd48
11 changed files with 389 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Linker files
*.ilk
# Debugger Files
*.pdb
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
*.so.*
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# Build directories
build/
Build/
build-*/
# CMake generated files
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
Makefile
install_manifest.txt
compile_commands.json
# Temporary files
*.tmp
*.log
*.bak
*.swp
# vcpkg
vcpkg_installed/
# debug information files
*.dwo
# test output & cache
Testing/
.cache/
.vscode/
+17
View File
@@ -0,0 +1,17 @@
#ifndef DELTA_ENTITY
#define DELTA_ENTITY
#include "renderer/camera.hpp"
#include "math.hpp"
class DeltaEntity {
public:
DeltaVector3 pos;
DeltaVector2 angle;
DeltaEntity(float x, float y, float z);
void draw(DeltaCamera camera);
void update(float deltaTime);
};
#endif
+8
View File
@@ -0,0 +1,8 @@
#ifndef DELTA_GLOBALS
#define DELTA_GLOBALS
const int SIMULATED_WIDTH = 1200;
const int SIMULATED_HEIGHT = 800;
const int FOCAL = 200;
#endif
+35
View File
@@ -0,0 +1,35 @@
#ifndef DELTA_MATH
#define DELTA_MATH
class DeltaVector2 {
public:
float x, y;
DeltaVector2(float xval, float yval);
DeltaVector2();
DeltaVector2 operator+(const DeltaVector2& other) const;
DeltaVector2 operator-(const DeltaVector2& other) const;
DeltaVector2 operator*(float a) const;
DeltaVector2& operator+=(const DeltaVector2& other);
DeltaVector2& operator-=(const DeltaVector2& other);
float distanceToVector(const DeltaVector2& vec) const;
float distanceToPos(float x, float y) const;
float dotProduct(const DeltaVector2& vec) const;
float angleTo(const DeltaVector2& vec) const;
float length() const;
};
class DeltaVector3 {
public:
float x, y, z;
DeltaVector3(float xval, float yval, float zval);
DeltaVector3();
float distanceToVector(const DeltaVector3 &vec) const;
float distanceToPos(float x, float y) const;
float dotProduct(const DeltaVector3 &vec) const;
float angleTo(const DeltaVector3 &vec) const;
float length() const;
};
float getDeterminant(const DeltaVector2 &pointA, const DeltaVector2 &pointB, const DeltaVector2 &candidate);
#endif
+10
View File
@@ -0,0 +1,10 @@
#ifndef DELTA_RENDERCALLS
#define DELTA_RENDERCALLS
#include <SDL3/SDL.h>
#include "math.hpp"
void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c);
#endif
+16
View File
@@ -0,0 +1,16 @@
#ifndef DELTA_CAMERA
#define DELTA_CAMERA
#include "math.hpp"
class DeltaCamera {
public:
DeltaVector3 pos;
DeltaVector2 angle;
DeltaCamera(float x, float y, float z);
DeltaVector2 projectPointVec(const DeltaVector3 &worldPos);
DeltaVector2 projectPoint(float worldX, float worldY, float worldZ);
};
#endif
+5
View File
@@ -0,0 +1,5 @@
clang++ -c src/*.cpp -I/opt/homebrew/include -Iinclude/
clang++ -c src/**/*.cpp -I/opt/homebrew/include -Iinclude/
clang++ *.o -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -lSDL3 -o out/debug.bin
rm ./*.o
./out/debug.bin
+64
View File
@@ -0,0 +1,64 @@
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <cmath>
#include <algorithm>
#include "globals.hpp"
#include "math.hpp"
#include "renderer/camera.hpp"
#include "renderer/calls.hpp"
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
DeltaVector2 temp1(125, 150), temp2(150, 120), temp3(100, 100);
DeltaVector2 topleft, bottomright, p;
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_SetAppMetadata("delta engine game", "0.0", "com.miletici.game");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("game", SIMULATED_WIDTH, SIMULATED_HEIGHT, 0, &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);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS;
}
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void *appstate)
{
const double now = ((double)SDL_GetTicks()) / 1000.0;
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);
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
}
+87
View File
@@ -0,0 +1,87 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <cmath>
#include "math.hpp"
// DeltaVector2
DeltaVector2::DeltaVector2(float xval, float yval) {
x = xval;
y = yval;
}
float DeltaVector2::length() const {
return std::sqrt(x * x + y * y);
}
float DeltaVector2::dotProduct(const DeltaVector2& vec) const {
return x * vec.x + y * vec.y;
}
float DeltaVector2::angleTo(const DeltaVector2& vec) const {
return std::acos(dotProduct(vec)) / (length() * vec.length());
}
DeltaVector2::DeltaVector2() {
x = 0;
y = 0;
}
DeltaVector2 DeltaVector2::operator+(const DeltaVector2& other) const {
return DeltaVector2(x + other.x, y + other.y);
}
DeltaVector2 DeltaVector2::operator-(const DeltaVector2& other) const {
return DeltaVector2(x - other.x, y - other.y);
}
DeltaVector2 DeltaVector2::operator*(float a) const {
return DeltaVector2(x * a, y * a);
}
DeltaVector2& DeltaVector2::operator+=(const DeltaVector2& other) {
x += other.x;
y += other.y;
return *this;
}
DeltaVector2& DeltaVector2::operator-=(const DeltaVector2& other) {
x -= other.x;
y -= other.y;
return *this;
}
// DeltaVector3
DeltaVector3::DeltaVector3() {
x = 0;
y = 0;
z = 0;
}
DeltaVector3::DeltaVector3(float xval, float yval, float zval) {
x = xval;
y = yval;
z = zval;
}
float DeltaVector3::length() const {
return std::sqrt(x * x + y * y + z * z);
}
float DeltaVector3::dotProduct(const DeltaVector3 &vec) const {
return x * vec.x + y * vec.y + z * vec.z;
}
float DeltaVector3::angleTo(const DeltaVector3 &vec) const {
return std::acos(dotProduct(vec)) / (length() * vec.length());
}
float getDeterminant(const DeltaVector2 &pointA, const DeltaVector2 &pointB, const DeltaVector2 &candidate) {
DeltaVector2 ab = pointB - pointA;
DeltaVector2 ac = candidate - pointA;
return (ab.y * ac.x) - (ab.x * ac.y);
}
+24
View File
@@ -0,0 +1,24 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <algorithm>
#include "renderer/calls.hpp"
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));
for (int y = begin.y; y < end.y; y++) {
for (int x = begin.x; x < end.x; x++) {
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);
}
}
}
}
+53
View File
@@ -0,0 +1,53 @@
// please do not touch this code.
// it is a waste of time looking at this code.
// please, dont. if you do continue, edit the value below
// as a warning for future you.
// hoursWasted = 2
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <cmath>
#include "renderer/camera.hpp"
#include "globals.hpp"
DeltaCamera::DeltaCamera(float x, float y, float z) {
pos = DeltaVector3(x, y, z);
angle = DeltaVector2();
}
DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) {
DeltaVector3 calculatedPos; DeltaVector2 projected;
float sineX = std::sin(angle.x);
float cosineX = std::cos(angle.x);
float sineY = std::sin(angle.y);
float cosineY = std::cos(angle.y);
// 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);
// pitch translate
calculatedPos.y = calculatedPos.y * cosineY - calculatedPos.z * sineY;
calculatedPos.z = calculatedPos.y * sineY + calculatedPos.z * 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;
// now gotta convert y to our y
projected.y = SIMULATED_HEIGHT - projected.y;
// we are done =D
return projected;
}
DeltaVector2 DeltaCamera::projectPoint(float worldX, float worldY, float worldZ) {
// simple conversion, dont iterate further
return projectPointVec({ worldX, worldY, worldZ });
}