=== Custom Transform Orientation ===
[blender.git] / source / blender / src / swapbuffers.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33
34 /* HRMS!!... blender has its own swapbuffers method. for sgi only that worked pretty nice.
35  * but with porting to linux and win, with mesa and opengl variations, it all grow
36  * out of control.
37  * with the introduction of Ghost (2002) we really should bring this back to a single
38  * method again. (ton)
39  */
40
41
42 #include <stdlib.h>
43 #include <stdio.h>
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48
49 #include "DNA_space_types.h"
50 #include "DNA_screen_types.h"
51
52 #include "BKE_global.h"
53
54 #include "BIF_gl.h"
55 #include "BIF_interface.h"
56 #include "BIF_mywindow.h"
57 #include "BIF_screen.h"
58 #include "BIF_space.h"
59
60 #include "winlay.h"
61
62 #if 0
63 static void copy_back_to_front(void)
64 {
65         int actually_swap= 0;
66         int winx, winy;
67         char *data;
68
69         winlay_get_winsize(&winx, &winy);
70         
71         if (actually_swap) {
72                 data= MEM_mallocN(4*winx*winy, "swap");
73                 glReadPixels(0, 0, winx, winy, GL_RGBA, GL_UNSIGNED_BYTE, data);
74         }
75         
76         mywinset(1);
77         glRasterPos2f(-0.5,-0.5);
78         glReadBuffer(GL_BACK);
79         glDrawBuffer(GL_FRONT);
80         glCopyPixels(0, 0, winx, winy, GL_COLOR);
81         glDrawBuffer(GL_BACK);
82         glFlush();
83
84         if (actually_swap) {
85                 glRasterPos2f(-0.5,-0.5);
86                 glDrawPixels(winx, winy, GL_RGBA, GL_UNSIGNED_BYTE, data);
87                 glFlush();
88                 MEM_freeN(data);
89         }
90 }
91 #endif
92
93 static void screen_swapbuffers_REDRAW(bScreen *sc)
94 {
95         ScrArea *sa;
96         int doswap= 0, swap;
97         
98         /* a new implementation: only using redraws and a normal swapbuffer */
99
100         /* all areas front ok? */
101         sa= sc->areabase.first;
102         while(sa) {
103                 if(sa->win && (sa->win_swap & WIN_FRONT_OK)==0) break;
104                 if(sa->headertype==0) sa->head_swap= WIN_EQUAL;
105                 if((sa->head_swap & WIN_FRONT_OK)==0) break;
106                 sa= sa->next;
107         }
108         if(sa==0) return;
109
110         sa= sc->areabase.first;
111         while(sa) {
112                 swap= sa->win_swap;
113                 if( (swap & WIN_BACK_OK) == 0) {
114                         scrarea_do_windraw(sa);
115
116                         doswap= 1;
117                         sa->win_swap= swap | WIN_BACK_OK;
118                 }
119                 else if( sa->win_swap==WIN_BACK_OK) doswap= 1;
120                 
121                 swap= sa->head_swap;
122                 if( (swap & WIN_BACK_OK) == 0) {
123                         if (sa->headertype) scrarea_do_headdraw(sa);
124                         doswap= 1;
125                         sa->head_swap = swap | WIN_BACK_OK;
126                 }
127                 else if( sa->head_swap==WIN_BACK_OK) doswap= 1;
128         
129                 sa= sa->next;
130         }
131
132         /* the whole backbuffer should now be ok */
133         if(doswap) {
134                 myswapbuffers();
135         }
136 }
137
138 #include "BMF_Api.h"
139 #include <stdio.h>
140
141 static void draw_debug_win(int win)
142 {
143         static int drawcounter= 0;
144         char buf[64];
145         int x, y;
146         int w, h;
147         
148         bwin_getsuborigin(win, &x, &y);
149         bwin_getsize(win, &w, &h);
150
151         mywinset(win);
152         
153         glMatrixMode(GL_PROJECTION);
154         glLoadIdentity();
155         glOrtho(0, w, 0, h, -1, 1);
156         glMatrixMode(GL_MODELVIEW);
157         glLoadIdentity();
158         
159         glColor3f(0.8, 0.8, 0.8);
160         glRecti(0, 0, w, h);
161
162         glColor3f(0.6, 0.6, 0.6);
163         glRecti(2, 2, w-4, h-4);
164         
165         glColor3ub(0, 0, 0);
166         glRasterPos2i(5, 5);
167         
168         sprintf(buf, "win: %d - (%d, %d, %d, %d) %d\n", win, x, y, w, h, drawcounter++);
169         BMF_DrawString(G.font, buf);
170 }
171
172 static void screen_swapbuffers_DEBUG(bScreen *sc)
173 {
174         ScrArea *sa;
175
176         for (sa= sc->areabase.first; sa; sa= sa->next) {
177                 draw_debug_win(sa->win);
178                 if (sa->headwin) draw_debug_win(sa->headwin);
179         }
180         
181         myswapbuffers();
182 }
183
184 static void screen_swapbuffers_DEBUG_SWAP(bScreen *sc)
185 {
186         ScrArea *sa;
187         int doswap= 0, swap;
188
189         /* a new implementation: only using redraws and a normal swapbuffer */
190
191         /* all areas front ok? */
192         sa= sc->areabase.first;
193         while(sa) {
194                 if(sa->win && (sa->win_swap & WIN_FRONT_OK)==0) break;
195                 if(!sa->headertype) sa->head_swap= WIN_EQUAL;
196                 if((sa->head_swap & WIN_FRONT_OK)==0) break;
197                 sa= sa->next;
198         }
199         if(sa==0) return;
200
201         sa= sc->areabase.first;
202         while(sa) {
203         
204                 swap= sa->win_swap;
205                 if( (swap & WIN_BACK_OK) == 0) {
206                         scrarea_do_windraw(sa);
207                         draw_debug_win(sa->win);
208                         
209                         doswap= 1;
210                         sa->win_swap= swap | WIN_BACK_OK;
211                 }
212                 else if( sa->win_swap==WIN_BACK_OK) doswap= 1;
213                 
214                 swap= sa->head_swap;
215                 if( (swap & WIN_BACK_OK) == 0) {
216                         if (sa->headertype) {
217                                 scrarea_do_headdraw(sa);
218                                 draw_debug_win(sa->headwin);
219                         }
220                         doswap= 1;
221                         sa->head_swap = swap | WIN_BACK_OK;
222                 }
223                 else if( sa->head_swap==WIN_BACK_OK) doswap= 1;
224         
225                 sa= sa->next;
226         }
227
228         if(doswap) {
229                 myswapbuffers();
230         }
231 }
232
233 static void screen_swapbuffers_SIMPLE(bScreen *sc)
234 {
235         ScrArea *sa;
236         
237         mywinset(1);
238         glClearColor(0.8, 0.6, 0.7, 1.0);
239         glClear(GL_COLOR_BUFFER_BIT);
240         
241         for (sa= sc->areabase.first; sa; sa= sa->next) {
242                 scrarea_do_windraw(sa);
243                 if (sa->headertype) scrarea_do_headdraw(sa);
244         }
245         
246         myswapbuffers();
247 }
248
249 static int drawmode_default= 'r';
250 int debug_swapbuffers_override= 0;
251
252 void set_debug_swapbuffers_ovveride(bScreen *sc, int mode)
253 {
254         ScrArea *sa;
255         for (sa= sc->areabase.first; sa; sa= sa->next) {
256                 sa->win_swap= 0;
257                 sa->head_swap= 0;
258         }
259         debug_swapbuffers_override= mode;
260 }
261
262 void screen_swapbuffers(void)
263 {
264         ScrArea *tempsa;
265
266         bScreen *sc= G.curscreen;
267         int drawmode;
268         
269         if (debug_swapbuffers_override) {
270                 drawmode= debug_swapbuffers_override;
271         } else {
272                 drawmode= drawmode_default;
273         }
274         
275         tempsa= curarea;
276         areawinset(1);
277         
278         if (drawmode=='s') {
279                 screen_swapbuffers_SIMPLE(sc);
280         } else if (drawmode=='d') {
281                 screen_swapbuffers_DEBUG(sc);
282         } else if (drawmode=='f') {
283                 screen_swapbuffers_DEBUG_SWAP(sc);
284         } else {
285                 screen_swapbuffers_REDRAW(sc);
286         }
287
288         areawinset(tempsa->win);
289 }