2 This file is part of the 3dengfx, realtime visualization system.
4 Copyright (C) 2005 John Tsiombikas <nuclear@siggraph.org>
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.
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.
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
21 /* OpenGL through GLUT
23 * Author: John Tsiombikas 2005
24 * Modified: John Tsiombikas 2006
27 #include "3dengfx_config.h"
29 #if GFX_LIBRARY == GLUT
33 #include "gfx_library.h"
34 #include "3dengfx/3denginefx.hpp"
35 #include "common/err_msg.h"
39 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
40 info("Initializing GLUT");
42 // fabricate the command line args
43 char *argv[] = {"3dengfx", 0};
46 glutInit(&argc, argv);
47 glutInitWindowSize(gparams->x, gparams->y);
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");
51 // determine color bits
53 if(gparams->dont_care_flags & DONT_CARE_BPP) {
56 switch(gparams->bpp) {
72 error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
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);
82 fxwt_glut_win = glutCreateWindow("3dengfx/glut");
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);
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);
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.
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);
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);
117 // if we don't have DONT_CARE_STENCIL make sure we have the stencil format
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);
128 void fxwt::destroy_graphics() {
129 info("Shutting down GLUT");
130 glutDestroyWindow(fxwt_glut_win);
136 #ifndef GLX_ARB_get_proc_address
139 void *glXGetProcAddress(const char *name) {
142 void *so = dlopen("libGL.so", RTLD_LAZY);
144 perror("dlopen failed");
149 sym = dlsym(so, name);
150 if((err_str = dlerror())) {
151 fprintf(stderr, "dlsym failed: %s\n", err_str);
158 #endif // GLX_ARB_get_proc_address
162 #endif // GFX_LIBRARY == GLUT