hello world

Programmer Tue

统计

留言簿(1)

Related Links

阅读排行榜

评论排行榜

SDL Programming

First SDL program:

  1 #include <string>
  2 #include <iostream>
  3 
  4 #include <SDL/SDL.h>
  5 #include <SDL/SDL_image.h>
  6 
  7 #define nullptr 0
  8 
  9 // Attributes of the screen.
 10 const int SCREEN_WIDTH = 640;
 11 const int SCREEN_HEIGHT = 480;
 12 const int SCREEN_BPP = 32;
 13 
 14 // Surfaces that will be used.
 15 SDL_Surface* message = nullptr;
 16 SDL_Surface* background = nullptr;
 17 SDL_Surface* screen = nullptr;
 18 
 19 SDL_Surface* loadImage(const std::string& fileName) {
 20     SDL_Surface* loadedImage = IMG_Load(fileName.c_str());
 21     SDL_Surface* optimizedImage = nullptr;
 22     if (loadedImage != nullptr) {
 23         optimizedImage = SDL_DisplayFormat(loadedImage);
 24         SDL_FreeSurface(loadedImage);
 25         loadedImage = nullptr;
 26     } else {
 27         std::cerr << "Error loading " << fileName << "" << IMG_GetError() << std::endl;
 28     }
 29     return optimizedImage;
 30 }
 31 
 32 void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
 33     SDL_Rect offset;
 34     offset.x = x;
 35     offset.y = y;
 36     SDL_BlitSurface(source, nullptr, destination, &offset);
 37 }
 38 
 39 int main(int argc, char* argv[]) {
 40     if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
 41         std::cerr << "Failed to initialize SDL system." << std::endl;
 42         return 1;
 43     }
 44     screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
 45     if (screen == nullptr) {
 46         std::cerr << "Failed to set video mode." << std::endl;
 47         return 1;
 48     }
 49     SDL_WM_SetCaption("Hello World", nullptr);
 50     message = loadImage("helloworld.jpg");
 51     background = loadImage("background.gif");
 52     applySurface(00, background, screen);
 53     applySurface(180140, message, screen);
 54     if (SDL_Flip(screen) == -1) {
 55         std::cerr << "Failed to flip screen." << std::endl;
 56         return 1;
 57     }
 58 
 59     /**
 60     typedef union SDL_Event {
 61         Uint8 type;
 62         SDL_ActiveEvent active;
 63         SDL_KeyboardEvent key;
 64         SDL_MouseMotionEvent motion;
 65         SDL_MouseButtonEvent button;
 66         SDL_JoyAxisEvent jaxis;
 67         SDL_JoyBallEvent jball;
 68         SDL_JoyHatEvent jhat;
 69         SDL_JoyButtonEvent jbutton;
 70         SDL_ResizeEvent resize;
 71         SDL_ExposeEvent expose;
 72         SDL_QuitEvent quit;
 73         SDL_UserEvent user;
 74         SDL_SysWMEvent syswm;
 75     } SDL_Event;
 76     **/
 77     bool quit = false;
 78     SDL_Event event;
 79     while (!quit) {
 80         while (SDL_PollEvent(&event)) {
 81             switch (event.type) {
 82             case SDL_QUIT:
 83                 std::cerr << "User requested to quit!" << std::endl;
 84                 quit = true;
 85                 break;
 86             case SDL_ACTIVEEVENT:
 87                 std::cout << "Active event!" << std::endl;
 88                 break;
 89             case SDL_KEYUP:
 90                 std::cout << "Key up event!" << std::endl;
 91                 break;
 92             case SDL_KEYDOWN:
 93                 std::cout << "Key down event!" << std::endl;
 94                 break;
 95             case SDL_MOUSEMOTION:
 96                 std::cout << "Mouse motion event!" << std::endl;
 97                 break;
 98             case SDL_MOUSEBUTTONUP:
 99                 std::cout << "Mouse button up event!" << std::endl;
100                 break;
101             case SDL_MOUSEBUTTONDOWN:
102                 std::cout << "Mouse button down event!" << std::endl;
103                 break;
104             case SDL_SYSWMEVENT:
105                 std::cout << "System specific event!" << std::endl;
106                 break;
107             default:
108                 std::cout << "Other event: " << event.type << std::endl;
109                 break;
110             }
111         }
112     }
113 
114     SDL_FreeSurface(message);
115     SDL_FreeSurface(background);
116     SDL_Quit();
117     return 0;
118 }

Configurations:

1. Download SDL-devel-1.2011 for win32: http://www.libsdl.org
2. Download SDL_image-devel-1.2.5 for win32: http://www.libsdl.org/projects/SDL_image/
3. Visual Studio.NET 2003: C/C++, Code Generation, Runtime Library, select: Multi-Threaded (Debug) DLL (/MD or /MDd)

posted on 2006-07-31 19:02 ZHENG Zhong 阅读(746) 评论(1)  编辑 收藏 引用 所属分类: cpp

评论

# re: SDL Programming 2007-02-06 00:38 sdl

sdl的文档,  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理