Skip to content

Instantly share code, notes, and snippets.

@alepez
Created September 21, 2022 13:54
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 alepez/bc7350cc9a53b6661744fd0e35ef6d7a to your computer and use it in GitHub Desktop.
Save alepez/bc7350cc9a53b6661744fd0e35ef6d7a to your computer and use it in GitHub Desktop.
SDL Example C++
/// Simple DirectMedia Layer Example
/// If everything is correctly installed, you should see a black window
/// which is gradually filled with white, starting from the top.
#include <SDL2/SDL.h>
int main() {
auto sdl_init_result = SDL_Init(SDL_INIT_VIDEO);
if (sdl_init_result != 0) {
return 1;
}
int width = 640;
int height = 480;
auto win = SDL_CreateWindow("Example", //
SDL_WINDOWPOS_UNDEFINED, //
SDL_WINDOWPOS_UNDEFINED, //
width, //
height, //
SDL_WINDOW_SHOWN //
);
if (win == nullptr) {
return 2;
}
auto surface = SDL_GetWindowSurface(win);
auto pixels = static_cast<int*>(surface->pixels);
/// Start from the top
int line = 0;
bool running = true;
while (running) {
/// Check if quit event is triggered
{
SDL_Delay(40);
SDL_Event event;
if (SDL_PollEvent(&event) == 1) {
if (event.type == SDL_QUIT) {
running = false;
}
}
}
/// Draw the line
for (int y = 0; y < width; ++y) {
pixels[line * width + y] = 0x00FFFFFF;
}
line += 1;
/// Exit when full
if (line == height) {
running = false;
}
SDL_UpdateWindowSurface(win);
}
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment