From f783db8a454bf2ec25e9e3766e683835a77b4b81 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Fri, 12 Apr 2024 23:41:06 -0400 Subject: [PATCH] split parts, vector, tc --- src/display.c | 21 +++++++----- src/display.h | 8 +++++ src/main.c | 94 ++++++++++++++++++++++++++++++--------------------- 3 files changed, 77 insertions(+), 46 deletions(-) diff --git a/src/display.c b/src/display.c index 948cb18..b1655a0 100644 --- a/src/display.c +++ b/src/display.c @@ -63,28 +63,27 @@ void render_color_buffer(void) { ); SDL_RenderCopy(renderer, color_buffer_texture, NULL, NULL); } + void draw_grid(int size) { if (color_buffer == NULL) { return; } for (int y = 0; y < window_height; y++) { for (int x = 0; x < window_width; x++) { - if (y % size == 0 || x % size == 0 ) { - color_buffer[(window_width * y) + x] = 0xFF333333; + if (y % size == 0 || x % size == 0) { + draw_pixel(x, y, 0xFF333333); } } } } -void draw_rect(int x, int y, int width, int height, uint32_t color){ +void draw_rect(int x, int y, int width, int height, uint32_t color) { if (color_buffer == NULL) { return; } - for (int i = y; i < height+y; i++) { + for (int i = y; i < height + y; i++) { for (int j = x; j < width; j++) { - int cur_x = x+j; - int cur_y = y+i; - color_buffer[(window_width * cur_y) + cur_x] = color; + draw_pixel(x + j, y + i, color); } } } @@ -95,4 +94,10 @@ void destroy_window(void) { SDL_DestroyWindow(window); SDL_DestroyTexture(color_buffer_texture); SDL_Quit(); -} \ No newline at end of file +} + +void draw_pixel(int x, int y, uint32_t color) { + if (x < window_width && y < window_height) { + color_buffer[(window_width * y) + x] = color; + } +} diff --git a/src/display.h b/src/display.h index 7bddb5d..4fa093f 100644 --- a/src/display.h +++ b/src/display.h @@ -1,5 +1,6 @@ #ifndef DISPLAY_H #define DISPLAY_H + #include #include #include @@ -13,10 +14,17 @@ extern int window_width; extern int window_height; bool initialize_window(void); + void draw_grid(int size); + +void draw_pixel(int x, int y, uint32_t color); + void draw_rect(int x, int y, int width, int height, uint32_t color); + void clear_color_buffer(uint32_t color); + void render_color_buffer(void); + void destroy_window(void); diff --git a/src/main.c b/src/main.c index e51e88f..c6f8d94 100644 --- a/src/main.c +++ b/src/main.c @@ -2,37 +2,54 @@ #include #include #include "display.h" +#include "vector.h" + +//////////////////////////////////////////////////////////// +// Declare an array of points +//////////////////////////////////////////////////////////// +#define N_POINTS (9 * 9 * 9) +vec3_t cube_points[N_POINTS]; + bool is_running = false; void setup(void) { - // Allocate the required memory in bytes to hold color buffer - color_buffer = (uint32_t *) malloc(sizeof(uint32_t) * window_width * window_height); + // Allocate the required memory in bytes to hold color buffer + color_buffer = (uint32_t *) malloc(sizeof(uint32_t) * window_width * window_height); - // Creating the SDL Texture that is uses to display the color buffer - color_buffer_texture = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, - window_width, - window_height - ); + // Creating the SDL Texture that is uses to display the color buffer + color_buffer_texture = SDL_CreateTexture( + renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + window_width, + window_height + ); + int point_count = 0; + for (float x = -1; x <= 1; x += 0.25) { + for (float y = -1; y <= 1; y += 0.25) { + for (float z = -1; z <= 1; z += 0.25) { + vec3_t new_point = { .x = x, .y = y, .z = z }; + cube_points[point_count++] = new_point; + } + } + } } void process_input(void) { - SDL_Event event; - SDL_PollEvent(&event); + SDL_Event event; + SDL_PollEvent(&event); - switch (event.type) { - case SDL_QUIT: - is_running = false; - break; - case SDL_KEYDOWN: - if (event.key.keysym.sym == SDLK_ESCAPE) { - is_running = false; - } - break; - } + switch (event.type) { + case SDL_QUIT: + is_running = false; + break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_ESCAPE) { + is_running = false; + } + break; + } } @@ -41,26 +58,27 @@ void update(void) { void render(void) { - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderClear(renderer); - clear_color_buffer(0xFF000000); - draw_grid(10); - draw_rect(50, 50, 640, 480, 0xFFDD00DD); - render_color_buffer(); + clear_color_buffer(0xFF000000); + draw_grid(10); + draw_rect(50, 50, 640, 480, 0xFFDD00DD); + render_color_buffer(); - SDL_RenderPresent(renderer); + SDL_RenderPresent(renderer); } int main(void) { - is_running = initialize_window(); - setup(); - while (is_running) { - process_input(); - update(); - render(); - } - destroy_window(); - return 0; + is_running = initialize_window(); + setup(); + + while (is_running) { + process_input(); + update(); + render(); + } + destroy_window(); + return 0; }