SDL rendering video files

Related functions

  1. include header files
#include<SDL.h>
  1. related functions

Initialize SDL

//Initialize SDL
SDL_Init(SDL_INIT_VIDEO);

//Exit SDL
SDL_Quit();

Two ways to create a window

//Create a window, specify some properties of the window, and return the window handle
SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_SHOWN|SDL_WINDOW_BORDER);

//Create a window from an existing window
SDL_CreateWindowForm(hwnd);

So far the window has been created but will not be displayed. After creating the window, you need to put the window into the renderer, and the renderer outputs it to the rendering driver layer.
Creating a window is to generate a window in the memory, and the display needs to be pushed to the graphics card; the graphics card projects it onto the screen through the driver.

Need to create renderer

SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags);

//Clear the renderer to prevent the last residual data from being output together and affecting the screen display.
int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);

//Push the data to the corresponding engine, the engine copies the data to the driver, and the driver projects the data to the screen
void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);

Set color to renderer

//Set the color used for drawing operations
int SDLCALL SDL_SetRenderDrawColor(SDL_Renderer * renderer,
                                           Uint8 r, Uint8 g, Uint8 b,
                                           Uint8 a);

Basic principles of SDL events

  1. SDL puts all events in a queue
  2. SDL event types
//Window events
SDL_WindowEvent

//Keyboard events
SDL_KeyboardEvent

//mouse event
SDL_MouseMotionEvent

//custom event
  1. SDL event handling
  • SDL_PollEvent polling processing, processed every once in a while
  • SDL_WaitEvent waits for event processing, processing in time without taking up too much CPU
//Define an event
SDL_Event event;
SDL_WaitEvent (& amp;event);

Texture rendering

  1. basic concept
  • Texture: describes the basic information of the image (take a line drawn as an example, the line contains multiple rgb/yuv data points, and the texture contains the starting and ending positions of the line and the gradient process of the color)
  • Advantages of using textures:
    The amount of data is small and it occupies less main memory;
    By passing the texture data to the graphics card, the graphics card hardware can be used to accelerate data calculation and rendering.
  1. SDL rendering basic process

The memory image contains all the information of the image. The renderer extracts the description information of the image to form a texture, and sends the texture to the graphics card for rendering and display.


















Renderer







exchange










memory image









texture









Window display







  1. Texture related functions
//Create texture, int access means that the texture type is divided into general and streaming, and the video is streaming
SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer,
                                                        Uint32 format,
                                                        int access, int w,
                                                        int h);