Last active
August 13, 2023 04:23
-
-
Save benjcal/60bbbaf297a4a3be5cb3464832365eea to your computer and use it in GitHub Desktop.
sdlsimple.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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