home *** CD-ROM | disk | FTP | other *** search
/ ring.yamanashi.ac.jp/pub/pc/freem/action/ / action.zip / a7xpg0_11.zip / a7xpg / src / abagames / util / sdl / Screen3D.d < prev    next >
Text File  |  2003-09-21  |  3KB  |  107 lines

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