SDL2 游戏开发日记(一)

SDL2 游戏开发日记(一)

SDL的基础教程请戳这里

创建游戏窗口

开发工具:VC++ 2013

新建项目 --> Win32 控制台应用程序

窗口类:

使用单例模式

窗口标题如果要显示中文,要把字符串转成UTF8编码

#pragma once
#include 
#include 
#include 
#include 
using namespace std;
#define theGame SDLGame::Instance()
#define DEFAULT_FPS 60
class SDLGame{
private:	
	int mFrameTime = 1000 / DEFAULT_FPS;
	SDL_Window *mWindow = NULL;	
	SDL_Renderer *mRenderer = NULL;
	SDL_Event mEvent;
	bool IsRunning;
	SDLGame(){
		IsRunning = true;
	}
	SDLGame(const SDLGame &){}
	void operator=(const SDLGame &){}
public:	
	static SDLGame &Instance(){
		static SDLGame instance;
		return instance;
	}	
	~SDLGame(){}
	void setFPS(int fps){
		mFrameTime = 1000 / fps;
	}
	bool Init(string title,int x,int y,int width,int height){
		do{
			if (SDL_Init(SDL_INIT_EVERYTHING)){
				printf("SDL_Init error:%s\n", SDL_GetError());
				break;
			}
			if (TTF_Init()){
				printf("TTF_Init error:%s\n", SDL_GetError());
				break;
			}
			int imageFlags = IMG_INIT_PNG;
			if (!(IMG_Init(imageFlags) & imageFlags)){				
				printf("IMG_Init error:%s\n",SDL_GetError());
				break;
			}
			mWindow = SDL_CreateWindow(title.c_str(), x, y, width, height, SDL_WINDOW_SHOWN);
			if (mWindow == NULL){
				printf("SDL_CreateWindow error:%s\n",SDL_GetError());
				break;
			}
			mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
			if (mRenderer == NULL){
				printf("SDL_CreateRenderer error:%s\n", SDL_GetError());
				break;
			}
			IsRunning = true;
			return true;
		} while (0);
		IsRunning = false;
		return false;
	}
	SDL_Renderer *getRenderer(){
		return mRenderer;
	}
	void Run(){
		while (IsRunning)
		{
			int start = SDL_GetTicks();
			while (SDL_PollEvent(&mEvent)){
				if (mEvent.type == SDL_QUIT){
					IsRunning = false;
				}
			}
			SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
			SDL_RenderClear(mRenderer);
			//
			//绘图
			//
			SDL_RenderPresent(mRenderer);
			int remaining_time = mFrameTime - (SDL_GetTicks() - start);
			if (remaining_time > 0){
				SDL_Delay(remaining_time);
			}
		}
		if (mRenderer != NULL)
			SDL_DestroyRenderer(mRenderer);
		if (mWindow != NULL)
			SDL_DestroyWindow(mWindow);		
		IMG_Quit();
		TTF_Quit();
		SDL_Quit();
	}
	void Destroy(){
		IsRunning = false;
	}
};

字符串转UTF8函数:

//
std::string wcharToUTF8(wchar_t *str){
	std::string strOutUTF8;
	int n = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
	char * str2 = new char[n];
	WideCharToMultiByte(CP_UTF8, 0, str, -1, str2, n, NULL, NULL);
	strOutUTF8 = str2;
	delete[]str2;
	str2 = NULL;
	return strOutUTF8;
}
//
std::string charToUTF8(char *str)
{
	std::string strUTF8 = "";
	WCHAR * str1;
	int n = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
	str1 = new WCHAR[n];
	MultiByteToWideChar(CP_ACP, 0, str, -1, str1, n);
	strUTF8 = wcharToUTF8(str1);
	delete[]str1;
	str1 = NULL;	
	return strUTF8;
}

int _tmain(int argc, _TCHAR* argv[])
{
	std::string title = charToUTF8("中文标题");
	theGame.Init(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT);
	theGame.Run();
	return 0;
}

隐藏控制台黑窗口

1、代码中加入:

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

2、主函数 _tmain 改为 main

3、在SDL_main.h中 108 - 110 行注释掉main的宏定义。

三步缺一不可,否则报错:

错误	1	error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用	

你可能感兴趣的:(SDL2,编程,SDL2,游戏开发)