Updated source, now the program replaces the red color with a yellow color through texture in SDL

This commit is contained in:
2025-02-23 23:06:51 -06:00
parent 493a609bb2
commit af4623a787
2 changed files with 28 additions and 3 deletions

BIN
renderer

Binary file not shown.

View File

@@ -4,10 +4,14 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
bool is_running = false; bool is_running = false;
SDL_Window* window = NULL; SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL; SDL_Renderer* renderer = NULL;
// Also known as frame buffer in other books, pointer to an array of color_buffer // Also known as frame buffer in other books, pointer to an array of color_buffer
uint32_t* color_buffer = NULL; uint32_t* color_buffer = NULL;
SDL_Texture* color_buffer_texture = NULL;
int window_width = 800; int window_width = 800;
int window_height = 600; int window_height = 600;
@@ -17,6 +21,7 @@ bool initialize_window(void) {
return false; return false;
} }
// Create a SDL window // Create a SDL window
window = SDL_CreateWindow( window = SDL_CreateWindow(
NULL, NULL,
@@ -42,9 +47,17 @@ bool initialize_window(void) {
} }
void setup(void){ void setup(void){
// Allocates the memory in bytes required to hold for color_buffer, using sizeof to allocate the # of bytes required
// Allocates the memory required for color_buffer, using sizeof to allocate the # of bytes required
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 a SDL texture that is used to display the color buffer
color_buffer_texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
window_width,
window_height
);
} }
void process_input(void){ void process_input(void){
@@ -65,6 +78,16 @@ void update(void){
//TODO //TODO
} }
void render_color_buffer(void){
SDL_UpdateTexture(
color_buffer_texture,
NULL,
color_buffer,
(int)(window_width * sizeof(uint32_t))
);
SDL_RenderCopy(renderer, color_buffer_texture, NULL, NULL);
}
void clear_color_buffer(uint32_t color){ void clear_color_buffer(uint32_t color){
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++){
@@ -76,7 +99,9 @@ void clear_color_buffer(uint32_t color){
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);
render_color_buffer();
clear_color_buffer(0xFFFFFF00); clear_color_buffer(0xFFFFFF00);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);