added 3dengfx into the repo, probably not the correct version for this
[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 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
37         info("Initializing SDL");
38
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__);
41                 return false;
42         }
43
44         if(!gparams->fullscreen) {
45                 const SDL_VideoInfo *vid_inf = SDL_GetVideoInfo();
46                 gparams->bpp = vid_inf->vfmt->BitsPerPixel;
47         }
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         int rbits, gbits, bbits;
52         switch(gparams->bpp) {
53         case 32:
54                 rbits = gbits = bbits = 8;
55                 break;
56                 
57         case 16:
58                 rbits = bbits = 5;
59                 gbits = 6;
60                 break;
61                 
62         default:
63                 error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
64                 return false;
65         }
66
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);
73
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);
79                 
80                 if(!SDL_SetVideoMode(gparams->x, gparams->y, gparams->bpp, flags)) {
81                         error("%s: Could not set requested video mode", __func__);
82                 }
83         }
84
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);
92
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);
97
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.
102          */
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__);
105                 return false;
106         }
107         
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__);
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                 return false;
122         }
123
124         return true;
125 }
126
127
128 void fxwt::destroy_graphics() {
129         info("Shutting down SDL...");
130         SDL_Quit();
131 }
132 #endif  // SDL