comparison ext/guichan-0.8.1/examples/openglsdl.hpp @ 89:fa33cda75471

* Reverting back to 2543 as requested by sleek
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 19 Jul 2008 11:38:52 +0000
parents 4a0efb7baf70
children
comparison
equal deleted inserted replaced
88:1c2842ebe393 89:fa33cda75471
1 /*
2 * Code that sets up an OpenGL application with Guichan using the
3 * Guichan OpenGL back end and the SDL back end (as OpenGL cannot
4 * load images nor check for user input an additional back end needs
5 * to be used).
6 */
7
8 #include <guichan.hpp>
9 #include <guichan/opengl.hpp>
10 // The openglsdlimageloader.hpp header file needs to be included
11 // in order to get the image loader that uses OpenGL and SDL.
12 #include <guichan/opengl/openglsdlimageloader.hpp>
13 #include <guichan/sdl.hpp>
14
15 namespace openglsdl
16 {
17 bool running = true;
18 SDL_Surface* screen;
19
20 // All back ends contain objects to make Guichan work on a
21 // specific target. They are a Graphics object to make Guichan
22 // able to draw itself using OpenGL, an input objec to make
23 // Guichan able to get user input using SDL and an ImageLoader
24 // object to make Guichan able to load images using OpenGL and SDL.
25 gcn::OpenGLGraphics* graphics;
26 gcn::SDLInput* input;
27 gcn::OpenGLSDLImageLoader* imageLoader;
28
29 /**
30 * Initialises the OpenGL and SDL application. This function creates the global
31 * Gui object that can be populated by various examples.
32 */
33 void init()
34 {
35 // We simply initialise OpenGL and SDL as we would do with any OpenGL
36 // and SDL application.
37 SDL_Init(SDL_INIT_VIDEO);
38 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
39
40 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_HWACCEL);
41
42 glViewport(0, 0, 640, 480);
43 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
44
45 // We want unicode for the SDLInput object to function properly.
46 SDL_EnableUNICODE(1);
47 // We also want to enable key repeat.
48 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
49
50 // Now it's time to initialise the Guichan OpenGL back end
51 // and the Guichan SDL back end.
52
53 imageLoader = new gcn::OpenGLSDLImageLoader();
54 // The ImageLoader Guichan should use needs to be passed to the Image object
55 // using a static function.
56 gcn::Image::setImageLoader(imageLoader);
57 graphics = new gcn::OpenGLGraphics();
58 // We need to tell the OpenGL Graphics object how big the screen is.
59 graphics->setTargetPlane(640, 480);
60 input = new gcn::SDLInput();
61
62 // Now we create the Gui object to be used with this OpenGL
63 // and SDL application.
64 globals::gui = new gcn::Gui();
65 // The Gui object needs a Graphics to be able to draw itself and an Input
66 // object to be able to check for user input. In this case we provide the
67 // Gui object with OpenGL and SDL implementations of these objects hence
68 // making Guichan able to utilise OpenGL and SDL.
69 globals::gui->setGraphics(graphics);
70 globals::gui->setInput(input);
71 }
72
73 /**
74 * Halts the OpenGL and SDL application.
75 */
76 void halt()
77 {
78 delete globals::gui;
79
80 delete imageLoader;
81 delete input;
82 delete graphics;
83
84 SDL_Quit();
85 }
86
87 /**
88 * Runs the OpenGL and SDL application.
89 */
90 void run()
91 {
92 // The main loop
93 while(running)
94 {
95 // Check user input
96 SDL_Event event;
97 while(SDL_PollEvent(&event))
98 {
99 if (event.type == SDL_KEYDOWN)
100 {
101 if (event.key.keysym.sym == SDLK_ESCAPE)
102 {
103 running = false;
104 }
105 if (event.key.keysym.sym == SDLK_f)
106 {
107 if (event.key.keysym.mod & KMOD_CTRL)
108 {
109 // Works with X11 only
110 SDL_WM_ToggleFullScreen(screen);
111 }
112 }
113 }
114 else if(event.type == SDL_QUIT)
115 {
116 running = false;
117 }
118
119 // After we have manually checked user input with SDL for
120 // any attempt by the user to halt the application we feed
121 // the input to Guichan by pushing the input to the Input
122 // object.
123 input->pushInput(event);
124 }
125 // Now we let the Gui object perform its logic.
126 globals::gui->logic();
127 // Now we let the Gui object draw itself.
128 globals::gui->draw();
129 // Finally we update the screen.
130 SDL_GL_SwapBuffers();
131 }
132 }
133 }