added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / fxwt / init_glut.cpp
1 /*
2 This file is part of the 3dengfx, realtime visualization system.
3
4 Copyright (C) 2005 John Tsiombikas <nuclear@siggraph.org>
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 GLUT
22  *
23  * Author: John Tsiombikas 2005
24  * Modified: John Tsiombikas 2006
25  */
26
27 #include "3dengfx_config.h"
28
29 #if GFX_LIBRARY == GLUT
30
31 #include <stdlib.h>
32 #include "init.hpp"
33 #include "gfx_library.h"
34 #include "3dengfx/3denginefx.hpp"
35 #include "common/err_msg.h"
36
37 int fxwt_glut_win;
38
39 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
40         info("Initializing GLUT");
41
42         // fabricate the command line args
43         char *argv[] = {"3dengfx", 0};
44         int argc = 1;
45         
46         glutInit(&argc, argv);
47         glutInitWindowSize(gparams->x, gparams->y);
48
49         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");
50
51         // determine color bits
52         int color_bits;
53         if(gparams->dont_care_flags & DONT_CARE_BPP) {
54                 color_bits = 1;
55         } else {
56                 switch(gparams->bpp) {
57                 case 32:
58                 case 24:
59                         color_bits = 8;
60                         break;
61
62                 case 16:
63                 case 15:
64                         color_bits = 5;
65                         break;
66
67                 case 12:
68                         color_bits = 4;
69                         break;
70
71                 default:
72                         error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
73                 }
74         }
75         
76         // construct the display mode
77         unsigned int disp_mode = GLUT_RGBA | GLUT_DOUBLE;
78         if(gparams->depth_bits) disp_mode |= GLUT_DEPTH;
79         if(gparams->stencil_bits) disp_mode |= GLUT_STENCIL;    
80         glutInitDisplayMode(disp_mode);
81
82         fxwt_glut_win = glutCreateWindow("3dengfx/glut");
83
84         int arbits, agbits, abbits, azbits, astencilbits;
85         arbits = glutGet(GLUT_WINDOW_RED_SIZE);
86         agbits = glutGet(GLUT_WINDOW_GREEN_SIZE);
87         abbits = glutGet(GLUT_WINDOW_BLUE_SIZE);
88         azbits = glutGet(GLUT_WINDOW_DEPTH_SIZE);
89         astencilbits = glutGet(GLUT_WINDOW_STENCIL_SIZE);
90         
91         info("Initialized video mode:");
92         info("    bpp: %d (%d%d%d)", arbits + agbits + abbits, arbits, agbits, abbits);
93         info("zbuffer: %d", azbits);
94         info("stencil: %d", astencilbits);
95
96         /* if the dont_care_flags does not contain DONT_CARE_BPP and our color bits
97          * does not match, we should return failure, however we test against
98          * the difference allowing a +/-1 difference in order to allow for 16bpp
99          * formats of either 565 or 555 and consider them equal.
100          */
101         if(!(gparams->dont_care_flags & DONT_CARE_BPP) && abs(arbits - color_bits) > 1 && abs(agbits - color_bits) > 1 && abs(abbits - color_bits) > 1) {
102                 error("%s: Could not set requested exact bpp mode", __func__);
103                 glutDestroyWindow(fxwt_glut_win);
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                         glutDestroyWindow(fxwt_glut_win);
113                         return false;
114                 }
115         }
116
117         // if we don't have DONT_CARE_STENCIL make sure we have the stencil format
118         // that was asked.
119         if(!(gparams->dont_care_flags & DONT_CARE_STENCIL) && astencilbits != gparams->stencil_bits) {
120                 error("%s: Could not set exact stencil format", __func__);
121                 glutDestroyWindow(fxwt_glut_win);
122                 return false;
123         }
124
125         return true;
126 }
127
128 void fxwt::destroy_graphics() {
129         info("Shutting down GLUT");
130         glutDestroyWindow(fxwt_glut_win);
131 }
132
133 #ifdef __unix__
134 #include <GL/glx.h>
135
136 #ifndef GLX_ARB_get_proc_address
137 #include <dlfcn.h>
138
139 void *glXGetProcAddress(const char *name) {
140         char *err_str;
141         void *sym;
142         void *so = dlopen("libGL.so", RTLD_LAZY);
143         if(!so) {
144                 perror("dlopen failed");
145                 return 0;
146         }
147         
148         dlerror();
149         sym = dlsym(so, name);
150         if((err_str = dlerror())) {
151                 fprintf(stderr, "dlsym failed: %s\n", err_str);
152                 sym = 0;
153         }
154         
155         dlclose(so);
156         return sym;
157 }
158 #endif  // GLX_ARB_get_proc_address
159
160 #endif  // __unix__
161
162 #endif  // GFX_LIBRARY == GLUT