added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / fxwt / init_gtk.cpp
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This file is part of the 3dengfx, realtime visualization system.
5
6 3dengfx is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 3dengfx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with 3dengfx; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* OpenGL through GTK
22  *
23  * Author: John Tsiombikas 2004
24  */
25
26 #include "3dengfx_config.h"
27
28 #if GFX_LIBRARY == GTK
29
30 #include <stdlib.h>
31 #include "init.hpp"
32 #include "gfx_library.h"
33 #include "3dengfx/3denginefx.hpp"
34 #include "common/err_msg.h"
35
36 GtkWidget *gl_window;
37 GtkWidget *gl_drawing_area;
38 GdkGLConfig *glconfig;
39
40 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
41         info("Initializing GTK and GTK-GLext");
42
43         char *argv[] = {"foo", (char*)0};
44         int argc = 1;
45         gtk_gl_init(&argc, (char***)&argv);
46
47         info("Trying to set video mode %dx%dx%d, d:%d s:%d %s",gparams->x, gparams->y, gparams->bpp, gparams->depth_bits, gparams->stencil_bits, gparams->fullscreen ? "fullscreen" : "windowed");
48
49         int rbits, gbits, bbits;
50         switch(gparams->bpp) {
51         case 32:
52                 rbits = gbits = bbits = 8;
53                 break;
54                 
55         case 16:
56                 rbits = bbits = 6;
57                 gbits = 5;
58                 break;
59                 
60         default:
61                 error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
62                 return false;
63         }
64
65         int gdkgl_attr[] = {
66                 GDK_GL_USE_GL, GDK_GL_DOUBLEBUFFER, GDK_GL_RGBA,
67                 GDK_GL_RED_SIZE, rbits,
68                 GDK_GL_GREEN_SIZE, gbits,
69                 GDK_GL_BLUE_SIZE, bbits,
70                 GDK_GL_DEPTH_SIZE, gparams->depth_bits,
71                 GDK_GL_STENCIL_SIZE, gparams->stencil_bits,
72                 GDK_GL_ATTRIB_LIST_NONE
73         };
74
75         //GdkGLConfig *glconfig;
76         if(!(glconfig = gdk_gl_config_new(gdkgl_attr))) {
77                 if(gparams->depth_bits == 32) gdkgl_attr[10] = 24;
78
79                 if(!(glconfig = gdk_gl_config_new(gdkgl_attr))) {
80                         error("%s: Could not set requested video mode", __func__);
81                 }
82         }
83
84         // now check the actual video mode we got
85         int arbits, agbits, abbits, azbits, astencilbits;
86         gdk_gl_config_get_attrib(glconfig, GDK_GL_RED_SIZE, &arbits);
87         gdk_gl_config_get_attrib(glconfig, GDK_GL_GREEN_SIZE, &agbits);
88         gdk_gl_config_get_attrib(glconfig, GDK_GL_BLUE_SIZE, &abbits);
89         gdk_gl_config_get_attrib(glconfig, GDK_GL_DEPTH_SIZE, &azbits);
90         gdk_gl_config_get_attrib(glconfig, GDK_GL_STENCIL_SIZE, &astencilbits);
91
92         info("Initialized video mode:");
93         info("    bpp: %d (%d%d%d)", arbits + agbits + abbits, arbits, agbits, abbits);
94         info("zbuffer: %d", azbits);
95         info("stencil: %d", astencilbits);
96
97         /* if the dont_care_flags does not contain DONT_CARE_BPP and our color bits
98          * does not match, we should return failure, however we test against
99          * the difference allowing a +/-1 difference in order to allow for 16bpp
100          * formats of either 565 or 555 and consider them equal.
101          */
102         if(!(gparams->dont_care_flags & DONT_CARE_BPP) && abs(arbits - rbits) > 1 && abs(agbits - gbits) > 1 && abs(abbits - bbits) > 1) {
103                 error("%s: Could not set requested exact bpp mode", __func__);
104                 return false;
105         }
106
107         // now if we don't have DONT_CARE_DEPTH in the dont_care_flags check for 
108         // exact depth buffer format, however consider 24 and 32 bit the same
109         if(!(gparams->dont_care_flags & DONT_CARE_DEPTH) && azbits != gparams->depth_bits) {
110                 if(!(gparams->depth_bits == 32 && azbits == 24 || gparams->depth_bits == 24 && azbits == 32)) {
111                         error("%s: Could not set requested exact zbuffer depth", __func__);
112                         return false;
113                 }
114         }
115
116         // if we don't have DONT_CARE_STENCIL make sure we have the stencil format
117         // that was asked.
118         if(!(gparams->dont_care_flags & DONT_CARE_STENCIL) && astencilbits != gparams->stencil_bits) {
119                 error("%s: Could not set exact stencil format", __func__);
120                 return false;
121         }
122
123         //gl_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
124         //gtk_window_set_title(gtk_window(gl_window), "3dengfx visualization window");
125         //gtk_container_set_reallocate_redraws(gtk_container(gl_window), TRUE);
126         //g_signal_connect(g_object(gl_window), "delete_event", g_callback(gtk_main_quit), NULL);
127                 
128         gl_drawing_area = gtk_drawing_area_new();
129         gtk_widget_set_size_request(gl_drawing_area, gparams->x, gparams->y);
130         
131         if(!gtk_widget_set_gl_capability(gl_drawing_area, glconfig, 0, TRUE, GDK_GL_RGBA_TYPE)) {
132                 error("%s: gtk_widget_set_gl_capability() failed", __func__);
133                 return false;
134         }
135
136         extern void set_gtk_callbacks(GtkWidget*);
137         set_gtk_callbacks(gl_drawing_area);
138
139         //gtk_container_add(gtk_container(gl_window), gl_drawing_area);
140         //gtk_widget_show(gl_drawing_area);
141         //gtk_widget_show(gl_window);
142
143         return true;
144 }
145
146 void fxwt::destroy_graphics() {
147         info("Shutting down GTK+");
148 }
149
150 #endif  // GTK