SDL创建视频窗口图像

该文详细介绍了使用SimpleDirectMediaLayer(SDL)库创建图形界面的过程,包括初始化SDL,创建窗口,生成渲染器,制作材质,更新和显示图像等关键步骤,是游戏开发或图形应用的基础教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建流程

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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值