python api for ID property access by Joseph Eager, copied from blender 2.4x.
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 #include "BLF_api.h"
35 #ifdef __cplusplus
36 }
37 #endif
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 /* 
44  * This little block needed for linking to Blender... 
45  */
46 #ifdef WIN32
47 #include <vector>
48 #include "BLI_winstuff.h"
49 #endif
50
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "GL/glew.h"
55
56 #include "BL_Material.h" // MAXTEX
57
58 /* Data types encoding the game world: */
59 #include "DNA_object_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_camera_types.h"
63 #include "DNA_world_types.h"
64 #include "DNA_mesh_types.h"
65 #include "DNA_meshdata_types.h"
66 #include "DNA_image_types.h"
67 #include "DNA_view3d_types.h"
68 #include "DNA_material_types.h"
69 #include "DNA_windowmanager_types.h"
70
71 #include "BKE_global.h"
72 #include "BKE_bmfont.h"
73 #include "BKE_image.h"
74
75 extern "C" {
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 }
82
83 /* end of blender block */
84
85 /* was in drawmesh.c */
86 void spack(unsigned int ucol)
87 {
88         char *cp= (char *)&ucol;
89         
90         glColor3ub(cp[3], cp[2], cp[1]);
91 }
92
93 void BL_warp_pointer(wmWindow *win, int x,int y)
94 {
95         WM_cursor_warp(win, x, y);
96 }
97
98 void BL_SwapBuffers(wmWindow *win)
99 {
100         wm_window_swap_buffers(win);
101 }
102
103 void DisableForText()
104 {
105         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* needed for texture fonts otherwise they render as wireframe */
106
107         if(glIsEnabled(GL_BLEND)) glDisable(GL_BLEND);
108         if(glIsEnabled(GL_ALPHA_TEST)) glDisable(GL_ALPHA_TEST);
109
110         if(glIsEnabled(GL_LIGHTING)) {
111                 glDisable(GL_LIGHTING);
112                 glDisable(GL_COLOR_MATERIAL);
113         }
114
115         if(GLEW_ARB_multitexture) {
116                 for(int i=0; i<MAXTEX; i++) {
117                         glActiveTextureARB(GL_TEXTURE0_ARB+i);
118
119                         if(GLEW_ARB_texture_cube_map)
120                                 if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
121                                         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
122
123                         if(glIsEnabled(GL_TEXTURE_2D))
124                                 glDisable(GL_TEXTURE_2D);
125                 }
126
127                 glActiveTextureARB(GL_TEXTURE0_ARB);
128         }
129         else {
130                 if(GLEW_ARB_texture_cube_map)
131                         if(glIsEnabled(GL_TEXTURE_CUBE_MAP_ARB))
132                                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
133
134                 if(glIsEnabled(GL_TEXTURE_2D))
135                         glDisable(GL_TEXTURE_2D);
136         }
137 }
138
139 void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height)
140 {       
141         /* gl prepping */
142         DisableForText();
143
144         glMatrixMode(GL_PROJECTION);
145         glPushMatrix();
146         glLoadIdentity();
147
148         glOrtho(0, width, 0, height, -100, 100);
149
150         glMatrixMode(GL_MODELVIEW);
151         glPushMatrix();
152         glLoadIdentity();
153
154         /* the actual drawing */
155         glColor3ub(255, 255, 255);
156         BLF_draw_default(xco, height-yco, 0.0f, (char *)text);
157
158         glMatrixMode(GL_PROJECTION);
159         glPopMatrix();
160         glMatrixMode(GL_MODELVIEW);
161         glPopMatrix();
162         glEnable(GL_DEPTH_TEST);
163 }
164
165 void BL_print_gamedebug_line_padded(const char* text, int xco, int yco, int width, int height)
166 {
167         /* This is a rather important line :( The gl-mode hasn't been left
168          * behind quite as neatly as we'd have wanted to. I don't know
169          * what cause it, though :/ .*/
170         DisableForText();
171
172         glMatrixMode(GL_PROJECTION);
173         glPushMatrix();
174         glLoadIdentity();
175         
176         glOrtho(0, width, 0, height, -100, 100);
177         
178         glMatrixMode(GL_MODELVIEW);
179         glPushMatrix();
180         glLoadIdentity();
181
182         /* draw in black first*/
183         glColor3ub(0, 0, 0);
184         BLF_draw_default(xco+2, height-yco-2, 0.0f, (char *)text);
185         glColor3ub(255, 255, 255);
186         BLF_draw_default(xco, height-yco, 0.0f, (char *)text);
187
188         glMatrixMode(GL_PROJECTION);
189         glPopMatrix();
190         glMatrixMode(GL_MODELVIEW);
191         glPopMatrix();
192         glEnable(GL_DEPTH_TEST);
193 }
194
195 void BL_HideMouse(wmWindow *win)
196 {
197         WM_cursor_set(win, CURSOR_NONE);
198 }
199
200
201 void BL_WaitMouse(wmWindow *win)
202 {
203         WM_cursor_set(win, CURSOR_WAIT);
204 }
205
206
207 void BL_NormalMouse(wmWindow *win)
208 {
209         WM_cursor_set(win, CURSOR_STD);
210 }
211 #define MAX_FILE_LENGTH 512
212
213
214 void BL_MakeScreenShot(struct ARegion *ar, const char* filename)
215 {
216         char copyfilename[MAX_FILE_LENGTH];
217         strcpy(copyfilename,filename);
218
219         // filename read - only
220         
221           /* XXX will need to change at some point */
222         //XXX BIF_screendump(0);
223
224         // write+read filename
225         //XXX write_screendump((char*) copyfilename);
226 }
227