SDL2教程(一):加载第一张图片hello world


SDL2实现加载图片(编译环境VS2017)


一、SDL2新方法

#define SDL_MAIN_HANDLED
#include "SDL.h"
#include<iostream>

using namespace std;

const int Window_WIDTH = 640;
const int Window_HEIGHT = 480;

SDL_Window* window = NULL;
SDL_Renderer* render = NULL;
SDL_Texture* tex = NULL;
添加头文件SDL.h 加了一句
#define SDL_MAIN_HANDLED

以为SDL 要用自己的main函数需要重定义SDL_main()

声明窗口的宽和高,声明一个窗口,渲染器,纹理

声明之后初始化为NULL(C11为NULLptr) 这是一个好的习惯

初始化窗口,渲染器

bool Init()
{
	window = SDL_CreateWindow("my first SDL",
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Window_WIDTH, Window_HEIGHT,
		SDL_WINDOW_SHOWN);
	if (window == NULL)
	{
		cout << "creat window error\n";
		return false;
	}
	render = SDL_CreateRenderer(window, -1, 0);
	if (render == NULL)
	{
		cout << "creat render error\n";
		return false;
	}
	return true;
}

如果为NULL则初始化失败,返回false

SDL_CreatWindow:第一个参数是窗口名字,第二三是窗口的坐标(SDL_winpos_undefined 为采用系统默认)

后面两个是宽和高,最后一个是window Flag,这里采用SDL_window_shown,即时呈现窗口

SDL_CreatRenderer:第一个参数为渲染的窗口,第二个是采用的驱动(默认-1为自动选择),最后一个为渲染标志

这里默认采取0


加载图片即为上面的hello world

bool LOAD_Image()
{
	SDL_Surface* hello = NULL;
	hello = SDL_LoadBMP("hello.bmp");
	if (hello == NULL)
	{
		cout << "load bmp error\n";
		return false;
	}
	tex = SDL_CreateTextureFromSurface(render, hello);
	SDL_FreeSurface(hello);
	if (tex == NULL)
	{
		cout << "creat surface tex error\n";
		return false;
	}
	return true;
}

SDL_surface可以理解为一个存放图片的变量

SDL_loadbmp 加载路径图片,这里采用相对路径,失败则返回NULL

tex = SDL_CreateTextureFromSurface(render, hello) 用hello初始化tex纹理,第一个参数为要加入到的渲染器

SDL_FreeSurface(hello);后面用不到surface hello了所以释放掉它,这是一个好习惯


放置图片

bool Put_Image()
{
	SDL_RenderCopy(render, tex, NULL, NULL);
	SDL_RenderPresent(render);
	SDL_Delay(1000);
	return true;
}

SDL_RenderCopy(render, tex, NULL, NULL);将纹理贴到渲染器上,后面两个参数是贴的范围,默认NULL为全部

SDL_RenderPresent(render);将渲染器的内容呈现到窗口

SDL_Delay(1000);延迟1秒


退出

void Quit()
{
	SDL_DestroyWindow(window);
	SDL_DestroyRenderer(render);
	SDL_DestroyTexture(tex);
	SDL_Quit();
}
正如表面意思这里就不解释了

全部代码为:

#define SDL_MAIN_HANDLED
#include "SDL.h"
#include<iostream>

using namespace std;

const int Window_WIDTH = 640;
const int Window_HEIGHT = 480;

SDL_Window* window = NULL;
SDL_Renderer* render = NULL;
SDL_Texture* tex = NULL;

bool Init()
{
	window = SDL_CreateWindow("my first SDL",
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Window_WIDTH, Window_HEIGHT,
		SDL_WINDOW_SHOWN);
	if (window == NULL)
	{
		cout << "creat window error\n";
		return false;
	}
	render = SDL_CreateRenderer(window, -1, 0);
	if (render == NULL)
	{
		cout << "creat render error\n";
		return false;
	}
	return true;
}

bool LOAD_Image()
{
	SDL_Surface* hello = NULL;
	hello = SDL_LoadBMP("hello.bmp");
	if (hello == NULL)
	{
		cout << "load bmp error\n";
		return false;
	}
	tex = SDL_CreateTextureFromSurface(render, hello);
	SDL_FreeSurface(hello);
	if (tex == NULL)
	{
		cout << "creat surface tex error\n";
		return false;
	}
	return true;
}

bool Put_Image()
{
	SDL_RenderCopy(render, tex, NULL, NULL);
	SDL_RenderPresent(render);
	SDL_Delay(1000);
	return true;
}

void Quit()
{
	SDL_DestroyWindow(window);
	SDL_DestroyRenderer(render);
	SDL_DestroyTexture(tex);
	SDL_Quit();
}

int main(int argc, char *args[])
{
	if (Init())
	{
		if(LOAD_Image()) Put_Image();
		Quit();
	}
	system("pause");
	return 0;
}

二、继承SDL1.2的老方法

#define SDL_MAIN_HANDLED
#include <SDL.h>
#include<iostream>

using namespace std;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char* argv[])
{
	SDL_Window* window = NULL;

	//The surface contained by the window
	SDL_Surface* screenSurface = NULL;
	SDL_Surface* hello = NULL;
	//Initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
	}
	else
	{
		cout << "successfully\n";
		window = SDL_CreateWindow("my SDL",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
		if (window == NULL) cout << "window error\n";
		else
		{
			cout << "window success\n";
			screenSurface = SDL_GetWindowSurface(window);
			hello = SDL_LoadBMP("hello.bmp");
			if (hello == NULL) cout << "load error\n";
			SDL_BlitSurface(hello, NULL,screenSurface , NULL);
			SDL_UpdateWindowSurface(window);
			SDL_Delay(2000);
			SDL_DestroyWindow(window);
			SDL_Quit();
		}
	}

	system("pause");
	return 0;
}

这里通过SDL_blitsurface将hello加到screensurface上然后将加载到后台缓冲区

最后通过updateWindowssurface更新前台缓冲区,呈现我们的图片

注:

笔者也是SDL在学者,错误之处望指正

维基帮助文档(用于函数查询)http://wiki.libsdl.org/FrontPage

猜你喜欢

转载自blog.csdn.net/qq_40953281/article/details/80024780