Patch:[#25163] BGE support for Blender Font objects - unicode support
[blender.git] / source / gameengine / BlenderRoutines / KX_BlenderGL.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL 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.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "KX_BlenderGL.h"
30
31 /* 
32  * This little block needed for linking to Blender... 
33  */
34 #ifdef WIN32
35 #include <vector>
36 #include "BLI_winstuff.h"
37 #endif
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "GL/glew.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BL_Material.h" // MAXTEX
47
48 /* Data types encoding the game world: */
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_camera_types.h"
53 #include "DNA_world_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_image_types.h"
57 #include "DNA_view3d_types.h"
58 #include "DNA_material_types.h"
59 #include "DNA_windowmanager_types.h"
60
61 #include "BKE_global.h"
62 #include "BKE_main.h"
63 #include "BKE_bmfont.h"
64 #include "BKE_image.h"
65
66 #include "BLI_path_util.h"
67
68 extern "C" {
69 #include "IMB_imbuf_types.h"
70 #include "IMB_imbuf.h"
71 #include "WM_api.h"
72 #include "WM_types.h"
73 #include "wm_event_system.h"
74 #include "wm_cursors.h"
75 #include "wm_window.h"
76 #include "BLF_api.h"
77 }
78
79 /* end of blender block */
80 void BL_warp_pointer(wmWindow *win, int x,int y)
81 {
82         WM_cursor_warp(win, x, y);
83 }
84
85 void BL_SwapBuffers(wmWindow *win)
86 {
87         wm_window_swap_buffers(win);
88 }
89
90 void DisableForText()
91 {
92         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* needed for texture fonts otherwise they render as wireframe */
93
94         if(glIsEnabled(GL_BLEND)) glDisable(GL_BLEND);
95         if(glIsEnabled(GL_ALPHA_TEST)) glDisable(GL_ALPHA_TEST);
96
97         if(glIsEnabled(GL_LIGHTING)) {
98                 glDisable(GL_LIGHTING);
99                 glDisable(GL_COLOR_MATERIAL);
100         }
101
102         if(GLEW_ARB_multitexture) {
103                 for(int i=0; i<MAXTEX; i++) {
104                         glActiveTextureARB(GL_TEXTURE0_ARB+i);
105
106                         if(GLEW_ARB_texture_cube_map)
107                                 if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
108                                         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
109
110                         if(glIsEnabled(GL_TEXTURE_2D))
111                                 glDisable(GL_TEXTURE_2D);
112                 }
113
114                 glActiveTextureARB(GL_TEXTURE0_ARB);
115         }
116         else {
117                 if(GLEW_ARB_texture_cube_map)
118                         if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
119                                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
120
121                 if(glIsEnabled(GL_TEXTURE_2D))
122                         glDisable(GL_TEXTURE_2D);
123         }
124 }
125
126 /* Print 3D text */
127 void BL_print_game_line(int fontid, const char* text, int size, int dpi, float* color, double* mat, float aspect)
128 {
129         /* gl prepping */
130         DisableForText();
131
132         /* the actual drawing */
133         glColor4fv(color);
134
135         /* multiply the text matrix by the object matrix */
136         BLF_enable(fontid, BLF_MATRIX|BLF_ASPECT);
137         BLF_matrix(fontid, mat);
138
139         /* aspect is the inverse scale that allows you to increase */
140         /* your resolution without sizing the final text size      */
141         /* the bigger the size, the smaller the aspect             */
142         BLF_aspect(fontid, aspect, aspect, aspect);
143
144         BLF_size(fontid, size, dpi);
145         BLF_position(fontid, 0, 0, 0);
146         BLF_draw(fontid, (char *)text, strlen(text));
147
148         BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
149         glEnable(GL_DEPTH_TEST);
150 }
151
152 void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height)
153 {       
154         /* gl prepping */
155         DisableForText();
156
157         glMatrixMode(GL_PROJECTION);
158         glPushMatrix();
159         glLoadIdentity();
160
161         glOrtho(0, width, 0, height, -100, 100);
162
163         glMatrixMode(GL_MODELVIEW);
164         glPushMatrix();
165         glLoadIdentity();
166
167         /* the actual drawing */
168         glColor3ub(255, 255, 255);
169         BLF_draw_default(xco, height-yco, 0.0f, (char *)text, 65535); /* XXX, use real len */
170
171         glMatrixMode(GL_PROJECTION);
172         glPopMatrix();
173         glMatrixMode(GL_MODELVIEW);
174         glPopMatrix();
175         glEnable(GL_DEPTH_TEST);
176 }
177
178 void BL_print_gamedebug_line_padded(const char* text, int xco, int yco, int width, int height)
179 {
180         /* This is a rather important line :( The gl-mode hasn't been left
181          * behind quite as neatly as we'd have wanted to. I don't know
182          * what cause it, though :/ .*/
183         DisableForText();
184
185         glMatrixMode(GL_PROJECTION);
186         glPushMatrix();
187         glLoadIdentity();
188         
189         glOrtho(0, width, 0, height, -100, 100);
190         
191         glMatrixMode(GL_MODELVIEW);
192         glPushMatrix();
193         glLoadIdentity();
194
195         /* draw in black first*/
196         glColor3ub(0, 0, 0);
197         BLF_draw_default(xco+2, height-yco-2, 0.0f, text, 65535); /* XXX, use real len */
198         glColor3ub(255, 255, 255);
199         BLF_draw_default(xco, height-yco, 0.0f, text, 65535); /* XXX, use real len */
200
201         glMatrixMode(GL_PROJECTION);
202         glPopMatrix();
203         glMatrixMode(GL_MODELVIEW);
204         glPopMatrix();
205         glEnable(GL_DEPTH_TEST);
206 }
207
208 void BL_HideMouse(wmWindow *win)
209 {
210         WM_cursor_set(win, CURSOR_NONE);
211 }
212
213
214 void BL_WaitMouse(wmWindow *win)
215 {
216         WM_cursor_set(win, CURSOR_WAIT);
217 }
218
219
220 void BL_NormalMouse(wmWindow *win)
221 {
222         WM_cursor_set(win, CURSOR_STD);
223 }
224 #define MAX_FILE_LENGTH 512
225
226 /* get shot from frontbuffer sort of a copy from screendump.c */
227 static unsigned int *screenshot(ScrArea *curarea, int *dumpsx, int *dumpsy)
228 {
229         int x=0, y=0;
230         unsigned int *dumprect= NULL;
231         
232         x= curarea->totrct.xmin;
233         y= curarea->totrct.ymin;
234         *dumpsx= curarea->totrct.xmax-x;
235         *dumpsy= curarea->totrct.ymax-y;
236
237         if (*dumpsx && *dumpsy) {
238                 
239                 dumprect= (unsigned int *)MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
240                 glReadBuffer(GL_FRONT);
241                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
242                 glFinish();
243                 glReadBuffer(GL_BACK);
244         }
245
246         return dumprect;
247 }
248
249 /* based on screendump.c::screenshot_exec */
250 void BL_MakeScreenShot(ScrArea *curarea, const char* filename)
251 {
252         char path[MAX_FILE_LENGTH];
253         strcpy(path,filename);
254
255         unsigned int *dumprect;
256         int dumpsx, dumpsy;
257         
258         dumprect= screenshot(curarea, &dumpsx, &dumpsy);
259         if(dumprect) {
260                 ImBuf *ibuf;
261                 BLI_path_abs(path, G.main->name);
262                 /* BKE_add_image_extension() checks for if extension was already set */
263                 BKE_add_image_extension(path, R_PNG); /* scene->r.imtype */
264                 ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0);
265                 ibuf->rect= dumprect;
266                 ibuf->ftype= PNG;
267
268                 IMB_saveiff(ibuf, path, IB_rect);
269
270                 ibuf->rect= NULL;
271                 IMB_freeImBuf(ibuf);
272                 MEM_freeN(dumprect);
273         }
274 }
275