comparison ext/guichan-0.8.2/examples/sdl.hpp @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 /*
2 * Code that sets up an SDL application with Guichan using the
3 * Guichan SDL back end.
4 */
5
6 #include <guichan.hpp>
7 #include <guichan/sdl.hpp>
8
9 namespace sdl
10 {
11 bool running = true;
12 SDL_Surface* screen;
13
14 // All back ends contain objects to make Guichan work on a
15 // specific target - in this case SDL - and they are a Graphics
16 // object to make Guichan able to draw itself using SDL, an
17 // input objec to make Guichan able to get user input using SDL
18 // and an ImageLoader object to make Guichan able to load images
19 // using SDL.
20 gcn::SDLGraphics* graphics;
21 gcn::SDLInput* input;
22 gcn::SDLImageLoader* imageLoader;
23
24 /**
25 * Initialises the SDL application. This function creates the global
26 * Gui object that can be populated by various examples.
27 */
28 void init()
29 {
30 // We simply initialise SDL as we would do with any SDL application.
31 SDL_Init(SDL_INIT_VIDEO);
32 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
33 // We want unicode for the SDLInput object to function properly.
34 SDL_EnableUNICODE(1);
35 // We also want to enable key repeat.
36 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
37
38 // Now it's time to initialise the Guichan SDL back end.
39
40 imageLoader = new gcn::SDLImageLoader();
41 // The ImageLoader Guichan should use needs to be passed to the Image object
42 // using a static function.
43 gcn::Image::setImageLoader(imageLoader);
44 graphics = new gcn::SDLGraphics();
45 // The Graphics object needs a target to draw to, in this case it's the
46 // screen surface, but any surface will do, it doesn't have to be the screen.
47 graphics->setTarget(screen);
48 input = new gcn::SDLInput();
49
50 // Now we create the Gui object to be used with this SDL application.
51 globals::gui = new gcn::Gui();
52 // The Gui object needs a Graphics to be able to draw itself and an Input
53 // object to be able to check for user input. In this case we provide the
54 // Gui object with SDL implementations of these objects hence making Guichan
55 // able to utilise SDL.
56 globals::gui->setGraphics(graphics);
57 globals::gui->setInput(input);
58 }
59
60 /**
61 * Halts the SDL application.
62 */
63 void halt()
64 {
65 delete globals::gui;
66
67 delete imageLoader;
68 delete input;
69 delete graphics;
70
71 SDL_Quit();
72 }
73
74 /**
75 * Runs the SDL application.
76 */
77 void run()
78 {
79 // The main loop
80 while(running)
81 {
82 // Check user input
83 SDL_Event event;
84 while(SDL_PollEvent(&event))
85 {
86 if (event.type == SDL_KEYDOWN)
87 {
88 if (event.key.keysym.sym == SDLK_ESCAPE)
89 {
90 running = false;
91 }
92 if (event.key.keysym.sym == SDLK_f)
93 {
94 if (event.key.keysym.mod & KMOD_CTRL)
95 {
96 // Works with X11 only
97 SDL_WM_ToggleFullScreen(screen);
98 }
99 }
100 }
101 else if(event.type == SDL_QUIT)
102 {
103 running = false;
104 }
105
106 // After we have manually checked user input with SDL for
107 // any attempt by the user to halt the application we feed
108 // the input to Guichan by pushing the input to the Input
109 // object.
110 input->pushInput(event);
111 }
112 // Now we let the Gui object perform its logic.
113 globals::gui->logic();
114 // Now we let the Gui object draw itself.
115 globals::gui->draw();
116 // Finally we update the screen.
117 SDL_Flip(screen);
118 }
119 }
120 }