这篇对SDL是如何响应键盘的做一个简单介介绍。
首先我们定义一个枚举,默认起始位为0,也可以显式的制定整数值。尽量养成使用符号常量的习惯,不要直接使用数字,例如if(option == 1),当我们有大量这种判断时,我们回头再来排查会相当麻烦。
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
跟之前一样,我们配置好头文件,lib文件和lib依赖项之后,我们在代码中首先定义一些变量
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//从指定路径加载图片surface
SDL_Surface* loadSurface(std::string path);
//SDL窗口
SDL_Window* gWindow = nullptr;
//SDL窗口surface
SDL_Surface* gScreenSurface = nullptr;
//用来存储不同图片的surface, 这里的KEY_PRESS_SURFACE_TOTAL = 5
SDL_Surface* gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL];
//当前显示的surface
SDL_Surface* gCurrentSurface = nullptr;
这里我们声明了一个函数loadSurface用来加载指定位置的图片,这样封装下可以有效降低代码量,也可以是程序看起来更清晰
SDL_Surface* loadSurface(std::string path)
{
//加载图片
SDL_Surface* loadSurface = SDL_LoadBMP(path.c_str());
if (loadSurface == nullptr)
printf("unable to load image %s!SDL_ERROR:%s\n", path.c_str(), SDL_GetError());
return loadSurface;
}
接下来我们修改loadMedia函数,我们加载5张不同的图片,后面用来响应键盘事件
bool loadMedia()
{
//loading success flag
bool success = true;
//load default surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("../picture/press.bmp");
if(gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = loadSurface("../picture/up.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = loadSurface("../picture/down.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = loadSurface("../picture/left.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = loadSurface("../picture/right.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
return success;
}
接下来我们在主函数中响应键盘上的方向键来呈现不同图片
在while循环前先赋值一个显示的surface,而后在SDL事件循环中判断是否触发了按键事件,如果是,我们根据按键类型呈现不同的图片
SDL_Event e;
bool quit = false;
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
while (quit == false)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
quit = true;
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
break;
case SDLK_DOWN:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
break;
case SDLK_LEFT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
break;
case SDLK_RIGHT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
break;
default:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
break;
}
}
}
//这里我们先把背景色填充下,不然背景会不断叠加
SDL_FillRect(gScreenSurface, nullptr, SDL_MapRGB(gScreenSurface->format, 0xff, 0xfd, 0xdd));
//拷贝到窗口的surface
SDL_BlitSurface(gCurrentSurface, nullptr, gScreenSurface, nullptr);
//更新窗口
SDL_UpdateWindowSurface(gWindow);
}
最后关闭程序前,别忘了释放内存空间
for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i)
{
SDL_FreeSurface(gKeyPressSurfaces[i]);
gKeyPressSurfaces[i] = NULL;
}
SDL_FreeSurface(gScreenSurface);
gScreenSurface = nullptr;
以下是运行效果
完整代码如下
#include
#include
//key press surface constants
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL,
};
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Surface* loadSurface(std::string path);
SDL_Window* gWindow = nullptr;
SDL_Surface* gScreenSurface = nullptr;
SDL_Surface* gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL];
SDL_Surface* gCurrentSurface = nullptr;
SDL_Surface* loadSurface(std::string path)
{
//load image at secified path
SDL_Surface* loadSurface = SDL_LoadBMP(path.c_str());
if (loadSurface == nullptr)
printf("unable to load image %s!SDL_ERROR:%s\n", path.c_str(), SDL_GetError());
return loadSurface;
}
bool init()
{
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize!SDL Error:%s\n", SDL_GetError());
success = false;
}
else
{
//create window
gWindow = SDL_CreateWindow("deal key press", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == nullptr)
{
printf("window could not created!SDL_Error:%s\n", SDL_GetError());
success = false;
}
else
{
//get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia()
{
//loading success flag
bool success = true;
//load default surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("../picture/press.bmp");
if(gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = loadSurface("../picture/up.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = loadSurface("../picture/down.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = loadSurface("../picture/left.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = loadSurface("../picture/right.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == nullptr)
{
printf("unable to load image!SDL_ERROR%s\n", SDL_GetError());
success = false;
}
return success;
}
void close()
{
for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i)
{
SDL_FreeSurface(gKeyPressSurfaces[i]);
gKeyPressSurfaces[i] = NULL;
}
SDL_FreeSurface(gScreenSurface);
gScreenSurface = nullptr;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}
int main(int argc, char* args[])
{
if (!init())
{
printf("failed to initialize!\n");
}
else
{
//load media
if (!loadMedia())
{
printf("failed to load media!\n");
}
else
{
//SDL_BlitSurface(gHelloWorld, nullptr, gScreenSurface, nullptr);
//update the surface
//SDL_UpdateWindowSurface(gWindow);
SDL_Event e;
bool quit = false;
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
while (quit == false)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
quit = true;
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
break;
case SDLK_DOWN:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
break;
case SDLK_LEFT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
break;
case SDLK_RIGHT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
break;
/*default:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
break;*/
}
}
}
SDL_FillRect(gScreenSurface, nullptr, SDL_MapRGB(gScreenSurface->format, 0xff, 0xfd, 0xdd));
//apply the current image
SDL_BlitSurface(gCurrentSurface, nullptr, gScreenSurface, nullptr);
//update the surface
SDL_UpdateWindowSurface(gWindow);
}
}
}
close();
return 0;
}