Compare commits

...

7 Commits

Author SHA1 Message Date
44a84bbc53 Remove compile_commands.json 2023-12-17 14:04:34 +00:00
b76e73d57b Add ability to change FOV 2023-08-27 20:58:12 +01:00
cc9f3f06db Clean up includes 2023-08-20 01:35:20 +01:00
06d685d6a6 Update compile_commands 2023-08-19 23:25:41 +01:00
33ce544a7e Free the canvas surface 2023-08-19 23:25:02 +01:00
3ab2f14235 Remove unnecessary variables 2023-08-19 22:57:12 +01:00
61681f5e62 Update .gitignore 2023-08-19 17:19:43 +01:00
3 changed files with 40 additions and 91 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
main
result.m4v
.cache
compile_commands.json

View File

@@ -1,81 +0,0 @@
[
{
"arguments": [
"/usr/bin/clang",
"-c",
"-g",
"-o",
"main",
"main.c"
],
"directory": "/home/abdelrahman/Sources/programming/starfield",
"file": "/home/abdelrahman/Sources/programming/starfield/main.c",
"output": "/home/abdelrahman/Sources/programming/starfield/main"
},
{
"arguments": [
"/usr/bin/clang-15",
"-cc1",
"-triple",
"x86_64-pc-linux-gnu",
"-emit-obj",
"-mrelax-all",
"--mrelax-relocations",
"-disable-free",
"-clear-ast-before-backend",
"-disable-llvm-verifier",
"-discard-value-names",
"-main-file-name",
"-mrelocation-model",
"pic",
"-pic-level",
"2",
"-pic-is-pie",
"-mframe-pointer=all",
"-fmath-errno",
"-ffp-contract=on",
"-fno-rounding-math",
"-mconstructor-aliases",
"-funwind-tables=2",
"-target-cpu",
"x86-64",
"-tune-cpu",
"generic",
"-mllvm",
"-treat-scalable-fixed-error-as-warning",
"-debug-info-kind=constructor",
"-dwarf-version=5",
"-debugger-tuning=gdb",
"-fcoverage-compilation-dir=/home/abdelrahman/Sources/programming/starfield",
"-resource-dir",
"/usr/lib/clang/15.0.7",
"-internal-isystem",
"/usr/lib/clang/15.0.7/include",
"-internal-isystem",
"/usr/local/include",
"-internal-isystem",
"/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../x86_64-pc-linux-gnu/include",
"-internal-externc-isystem",
"/include",
"-internal-externc-isystem",
"/usr/include",
"-fdebug-compilation-dir=/home/abdelrahman/Sources/programming/starfield",
"-ferror-limit",
"19",
"-stack-protector",
"2",
"-fgnuc-version=4.2.1",
"-fcolor-diagnostics",
"-faddrsig",
"-D__GCC_HAVE_DWARF2_CFI_ASM=1",
"-x",
"c",
"-o",
"/tmp/main-118336.o",
"main.c"
],
"directory": "/home/abdelrahman/Sources/programming/starfield",
"file": "/home/abdelrahman/Sources/programming/starfield/main.c",
"output": "/tmp/main-118336.o"
}
]

48
main.c
View File

@@ -1,5 +1,6 @@
#include "aliases.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
@@ -8,10 +9,8 @@
#include <SDL2/SDL_video.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define WINDOW_WIDTH 800
@@ -29,6 +28,13 @@
#define PROJECTION_PLANE 1.0
#define RAD(DEG) ((DEG * M_PI) / 180.0)
#define DEG(RAD) ((RAD * 180.0) / M_PI)
#define FOV_MIN 1.0
#define FOV_MAX 170.0
#define FOV_INC 3.0
typedef struct {
u32 x;
u32 y;
@@ -61,7 +67,7 @@ void init_starfield(vec3f_t *stars, u32 count, i32 spread);
void update_startfield(vec3f_t *stars, u32 count, i32 spread, u32 speed,
f64 delta);
void render_starfield(SDL_Surface *surface, vec3f_t *stars, u32 count,
i32 spread, f64 aov);
i32 spread, f64 fov);
int main(void) {
srand(time(NULL));
@@ -86,6 +92,7 @@ int main(void) {
init_starfield(stars, STAR_COUNT, STAR_SPREAD);
f64 delta = 1.0 / 60.0; // constant delta
f64 fov = 60.0;
while (running) {
while (SDL_PollEvent(&event)) {
@@ -93,6 +100,26 @@ int main(void) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
fov -= FOV_INC;
if (fov < FOV_MIN) {
fov = FOV_MIN;
}
break;
case SDLK_DOWN:
fov += FOV_INC;
if (fov > FOV_MAX) {
fov = FOV_MAX;
}
break;
}
break;
}
}
@@ -100,16 +127,15 @@ int main(void) {
render_clear(canvas, BG_COLOR);
u32 scalar = 10;
vec2i_t pos;
render_starfield(canvas, stars, STAR_COUNT, STAR_SPREAD, 60.0);
render_starfield(canvas, stars, STAR_COUNT, STAR_SPREAD, fov);
SDL_BlitSurface(canvas, NULL, surface, NULL);
SDL_UpdateWindowSurface(window);
}
SDL_FreeSurface(canvas);
SDL_DestroyWindow(window);
SDL_Quit();
@@ -219,17 +245,19 @@ void update_startfield(vec3f_t *stars, u32 count, i32 spread, u32 speed,
}
void render_starfield(SDL_Surface *surface, vec3f_t *stars, u32 count,
i32 spread, f64 aov) {
i32 spread, f64 fov) {
vec2f_t projected[count];
vec2i_t coords;
f64 tangent = tan(RAD(fov * 0.5));
for (u32 i = 0; i < count; ++i) {
if (stars[i].z == 0.0) {
continue;
}
projected[i] =
(vec2f_t){.x = stars[i].x / stars[i].z, .y = stars[i].y / stars[i].z};
projected[i] = (vec2f_t){.x = stars[i].x / stars[i].z / tangent,
.y = stars[i].y / stars[i].z / tangent};
if (projected[i].x < NORMALISED_MIN || projected[i].x > NORMALISED_MAX ||
projected[i].y < NORMALISED_MIN || projected[i].y > NORMALISED_MAX ||