Created
September 6, 2024 20:35
-
-
Save xeekworx/b0cb4943d9037d0001253a1fe5b2f701 to your computer and use it in GitHub Desktop.
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 <SDL.h> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <algorithm> | |
#include <format> | |
int main(int argc, char* argv[]) | |
{ | |
std::string app_title = "SDL Box Example"; | |
int screen_width = 1024, screen_height = 768; | |
SDL_Window* main_window = NULL; | |
SDL_Renderer* renderer = NULL; | |
SDL_Event event = { 0 }; | |
bool should_quit = false; | |
SDL_FRect box_destination = { }; // Use SDL_FRect instead of SDL_Rect for subpixel rendering. | |
std::stringstream error; // Aid in creating error strings for output later. | |
try { | |
// INITIALIZATION: | |
// ------------------------------------------------------------------------------------------------------------ | |
// Initialize SDL and create a window and renderer | |
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) { | |
error << "Failed to initialize SDL: " << SDL_GetError(); | |
throw(error.str()); | |
} | |
if ((main_window = SDL_CreateWindow( | |
app_title.c_str(), | |
SDL_WINDOWPOS_CENTERED, // Use Centered Top Coordinate | |
SDL_WINDOWPOS_CENTERED, // Use Centered Left Coordinate | |
screen_width, screen_height, // Width & Height | |
0 // Flags (Window is implicitly shown if SDL_WINDOW_HIDDEN is not set) | |
)) == 0) { | |
error << "Failed to create a window: " << SDL_GetError(); | |
throw(error.str()); | |
} | |
// -1 to use the first available render driver. | |
// 0 to default to SDL_RENDERER_ACCELERATED (hardware), and fallback or fallback to software if necessary. | |
if ((renderer = SDL_CreateRenderer( | |
main_window, // The window to associate with this renderer, multiple windows & renderers are possible | |
-1, // Index of render driver, -1 for default | |
0 // Flags, 0 to default to SDL_RENDERER_ACCELERATED (hardware), and fallback or fallback to | |
// software if necessary. | |
)) == 0) { | |
error << "Failed to create the renderer: " << SDL_GetError(); | |
throw(error.str()); | |
} | |
// Comment this line out if you do not want VSync. VSync is when the rendering waits for the monitor to finish | |
// drawing the last frame so that you do not start drawing the current frame before it's done. Without VSync | |
// you could see just how fast your computer can render, but also it prevents seeing screen tearing (only | |
// partial frames being drawn). | |
SDL_RenderSetVSync(renderer, 1); // 1 for on, 0 for off. | |
// With this on you might expect a framerate of around 59 or 60 depending on your monitor. I have a gaming | |
// monitor with a refresh rate of 165, so I will see my fps as close to 165. | |
// CONFIGURE THE BOX / PLAYER, IT'S INITIAL LOCATION AND SIZE: | |
// ------------------------------------------------------------------------------------------------------------ | |
box_destination.w = 100.0f; | |
box_destination.h = 100.0f; | |
box_destination.x = screen_width / 2.F - box_destination.w / 2.F; // Center is half the screen width minus half the box width | |
box_destination.y = screen_height / 2.F - box_destination.h / 2.F; // Center is half the screen height minus half the box height | |
// PRIMARY & EVENT LOOP: | |
while (!should_quit) { | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
should_quit = 1; | |
break; | |
case SDL_KEYDOWN: | |
switch (event.key.keysym.sym) { | |
case SDLK_ESCAPE: | |
should_quit = 1; | |
break; | |
} | |
break; | |
} | |
} | |
// RENDERING: | |
// -------------------------------------------------------------------------------------------------------- | |
// Clear the background: | |
// Always start fresh and clear, you're never guaranteed to have a blank canvas or the previous rendered | |
// canvas here. | |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | |
SDL_RenderClear(renderer); | |
// Render the box: | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Black with an opacity of 255 (totally visible) | |
SDL_RenderFillRectF(renderer, &box_destination); | |
// Show on screen: | |
SDL_RenderPresent(renderer); | |
} | |
} | |
catch (std::string error_str) { | |
// This is a simple way to show a message box, if main_window failed to create this will still work | |
// since main_window will be NULL (the message box will just not have a parent): | |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, app_title.c_str(), error_str.c_str(), main_window); | |
// Output the error to the console, if you have one | |
std::cout << error_str << std::endl; | |
} | |
// CLEAN-UP & SHUTDOWN: | |
// ---------------------------------------------------------------------------------------------------------------- | |
if (renderer) SDL_DestroyRenderer(renderer); | |
if (main_window) SDL_DestroyWindow(main_window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment