comparison ext/guichan-0.8.1/examples/allegro.hpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /*
2 * Code that sets up an Allegro application with Guichan using the
3 * Guichan Allegro back end.
4 */
5
6 #include <guichan.hpp>
7 #include <guichan/allegro.hpp>
8
9 namespace allegro
10 {
11 BITMAP* screenBuffer;
12
13 // All back ends contain objects to make Guichan work on a
14 // specific target - in this case Allegro - and they are a Graphics
15 // object to make Guichan able to draw itself using Allegro, an
16 // input objec to make Gopenglsdluichan able to get user input using Allegro
17 // and an ImageLoader object to make Guichan able to load images
18 // using Allegro.
19 gcn::AllegroGraphics* graphics;
20 gcn::AllegroInput* input;
21 gcn::AllegroImageLoader* imageLoader;
22
23 /**
24 * Initialises the Allegro application. This function creates the global
25 * Gui object that can be populated by various examples.
26 */
27 void init()
28 {
29 // We simply initialise Allegro as we would do with any Allegro application.
30 allegro_init();
31
32 int bpp = desktop_color_depth();
33 if (bpp == 0)
34 {
35 bpp = 16;
36 }
37
38 set_color_depth(bpp);
39
40 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0))
41 {
42 if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0))
43 {
44 throw GCN_EXCEPTION("Unable to set graphics mode");
45 }
46 }
47
48 screenBuffer = create_bitmap(SCREEN_W, SCREEN_H);
49
50 if (screenBuffer == NULL)
51 {
52 throw GCN_EXCEPTION("Unable to create a screen buffer");
53 }
54
55 install_keyboard();
56 install_mouse();
57 install_timer();
58
59 // Now it's time to initialise the Guichan Allegro back end.
60
61 imageLoader = new gcn::AllegroImageLoader();
62 // The ImageLoader Guichan should use needs to be passed to the Image object
63 // using a static function.
64 gcn::Image::setImageLoader(imageLoader);
65 graphics = new gcn::AllegroGraphics();
66 // Set the target for the graphics object to be the doublebuffer
67 // for the screen. Drawing to the screen directly is not a good
68 // idea, as it will produce flicker, unless you use page flipping.
69 graphics->setTarget(screenBuffer);
70 input = new gcn::AllegroInput();
71
72 // Now we create the Gui object to be used with this Allegro application.
73 globals::gui = new gcn::Gui();
74 // The Gui object needs a Graphics to be able to draw itself and an Input
75 // object to be able to check for user input. In this case we provide the
76 // Gui object with Allegro implementations of these objects hence making Guichan
77 // able to utilise Allegro.
78 globals::gui->setGraphics(graphics);
79 globals::gui->setInput(input);
80 }
81
82 /**
83 * Halts the Allegro application.
84 */
85 void halt()
86 {
87 delete globals::gui;
88
89 delete imageLoader;
90 delete input;
91 delete graphics;
92
93 destroy_bitmap(screenBuffer);
94 }
95
96 /**
97 * Runs the Allegro application.
98 */
99 void run()
100 {
101 while(!key[KEY_ESC])
102 {
103 // Now we let the Gui object perform its logic.
104 globals::gui->logic();
105 // Now we let the Gui object draw itself.
106 globals::gui->draw();
107
108 // We draw the mouse pointer manually, as Allegro's mouse
109 // drawing code is so wierd.
110 draw_sprite(screenBuffer, mouse_sprite, mouse_x, mouse_y);
111
112 // Finally we update the screen.
113 blit(screenBuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
114 }
115 }
116 }