一、创建流程
1.SDL初始化
flags可以填:
SDL_INIT_TIMER:定时器
SDL_INIT_AUDIO:音频
SDL_INIT_VIDEO:视频
SDL_INIT_JOYSTICK:摇杆
SDL_INIT_HAPTIC:触摸屏
SDL_INIT_GAMECONTROLLER:游戏控制器
SDL_INIT_EVENTS:事件
SDL_INIT_EVERYTHING:包含上述所有选项
SDL_INIT_NOPARACHUTE:不捕获关键信号
extern DECLSPEC int SDLCALL SDL_Init(Uint32 flags);
2.创建窗口
title : 窗口名字
x : 在显示屏上的位置
y : 在显示屏上的位置
w : 窗口宽度
h : 窗口高度
flags :窗口的属性 可以填:
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.生成渲染器
window:第二步生成的窗口
index :-1
flags:渲染时使用硬件还是软件
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.生成材质
renderer : 第三步的渲染器
format: 材质的像素格式
access:是否可以加锁
w: 宽
h: 高
extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer,
Uint32 format,
int access, int w,
int h);
5.内存数据写入材质
texture:第四步材质
rect:
pixels:内存数据
pitch:一行多长按字节算
extern DECLSPEC int SDLCALL SDL_UpdateTexture(SDL_Texture * texture,
const SDL_Rect * rect,
const void *pixels, int pitch);
6.清理屏幕
extern DECLSPEC int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);
7.复制材质到渲染
renderer : 渲染
texture :材质
srcrect : 原图位置和尺寸
dstrect : 目标位置和尺寸
extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
SDL_Texture * texture,
const SDL_Rect * srcrect,
const SDL_Rect * dstrect);
8.渲染
extern DECLSPEC void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);
二、完整代码
#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;
/* 初始化sdl */
if (SDL_Init(SDL_INIT_VIDEO))
{
cout << SDL_GetError() << endl;
return -1;
}
/**
para 1 窗口名字
para 2 窗口在界面中的位置
para 3 窗口在界面中的位置
para 4 窗口自身宽
para 5 窗口自身高
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;
}
/* 生成渲染器 */
/**
para 1 渲染的窗口
para 2
para 3 使用硬件渲染还是软件渲染
*/
auto render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
if (!render)
{
cout << SDL_GetError() << endl;
return -1;
}
/* 生成材质 */
auto texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, //可以加锁
width, height);
if (!texture)
{
cout << SDL_GetError() << endl;
return -1;
}
/* 存放图像数据 */
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
}
}
/* 内存数据写入材质 */
SDL_UpdateTexture(texture, NULL, r, width * 4);
/* 清理屏幕 */
SDL_RenderClear(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, //原图位置和尺寸
&sdl_rect); //目标位置和尺寸
/* 具体渲染 */
SDL_RenderPresent(render);
}
getchar();
return 0;
}