『SDL』SDL_Window、SDL_Renderer、SDL_Texture、SDL_Surface简单程序

支持多种普片格式的加载,可选用SDL_Image库(依赖于SDL和一些其他第三方)。


#include "SDL.h"

int _tmain(int argc, _TCHAR* argv[])
{
	SDL_Window *myWindow = NULL;
	SDL_Renderer *myRenderer = NULL;
	SDL_Texture *myTexture = NULL, *hisTexture = NULL;
	SDL_Surface* picture;
	int nRenderDrivers;

	if  (SDL_Init(SDL_INIT_VIDEO) < 0) 
	{
		exit(1);
	}
	atexit(SDL_Quit);

	//Load image 
	picture = SDL_LoadBMP("qqtest.bmp"); 

	myWindow = SDL_CreateWindow("testSdl", 240, 240, picture->w, picture->h, SDL_WINDOW_RESIZABLE|SDL_WINDOW_SHOWN);

	//获取当前可用画图驱动 window中有3个,第一个为d3d,第二个为opengl,第三个为software
	nRenderDrivers = SDL_GetNumRenderDrivers();
	//创建渲染器,第二个参数为选用的画图驱动,0代表d3d
	myRenderer = SDL_CreateRenderer(myWindow, 0, SDL_RENDERER_ACCELERATED);

	SDL_RendererInfo info;
	SDL_GetRendererInfo(myRenderer, &info);

	SDL_GetRenderDriverInfo(0, &info);    //d3d
	SDL_GetRenderDriverInfo(1, &info);    //opgl
	SDL_GetRenderDriverInfo(2, &info);    //software

	//创建纹理
	myTexture = SDL_CreateTexture(myRenderer, /*SDL_PIXELFORMAT_RGB24*/picture->format->format, SDL_TEXTUREACCESS_STREAMING, picture->w, picture->h);

	//计算yuv一行数据占的字节数
	//int iPitch = picture->w * picture->format->BitsPerPixel / 8;//SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_RGB24);    

	{
#if 1
		int i = SDL_UpdateTexture(myTexture, NULL, picture->pixels, picture->pitch);
		SDL_RenderClear(myRenderer);
		SDL_RenderCopy(myRenderer, myTexture, NULL, NULL);
#else
		hisTexture = SDL_CreateTextureFromSurface(myRenderer, picture);
		SDL_FreeSurface(picture);
		//SDL_UpdateTexture(myTexture, &sdlRT, picture->pixels, iPitch);
		SDL_RenderClear(myRenderer);
		SDL_RenderCopy(myRenderer, myTexture, NULL, NULL);
#endif
		SDL_RenderPresent(myRenderer);

		SDL_Delay(4000);
	}
	
	SDL_DestroyTexture(myTexture);
	SDL_DestroyRenderer(myRenderer);
	SDL_DestroyWindow(myWindow);

	return 0;
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值