Skip to content

Instantly share code, notes, and snippets.

@wareya
Last active December 17, 2016 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wareya/8c270cc142ba99f7e14aec1066d79766 to your computer and use it in GitHub Desktop.
Save wareya/8c270cc142ba99f7e14aec1066d79766 to your computer and use it in GitHub Desktop.
Minimal low-CPU desktop style SDL2 mainloop with drawing, with mouse use
#include <SDL2/SDL.h>
#undef main
#include <stdio.h>
#include <stdint.h>
SDL_Rect d = {0, 0, 800, 600};
int mouse_x = 0;
int mouse_y = 0;
int main()
{
SDL_Window * window;
SDL_Renderer * renderer;
SDL_CreateWindowAndRenderer(d.w, d.h, SDL_WINDOW_RESIZABLE, &window, &renderer);
bool damage = true;
for(;;)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
exit(0);
if (event.type == SDL_WINDOWEVENT)
{
damage = true;
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
d.w = event.window.data1, d.h = event.window.data2;
}
if (event.type == SDL_MOUSEMOTION or event.type == SDL_MOUSEBUTTONDOWN or event.type == SDL_MOUSEBUTTONUP or event.type == SDL_MOUSEWHEEL)
SDL_GetMouseState(&mouse_x, &mouse_y), damage = true;
}
if(damage)
{
damage = false;
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(renderer, &d);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawLine(renderer, 0, 0, d.w, d.h);
SDL_RenderDrawLine(renderer, mouse_x-5, mouse_y-5, mouse_x+5, mouse_y+5);
SDL_RenderDrawLine(renderer, mouse_x-5, mouse_y+5, mouse_x+5, mouse_y-5);
SDL_RenderPresent(renderer);
}
else
SDL_Delay(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment