【简介】学习在Code::Blocks 13.12中,使用SDL2.0库的方法。基于g++的C++11选项
原书下载地址:http://it-ebooks.info/book/2960/
SDL2.0官方地址:http://www.libsdl.org/
第一章:程序的基本架构
main.cpp的内容
#include "Game.h"
Game* g_game = 0; //全局实例
//这里演示了基本框架结构
int main(int argc, char* argv[])
{
g_game = new Game(); //Game类对象实例
g_game->init("Charpter 1", 100, 100, 640, 480, 0); //初始化
//标题:Charpter 1,
//x,y位置:100, 100
//窗口宽高:640, 480
//窗口标志:0
//标志建议使用:SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL
while(g_game->running()) //开始主循环
{
g_game->handleEvents(); //处理输入
g_game->update(); //计算时间和碰撞
g_game->render(); //渲染到屏幕
}
g_game->clean(); //清理资源,退出
return 0;
}
Game.h的内容
#ifndef __Game__
#define __Game__
#include <SDL.h>
class Game
{
public:
Game();
~Game();
bool init(const char* title, int xpos, int ypos, int width, int height, int flags); //初始化
void render();
void update();
void handleEvents();
void clean();
bool running() { return m_bRunning; } //返回运行状态
private:
SDL_Window* m_pWindow; //窗口资源指针
SDL_Renderer* m_pRenderer; //渲染器资源指针
bool m_bRunning;
};
#endif // __Game__
Game.cpp的内容
#include <iostream>
//注:对于在VS 2012 EXPRESS的IDE中使用std::cout,
//需要包含Windows.h,
//然后调用AllocConsole(); 和freopen("CON", "w", stdout);
//将上述内容添加到main.cpp中
#include "Game.h"
Game::Game()
{
}
Game::~Game()
{
}
bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)
{
if(SDL_Init(SDL_INIT_EVERYTHING) == 0) //SDL_Init成功。参数也可写为SDL_INIT_VIDEO | SDL_INIT_TIMER
{
std::cout << "SDL init success\n";
//调用SDL的创建窗口函数,返回窗口资源指针
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if(m_pWindow !=0) //窗口创建成功
{
std::cout << "window creation success\n";
//创建渲染器资源
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer != 0) //渲染器创建成功
{
std::cout << "renderer creation success\n";
//设置当前渲染色,rgba,红绿蓝透明四通道
SDL_SetRenderDrawColor(m_pRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
}else {
std::cout << "renderer init fail\n";
return false; //渲染器失败
}
}else {
std::cout << "window init fail\n";
return false; //窗口创建失败
}
}else{
std::cout << "SDL init fail\n";
return false; //SDL库初始失败
}
std::cout << "init success\n";
m_bRunning = true; //所有初始都成功,可以开始主循环了
return true;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer); //清屏
//绘制对象
SDL_RenderPresent(m_pRenderer); //双缓冲翻转,新内容显示到屏幕上
}
void Game::update()
{
}
void Game::handleEvents()
{
SDL_Event event; //这个变量放这里合适否?每次循环都会生成这个局部变量
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
m_bRunning = false; //结束循环
break;
default:
break;
}
} //事件处理在第4章详细介绍
}
void Game::clean()
{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow); //销毁资源,可以加上判断
//if(m_pWindow) { SDL_DestroyWindow(m_pWindow); }
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
运行结果:
最后:关于全屏
//修改Game.h中的init()函数原型
bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
//修改Game.cpp中的init()函数实现
bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
flags = 0;
if(fullscreen)
flags = SDL_WINDOW_FULLSCREEN;
//上述代码放在SDL_CreateWindow()之前
...
//main.cpp中的调用代码,改为
if(g_game->init("Chapter 1", 100, 100, 640, 480, true));
//true传入,指示全屏创建窗口