Updated source, now the program replaces the red color with a yellow color through texture in SDL
This commit is contained in:
31
src/main.c
31
src/main.c
@@ -4,10 +4,14 @@
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
bool is_running = false;
|
||||
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Renderer* renderer = NULL;
|
||||
|
||||
// Also known as frame buffer in other books, pointer to an array of color_buffer
|
||||
uint32_t* color_buffer = NULL;
|
||||
SDL_Texture* color_buffer_texture = NULL;
|
||||
|
||||
int window_width = 800;
|
||||
int window_height = 600;
|
||||
|
||||
@@ -17,6 +21,7 @@ bool initialize_window(void) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// Create a SDL window
|
||||
window = SDL_CreateWindow(
|
||||
NULL,
|
||||
@@ -42,9 +47,17 @@ bool initialize_window(void) {
|
||||
}
|
||||
|
||||
void setup(void){
|
||||
|
||||
// Allocates the memory required for color_buffer, using sizeof to allocate the # of bytes required
|
||||
// Allocates the memory in bytes required to hold for color_buffer, using sizeof to allocate the # of bytes required
|
||||
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){
|
||||
@@ -65,6 +78,16 @@ void update(void){
|
||||
//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){
|
||||
for (int y = 0; y < window_height; y++){
|
||||
for (int x = 0; x < window_width; x++){
|
||||
@@ -76,7 +99,9 @@ void clear_color_buffer(uint32_t color){
|
||||
void render(void){
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
|
||||
render_color_buffer();
|
||||
|
||||
clear_color_buffer(0xFFFFFF00);
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
Reference in New Issue
Block a user