#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); }