Home | History | Annotate | Download | only in Win32
      1 //
      2 // Book:      OpenGL(R) ES 2.0 Programming Guide
      3 // Authors:   Aaftab Munshi, Dan Ginsburg, Dave Shreiner
      4 // ISBN-10:   0321502795
      5 // ISBN-13:   9780321502797
      6 // Publisher: Addison-Wesley Professional
      7 // URLs:      http://safari.informit.com/9780321563835
      8 //            http://www.opengles-book.com
      9 //
     10 
     11 // esUtil_win32.c
     12 //
     13 //    This file contains the Win32 implementation of the windowing functions.
     14 
     15 
     16 ///
     17 // Includes
     18 //
     19 
     20 #include <windows.h>
     21 #include "esUtil.h"
     22 
     23 //////////////////////////////////////////////////////////////////
     24 //
     25 //  Private Functions
     26 //
     27 //
     28 
     29 ///
     30 //  ESWindowProc()
     31 //
     32 //      Main window procedure
     33 //
     34 LRESULT WINAPI ESWindowProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
     35 {
     36    LRESULT  lRet = 1;
     37 
     38    switch (uMsg)
     39    {
     40       case WM_CREATE:
     41          break;
     42 
     43       case WM_SIZE:
     44          {
     45             ESContext *esContext = (ESContext*)(LONG_PTR) GetWindowLongPtr ( hWnd, GWL_USERDATA );
     46             if ( esContext ) {
     47                esContext->width = LOWORD( lParam );
     48                esContext->height = HIWORD( lParam );
     49                InvalidateRect( esContext->hWnd, NULL, FALSE );
     50             }
     51          }
     52 
     53       case WM_PAINT:
     54          {
     55             ESContext *esContext = (ESContext*)(LONG_PTR) GetWindowLongPtr ( hWnd, GWL_USERDATA );
     56 
     57             if ( esContext && esContext->drawFunc )
     58                esContext->drawFunc ( esContext );
     59 
     60             if ( esContext )
     61               ValidateRect( esContext->hWnd, NULL );
     62          }
     63          break;
     64 
     65       case WM_DESTROY:
     66          PostQuitMessage(0);
     67          break;
     68 
     69       case WM_CHAR:
     70          {
     71             POINT      point;
     72             ESContext *esContext = (ESContext*)(LONG_PTR) GetWindowLongPtr ( hWnd, GWL_USERDATA );
     73 
     74             GetCursorPos( &point );
     75 
     76             if ( esContext && esContext->keyFunc )
     77 	            esContext->keyFunc ( esContext, (unsigned char) wParam,
     78 		                             (int) point.x, (int) point.y );
     79 }
     80          break;
     81 
     82       default:
     83          lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
     84          break;
     85    }
     86 
     87    return lRet;
     88 }
     89 
     90 //////////////////////////////////////////////////////////////////
     91 //
     92 //  Public Functions
     93 //
     94 //
     95 
     96 ///
     97 //  WinCreate()
     98 //
     99 //      Create Win32 instance and window
    100 //
    101 GLboolean WinCreate ( ESContext *esContext, LPCTSTR title )
    102 {
    103    WNDCLASS wndclass = {0};
    104    DWORD    wStyle   = 0;
    105    RECT     windowRect;
    106    HINSTANCE hInstance = GetModuleHandle(NULL);
    107 
    108    wndclass.style         = CS_OWNDC;
    109    wndclass.lpfnWndProc   = (WNDPROC)ESWindowProc;
    110    wndclass.hInstance     = hInstance;
    111    wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    112    wndclass.lpszClassName = TEXT("opengles2.0");
    113 
    114    if (!RegisterClass (&wndclass) )
    115       return FALSE;
    116 
    117    wStyle = WS_VISIBLE | WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION | WS_SIZEBOX;
    118 
    119    // Adjust the window rectangle so that the client area has
    120    // the correct number of pixels
    121    windowRect.left = 0;
    122    windowRect.top = 0;
    123    windowRect.right = esContext->width;
    124    windowRect.bottom = esContext->height;
    125 
    126    AdjustWindowRect ( &windowRect, wStyle, FALSE );
    127 
    128    esContext->hWnd = CreateWindow(
    129                          TEXT("opengles2.0"),
    130                          title,
    131                          wStyle,
    132                          0,
    133                          0,
    134                          windowRect.right - windowRect.left,
    135                          windowRect.bottom - windowRect.top,
    136                          NULL,
    137                          NULL,
    138                          hInstance,
    139                          NULL);
    140 
    141    // Set the ESContext* to the GWL_USERDATA so that it is available to the
    142    // ESWindowProc
    143    SetWindowLongPtr (  esContext->hWnd, GWL_USERDATA, (LONG) (LONG_PTR) esContext );
    144 
    145    if ( esContext->hWnd == NULL )
    146       return GL_FALSE;
    147 
    148    ShowWindow ( esContext->hWnd, TRUE );
    149 
    150    return GL_TRUE;
    151 }
    152 
    153 ///
    154 //  winLoop()
    155 //
    156 //      Start main windows loop
    157 //
    158 void WinLoop ( ESContext *esContext )
    159 {
    160    MSG msg = { 0 };
    161    int done = 0;
    162    DWORD lastTime = GetTickCount();
    163 
    164    while (!done)
    165    {
    166       int gotMsg = (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0);
    167       DWORD curTime = GetTickCount();
    168       float deltaTime = (float)( curTime - lastTime ) / 1000.0f;
    169       lastTime = curTime;
    170 
    171       if ( gotMsg )
    172       {
    173          if (msg.message==WM_QUIT)
    174          {
    175              done=1;
    176          }
    177          else
    178          {
    179              TranslateMessage(&msg);
    180              DispatchMessage(&msg);
    181          }
    182       }
    183       else
    184          SendMessage( esContext->hWnd, WM_PAINT, 0, 0 );
    185 
    186       // Call update function if registered
    187       if ( esContext->updateFunc != NULL )
    188          esContext->updateFunc ( esContext, deltaTime );
    189    }
    190 }
    191