Render a window
How to operate
SDL is implemented in C, so if you need extern “C” in C++ then introduce a header file
#include <SDL.h>
Copy the code
Create window render
Extern "C" {#include "SDL. H "} int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("SDL2 Window", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS); SDL_Renderer* render = SDL_CreateRenderer(window, -1, 0); SDL_SetRenderDrawColor(render, 255, 255, 0, 255); SDL_RenderClear(render); SDL_RenderPresent(render); SDL_Delay(5000); // Run end destroy SDL_DestroyWindow(window); SDL_Quit(); return 0; }Copy the code
The above code can be run directly to see the effect
API brief Introduction
Create and destroy Windows
/ / create SDL_Window * window = SDL_CreateWindow (" window "SDL2, 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS); // Run end destroy SDL_DestroyWindow(window);Copy the code
Create renderer
SDL_Renderer* render = SDL_CreateRenderer(window, -1, 0); We can render a yellow background in the window SDL_SetRenderDrawColor(render, 255, 255, 0, 255); SDL_RenderClear(render); SDL_RenderPresent(render); SDL_Delay(3000); If (renderer) {SDL_DestroyRenderer(renderer); }Copy the code
Simple use of SDL
The above is the simplest use of SDL, drawing a yellow window on the screen
We can also draw basic graphics and the API is very simple