Last active
April 21, 2021 02:22
-
-
Save Michal-Atlas/a4a7b859337a485793ae03dcdaa5119c to your computer and use it in GitHub Desktop.
Minimal SDL Runnable program, with Comments
This file contains hidden or 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
cmake_minimum_required(VERSION 3.16) | |
project(MyFirstSDL) | |
set(CMAKE_CXX_STANDARD 14) | |
add_executable(MyFirstSDL main.cpp) | |
target_link_libraries(MyFirstSDL SDL2) |
This file contains hidden or 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 <SDL2/SDL.h> | |
#define NAME "Window" | |
#define SCREEN_WIDTH 400 | |
#define SCREEN_HEIGHT 400 | |
int main() { | |
SDL_Window *window = SDL_CreateWindow( | |
NAME, | |
SDL_WINDOWPOS_UNDEFINED, | |
SDL_WINDOWPOS_UNDEFINED, | |
SCREEN_WIDTH, SCREEN_HEIGHT, | |
SDL_WINDOW_OPENGL); // No need to touch; This Initializes and creates your Window | |
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | |
// No need to touch; This gives you a Renderer, which you will pass to render functions so they know how and where to draw, in terms of the program Window | |
for (;;) { | |
// Let's Draw a simple pixel | |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Here I define an RGBA color to draw with, this is set in the renderer, so the following function calls will use it, instead of you having to define a color each time; | |
SDL_RenderDrawPoint(renderer, 40, 50); // Draws a single Pixel at coordinates [40, 50], based on the previously set color; | |
// Remember, all coordinates are measured, from the TOP-Left corner; | |
// Let's Draw a Rectangle, using an object | |
SDL_Rect rect; // This is an object, which you can easily pass to a function to draw an entire rectangle | |
rect.x = 100; // Top left corner X coordinate | |
rect.y = 100; // Top left corner Y coordinate | |
rect.w = 20; // Width | |
rect.h = 20; // Height | |
SDL_RenderFillRect(renderer, &rect); // Now we pass the rect and get it drawn to the screen | |
SDL_RenderPresent(renderer); // Up until now, nothing happened, because all functions render to a buffer; | |
// This tells SDL to write whatever is in the buffer to the screen; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment