为了熟悉SDL , 最近在写一些小的Demo , SDL的事件功能只是它的功能之一,而键盘事件是属于事件功能其中之一,还有鼠标事件、应用程序的可见度改变等等事件。
博主的环境是 wingw32 + eclipse CDT , 前面本来不想装wing32,想直接用cywin凑合着用,但是当把环境搭建好之后才发现,SDL中的头文件和cywin中的某个头文件中的某个宏冲突了,定义了相同的宏,所以会造成编译错误,最后没办法才安装了wing32.
先看看工程的目录结构:
src/include 代表头文件信息 , Debug下面的stdout.txt是wing32中编译器执行工程后的输出信息。当然,你也可以设置在当前eclipse的控制台输出,请参照http://blog.sina.com.cn/s/blog_5607cf010100vq2d.html.
SDL_init.h:
/* * SDL_init.h * * Created on: 2013-9-28 * Author: Christian */ #ifndef SDL_INIT_H_ #define SDL_INIT_H_ #include <iostream> #include <SDL/SDL.h> void createSDL(const char* title); void destorySDL(void); #endif /* SDL_INIT_H_ */
里面封装了两个函数,用于SDL的初始化和销毁,createSDL中的title指针参数代表当前窗口的名字。下面是两个函数的实现。
SDL_init.cpp:
/* * SDL_init.cpp * * Created on: 2013-9-28 * Author: Christian */ #include "include/SDL_init.h" void createSDL(const char* title) { if (SDL_WasInit(SDL_INIT_EVERYTHING) != 0) { std::cout << "SDL is already running.\n"; return; } if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { std::cout << "SDL_Init Failed:" << SDL_GetError() << std::endl; SDL_ClearError(); return; } //设置窗口 caption if (title != NULL) { SDL_WM_SetCaption(title, NULL); } std::cout << "SDL is running successfully.\n"; return; } void destorySDL(void) { if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) { std::cout << "SDL was NOT running!\n"; return; } SDL_Quit(); std::cout << "SDL_Quit successfully.\n"; return; }
这样初始化环境就搞定了,用起来会十分方便。
下面是主要代码,用于记录案件信息
SDL_demo.cpp:
//============================================================================ // Name : SDL_demo.cpp // Author : Christian // Version : // Copyright : Copyright (c) Christian 2012/9/28 // Description : SDL Demo in C++ //============================================================================ #include "include/SDL_init.h" using namespace std; //读取按键的值 void readKeyboard() { SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); //create a new event SDL_Event event; Uint8 type; SDLKey history; SDLKey current; do { SDL_PollEvent(&event); type = event.type; switch (type) { case SDL_KEYDOWN: case SDL_KEYUP: current = event.key.keysym.sym; if(history != current){ //print key name std::cout << current << "\n"; history = current; } break; default: break; } } while (type != SDL_QUIT); } //entry int main(int argc, char* args[]) { createSDL("Christian Test"); readKeyboard(); atexit(destorySDL); return EXIT_SUCCESS; }
运行效果:
黑色屏幕对吧,看看标题,是咱们设置的Christian test.
在运行期间我按下了christian这些个字母,下面看看输出信息:
红色框里的代码按下的键盘码,在SDL的头文件SDL_keysym.h中有定义。
typedef enum { /** @name ASCII mapped keysyms * The keyboard syms have been cleverly chosen to map to ASCII */ /*@{*/ SDLK_UNKNOWN = 0, SDLK_FIRST = 0, SDLK_BACKSPACE = 8, SDLK_TAB = 9, SDLK_CLEAR = 12, SDLK_RETURN = 13, SDLK_PAUSE = 19, SDLK_ESCAPE = 27, SDLK_SPACE = 32, SDLK_EXCLAIM = 33, SDLK_QUOTEDBL = 34, SDLK_HASH = 35, SDLK_DOLLAR = 36, SDLK_AMPERSAND = 38, SDLK_QUOTE = 39, SDLK_LEFTPAREN = 40, SDLK_RIGHTPAREN = 41, SDLK_ASTERISK = 42, SDLK_PLUS = 43, SDLK_COMMA = 44, SDLK_MINUS = 45, SDLK_PERIOD = 46, SDLK_SLASH = 47, SDLK_0 = 48, SDLK_1 = 49, SDLK_2 = 50, SDLK_3 = 51, SDLK_4 = 52, SDLK_5 = 53, SDLK_6 = 54, SDLK_7 = 55, SDLK_8 = 56, SDLK_9 = 57, SDLK_COLON = 58, SDLK_SEMICOLON = 59, SDLK_LESS = 60, SDLK_EQUALS = 61, SDLK_GREATER = 62, SDLK_QUESTION = 63, SDLK_AT = 64, /* Skip uppercase letters */ SDLK_LEFTBRACKET = 91, SDLK_BACKSLASH = 92, SDLK_RIGHTBRACKET = 93, SDLK_CARET = 94, SDLK_UNDERSCORE = 95, SDLK_BACKQUOTE = 96, SDLK_a = 97, SDLK_b = 98, SDLK_c = 99, SDLK_d = 100, SDLK_e = 101, SDLK_f = 102, SDLK_g = 103, SDLK_h = 104, SDLK_i = 105, SDLK_j = 106, SDLK_k = 107, SDLK_l = 108, SDLK_m = 109, SDLK_n = 110, SDLK_o = 111, SDLK_p = 112, SDLK_q = 113, SDLK_r = 114, SDLK_s = 115, SDLK_t = 116, SDLK_u = 117, SDLK_v = 118, SDLK_w = 119, SDLK_x = 120, SDLK_y = 121, SDLK_z = 122, SDLK_DELETE = 127, /* End of ASCII mapped keysyms */ /*@}*/ ................................................ SDLK_LAST } SDLKey;
看到了吧,SDLKey这个枚举,就是SDL为我们定义的键盘码,每个码对应相应的按键。
很简单对吧,我也这样觉得,另外本人用SDL加载图片的时候会出现黑色闪屏,有高手能帮忙解决一下这个问题的可以和我联系一下。
这里是[email protected],欢迎交流。
本人原创作品,转载请标明出处。