SDL2.0学习笔记2---基于SDL的基本游戏架构

一、基本游戏架构

                                                              

二、示例

1. example01---基于基本游戏架构的example00:

/******************************************************************************
game strtucture:
		
		initialise
			|
			|
	----get input
	|		|
	|		|
	|	do physics
	|		|
	|		|
	 -----render
			|
			|
		   exit

base on game strture, modify example00
******************************************************************************/
SDL_Window	*g_pWindow = NULL;
SDL_Renderer*g_pRenderer = NULL;
bool g_bRunning = false;
Uint8 g_r = 0, g_g = 0, g_b = 0;

bool init(const char *title, int xpos, int ypos, int height, int width, int flags)
{
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)	return false;

	g_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);
	if (NULL == g_pWindow)	return false;

	g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);

	return true;

}

void render()
{
	SDL_SetRenderDrawColor(g_pRenderer, g_r, g_g, g_b, 255);

	SDL_RenderClear(g_pRenderer);

	SDL_RenderPresent(g_pRenderer);

}

void update()
{
	SDL_Delay(3000);
}

void handleEvents() 
{
	g_g += 30;
}
void clean()
{
	SDL_Quit();
}

void example01()
{
	if (false == init("example01:Setting up SDL", 
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
		640, 480, SDL_WINDOW_SHOWN))
	{
		return;
	}
	
	g_bRunning = true;

	while (g_bRunning)
	{
		handleEvents();
		update();
		render();
	}

	clean();

	return;
}

2. example02---C++封装的example01:

/******************************************************************************
	example02: c++ package of example01
******************************************************************************/
class Game
{
public:
	Game():m_pWindow(NULL),m_pRenderer(NULL), m_bRunning(false){};
	~Game(){};

	bool init(const char* title, int xpos, int ypos, int width, int height, int flags);
	void render();
	void update();
	void handleEvents();
	bool running() {return m_bRunning; }
	void clean();
private:
	SDL_Window		*m_pWindow;
	SDL_Renderer	*m_pRenderer;
	bool			m_bRunning;
};

bool Game::init(const char *title, int xpos, int ypos, int height, int width, int flags)
{
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)	return false;

	m_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);
	if (NULL == m_pWindow)	return false;

	m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
	
	SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255);

	m_bRunning = true;

	return true;

}

void Game::render()
{
	SDL_RenderClear(m_pRenderer);

	SDL_RenderPresent(m_pRenderer);
}

void Game::clean()
{
	SDL_DestroyWindow(m_pWindow);
	SDL_DestroyRenderer(m_pRenderer);
	SDL_Quit();
}

void Game::handleEvents()
{
	SDL_Event sdl_event;
	if (SDL_PollEvent(&sdl_event))
	{
		switch(sdl_event.type)
		{
		case SDL_QUIT:
			m_bRunning = false;
			break;
		default:
			break;
		}
	}
}

void Game::update()
{
}

void example02()
{
	Game game;
	
	game.init("example02: game structure", 200, 200, 640, 480, 0);

	while (game.running())
	{
		game.handleEvents();
		game.update();
		game.render();
	}

	game.clean();

}


你可能感兴趣的:(SDL2.0学习笔记2---基于SDL的基本游戏架构)