Skip to content

Instantly share code, notes, and snippets.

@JL2210
Created September 22, 2024 23:01
Show Gist options
  • Save JL2210/4cad6fd8db6cbd13d5a6583cffe465ae to your computer and use it in GitHub Desktop.
Save JL2210/4cad6fd8db6cbd13d5a6583cffe465ae to your computer and use it in GitHub Desktop.
sdl2 test app by @miguelmartin75 with SDL_Renderer instead of OpenGL (and in C)
#include <stdio.h>
#include <SDL.h>
#include <SDL_render.h>
int main(void)
{
// Initialize SDL with video
SDL_Init(SDL_INIT_VIDEO);
// Create a window and a renderer
SDL_Window* window;
SDL_Renderer *renderer;
SDL_CreateWindowAndRenderer(640, 480, 0, &window, &renderer);
// if failed to create a window
if(!window)
{
// we'll print an error message and exit
fputs("Error failed to create window!\n", stderr);
return 1;
}
// if we failed to create a renderer
if(!renderer)
{
// we'll print out an error message and exit
fputs("Error failed to create a renderer!\n", stderr);
return 2;
}
SDL_Event event; // used to store any events from the OS
int running = 1; // used to determine if we're running the game
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
while(running)
{
// poll for events from SDL
while(SDL_PollEvent(&event))
{
// determine if the user still wants to have the window open
// (this basically checks if the user has pressed 'X')
running = event.type != SDL_QUIT;
}
// buffer is invalidated every frame, clear it
SDL_RenderClear(renderer);
// update the screen
SDL_RenderPresent(renderer);
}
// Destroy the renderer
SDL_DestroyRenderer(renderer);
// Destroy the window
SDL_DestroyWindow(window);
// And quit SDL
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment