作者:龍飛
3.1:一些小的修改
我覺得寫C++的程序,一是看起來確實比較C++一點,二是相對于C的“精煉”,C++要的是“健壯”。所以,其實我不太滿意用C風格字符串作為ScreenSurface的成員數據,所以做了修改。這也是為了在程序中構建ScreenSurface對象的時候可以使用string。
class ScreenSurface
{
private:
//
std::string windowName;
public:
//
ScreenSurface(int w, int h, const std::string& window_name = "NULL", int b = 0, Uint32 f = 0);
};
相應的,我們修改了2個構造函數。
ScreenSurface::ScreenSurface():
width(640), height(480), bpp(32), flags(0), windowName("NULL")
{
if ( screenNum > 0 )
throw ErrorInfo("DONOT create more than ONE screen!");
if ( SDL_Init(SDL_INIT_VIDEO < 0 ) )
throw ErrorInfo(SDL_GetError());
pScreen = SDL_SetVideoMode(width, height, bpp, flags);
screenNum++;
}
ScreenSurface::ScreenSurface(int w, int h, const std::string& window_name, int b, Uint32 f):
width(w), height(h), bpp(b), flags(f)
{
if ( screenNum > 0 )
throw ErrorInfo("DONOT create more than ONE screen!");
if ( SDL_Init(SDL_INIT_VIDEO < 0 ) )
throw ErrorInfo(SDL_GetError());
pScreen = SDL_SetVideoMode(width, height, bpp, flags);
screenNum++;
if ( window_name != "NULL" ) {
windowName = window_name;
SDL_WM_SetCaption(windowName.c_str(), 0);
}
else
windowName = "NULL";
}
第二個地方,我修改了TextSurface構造函數的參數順序,并且將默認的字體改為Windows都自帶的“新羅馬時代”字體times.ttf。我將字體參數放在最后,將字體大小參數提前了,這樣更符合習慣上的使用規律。
class TextSurface: public DisplaySurface
{
public:
TextSurface(const std::string& msg_name, const std::string& message, const ScreenSurface& screen,
Uint8 r = 0xFF, Uint8 g = 0xFF, Uint8 b = 0xFF,
int ttf_size = 28, const std::string& ttf_fileName = "times.ttf");
~TextSurface();
};
(在DisplaySurface里相應的構造函數也做類似的修改,略)
3.2:回顧SDL事件輪詢
SDL_PollEvent()的作用,是事件一旦被觸發,就會響應一次,注意它的響應并不是連續不斷的。比如你按下某個鍵,即觸發了一次事件。即使你按著不松開,也僅僅是觸發了一次,所以SDL_PollEvent()也只響應一次。
下面的程序,演示鍵盤事件中,方向鍵被按下后的反饋信息。
3.3:演示程序
//UVi Soft (2008)
//Long Fei (lf426), E-mail: zbln426@163.com
#include "SurfaceClass.h"
int game(int argc, char* argv[]);
int main(int argc ,char* argv[])
{
int mainRtn = 0;
try {
mainRtn = game(argc, argv);
}
catch ( const ErrorInfo& info ) {
info.show();
return -1;
}
return mainRtn;
}
int game(int argc ,char* argv[])
{
//Create a SDL screen.
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const std::string WINDOW_NAME = "Key Presses";
ScreenSurface screen(SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_NAME);
//Fill background.(default is black)
screen.fillColor();
screen.flip();
//Load a textSurface
TextSurface upMessage("upMsg", "Up was pressed.", screen);
TextSurface downMessage("downMsg", "Down was pressed.", screen, 0xFF, 0, 0);
TextSurface leftMessage("leftMsg", "Left was pressed.", screen, 0, 0xFF, 0);
TextSurface rightMessage("rightMsg", "Right was pressed.", screen, 0, 0, 0xFF);
TextSurface otherMessage("otherMsg", "Other key was pressed.", screen, 100, 100, 100, 35);
//Main loop.Press ESC or click X to quit.
bool gameOver = false;
SDL_Event gameEvent;
int x = 200;
int y = 200;
while( gameOver == false ){
while ( SDL_PollEvent(&gameEvent) != 0 ){
if ( gameEvent.type == SDL_KEYDOWN ){
screen.fillColor();
switch ( gameEvent.key.keysym.sym ){
case SDLK_UP:
upMessage.blit(x, y--);
break;
case SDLK_DOWN:
downMessage.blit(x, y++);
break;
case SDLK_LEFT:
leftMessage.blit(x--, y);
break;
case SDLK_RIGHT:
rightMessage.blit(x++, y);
break;
default:
otherMessage.blit(x, y);
}
screen.flip();
}
if ( gameEvent.type == SDL_QUIT ){
gameOver = true;
}
if ( gameEvent.type == SDL_KEYUP ){
if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
gameOver = true;
}
}
}
}
return 0;
}
posted on 2008-03-25 11:56
lf426 閱讀(1846)
評論(0) 編輯 收藏 引用 所屬分類:
SDL入門教程