Window.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright © 2009 James Legg.
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 #include "Window.h"
00012 
00013 #include <iostream>
00014 
00015 #include <Debug.h>
00016 #include <GL/glew.h>
00017 #include <GL/gl.h>
00018 
00019 namespace Graphics
00020 {
00021 
00022 Window::Window()
00023     :   created(false),
00024         bpp(32),
00025         flags(SDL_OPENGL | SDL_RESIZABLE)
00026 {
00027 }
00028 
00029 Window & Window::get_instance()
00030 {
00031     static Window * window = new Window;
00032     return *window;
00033 }
00034 
00035 void Window::set_size(unsigned int width_in, unsigned int height_in, bool fullscreen_in)
00036 {
00037     width = width_in;
00038     height = height_in;
00039     fullscreen = fullscreen_in;
00040     flags = SDL_OPENGL | SDL_RESIZABLE | SDL_SWSURFACE;
00041     bpp = 32;
00042     if (fullscreen) flags |= SDL_FULLSCREEN; 
00043     bool need_glew_init = false;
00044     if (!created)
00045     {
00046         if (SDL_Init(SDL_INIT_VIDEO) == -1)
00047         {
00048             std::cerr << "Error initalising SDL.\n";
00049             throw;
00050         }
00051         
00052         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
00053         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
00054         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
00055         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
00056         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
00057         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
00058         
00059         SDL_WM_SetIcon(SDL_LoadBMP("data/icons/r32.bmp"), NULL);
00060         
00061         created = true;
00062         need_glew_init = true;
00063     }
00064     if (SDL_SetVideoMode(width, height, bpp, flags ) == 0)
00065     {
00066         /* Might happen if no DISPLAY variable, fullscreen resolution not
00067          * available, or some other error. */
00068         std::cerr << "Error creating OpenGL window." << std::endl;
00069         throw; 
00070     }
00071     DEBUG_MESSAGE("Got a window.");
00072     if (need_glew_init)
00073     {
00074         // initalise GLEW on first run.
00075         glewInit();
00076         GLenum err = glewInit();
00077         if (GLEW_OK != err)
00078         {
00079           // glewInit failed
00080           std::cerr << "Error initalising GLEW." << std::endl;
00081           throw;
00082         }
00083         DEBUG_MESSAGE("Using GLEW " << glewGetString(GLEW_VERSION));
00084     }
00085     SDL_WM_SetCaption("Racer", "Racer");
00086     glViewport(0, 0, width, height);
00087 }
00088 
00089 Window::~Window()
00090 {
00091 
00092 }
00093 
00094 void Window::swap_buffers()
00095 {
00096     SDL_GL_SwapBuffers();
00097 }
00098 
00099 
00100 void Window::set_ortho_projection()
00101 {
00102     glMatrixMode(GL_PROJECTION);
00103     glLoadIdentity();
00104     glOrtho(0.0, (GLdouble) width, 0.0, (GLdouble) height, -1.0, 1.0);
00105     glMatrixMode(GL_MODELVIEW);
00106 }
00107 
00108 unsigned int Window::get_height() const
00109 {
00110     return height;
00111 }
00112 
00113 unsigned int Window::get_width() const
00114 {
00115     return width;
00116 }
00117 
00118 }

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:13 2010 by Doxygen version 1.4.7 for Racer version svn335.