| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program 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, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #include "windowHandler.h" |
|---|
| 17 | #include <stdio.h> |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | \brief Resizes the Window |
|---|
| 21 | \param width the new size in the width. |
|---|
| 22 | \param height the new size in the height. |
|---|
| 23 | */ |
|---|
| 24 | void WindowHandler::ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window |
|---|
| 25 | { |
|---|
| 26 | if (height==0) // Prevent A Divide By Zero By |
|---|
| 27 | { |
|---|
| 28 | height=1;// Making Height Equal One |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | glViewport(0,0,width,height); // Reset The Current Viewport |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix |
|---|
| 35 | glLoadIdentity(); |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | // Calculate The Aspect Ratio Of The Window |
|---|
| 39 | gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); |
|---|
| 40 | gluLookAt (0,0,15, 0,0,0, 0,1,0); |
|---|
| 41 | |
|---|
| 42 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix |
|---|
| 43 | glLoadIdentity(); // Reset The Modelview Matrix |
|---|
| 44 | |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | \brief initializes openGL |
|---|
| 49 | */ |
|---|
| 50 | int WindowHandler::InitGL(GLvoid) // All Setup For OpenGL Goes Here |
|---|
| 51 | { |
|---|
| 52 | glEnable(GL_TEXTURE_2D); // Enable Texture Mapping |
|---|
| 53 | glShadeModel(GL_SMOOTH); // Enable Smooth Shading |
|---|
| 54 | glClearColor(0.00f, 0.00f, 0.00f, 0.0f); // Black Background |
|---|
| 55 | glClearDepth(1.0f); // Depth Buffer Setup |
|---|
| 56 | glEnable(GL_DEPTH_TEST); // Enables Depth Testing |
|---|
| 57 | glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do |
|---|
| 58 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations |
|---|
| 59 | |
|---|
| 60 | return TRUE; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | \brief kills the window |
|---|
| 65 | */ |
|---|
| 66 | GLvoid WindowHandler::KillGLWindow(GLvoid) // Properly Kill The Window |
|---|
| 67 | { |
|---|
| 68 | SDL_Quit(); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | \brief creates a new Window for the scene |
|---|
| 73 | \param title the name of the Window |
|---|
| 74 | \param width the width of the window |
|---|
| 75 | \param height the height of the window |
|---|
| 76 | \param bits the bits per pixel to use |
|---|
| 77 | \param fullscreenflag TRUE if fullscreen FALSE if windowed/ |
|---|
| 78 | \returns FALSE if initialisation failed, TRUE otherwise |
|---|
| 79 | */ |
|---|
| 80 | BOOL WindowHandler::CreateGLWindow(char* title, int width, int height, int bits, BOOL fullscreenflag) |
|---|
| 81 | { |
|---|
| 82 | Uint32 flags; |
|---|
| 83 | int size; |
|---|
| 84 | |
|---|
| 85 | /* Initialize SDL */ |
|---|
| 86 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
|---|
| 87 | fprintf(stderr, "Couldn't init SDL: %s\n", SDL_GetError()); |
|---|
| 88 | return FALSE; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | flags = SDL_OPENGL; |
|---|
| 92 | if ( fullscreenflag ) { |
|---|
| 93 | flags |= SDL_FULLSCREEN; |
|---|
| 94 | } |
|---|
| 95 | SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 1 ); |
|---|
| 96 | if ( (screen = SDL_SetVideoMode(width, height, 0, flags)) == NULL ) { |
|---|
| 97 | return FALSE; |
|---|
| 98 | } |
|---|
| 99 | SDL_GL_GetAttribute( SDL_GL_STENCIL_SIZE, &size); |
|---|
| 100 | |
|---|
| 101 | ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen |
|---|
| 102 | |
|---|
| 103 | if (!InitGL()) // Initialize Our Newly Created GL Window |
|---|
| 104 | { |
|---|
| 105 | KillGLWindow(); // Reset The Display |
|---|
| 106 | return FALSE; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | return TRUE; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|