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);
}
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);
}
}
}
@ -96,3 +95,9 @@ void destroy_window(void) {
SDL_DestroyTexture(color_buffer_texture);
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
#define DISPLAY_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
@ -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);

View File

@ -2,37 +2,54 @@
#include <stdbool.h>
#include <SDL2/SDL.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;
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;
}