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 #define GOOD_ASPECT(x) ((x) > 1.1f && (x) < 2.0f)
38 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
39 info("Initializing SDL");
41 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) == -1) {
42 error("%s: Could not initialize SDL library.", __func__);
47 SDL_VideoDriverName(driver, sizeof driver);
48 info("SDL video driver: %s", driver);
50 const SDL_VideoInfo *vid_inf = SDL_GetVideoInfo();
51 if(gparams->fullscreen) {
52 if(gparams->dont_care_flags & DONT_CARE_SIZE) {
53 int curx = vid_inf->current_w;
54 int cury = vid_inf->current_h;
55 float aspect = (float)curx / cury;
57 if(!GOOD_ASPECT(aspect)) {
58 SDL_Rect **modes = SDL_ListModes(0, SDL_OPENGL | SDL_FULLSCREEN);
59 info("Current mode %dx%d sounds like multi-monitor. Available modes:", curx, cury);
60 for(int i=0; modes[i]; i++) {
61 info(" %dx%d", modes[i]->w, modes[i]->h);
62 aspect = (float)modes[i]->w / modes[i]->h;
63 if(GOOD_ASPECT(aspect) && (modes[i]->w == vid_inf->current_w || modes[i]->h == vid_inf->current_h)) {
73 gparams->bpp = vid_inf->vfmt->BitsPerPixel;
76 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");
78 int rbits, gbits, bbits;
79 switch(gparams->bpp) {
81 rbits = gbits = bbits = 8;
90 error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
94 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rbits);
95 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, gbits);
96 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, bbits);
97 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, gparams->depth_bits);
98 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, gparams->stencil_bits);
99 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
101 unsigned long flags = SDL_OPENGL;
102 if(gparams->fullscreen) flags |= SDL_FULLSCREEN;
103 if(!SDL_SetVideoMode(gparams->x, gparams->y, gparams->bpp, flags)) {
104 if(gparams->depth_bits == 32) gparams->depth_bits = 24;
105 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, gparams->depth_bits);
107 if(!SDL_SetVideoMode(gparams->x, gparams->y, gparams->bpp, flags)) {
108 error("%s: Could not set requested video mode", __func__);
112 // now check the actual video mode we got
113 int arbits, agbits, abbits, azbits, astencilbits;
114 SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &arbits);
115 SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &agbits);
116 SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &abbits);
117 SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &azbits);
118 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &astencilbits);
120 info("Initialized video mode:");
121 info(" bpp: %d (%d%d%d)", arbits + agbits + abbits, arbits, agbits, abbits);
122 info("zbuffer: %d", azbits);
123 info("stencil: %d", astencilbits);
125 /* if the dont_care_flags does not contain DONT_CARE_BPP and our color bits
126 * does not match, we should return failure, however we test against
127 * the difference allowing a +/-1 difference in order to allow for 16bpp
128 * formats of either 565 or 555 and consider them equal.
130 if(!(gparams->dont_care_flags & DONT_CARE_BPP) && abs(arbits - rbits) > 1 && abs(agbits - gbits) > 1 && abs(abbits - bbits) > 1) {
131 error("%s: Could not set requested exact bpp mode", __func__);
135 // now if we don't have DONT_CARE_DEPTH in the dont_care_flags check for
136 // exact depth buffer format, however consider 24 and 32 bit the same
137 if(!(gparams->dont_care_flags & DONT_CARE_DEPTH) && azbits != gparams->depth_bits) {
138 if(!((gparams->depth_bits == 32 && azbits == 24) || (gparams->depth_bits == 24 && azbits == 32))) {
139 error("%s: Could not set requested exact zbuffer depth", __func__);
144 // if we don't have DONT_CARE_STENCIL make sure we have the stencil format
146 if(!(gparams->dont_care_flags & DONT_CARE_STENCIL) && astencilbits != gparams->stencil_bits) {
147 error("%s: Could not set exact stencil format", __func__);
155 void fxwt::destroy_graphics() {
156 info("Shutting down SDL...");