From a005ded219b18f105098b977c6119f59d9e9c757 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Tue, 15 Aug 2023 23:10:37 +0100 Subject: [PATCH] Set up initial rendering --- compile_commands.json | 4 +- main.c | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 main.c diff --git a/compile_commands.json b/compile_commands.json index 5814d59..951c132 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -77,11 +77,11 @@ "-x", "c", "-o", - "/tmp/main-b9c948.o", + "/tmp/main-7469c3.o", "main.c" ], "directory": "/home/abdelrahman/Sources/programming/starfield", "file": "/home/abdelrahman/Sources/programming/starfield/main.c", - "output": "/tmp/main-b9c948.o" + "output": "/tmp/main-7469c3.o" } ] diff --git a/main.c b/main.c new file mode 100644 index 0000000..2cdb33b --- /dev/null +++ b/main.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 + +#define BG_COLOR 0x060616ff + +void render_clear(SDL_Surface *surface, uint32_t color); +void fill_rect(SDL_Surface *surface, uint32_t x, uint32_t y, uint32_t w, + uint32_t h, uint32_t color); + +int main(void) { + SDL_Init(SDL_INIT_EVERYTHING); + + SDL_Window *window = SDL_CreateWindow("Starfield", SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, + WINDOW_HEIGHT, SDL_WINDOW_SHOWN); + + SDL_Surface *surface = SDL_GetWindowSurface(window); + + SDL_Surface *canvas = SDL_CreateRGBSurfaceWithFormat( + 0, surface->w, surface->h, 32, SDL_PIXELFORMAT_ABGR32); + + bool running = true; + + SDL_Event event = {0}; + + while (running) { + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + running = false; + break; + } + } + + render_clear(canvas, BG_COLOR); + + SDL_BlitSurface(canvas, NULL, surface, NULL); + + SDL_UpdateWindowSurface(window); + } + + SDL_DestroyWindow(window); + + SDL_Quit(); + + return EXIT_SUCCESS; +} + +void render_clear(SDL_Surface *surface, uint32_t color) { + uint32_t length = surface->w * surface->h; + + SDL_LockSurface(surface); + + uint32_t *pixels = (uint32_t *)(surface->pixels); + + for (uint32_t i = 0; i < length; ++i) { + pixels[i] = color; + } + + SDL_UnlockSurface(surface); +} + +void fill_rect(SDL_Surface *surface, uint32_t x, uint32_t y, uint32_t w, + uint32_t h, uint32_t color) { + SDL_LockSurface(surface); + + uint32_t *pixels = (uint32_t *)(surface->pixels); + + for (uint32_t row = 0; row < h; ++row) { + for (uint32_t col = 0; col < w; ++col) { + uint32_t i = (y + row) * surface->w + (x + col); + + pixels[i] = color; + } + } + + SDL_UnlockSurface(surface); +}