Basic SDL2 2D C++ Application (c++1y)
This file contains 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
#include <iostream> | |
#include <SDL2/SDL.h> | |
constexpr int SCREEN_WIDTH = 800; | |
constexpr int SCREEN_HEIGHT = 600; | |
int main(int argc, char *argv[]) { | |
if (SDL_Init( SDL_INIT_VIDEO) < 0) { | |
std::cerr << "There was an error initializing SDL2: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
SDL_Window* displayWindow = SDL_CreateWindow("Basic SDL2 2D application", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); | |
if (displayWindow == nullptr) { | |
std::cerr << "There was an error creating the window: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
SDL_Renderer* renderer = SDL_CreateRenderer(displayWindow, -1, SDL_RendererFlags::SDL_RENDERER_ACCELERATED); | |
if (renderer == nullptr) { | |
std::cerr << "There was an error creating the renderer: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
// draw sample stuff | |
SDL_RenderClear(renderer); | |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); | |
SDL_Rect rect { 100, 100, SCREEN_WIDTH - 200, SCREEN_HEIGHT - 200 }; | |
SDL_RenderFillRect(renderer, &rect); | |
SDL_RenderPresent(renderer); | |
// sleep for 5 seconds | |
SDL_Delay(5000); | |
// Clean up | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(displayWindow); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment