home *** CD-ROM | disk | FTP | other *** search
/ GameStar Special 2004 August / GSSH0804.iso / Action / TumikiFighters / tf0_2.exe / tf / src / abagames / util / sdl / screen3d.d < prev    next >
Text File  |  2004-05-15  |  3KB  |  113 lines

  1. /*
  2.  * $Id: screen3d.d,v 1.2 2004/05/14 14:35:39 kenta Exp $
  3.  *
  4.  * Copyright 2003 Kenta Cho. All rights reserved.
  5.  */
  6. module abagames.util.sdl.screen3d;
  7.  
  8. private import std.string;
  9. private import SDL;
  10. private import opengl;
  11. private import abagames.util.logger;
  12. private import abagames.util.sdl.screen;
  13. private import abagames.util.sdl.sdlexception;
  14.  
  15. /**
  16.  * SDL screen handler(3D, OpenGL).
  17.  */
  18. public class Screen3D: Screen {
  19.  public:
  20.   static float brightness = 1;
  21.   static int width = 640;
  22.   static int height = 480;
  23.   static bool windowMode = false;
  24.   static float nearPlane = 0.1;
  25.   static float farPlane = 1000;
  26.  
  27.  private:
  28.  
  29.   protected abstract void init();
  30.   protected abstract void close();
  31.  
  32.   public void initSDL() {
  33.     // Initialize SDL.
  34.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  35.       throw new SDLInitFailedException(
  36.     "Unable to initialize SDL: " ~ std.string.toString(SDL_GetError()));
  37.     }
  38.     // Create an OpenGL screen.
  39.     Uint32 videoFlags;
  40.     if (windowMode) {
  41.       videoFlags = SDL_OPENGL | SDL_RESIZABLE;
  42.     } else {
  43.       videoFlags = SDL_OPENGL | SDL_FULLSCREEN;
  44.     } 
  45.     if (SDL_SetVideoMode(width, height, 0, videoFlags) == null) {
  46.       throw new SDLInitFailedException
  47.     ("Unable to create SDL screen: " ~ std.string.toString(SDL_GetError()));
  48.     }
  49.     glViewport(0, 0, width, height);
  50.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  51.     resized(width, height);
  52.     SDL_ShowCursor(SDL_DISABLE);
  53.     init();
  54.   }
  55.  
  56.   // Reset viewport when the screen is resized.
  57.  
  58.   public void screenResized() {
  59.     glViewport(0, 0, width, height);
  60.     glMatrixMode(GL_PROJECTION);
  61.     glLoadIdentity();
  62.     //gluPerspective(45.0f, cast(GLfloat) width / cast(GLfloat) height, nearPlane, farPlane);
  63.     glFrustum(-nearPlane,
  64.           nearPlane,
  65.           -nearPlane * cast(GLfloat)height / cast(GLfloat)width,
  66.           nearPlane * cast(GLfloat)height / cast(GLfloat)width,
  67.           0.1f, farPlane);
  68.     glMatrixMode(GL_MODELVIEW);
  69.   }
  70.  
  71.   public void resized(int width, int height) {
  72.     this.width = width; this.height = height;
  73.     screenResized();
  74.   }
  75.  
  76.   public void closeSDL() {
  77.     close();
  78.     SDL_ShowCursor(SDL_ENABLE);
  79.   }
  80.  
  81.   public void flip() {
  82.     handleError();
  83.     SDL_GL_SwapBuffers();
  84.   }
  85.  
  86.   public void clear() {
  87.     glClear(GL_COLOR_BUFFER_BIT);
  88.   }
  89.  
  90.   public void handleError() {
  91.     GLenum error = glGetError();
  92.     if (error == GL_NO_ERROR) return;
  93.     closeSDL();
  94.     throw new Exception("OpenGL error");
  95.   }
  96.  
  97.   protected void setCaption(char[] name) {
  98.     SDL_WM_SetCaption(std.string.toStringz(name), null);
  99.   }
  100.  
  101.   public static void setColor(float r, float g, float b) {
  102.     glColor3f(r * brightness, g * brightness, b * brightness);
  103.   }
  104.  
  105.   public static void setColor(float r, float g, float b, float a) {
  106.     glColor4f(r * brightness, g * brightness, b * brightness, a);
  107.   }
  108.  
  109.   public static void setClearColor(float r, float g, float b, float a) {
  110.     glClearColor(r * brightness, g * brightness, b * brightness, a);
  111.   }
  112. }
  113.