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