This commit is contained in:
Tyrel Souza 2024-04-12 00:27:14 -04:00
commit ace4f8c9e3
5 changed files with 217 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

22
CMakeLists.txt Normal file
View File

@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.28)
project(renderer C)
find_package(SDL2 REQUIRED)
set(CMAKE_C_STANDARD 99)
# Include SDL2 headers
include_directories(${SDL2_INCLUDE_DIRS})
# Add all source files
file(GLOB SOURCES "src/*.c")
add_executable(renderer ${SOURCES})
# Link SDL2 library to the executable
target_link_libraries(renderer PRIVATE ${SDL2_LIBRARIES})
# Set compiler options
target_compile_options(renderer PRIVATE -Wall -g) # -g adds debugging information
# Set optimization level (optional)
# target_compile_options(renderer PRIVATE -O3) # Example: set optimization level to maximum

98
src/display.c Normal file
View File

@ -0,0 +1,98 @@
#include "display.h"
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
uint32_t *color_buffer = NULL;
SDL_Texture *color_buffer_texture = NULL;
int window_width = 800;
int window_height = 600;
bool initialize_window(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "Error initializing SDL\n.");
return false;
}
// Use SDL to query what thef fullscreen max. w/h
SDL_DisplayMode display_mode;
SDL_GetCurrentDisplayMode(0, &display_mode);
window_width = display_mode.w;
window_height = display_mode.h;
// Create SDL Window
window = SDL_CreateWindow(
NULL,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
window_width,
window_height,
SDL_WINDOW_BORDERLESS
);
if (!window) {
fprintf(stderr, "Error creating SDL Window\n.");
return false;
}
// Create SDL Renderer
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
fprintf(stderr, "Error creating SDL Renderer\n.");
return false;
}
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
return true;
}
void clear_color_buffer(uint32_t color) {
if (color_buffer == NULL) {
return;
}
for (int y = 0; y < window_height; y++) {
for (int x = 0; x < window_width; x++) {
color_buffer[(window_width * y) + x] = color;
}
}
}
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 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;
}
}
}
}
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 j = x; j < width; j++) {
int cur_x = x+j;
int cur_y = y+i;
color_buffer[(window_width * cur_y) + cur_x] = color;
}
}
}
void destroy_window(void) {
free(color_buffer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_DestroyTexture(color_buffer_texture);
SDL_Quit();
}

23
src/display.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
extern SDL_Window *window;
extern SDL_Renderer *renderer;
extern uint32_t *color_buffer;
extern SDL_Texture *color_buffer_texture;
extern int window_width;
extern int window_height;
bool initialize_window(void);
void draw_grid(int size);
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);
#endif //DISPLAY_H

66
src/main.c Normal file
View File

@ -0,0 +1,66 @@
#include <stdint.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "display.h"
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);
// 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
);
}
void process_input(void) {
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;
}
}
void update(void) {
}
void render(void) {
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();
SDL_RenderPresent(renderer);
}
int main(void) {
is_running = initialize_window();
setup();
while (is_running) {
process_input();
update();
render();
}
destroy_window();
return 0;
}