split parts, vector, tc

This commit is contained in:
Tyrel Souza 2024-04-12 23:41:06 -04:00
parent edfc64d225
commit f783db8a45
3 changed files with 77 additions and 46 deletions

View File

@ -63,28 +63,27 @@ void render_color_buffer(void) {
); );
SDL_RenderCopy(renderer, color_buffer_texture, NULL, NULL); SDL_RenderCopy(renderer, color_buffer_texture, NULL, NULL);
} }
void draw_grid(int size) { void draw_grid(int size) {
if (color_buffer == NULL) { if (color_buffer == NULL) {
return; return;
} }
for (int y = 0; y < window_height; y++) { for (int y = 0; y < window_height; y++) {
for (int x = 0; x < window_width; x++) { for (int x = 0; x < window_width; x++) {
if (y % size == 0 || x % size == 0 ) { if (y % size == 0 || x % size == 0) {
color_buffer[(window_width * y) + x] = 0xFF333333; 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) { if (color_buffer == NULL) {
return; return;
} }
for (int i = y; i < height+y; i++) { for (int i = y; i < height + y; i++) {
for (int j = x; j < width; j++) { for (int j = x; j < width; j++) {
int cur_x = x+j; draw_pixel(x + j, y + i, color);
int cur_y = y+i;
color_buffer[(window_width * cur_y) + cur_x] = color;
} }
} }
} }
@ -95,4 +94,10 @@ void destroy_window(void) {
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_DestroyTexture(color_buffer_texture); SDL_DestroyTexture(color_buffer_texture);
SDL_Quit(); SDL_Quit();
} }
void draw_pixel(int x, int y, uint32_t color) {
if (x < window_width && y < window_height) {
color_buffer[(window_width * y) + x] = color;
}
}

View File

@ -1,5 +1,6 @@
#ifndef DISPLAY_H #ifndef DISPLAY_H
#define DISPLAY_H #define DISPLAY_H
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@ -13,10 +14,17 @@ extern int window_width;
extern int window_height; extern int window_height;
bool initialize_window(void); bool initialize_window(void);
void draw_grid(int size); 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 draw_rect(int x, int y, int width, int height, uint32_t color);
void clear_color_buffer(uint32_t color); void clear_color_buffer(uint32_t color);
void render_color_buffer(void); void render_color_buffer(void);
void destroy_window(void); void destroy_window(void);

View File

@ -2,37 +2,54 @@
#include <stdbool.h> #include <stdbool.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "display.h" #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; bool is_running = false;
void setup(void) { void setup(void) {
// Allocate the required memory in bytes to hold color buffer // Allocate the required memory in bytes to hold color buffer
color_buffer = (uint32_t *) malloc(sizeof(uint32_t) * window_width * window_height); color_buffer = (uint32_t *) malloc(sizeof(uint32_t) * window_width * window_height);
// Creating the SDL Texture that is uses to display the color buffer // Creating the SDL Texture that is uses to display the color buffer
color_buffer_texture = SDL_CreateTexture( color_buffer_texture = SDL_CreateTexture(
renderer, renderer,
SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, SDL_TEXTUREACCESS_STREAMING,
window_width, window_width,
window_height 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) { void process_input(void) {
SDL_Event event; SDL_Event event;
SDL_PollEvent(&event); SDL_PollEvent(&event);
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
is_running = false; is_running = false;
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) { if (event.key.keysym.sym == SDLK_ESCAPE) {
is_running = false; is_running = false;
} }
break; break;
} }
} }
@ -41,26 +58,27 @@ void update(void) {
void render(void) { void render(void) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
clear_color_buffer(0xFF000000); clear_color_buffer(0xFF000000);
draw_grid(10); draw_grid(10);
draw_rect(50, 50, 640, 480, 0xFFDD00DD); draw_rect(50, 50, 640, 480, 0xFFDD00DD);
render_color_buffer(); render_color_buffer();
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
int main(void) { int main(void) {
is_running = initialize_window(); is_running = initialize_window();
setup(); setup();
while (is_running) {
process_input(); while (is_running) {
update(); process_input();
render(); update();
} render();
destroy_window(); }
return 0; destroy_window();
return 0;
} }