其他分享
首页 > 其他分享> > SDL2.0渲染图像

SDL2.0渲染图像

作者:互联网

SDL封装了opengl和directx包含图像显示和音频播放
SDL的API文档
https://wiki.libsdl.org/
SDL渲染图像的步骤

1, 初始化接口

int SDLCALL SDL_Init(Uint32 flags);

2, 创建窗口

SDL_Window * SDLCALL SDL_CreateWindow(const char *title,
                                      int x, int y, int w,
                                      int h, Uint32 flags);

3,创建渲染器

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

4,定义出材质

SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer,
                                                        Uint32 format,
                                                        int access, int w,
                                                        int h);

5,图像数据写入显存中

int SDLCALL SDL_UpdateTexture(SDL_Texture * texture,
                                              const SDL_Rect * rect,
                                              const void *pixels, 
                                              int pitch);

6,渲染
渲染有三个步骤
//清理屏幕


int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);

//材质的数据复制到渲染器中

int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
                                           SDL_Texture * texture,
                                           const SDL_Rect * srcrect,
                                           const SDL_Rect * dstrect);

//具体渲染

void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);

示例:

#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
#pragma comment(lib, "SDL2.lib")
#undef main
int main(int argc, char* argv[])
{
	int w = 800;
	int h = 600;

	//初始化SDL video库
	if (SDL_Init(SDL_INIT_VIDEO)) {
		cout << SDL_GetError() << endl;
		return -1;
	}
	//生成SDL窗口 (画画需要一个画板)
	SDL_Window* screen = SDL_CreateWindow("test sdl ffmpeg",
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		w, h,
		SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
	);
	if (!screen) {
		cout << SDL_GetError() << endl;
		return -1;
	}
	//生成渲染器 (创建个画笔)
	SDL_Renderer* render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
	if (!render) {
		cout << SDL_GetError() << endl;
		return -2;
	}
	//生成材质 (确定要画一个什么类型画)
	SDL_Texture* texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_ARGB8888,
		SDL_TEXTUREACCESS_STREAMING,//可加锁
		w, h);
	if (!texture) {
		cout << SDL_GetError() << endl;
		return -4;
	}
	unsigned char tmp = 255;

	shared_ptr<unsigned char> rgb(new unsigned char[w * h * 4]);
	unsigned char* r = rgb.get();
	while (1) {
		//判断退出
		SDL_Event ev;
		SDL_WaitEventTimeout(&ev, 10);//SDL的事件发送
		if (ev.type == SDL_QUIT) {
			SDL_DestroyWindow(screen);
			break;
		}
		--tmp;
		for (int j = 0; j < h; ++j) {
			int b = j * w * 4;
			for (int i = 0; i < w * 4; i += 4) {
				r[b + i] = tmp;	   //B
				r[b + i + 1] = 0;  //G
				r[b + i + 2] = 0;  //R
				r[b + i + 3] = 0;  //A
			}
		}
		//清理屏幕 
		SDL_RenderClear(render);

		//将内存数据写入材质 (内存数据转成显存)
		SDL_UpdateTexture(texture, nullptr, r, w * 4);

		//复制材质到渲染 (这里可以进行图像的截取)
		SDL_Rect sdl_rect;
		sdl_rect.x = 0;
		sdl_rect.y = 0;
		sdl_rect.w = w;
		sdl_rect.h = h;
		SDL_RenderCopy(render, texture,
			nullptr,//原图位置和尺寸
			&sdl_rect//目标位置和尺寸
		);
		//具体的进行渲染 (开始作画)
		SDL_RenderPresent(render);

	}

	return 0;
}

标签:SDL2.0,渲染,int,SDLCALL,sdl,图像,SDL,rect
来源: https://blog.csdn.net/qq_40684669/article/details/121499631