home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser 2002 January / STC_CD_01_2002.iso / JAGUAR / JAG_SRC / SOURCE / VIDEO.C < prev   
C/C++ Source or Header  |  2001-08-17  |  11KB  |  282 lines

  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Jagulator: Atari Jaguar Console Emulation Project (video.c)
  3. // -----------------------------------------------------------------------------
  4. // Jagulator is the Copyright (c) RealityMan 1998-2001 and is provided "as is" 
  5. // without any expressed or implied warranty. I have no Trademarks, Legal or 
  6. // otherwise. Atari, Jaguar and the Atari Logo are copyright Hasbro Inc. All 
  7. // other Copyrights and Trademarks are acknowledged. This project is in no way 
  8. // linked to Atari/Hasbro or other associated Atari companies.                
  9. //
  10. // 07-07-2001 GH: New Source, Rewritten for Release 1.5.0
  11. // 00-00-0000 GH: All Previous Source Considered as Development Code Only
  12.  
  13. #include "core.h"
  14.  
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // Globals 
  17.  
  18.    HGLRC hRCMain;                      // Permanent Rendering Context
  19.    HDC hDCMain;                        // Private GDI Device Context
  20.    PAINTSTRUCT psMain;                 // Paint Structure
  21.  
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // Get OpenGL Information 
  24.  
  25.    void v_getglinfo( void )
  26.    {
  27.       FILE *glinfo;                    // OpenGL Information File Pointer
  28.       char *extensions = NULL;         // OpenGL Extension Pointer
  29.       int   nPixelFormat;              // Returned Pixel Format
  30.       static char buf[MAX_PATH];       // Temporary Buffer
  31.       static PIXELFORMATDESCRIPTOR pfd = {
  32.          sizeof(PIXELFORMATDESCRIPTOR),   
  33.          1,                            // Version of this Structure
  34.          PFD_DRAW_TO_WINDOW |          // Draw to Window (Not Bitmap)
  35.          PFD_SUPPORT_OPENGL |          // Support OpenGL Calls
  36.          PFD_DOUBLEBUFFER   |          // Double Buffered Mode
  37.          PFD_GENERIC_ACCELERATED |
  38.          PFD_TYPE_RGBA,                // RGBA Color Mode
  39.          32,                           // Want 'cdepth' Color
  40.          0, 0, 0, 0, 0, 0,             // Not Used to Select Mode
  41.          0, 0,                         // Not Used to Select Mode
  42.          0, 0, 0, 0, 0,                // Not Used to Select Mode
  43.          32,                           // 'cdepth' Depth Buffer
  44.          0,                            // Not Used to Select Mode
  45.          0,                            // Not Used to Select Mode
  46.          PFD_MAIN_PLANE,               // Draw in Main Plane
  47.          0,                            // Not Used to Select Mode
  48.          0, 0, 0                       // Not Used to Select Mode
  49.       };
  50.       extern HWND hwndMain;
  51.  
  52.       hDCMain = GetDC( hwndMain );     // Store the Device Context
  53.  
  54.       // Choose a Pixel Format that Best Matches Above
  55.       nPixelFormat = ChoosePixelFormat( hDCMain, &pfd );
  56.  
  57.       // Set the Pixel Format for the Device Context
  58.       SetPixelFormat( hDCMain, nPixelFormat, &pfd );
  59.  
  60.       // Create the Rendering Context and Make it Current
  61.       hRCMain = wglCreateContext( hDCMain );
  62.       wglMakeCurrent( hDCMain, hRCMain );
  63.  
  64.       // Setup Temporary File
  65.       GetTempPath( MAX_PATH, buf );
  66.       strcat( buf, "oglinfo" );
  67.  
  68.       glinfo = fopen( buf, "w" );
  69.       // Get the OpenGL Vendor
  70.       extensions = (char *)glGetString( GL_VENDOR );
  71.       fwrite( extensions, strlen(extensions) + 1, 1, glinfo );
  72.       // Get the OpenGL Renderer
  73.       extensions = (char *)glGetString( GL_RENDERER );
  74.       fwrite( extensions, strlen(extensions) + 1, 1, glinfo );
  75.       // Get the OpenGL Version
  76.       extensions = (char *)glGetString( GL_VERSION );
  77.       fwrite( extensions, strlen(extensions) + 1, 1, glinfo );
  78.       // Get the OpenGL Extension List
  79.       extensions = (char *)glGetString( GL_EXTENSIONS );
  80.       fwrite( extensions, strlen(extensions) + 1, 1, glinfo );
  81.       fclose( glinfo );
  82.       
  83.       // Deselect the Current Rendering Context and Delete it
  84.       wglMakeCurrent( hDCMain, NULL );
  85.       wglDeleteContext( hRCMain );
  86.    }
  87.  
  88. ////////////////////////////////////////////////////////////////////////////////
  89. // Initialise OpenGL 
  90.  
  91.    int v_init( int width, int height )
  92.    {
  93.       int nPixelFormat;                // Returned Pixel Format
  94.       static PIXELFORMATDESCRIPTOR pfd = {
  95.          sizeof(PIXELFORMATDESCRIPTOR),   
  96.          1,                            // Version of this Structure
  97.          PFD_DRAW_TO_WINDOW |          // Draw to Window (Not Bitmap)
  98.          PFD_SUPPORT_OPENGL |          // Support OpenGL Calls
  99.          PFD_DOUBLEBUFFER   |          // Double Buffered Mode
  100.          PFD_GENERIC_ACCELERATED |
  101.          PFD_TYPE_RGBA,                // RGBA Color Mode
  102.          32,                           // Want 32-Bit Color
  103.          0, 0, 0, 0, 0, 0,             // Not Used to Select Mode
  104.          0, 0,                         // Not Used to Select Mode
  105.          0, 0, 0, 0, 0,                // Not Used to Select Mode
  106.          32,                           // Size of Depth Buffer
  107.          0,                            // Not Used to Select Mode
  108.          0,                            // Not Used to Select Mode
  109.          PFD_MAIN_PLANE,               // Draw in Main Plane
  110.          0,                            // Not Used to Select Mode
  111.          0, 0, 0                       // Not Used to Select Mode
  112.       };
  113.  
  114.       // Resize window so that the desired resolution is completely shown.
  115.       // (Width + 9, Height + 75)
  116.       MoveWindow( hwndMain, 20, 20, width + 9, height + 46, TRUE );
  117.       //ShowWindow( hwndStatus, SW_HIDE );  // Hide Status Bar
  118.  
  119.       hDCMain = GetDC( hwndMain );     // Store the Device Context
  120.  
  121.       // Choose a Pixel Format that Best Matches Above
  122.       nPixelFormat = ChoosePixelFormat( hDCMain, &pfd );
  123.  
  124.       if( !nPixelFormat )
  125.       {
  126.          MessageBox( NULL, "ChoosePixelFormat() Failed.", "OpenGL", 
  127.                      MB_ICONEXCLAMATION | MB_OK );
  128.          return( 1 );
  129.       }
  130.  
  131.       // Set the Pixel Format for the Device Context
  132.       SetPixelFormat( hDCMain, nPixelFormat, &pfd );
  133.  
  134.       // Create the Rendering Context and Make it Current
  135.       hRCMain = wglCreateContext( hDCMain );
  136.       wglMakeCurrent( hDCMain, hRCMain );
  137.  
  138.       return( 0 );
  139.    }
  140.  
  141. ////////////////////////////////////////////////////////////////////////////////
  142. // Uninitialise OpenGL 
  143.  
  144.    void v_deinit( void )
  145.    {
  146.       hDCMain = BeginPaint( hwndMain, &psMain );
  147.       wglMakeCurrent( hDCMain, hRCMain );
  148.       glClearColor( 0.0f, 0.0f, 0.0f, 1.0f);
  149.       glClearDepth( 1.0f );
  150.       glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  151.       SwapBuffers( hDCMain );      glClearColor( 0.0f, 0.0f, 0.0f, 1.0f);
  152.       wglMakeCurrent( NULL, NULL );
  153.       EndPaint( hwndMain, &psMain );
  154.       // Deselect the Current Rendering Context and Delete it
  155.       wglMakeCurrent( hDCMain, NULL );
  156.       wglDeleteContext( hRCMain );
  157.    }
  158.  
  159. ////////////////////////////////////////////////////////////////////////////////
  160. // Start Frame 
  161.  
  162.    static void v_startframe( void )
  163.    {
  164.       hDCMain = BeginPaint( hwndMain, &psMain );
  165.       wglMakeCurrent( hDCMain, hRCMain );
  166.    }
  167.  
  168. ////////////////////////////////////////////////////////////////////////////////
  169. // End Frame 
  170.  
  171.    void v_endframe( void )
  172.    {
  173.       wglMakeCurrent( NULL, NULL );
  174.       EndPaint( hwndMain, &psMain );
  175.    }
  176.  
  177. ////////////////////////////////////////////////////////////////////////////////
  178. // Define Draw Mode 
  179.  
  180.    static void v_realdrawmode( void )
  181.    {
  182.       glViewport(0, 0, cfg.hres, cfg.vres);
  183. /*   
  184.       glMatrixMode(GL_PROJECTION);
  185.       glLoadIdentity();
  186.       glOrtho(0.0, (GLfloat)cfg.hres, 0.0, (GLfloat)cfg.vres, -1.0, 1.0);
  187.       glMatrixMode(GL_MODELVIEW);
  188. */
  189.       glMatrixMode(GL_PROJECTION);
  190.       glLoadIdentity();
  191.       glOrtho(0, (GLfloat)cfg.hres, (GLfloat)cfg.vres, 0, -1, 1);
  192.       glMatrixMode(GL_MODELVIEW);
  193.       glLoadIdentity(); 
  194.    }
  195.  
  196. ////////////////////////////////////////////////////////////////////////////////
  197. // Clear Frame Buffer 
  198.  
  199.    static void v_clear( int color )
  200.    {
  201.       //glClearColor( (GLclampf)(0.03 * color), (GLclampf)(0.03 * color), 
  202.       //              (GLclampf)(0.03 * color), (GLclampf)(1.0f) );
  203.       //glClearColor( 0.0f, 0.0f, 0.0f, 1.0f);
  204.       glClearColor( st.br, st.bg, st.bb, 1.0f);
  205.       glClearDepth( 1.0f );
  206.       glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  207.    }
  208.  
  209. ////////////////////////////////////////////////////////////////////////////////
  210. // Swap Front and Back Buffers
  211.  
  212.    static void v_swap( void )
  213.    {
  214.       SwapBuffers( hDCMain );
  215.  
  216.       glViewport( 0, 0, cfg.hres, cfg.vres );
  217.  
  218.       v_clear( 0 );    
  219.  
  220.       v_realdrawmode();
  221.    }
  222.  
  223. ////////////////////////////////////////////////////////////////////////////////
  224. // Open Display 
  225.  
  226.    static void v_opendisplay( void )
  227.    {
  228.       #ifdef DEBUG
  229.       print( CYAN"Graphics Initialized: [%i x %i]\n", cfg.hres, cfg.vres );
  230.       #endif
  231.       
  232.       //v_init( cfg.hres, cfg.vres );
  233.  
  234.       v_swap();
  235.    }
  236.  
  237.  
  238. ////////////////////////////////////////////////////////////////////////////////
  239. //                         Public Video Routines                              //
  240. ////////////////////////////////////////////////////////////////////////////////
  241.  
  242. ////////////////////////////////////////////////////////////////////////////////
  243. // Open Display 
  244.  
  245.    void obj_opendisplay( void )
  246.    {
  247.       if( st.opened ) return;         // Return if Already Opened
  248.       st.opened = 1;                  // Set 'Opened' Flag
  249.  
  250.       #ifdef DEBUG
  251.       print( CYAN"Open Display\n" );
  252.       #endif
  253.     
  254.       v_opendisplay();                 // Open the Graphics Display
  255.    }
  256.  
  257. ////////////////////////////////////////////////////////////////////////////////
  258. // Start of Frame 
  259.  
  260.    void obj_framestart( void )
  261.    {
  262.       if( st.frameopen ) return;      // Return if Frame Already Opened
  263.       st.frameopen = 1;               // Set 'Opened' Flag
  264.    
  265.       v_startframe();
  266.       obj_opendisplay();               // Open Graphics Display
  267.    }
  268.  
  269. ////////////////////////////////////////////////////////////////////////////////
  270. // End of Frame
  271.  
  272.    void obj_frameend( void )
  273.    {
  274.       if( !st.frameopen ) return;     // Return if Frame Already Ended
  275.       st.frameopen = 0;               // Unset 'Opened' Flag
  276.  
  277.       v_swap();                        // Show Buffer on Screen
  278.  
  279.       v_endframe();
  280.    }
  281.  
  282.