1. Create process
1. SDL initialization
Flags can be filled in:
SDL_INIT_TIMER: timer
SDL_INIT_AUDIO: audio
SDL_INIT_VIDEO: video
SDL_INIT_JOYSTICK: Joystick
SDL_INIT_HAPTIC: touch screen
SDL_INIT_GAMECONTROLLER: game controller
SDL_INIT_EVENTS: Events
SDL_INIT_EVERYTHING: contains all the above options
SDL_INIT_NOPARACHUTE: do not catch key signals
extern DECLSPEC int SDLCALL SDL_Init(Uint32 flags);
2. Create window
title : window name
x : position on the display
y : position on the display
w : window width
h : window height
flags: Attributes of the window can be filled in:
SDL_WINDOW_FULLSCREEN = 0x00000001, /**< fullscreen window */ SDL_WINDOW_OPENGL = 0x00000002, /**< window usable with OpenGL context */ SDL_WINDOW_SHOWN = 0x00000004, /**< window is visible */ SDL_WINDOW_HIDDEN = 0x00000008, /**< window is not visible */ SDL_WINDOW_BORDERLESS = 0x00000010, /**< no window decoration */ SDL_WINDOW_RESIZABLE = 0x00000020, /**< window can be resized */ SDL_WINDOW_MINIMIZED = 0x00000040, /**< window is minimized */ SDL_WINDOW_MAXIMIZED = 0x00000080, /**< window is maximized */ SDL_WINDOW_INPUT_GRABBED = 0x00000100, /**< window has grabbed input focus */ SDL_WINDOW_INPUT_FOCUS = 0x00000200, /**< window has input focus */ SDL_WINDOW_MOUSE_FOCUS = 0x00000400, /**< window has mouse focus */ SDL_WINDOW_FULLSCREEN_DESKTOP = ( SDL_WINDOW_FULLSCREEN | 0x00001000 ), SDL_WINDOW_FOREIGN = 0x00000800, /**< window not created by SDL */ SDL_WINDOW_ALLOW_HIGHDPI = 0x00002000, /**< window should be created in high-DPI mode if supported. On macOS NSHighResolutionCapable must be set true in the application's Info.plist for this to have any effect. */ SDL_WINDOW_MOUSE_CAPTURE = 0x00004000, /**< window has mouse captured (unrelated to INPUT_GRABBED) */ SDL_WINDOW_ALWAYS_ON_TOP = 0x00008000, /**< window should always be above others */ SDL_WINDOW_SKIP_TASKBAR = 0x00010000, /**< window should not be added to the taskbar */ SDL_WINDOW_UTILITY = 0x00020000, /**< window should be treated as a utility window */ SDL_WINDOW_TOOLTIP = 0x00040000, /**< window should be treated as a tooltip */ SDL_WINDOW_POPUP_MENU = 0x00080000, /**< window should be treated as a popup menu */ SDL_WINDOW_VULKAN = 0x10000000 /**< window usable for Vulkan surface */
extern DECLSPEC SDL_Window * SDLCALL SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags);
3. Generate a renderer
window: the window generated in the second step
index: -1
flags: use hardware or software when rendering
SDL_RENDERER_SOFTWARE = 0x00000001, /**<the renderer is a software fallback */ SDL_RENDERER_ACCELERATED = 0x00000002, /**<the renderer uses hardware acceleration */ SDL_RENDERER_PRESENTVSYNC = 0x00000004, /**< Present is synchronized with the refresh rate */ SDL_RENDERER_TARGETTEXTURE = 0x00000008 /**<the renderer supports rendering to texture */
extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags);
4. Generate material
renderer : the renderer of the third step
format: the pixel format of the material
access: whether it can be locked
w: width
h: high
extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h);
5. Memory data write material
texture: the fourth step material
rect:
pixels: memory data
pitch: the length of a line is calculated in bytes
extern DECLSPEC int SDLCALL SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect, const void *pixels, int pitch);
6. Clean up the screen
extern DECLSPEC int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);
7. Copy material to render
renderer: rendering
texture: material
srcrect : original image position and size
dstrect : target position and size
extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect);
8. Rendering
extern DECLSPEC void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);
2. Complete code
#include <iostream> #include <sdl/SDL.h> using namespace std; #pragma comment(lib, "SDL2.lib") #undef main int sdl_flush_rgb_windows(int argc, char* argv[]) {<!-- --> int width = 800; int height = 900; /* Initialize sdl */ if (SDL_Init(SDL_INIT_VIDEO)) {<!-- --> cout << SDL_GetError() << endl; return -1; } /** para 1 window name para 2 The position of the window in the interface para 3 The position of the window in the interface para 4 window itself width para 5 window height What operations are allowed by para 6? */ auto screen = SDL_CreateWindow("SDL_Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); if (!screen) {<!-- --> cout << SDL_GetError() << endl; return -1; } /* generate renderer */ /** para 1 the window to render para 2 para 3 use hardware rendering or software rendering */ auto render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED); if (!render) {<!-- --> cout << SDL_GetError() << endl; return -1; } /* generate material */ auto texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, //can be locked width, height); if (!texture) {<!-- --> cout << SDL_GetError() << endl; return -1; } /* store image data */ shared_ptr<unsigned char> rgb(new unsigned char[width * height * 4]); auto r = rgb. get(); unsigned char tmp = 255; while (1) {<!-- --> SDL_Event ev; SDL_WaitEventTimeout( & ev, 10); if (ev.type == SDL_QUIT) {<!-- --> SDL_DestroyWindow(screen); break; } tmp--; for (int i = 0; i < height; i ++ ) {<!-- --> int b = i * width * 4; for (int j = 0; j < width * 4; j + = 4) {<!-- --> r[b + j] = 255; // B r[b + j + 1] = tmp; // G r[b + j + 2] = 255; // R r[b + j + 3] = 255; // A } } /* Memory data write material */ SDL_UpdateTexture(texture, NULL, r, width * 4); /* clear the screen */ SDL_RenderClear(render); /* copy material to render */ SDL_Rect sdl_rect; sdl_rect.x = 0; sdl_rect.y = 0; sdl_rect.w = width; sdl_rect.h = height; SDL_RenderCopy(render, texture, NULL, //Original image position and size & amp;sdl_rect); //target position and size /* Specific rendering */ SDL_RenderPresent(render); } getchar(); return 0; }