From ace4f8c9e3023aa15755bb648928c74180b9b538 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Fri, 12 Apr 2024 00:27:14 -0400 Subject: [PATCH] Initial --- .idea/.gitignore | 8 ++++ CMakeLists.txt | 22 +++++++++++ src/display.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ src/display.h | 23 ++++++++++++ src/main.c | 66 ++++++++++++++++++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 CMakeLists.txt create mode 100644 src/display.c create mode 100644 src/display.h create mode 100644 src/main.c diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..743c15e --- /dev/null +++ b/CMakeLists.txt @@ -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 diff --git a/src/display.c b/src/display.c new file mode 100644 index 0000000..948cb18 --- /dev/null +++ b/src/display.c @@ -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(); +} \ No newline at end of file diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..7bddb5d --- /dev/null +++ b/src/display.h @@ -0,0 +1,23 @@ +#ifndef DISPLAY_H +#define DISPLAY_H +#include +#include +#include +#include + +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 diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..e51e88f --- /dev/null +++ b/src/main.c @@ -0,0 +1,66 @@ +#include +#include +#include +#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; +}