Skip to content

Instantly share code, notes, and snippets.

@benjcal
Last active August 13, 2023 04:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjcal/60bbbaf297a4a3be5cb3464832365eea to your computer and use it in GitHub Desktop.
Save benjcal/60bbbaf297a4a3be5cb3464832365eea to your computer and use it in GitHub Desktop.
sdlsimple.h
#ifndef H_SDLSIMPLE
#define H_SDLSIMPLE
#include <SDL2/SDL.h>
#include <stdbool.h>
#include <stdio.h>
// defines
typedef struct sdlsimple_events_t {
bool quit;
// TODO: add more events
} sdlsimple_events_t;
typedef struct sdlsimple_context_t {
sdlsimple_events_t events;
SDL_Window *window;
SDL_Renderer *renderer;
} sdlsimple_context_t;
void sdlsimple_init(sdlsimple_context_t *ctx, int width, int height);
void sdlsimple_start(sdlsimple_context_t *ctx);
void sdlsimple_end(sdlsimple_context_t *ctx);
void sdlsimple_clear(sdlsimple_context_t *ctx, int r, int g, int b);
void sdlsimple_set_pixel(sdlsimple_context_t *ctx, int x, int y, int r, int g, int b);
bool sdlsimple_should_quit(sdlsimple_context_t *ctx);
#ifdef SDLSIMPLE_IMPLEMENTATION
void sdlsimple_init(sdlsimple_context_t *ctx, int width, int height) {
if (SDL_Init(SDL_INIT_VIDEO) < 0)
printf("Error: %s\n", SDL_GetError());
if (SDL_CreateWindowAndRenderer(width, height, SDL_WINDOW_SHOWN, &ctx->window, &ctx->renderer) < 0)
printf("Error: %s\n", SDL_GetError());
}
void sdlsimple_start(sdlsimple_context_t *ctx) {
SDL_Event e;
while(SDL_PollEvent(&e)) {
if(e.type == SDL_QUIT) {
ctx->events.quit = true;
} else {
ctx->events.quit = false;
}
}
}
void sdlsimple_end(sdlsimple_context_t *ctx) {
SDL_RenderPresent(ctx->renderer);
}
void sdlsimple_clear(sdlsimple_context_t *ctx, int r, int g, int b) {
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, 0);
SDL_RenderClear(ctx->renderer);
}
void sdlsimple_set_pixel(sdlsimple_context_t *ctx, int x, int y, int r, int g, int b) {
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, 0);
SDL_RenderDrawPoint(ctx->renderer, x, y);
}
bool sdlsimple_should_quit(sdlsimple_context_t *ctx) {
return ctx->events.quit;
}
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment