baseline effort of portability

This commit is contained in:
Alexander Bass 2024-05-04 20:15:07 -04:00
parent 92aa9071fe
commit 45a9670357
3 changed files with 16 additions and 19 deletions

View file

@ -9,6 +9,7 @@
#include "types.h" #include "types.h"
#include "vec3.h" #include "vec3.h"
#include <math.h> #include <math.h>
vec3_t sample_rgb(texture_t tex, size_t index) { vec3_t sample_rgb(texture_t tex, size_t index) {
u8 red = tex.bytes[index]; u8 red = tex.bytes[index];
u8 green = tex.bytes[index + 1]; u8 green = tex.bytes[index + 1];

View file

@ -1,6 +1,6 @@
// Alexander Bass // Alexander Bass
// Created 4/16/24 // Created 4/16/24
// Modified 4/23/24 // Modified 5/4/24
#include "donut.h" #include "donut.h"
#include "point.h" #include "point.h"
@ -23,10 +23,11 @@
#define DEPTH WIDTH #define DEPTH WIDTH
#define SECONDS_TO_NANOSECONDS 1000000000 #define SECONDS_TO_NANOSECONDS 1000000000
#define FRAMERATE 30 #define FRAMERATE 60
#define LARGEST_PIXEL_CHAR_STRING "\033[38;2;255;255;255m██" #define LARGEST_PIXEL_CHAR_STRING "\033[38;2;255;255;255m██"
#define LARGEST_PIXEL_CHAR_LEN sizeof(LARGEST_PIXEL_CHAR_STRING) - 1 #define LARGEST_PIXEL_CHAR_LEN sizeof(LARGEST_PIXEL_CHAR_STRING) - 1
#define CHAR_BUFFER_CAPACITY (HEIGHT * WIDTH * LARGEST_PIXEL_CHAR_LEN + HEIGHT) * sizeof(char)
void draw(vec4_t *, const point_list_t *, char *); void draw(vec4_t *, const point_list_t *, char *);
void update(point_list_t *, u64); void update(point_list_t *, u64);
@ -47,19 +48,20 @@ int main() {
pixel_buffer[i] = vec4(0.0, 0.0, 0.0, 0.0); pixel_buffer[i] = vec4(0.0, 0.0, 0.0, 0.0);
}; };
size_t char_buffer_capacity = (HEIGHT * WIDTH * LARGEST_PIXEL_CHAR_LEN + HEIGHT); char *screen_char_buffer = malloc(CHAR_BUFFER_CAPACITY);
char *screen_char_buffer = malloc(char_buffer_capacity * sizeof(char));
#ifdef __GLIBC__
__SYSCALL_SLONG_TYPE frame_budget = SECONDS_TO_NANOSECONDS / FRAMERATE; __SYSCALL_SLONG_TYPE frame_budget = SECONDS_TO_NANOSECONDS / FRAMERATE;
struct timespec frame_start, frame_end, sleep_time; struct timespec frame_start, frame_end, sleep_time;
sleep_time.tv_sec = 0; sleep_time.tv_sec = 0;
sleep_time.tv_nsec = frame_budget; sleep_time.tv_nsec = frame_budget;
#endif
u64 counter = 0; u64 counter = 0;
while (true) { while (true) {
#ifdef __GLIBC__
clock_gettime(CLOCK_MONOTONIC_RAW, &frame_start); clock_gettime(CLOCK_MONOTONIC_RAW, &frame_start);
#endif
// Move cursor to top left // Move cursor to top left
printf("\033[0;0H"); printf("\033[0;0H");
// Draw points // Draw points
@ -67,6 +69,7 @@ int main() {
// Rotate points // Rotate points
update(&points, counter); update(&points, counter);
#ifdef __GLIBC__
// Compute time until next frame // Compute time until next frame
clock_gettime(CLOCK_MONOTONIC_RAW, &frame_end); clock_gettime(CLOCK_MONOTONIC_RAW, &frame_end);
__SYSCALL_SLONG_TYPE diff_ns = frame_end.tv_nsec - frame_start.tv_nsec; __SYSCALL_SLONG_TYPE diff_ns = frame_end.tv_nsec - frame_start.tv_nsec;
@ -74,15 +77,20 @@ int main() {
diff_ns += SECONDS_TO_NANOSECONDS * diff_s; diff_ns += SECONDS_TO_NANOSECONDS * diff_s;
__SYSCALL_SLONG_TYPE excess = diff_ns > frame_budget ? 0 : frame_budget - diff_ns; __SYSCALL_SLONG_TYPE excess = diff_ns > frame_budget ? 0 : frame_budget - diff_ns;
// Framerate is dropping
// if (excess == 0) { // if (excess == 0) {
// printf("\033[38;2;255;255;255mCan't keep up; Exiting. Ran for %lu frames\n", counter); // printf("\033[38;2;255;255;255mCan't keep up; Exiting. Ran for %lu frames\n", counter);
// exit(0); // exit(0);
// } // }
sleep_time.tv_nsec = excess; sleep_time.tv_nsec = excess;
counter += 1;
// Wait until next frame // Wait until next frame
nanosleep(&sleep_time, NULL); nanosleep(&sleep_time, NULL);
#else
// At least it's portable
sleep(1);
#endif
counter += 1;
} }
free_point_list(&points); free_point_list(&points);

View file

@ -1,12 +0,0 @@
#include <iostream>
using namespace std;
int test() {
return 0;
}
int main() {
cout << test();
}