Initial revision
[blender-staging.git] / intern / bmfont / test / BMF_Test.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 /**
33
34  * $Id$
35  * Copyright (C) 2001 NaN Technologies B.V.
36  * Simple test file for the bitmap font library using GHOST.
37  * @author      Maarten Gribnau
38  * @date        November 15, 2001
39  */
40
41 #include <iostream>
42
43 #if defined(WIN32) || defined(__APPLE__)
44 #ifdef WIN32
45 #include <windows.h>
46 #include <GL/gl.h>
47 #else // WIN32
48 // __APPLE__ is defined
49 #include <AGL/gl.h>
50 #endif // WIN32
51 #else // defined(WIN32) || defined(__APPLE__)
52 #include <GL/gl.h>
53 #endif // defined(WIN32) || defined(__APPLE__)
54
55
56 #include "STR_String.h"
57 #include "GHOST_Rect.h"
58
59 #include "GHOST_ISystem.h"
60 #include "GHOST_IEvent.h"
61 #include "GHOST_IEventConsumer.h"
62
63 #include "BMF_Api.h"
64
65 static class Application* fApp;
66 static GHOST_ISystem* fSystem = 0;
67
68
69 static void drawGL()
70 {
71         GLint x = 10, y = 10;
72
73         ::glRasterPos2i(x, y);
74         BMF_FontPtr font = BMF_GetFont(BMF_kHelvetica10);
75         BMF_DrawString(font, "Helvetica 10 point");
76         y += 14;
77         ::glRasterPos2i(x, y);
78         font = BMF_GetFont(BMF_kHelvetica12);
79         BMF_DrawString(font, "Helvetica 12 point");
80         y += 16;
81         ::glRasterPos2i(x, y);
82         font = BMF_GetFont(BMF_kHelveticaBold8);
83         BMF_DrawString(font, "Helvetica Bold 8 point");
84         y += 12;
85         ::glRasterPos2i(x, y);
86         font = BMF_GetFont(BMF_kHelveticaBold10);
87         BMF_DrawString(font, "Helvetica Bold 10 point");
88         y += 14;
89         ::glRasterPos2i(x, y);
90         font = BMF_GetFont(BMF_kHelveticaBold12);
91         BMF_DrawString(font, "Helvetica Bold 12 point");
92         y += 16;
93         ::glRasterPos2i(x, y);
94         font = BMF_GetFont(BMF_kHelveticaBold14);
95         BMF_DrawString(font, "Helvetica Bold 14 point");
96         y += 18;
97         ::glRasterPos2i(x, y);
98         font = BMF_GetFont(BMF_kScreen12);
99         BMF_DrawString(font, "Screen 12 point");
100         y += 16;
101         ::glRasterPos2i(x, y);
102         font = BMF_GetFont(BMF_kScreen14);
103         BMF_DrawString(font, "Screen 14 point");
104         y += 18;
105         ::glRasterPos2i(x, y);
106         font = BMF_GetFont(BMF_kScreen15);
107         BMF_DrawString(font, "Screen 15 point");
108 }
109
110
111 static void setViewPortGL(GHOST_IWindow* window)
112 {
113     window->activateDrawingContext();
114     GHOST_Rect bnds;
115         window->getClientBounds(bnds);
116
117         ::glViewport(0, 0, bnds.getWidth(), bnds.getHeight());
118
119         ::glMatrixMode(GL_PROJECTION);
120         ::glLoadIdentity();
121         ::glOrtho(0, bnds.getWidth(), 0, bnds.getHeight(), -10, 10);
122         
123         ::glClearColor(.2f,0.0f,0.0f,0.0f);
124         ::glClear(GL_COLOR_BUFFER_BIT);
125 }
126
127
128
129 class Application : public GHOST_IEventConsumer {
130 public:
131         Application(GHOST_ISystem* system);
132         virtual bool processEvent(GHOST_IEvent* event);
133
134         GHOST_ISystem* m_system;
135         GHOST_IWindow* m_mainWindow;
136         bool m_exitRequested;
137 };
138
139
140 Application::Application(GHOST_ISystem* system)
141         : m_system(system), m_mainWindow(0), m_exitRequested(false)
142 {
143         fApp = this;
144
145         // Create the main window
146         STR_String title1 ("gears - main window");
147         m_mainWindow = system->createWindow(title1, 10, 64, 320, 200, GHOST_kWindowStateNormal, GHOST_kDrawingContextTypeOpenGL);
148     if (!m_mainWindow) {
149                 std::cout << "could not create main window\n";
150         exit(-1);
151     }
152 }
153
154
155 bool Application::processEvent(GHOST_IEvent* event)
156 {
157         bool handled = true;
158
159         switch (event->getType()) {
160         case GHOST_kEventWindowClose:
161                 {
162                 GHOST_TEventWindowData* windowData = (GHOST_TEventWindowData*)((GHOST_IEvent*)event)->getData();
163                 GHOST_IWindow* window = windowData->window;
164                 if (window == m_mainWindow) {
165                         exit(0);
166                 }
167                 else {
168                         m_system->disposeWindow(window);
169                 }
170                 }
171                 break;
172
173         case GHOST_kEventWindowActivate:
174                 handled = false;
175                 break;
176         case GHOST_kEventWindowDeactivate:
177                 handled = false;
178                 break;
179         case GHOST_kEventWindowUpdate:
180                 {
181                         GHOST_TEventWindowData* windowData = (GHOST_TEventWindowData*)((GHOST_IEvent*)event)->getData();
182                         GHOST_IWindow* window = windowData->window;
183                         if (!m_system->validWindow(window)) break;
184                         {
185                                 setViewPortGL(window);
186                                 drawGL();
187                                 window->swapBuffers();
188                         }
189                 }
190                 break;
191                 
192         default:
193                 handled = false;
194                 break;
195         }
196         return handled;
197 }
198
199
200 int main(int /*argc*/, char** /*argv*/)
201 {
202         // Create the system
203         GHOST_ISystem::createSystem();
204         fSystem = GHOST_ISystem::getSystem();
205
206         if (fSystem) {
207                 // Create an application object
208                 Application app (fSystem);
209
210                 // Add the application as event consumer
211                 fSystem->addEventConsumer(&app);
212
213                 // Enter main loop
214                 while (!app.m_exitRequested) {
215                         fSystem->processEvents();
216                         fSystem->dispatchEvents();
217                 }
218         }
219
220         // Dispose the system
221         GHOST_ISystem::disposeSystem();
222
223         return 0;
224 }
225