Mondschein Engine  0.3.0
visual.cpp
1 /* Mondschein Engine is a fast, powerful, and easy-to-use 3D realtime rendering engine.
2  *
3  * Copyright (C) 2009-2013 by Andreas Amereller
4  * E-Mail: andreas.amereller.dev@googlemail.com
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include "sdl/visual.h"
22 #include <GL/gl.h>
23 
24 using namespace mondschein;
25 using namespace display;
26 
27 SDL_Visual::SDL_Visual() : Visual(), renderer::Context(), visual()
28 {
29  return;
30 }
31 
32 SDL_Visual::SDL_Visual(const visual_attribs_t &_dsp,const renderer::context_attribs_t &_c) :
33  Visual(_dsp), renderer::Context(_c), visual()
34 {
35  return;
36 }
37 
38 SDL_Visual::~SDL_Visual()
39 {
40  SDL_FreeSurface(visual);
41  return;
42 }
43 
45 {
48  /* Set the visual flags */
49  uint32 flags=SDL_HWACCEL|SDL_OPENGL;
50  if (dsp.fullscreen) flags |= SDL_FULLSCREEN;
51 
52  /* Configure the context */
53  SDL_GL_SetAttribute(SDL_GL_RED_SIZE,c.color_depth(0));
54  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,c.color_depth(1));
55  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,c.color_depth(2));
56  SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,c.color_depth(3));
57  SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,c.accum_depth(0));
58  SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,c.accum_depth(1));
59  SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,c.accum_depth(2));
60  SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,c.accum_depth(3));
61  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,c.depth_buffer_size);
62  SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,c.stencil_buffer_size);
63  if (c.doublebuffer) SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
64  else SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,0);
65 
66  /* Create the context and visual */
67  visual=SDL_SetVideoMode(dsp.width,dsp.height,dsp.bpp,flags);
68  if (visual == NULL)
69  {
70  std::string err("Mondschein Engine ERROR: ");
71  err+="Video mode or GL visual not supported";
72  err+=", exception raised in function\n\t";
73  err+="void mondschein::display::SDL_Visual::init()";
74  throw boost::enable_current_exception(exception()) << exception_error(err);
75  }
76  /* Write back the real visual and context configuration */
77  int db=0;
78  dsp.width=visual->w;
79  dsp.height=visual->h;
80  dsp.bpp=visual->format->BitsPerPixel;
81  dsp.fullscreen=visual->flags&SDL_FULLSCREEN;
82  set_visual_attribs(dsp);
83 
84  SDL_GL_GetAttribute(SDL_GL_RED_SIZE,&c.color_depth(0));
85  SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE,&c.color_depth(1));
86  SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE,&c.color_depth(2));
87  SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE,&c.color_depth(3));
88  SDL_GL_GetAttribute(SDL_GL_ACCUM_RED_SIZE,&c.accum_depth(0));
89  SDL_GL_GetAttribute(SDL_GL_ACCUM_GREEN_SIZE,&c.accum_depth(1));
90  SDL_GL_GetAttribute(SDL_GL_ACCUM_BLUE_SIZE,&c.accum_depth(2));
91  SDL_GL_GetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,&c.accum_depth(3));
92  SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE,&c.depth_buffer_size);
93  SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE,&c.stencil_buffer_size);
94  SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER,&db);
95  c.doublebuffer=db;
97  return;
98 }
99 
101 {
102  SDL_FreeSurface(visual);
103  try
104  {
105  init();
106  }
107  catch (exception &e)
108  {
109  std::string err(*boost::get_error_info<exception_error>(e));
110  err+=", called in function\n\t";
111  err+="void mondschein::display::SDL_Visual::update()";
112  throw boost::enable_current_exception(e) << exception_error(err);
113  }
114  return;
115 }
116 
118 {
119  #ifdef DEBUG
120  std::cerr<<"Mondschein Engine DEBUG: SDL visuals cannot be made current."<<std::endl;
121  #endif // DEBUG
122 }
123 
125 {
126  SDL_GL_SwapBuffers();
127  return;
128 }