作者:龙飞 准备一张640*480的bmp位图(back.bmp)作为背景,另外一张小一点的(比如100*100的front.bmp)作为前景。 1.2:演示程序的完整源代码。 #include <iostream>
#include "SDL/SDL.h" SDL_Surface* pScreen = 0; SDL_Surface* pBack = 0; SDL_Surface* pFront = 0; void pressESCtoQuitPlus(); void loopRender(); int main(int argc, char* argv[]) { try { if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_Init() failed!\n" << s << std::endl; return -1; } const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; const Uint32 SCREEN_FLAGS = SDL_FULLSCREEN; pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SCREEN_FLAGS); try { if ( pScreen == 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_SetVideoMode() failed!\n" << s << std::endl; SDL_Quit(); return -1; } pBack = SDL_LoadBMP("back.bmp"); try { if ( pBack == 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_LoadBMP() failed!\n" << s << std::endl; SDL_Quit(); return -1; } pFront = SDL_LoadBMP("front.bmp"); try { if ( pFront == 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_LoadBMP() failed!\n" << s << std::endl; SDL_Quit(); return -1; } try { pressESCtoQuitPlus(); } catch ( const char* s ) { std::cerr << "pressESCtoQuitPlus() failed!\n" << s << std::endl; SDL_Quit(); return -1; } SDL_Quit(); return 0; } void pressESCtoQuitPlus() { bool gameOver = false; while( gameOver == false ){ SDL_Event gameEvent; while ( SDL_PollEvent(&gameEvent) != 0 ){ if ( gameEvent.type == SDL_QUIT ){ gameOver = true; } if ( gameEvent.type == SDL_KEYUP ){ if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){ gameOver = true; } } } loopRender(); } return; } void loopRender() { SDL_Rect* pSrcRect = 0; SDL_Rect* pDstRect = 0; if ( SDL_BlitSurface(pBack, pSrcRect, pScreen, pDstRect) != 0 ) throw SDL_GetError(); if ( SDL_BlitSurface(pFront, pSrcRect, pScreen, pDstRect) != 0 ) throw SDL_GetError(); if ( SDL_Flip(pScreen) != 0 ) throw SDL_GetError(); return; } 1.3:程序改动说明。 1) 使用了全屏幕显示; 2) 使用了改动后的pressESCtoQuit()函数,用于在全屏模式下方便退出。 3) 将一些变量修改为全局变量,以提供main()所调用的函数使用。 |
|