2 This file is part of the 3dengfx, realtime visualization system.
4 Copyright (C) 2004, 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
23 * Author: John Tsiombikas 2004
26 #include "3dengfx_config.h"
28 #if GFX_LIBRARY == SDL
32 #include "gfx_library.h"
33 #include "3dengfx/3denginefx.hpp"
34 #include "common/err_msg.h"
36 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
37 info("Initializing SDL");
39 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) == -1) {
40 error("%s: Could not initialize SDL library.", __func__);
44 if(!gparams->fullscreen) {
45 const SDL_VideoInfo *vid_inf = SDL_GetVideoInfo();
46 gparams->bpp = vid_inf->vfmt->BitsPerPixel;
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 int rbits, gbits, bbits;
52 switch(gparams->bpp) {
54 rbits = gbits = bbits = 8;
63 error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
67 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rbits);
68 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, gbits);
69 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, bbits);
70 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, gparams->depth_bits);
71 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, gparams->stencil_bits);
72 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
74 unsigned long flags = SDL_OPENGL;
75 if(gparams->fullscreen) flags |= SDL_FULLSCREEN;
76 if(!SDL_SetVideoMode(gparams->x, gparams->y, gparams->bpp, flags)) {
77 if(gparams->depth_bits == 32) gparams->depth_bits = 24;
78 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, gparams->depth_bits);
80 if(!SDL_SetVideoMode(gparams->x, gparams->y, gparams->bpp, flags)) {
81 error("%s: Could not set requested video mode", __func__);
85 // now check the actual video mode we got
86 int arbits, agbits, abbits, azbits, astencilbits;
87 SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &arbits);
88 SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &agbits);
89 SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &abbits);
90 SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &azbits);
91 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &astencilbits);
93 info("Initialized video mode:");
94 info(" bpp: %d (%d%d%d)", arbits + agbits + abbits, arbits, agbits, abbits);
95 info("zbuffer: %d", azbits);
96 info("stencil: %d", astencilbits);
98 /* if the dont_care_flags does not contain DONT_CARE_BPP and our color bits
99 * does not match, we should return failure, however we test against
100 * the difference allowing a +/-1 difference in order to allow for 16bpp
101 * formats of either 565 or 555 and consider them equal.
103 if(!(gparams->dont_care_flags & DONT_CARE_BPP) && abs(arbits - rbits) > 1 && abs(agbits - gbits) > 1 && abs(abbits - bbits) > 1) {
104 error("%s: Could not set requested exact bpp mode", __func__);
108 // now if we don't have DONT_CARE_DEPTH in the dont_care_flags check for
109 // exact depth buffer format, however consider 24 and 32 bit the same
110 if(!(gparams->dont_care_flags & DONT_CARE_DEPTH) && azbits != gparams->depth_bits) {
111 if(!(gparams->depth_bits == 32 && azbits == 24 || gparams->depth_bits == 24 && azbits == 32)) {
112 error("%s: Could not set requested exact zbuffer depth", __func__);
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__);
128 void fxwt::destroy_graphics() {
129 info("Shutting down SDL...");