SDL渲染应用(1)
基本流程
案例一:使用SDL渲染一幅RGB图像数据
#include<iostream>
#include<SDL.h>
#pragma comment(lib,"SDL2.lib")
#undef main()
using namespace std;
int main(int argc, char* argv[])
{
int w = 800;
int h = 600;
if (SDL_Init(SDL_INIT_VIDEO))
{
cout << SDL_GetError() << endl;
return -1;
}
auto 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;
}
auto render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
if (!render)
{
cout << SDL_GetError() << endl;
return -1;
}
auto texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h);
if (!texture)
{
cout << SDL_GetError() << endl;
return -1;
}
shared_ptr<unsigned char> rgb(new unsigned char[w * h * 4]);
auto r = rgb.get();
for (int j = 0; j < h; j++)
{
int lineR = j * w * 4;
for (int i = 0; i < w * 4; i += 4)
{
r[lineR + i] = 255;
r[lineR + i + 1] = 0;
r[lineR + i + 2] = 0;
r[lineR + i + 3] = 0;
}
}
SDL_UpdateTexture(texture, NULL, r, w * 4);
SDL_RenderClear(render);
SDL_Rect destRect;
destRect.x = 0;
destRect.y = 0;
destRect.w = w;
destRect.h = h;
if (SDL_RenderCopy(render, texture, NULL, &destRect))
{
cout << SDL_GetError() << endl;
return -7;
}
SDL_RenderPresent(render);
getchar();
return 0;
}
案例二:模拟动态修改图像数据
动态修改是想模拟一下多张图片的渲染过程,这里面也就是通过SDL渲染多幅画面(画面是不同的),使画面发生变化。在渲染发生变化时,也就是图片中的过程循环进行,所以我们可以加一个循环,让渲染的这个过程一直进行。 那么新的问题就是,循环需要有一个跳出条件。这里面可以用sdl的事件来进行,增加了一个事件与退出事件的条件,设置一个超时时间。
这里的这个超时时间可以代表的视频中的一个帧率,间隔20ms,一秒是50帧,这里面以后的渲染操作也会占用时间,所以如果按照帧率的要求,这个时间不是直接就能写上去的,还要考虑一下。
#include<iostream>
#include<SDL.h>
#pragma comment(lib,"SDL2.lib")
#undef main()
using namespace std;
int main(int argc, char* argv[])
{
int w = 800;
int h = 600;
if (SDL_Init(SDL_INIT_VIDEO))
{
cout << SDL_GetError() << endl;
return -1;
}
auto 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;
}
auto render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
if (!render)
{
cout << SDL_GetError() << endl;
return -1;
}
auto texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h);
if (!texture)
{
cout << SDL_GetError() << endl;
return -1;
}
shared_ptr<unsigned char> rgb(new unsigned char[w * h * 4]);
auto r = rgb.get();
unsigned char tmp = 255;
for (;;)
{
SDL_Event ev;
SDL_WaitEventTimeout(&ev, 30);
if (ev.type == SDL_QUIT)
{
SDL_DestroyWindow(screen);
break;
}
tmp--;
for (int j = 0; j < h; j++)
{
int lineR = j * w * 4;
for (int i = 0; i < w * 4; i += 4)
{
r[lineR + i] = 0;
r[lineR + i + 1] = tmp;
r[lineR + i + 2] = 0;
r[lineR + i + 3] = 0;
}
}
SDL_UpdateTexture(texture, NULL, r, w * 4);
SDL_RenderClear(render);
SDL_Rect destRect;
destRect.x = 0;
destRect.y = 0;
destRect.w = w;
destRect.h = h;
if (SDL_RenderCopy(render, texture, NULL, &destRect))
{
cout << SDL_GetError() << endl;
return -7;
}
SDL_RenderPresent(render);
}
getchar();
return 0;
}
参考: https://www.cnblogs.com/yongdaimi/p/15554265.html https://blog.csdn.net/qq21497936/article/details/108602694 https://blog.csdn.net/fan1102958151/article/details/115866458
|