comparison ext/guichan-0.8.1/examples/hge.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 HGE application with Guichan using the
3 * Guichan HGE back end.
4 */
5
6 #include <guichan.hpp>
7 #include <guichan/hge.hpp>
8 #include <string>
9
10 namespace hge
11 {
12 HGE *hge = NULL;
13 bool running = false;
14
15 // All back ends contain objects to make Guichan work on a
16 // specific target - in this case HGE - and they are a Graphics
17 // object to make Guichan able to draw itself using HGE, an
18 // input objec to make Guichan able to get user input using HGE
19 // and an ImageLoader object to make Guichan able to load images
20 // using SDL.
21 gcn::HGEGraphics* graphics;
22 gcn::HGEInput* input;
23 gcn::HGEImageLoader* imageLoader;
24
25 /**
26 * The frame function for the HGE application.
27 */
28 bool frameFunc()
29 {
30 if (hge->Input_GetKeyState(HGEK_ESCAPE) || running)
31 {
32 return true;
33 }
34
35 // Now we let the Gui object perform its logic.
36 globals::gui->logic();
37 // Next we begin a scene.
38 hge->Gfx_BeginScene();
39 hge->Gfx_Clear(0);
40 // Now we let the Gui object draw itself.
41 globals::gui->draw();
42 // Finally we end the scene causing the screen to be updated.
43 hge->Gfx_EndScene();
44
45 return false;
46 }
47
48 /**
49 * Initialises the HGE application. This function creates the global
50 * Gui object that can be populated by various examples.
51 */
52 void init()
53 {
54 // We simply initialise HGE as we would do with any HGE application.
55 hge = hgeCreate(HGE_VERSION);
56
57 hge->System_SetState(HGE_FRAMEFUNC, frameFunc);
58 hge->System_SetState(HGE_SCREENWIDTH, 640);
59 hge->System_SetState(HGE_SCREENHEIGHT, 480);
60 hge->System_SetState(HGE_WINDOWED, true);
61 hge->System_SetState(HGE_HIDEMOUSE, false);
62 hge->System_SetState(HGE_USESOUND, false);
63 hge->System_SetState(HGE_SHOWSPLASH, false);
64 hge->System_SetState(HGE_LOGFILE, "hgelog.txt");
65
66 if (!hge->System_Initiate())
67 {
68 throw GCN_EXCEPTION("Unable to initialse HGE: " + std::string(hge->System_GetErrorMessage()));
69 }
70
71 // Now it's time to initialise the Guichan HGE back end.
72
73 imageLoader = new gcn::HGEImageLoader();
74 // The ImageLoader Guichan should use needs to be passed to the Image object
75 // using a static function.
76 gcn::Image::setImageLoader(imageLoader);
77 graphics = new gcn::HGEGraphics();
78 input = new gcn::HGEInput();
79
80 // Now we create the Gui object to be used with this HGE application.
81 globals::gui = new gcn::Gui();
82 // The Gui object needs a Graphics to be able to draw itself and an Input
83 // object to be able to check for user input. In this case we provide the
84 // Gui object with HGE implementations of these objects hence making Guichan
85 // able to utilise HGE.
86 globals::gui->setGraphics(graphics);
87 globals::gui->setInput(input);
88 }
89
90 /**
91 * Halts the HGE application.
92 */
93 void halt()
94 {
95 delete globals::gui;
96
97 delete imageLoader;
98 delete input;
99 delete graphics;
100
101 hge->System_Shutdown();
102 hge->Release();
103 }
104
105 /**
106 * Runs the HGE application.
107 */
108 void run()
109 {
110 hge->System_Start();
111 }
112 }