SDL扩展 SDL_image 支持多种格式图片加载:BMP GIF PNG TGA PCX … SDL_net 跨平台网络库 SDL_mixer audio mixer library. 支持MP3 MIDI OGG SDL_ttf 支持TrueType字体 SDL_rtf support the rendering of the Rich Text Format (RTF).
SDL_Init() 初始化标识
1 2 3 4 5 6 7
SDL_INIT_HAPTIC Force feedback subsystem 力反馈 SDL_INIT_AUDIO Audio subsystem SDL_INIT_VIDEO Video subsystem SDL_INIT_TIMER Timer subsystem SDL_INIT_JOYSTICK Joystick subsystem SDL_INIT_EVERYTHING All subsystems SDL_INIT_noparachute Don't catch fatal signals
查看一个子系统是否已被初始化:
1 2
if (SDL_WasInit(SDL_INIT_VIDEO) != 0) cout << "video was initialized";
1 2 3 4 5
SDL_CreateRenderer() SDL_RENDERER_SOFTWARE Use software rendering SDL_RENDERER_ACCELERATED Use hardware acceleration SDL_RENDERER_PRESENTVSYNC Synchronize renderer update with screen's refresh rate SDL_RENDERER_TARGETTEXTURE Supports render to texture
游戏程序结构 初始化 游戏循环:获取输入 物理运算 渲染 退出
window flags
1 2 3 4 5 6 7 8 9 10 11 12
SDL_WINDOW_FULLSCREEN Make the window fullscreen SDL_WINDOW_OPENGL Window can be used with as an OpenGL context SDL_WINDOW_SHOWN The window is visible SDL_WINDOW_HIDDEN Hide the window SDL_WINDOW_BORDERLESS No border on the window SDL_WINDOW_RESIZABLE Enable resizing of the window SDL_WINDOW_MINIMIZED Minimize the window SDL_WINDOW_MAXIMIZED Maximize the window SDL_WINDOW_INPUT_GRABBED Window has grabbed input focus SDL_WINDOW_INPUT_FOCUS Window has input focus SDL_WINDOW_MOUSE_FOCUS Window has mouse focus SDL_WINDOW_FOREIGN The window was not created using SDL
Fixed frames per second (FPS) is not necessarily always a good option, especially when your game includes more advanced physics. It is worth bearing this in mind when you move on from this book and start developing your own games. Fixed FPS will, however, be fine for the small 2D games, which we will work towards in this book.
SDL joystick event SDL_JoyAxisEvent Axis motion information SDL_JoyButtonEvent Button press and release information SDL_JoyBallEvent Trackball event motion information SDL_JoyHatEvent Joystick hat position change
SDL joystick event Type value SDL_JoyAxisEvent SDL_JOYAXISMOTION SDL_JoyButtonEvent SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP SDL_JoyBallEvent SDL_JOYBALLMOTION SDL_JoyHatEvent SDL_JOYHATMOTION
不同的游戏控制器 按钮和轴可能有不同的值 比如Xbox360 controller, PS3 controller Xbox360 controller: Two analog sticks Analog sticks press as buttons Start and Select buttons Four face buttons: A, B, X, and Y Four triggers: two digital and two analog A digital directional pad
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) SDL_InitSubSystem(SDL_INIT_JOYSTICK); if (SDL_NumJoysticks() > 0) { for (int i=0; i<SDL_NumJoysticks(); ++i) { SDL_Joystick* joy = SDL_JoystickOpen(i); if (SDL_JoystickOpened(i) == 1) m_joysticks.push_back(joy); } SDL_JoystickEventState(SDL_ENABLE); }
SDL_JoystickClose(joy);
分辨是哪个控制器的事件
1 2
if (event.type == SDL_JOYAXISMOTION) int whichOne = event.jaxis.which;
SDL Mouse Event Purpose SDL_MouseButtonEvent A button on the mouse has been pressed or released SDL_MouseMotionEvent The mouse has been moved SDL_MouseWheelEvent The mouse wheel has moved
SDL Mouse Event Type Value SDL_MouseButtonEvent SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP SDL_MouseMotionEvent SDL_MOUSEMOTION SDL_MouseWheelEvent SDL_MOUSEWHEEL
1 2 3 4 5
// 鼠标按钮 // SDL numbers these as 0 for left, 1 for middle, and 2 for right.
if (event.type == SDL_MOUSEBUTTONDOWN) if (event.button.button == SDL_BUTTON_LEFT)