initial commit
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
Reference in New Issue
Block a user