Skip to content

Instantly share code, notes, and snippets.

@sorin-ref
Last active April 23, 2022 09:37
Show Gist options
  • Save sorin-ref/eb203ff67e3105a571955985c3cbbedc to your computer and use it in GitHub Desktop.
Save sorin-ref/eb203ff67e3105a571955985c3cbbedc to your computer and use it in GitHub Desktop.
#include <SDL.h>
int main()
{
SDL_Init(SDL_INIT_VIDEO);
const int w = 800, h = 600;
SDL_Window *window = SDL_CreateWindow("Drawing-sample", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, 0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Event event;
const int x0 = 0, y0 = h; // on macOS, the window's top-left position is (0, h)
const int step = 1, delay = 1;
int quit = 0, x = x0;
while (!quit)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // white
SDL_RenderClear(renderer); // background
SDL_SetRenderDrawColor(renderer, 64, 64, 64, 255); // dark gray
SDL_RenderDrawLine(renderer, x0, y0, x0 + w, y0 + h); // oblique line from top-left to bottom-right
SDL_SetRenderDrawColor(renderer, 255, 128, 64, 255); // orange
SDL_RenderDrawLine(renderer, x0 + x, y0, x0 + x, y0 + h); // vertical line at current coordinate x
SDL_RenderPresent(renderer);
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = 1;
break;
case SDL_MOUSEBUTTONDOWN:
x += step;
if (x >= x0 + w) // when we reach right
x = x0; // restart from left
break;
}
SDL_Delay(delay);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment