resolution don't care option, heuristic to avoid multimon sizes
[summerhack] / src / 3dengfx / src / fxwt / init_sdl.cpp
1 /*
2 This file is part of the 3dengfx, realtime visualization system.
3
4 Copyright (C) 2004, 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 SDL
22  *
23  * Author: John Tsiombikas 2004
24  */
25
26 #include "3dengfx_config.h"
27
28 #if GFX_LIBRARY == SDL
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 #define GOOD_ASPECT(x)  ((x) > 1.1f && (x) < 2.0f)
37
38 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
39         info("Initializing SDL");
40
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__);
43                 return false;
44         }
45
46         char driver[128];
47         SDL_VideoDriverName(driver, sizeof driver);
48         info("SDL video driver: %s", driver);
49
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;
56
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)) {
64                                                 curx = modes[i]->w;
65                                                 cury = modes[i]->h;
66                                         }
67                                 }
68                         }
69                         gparams->x = curx;
70                         gparams->y = cury;
71                 }
72         } else {
73                 gparams->bpp = vid_inf->vfmt->BitsPerPixel;
74         }
75
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");
77
78         int rbits, gbits, bbits;
79         switch(gparams->bpp) {
80         case 32:
81                 rbits = gbits = bbits = 8;
82                 break;
83
84         case 16:
85                 rbits = bbits = 5;
86                 gbits = 6;
87                 break;
88
89         default:
90                 error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
91                 return false;
92         }
93
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);
100
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);
106
107                 if(!SDL_SetVideoMode(gparams->x, gparams->y, gparams->bpp, flags)) {
108                         error("%s: Could not set requested video mode", __func__);
109                 }
110         }
111
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);
119
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);
124
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.
129          */
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__);
132                 return false;
133         }
134
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__);
140                         return false;
141                 }
142         }
143
144         // if we don't have DONT_CARE_STENCIL make sure we have the stencil format
145         // that was asked.
146         if(!(gparams->dont_care_flags & DONT_CARE_STENCIL) && astencilbits != gparams->stencil_bits) {
147                 error("%s: Could not set exact stencil format", __func__);
148                 return false;
149         }
150
151         return true;
152 }
153
154
155 void fxwt::destroy_graphics() {
156         info("Shutting down SDL...");
157         SDL_Quit();
158 }
159 #endif  // SDL