Home | History | Annotate | Download | only in jni
      1 /* San Angeles Observation OpenGL ES version example
      2  * Copyright 2004-2005 Jetro Lauha
      3  * All rights reserved.
      4  * Web: http://iki.fi/jetro/
      5  *
      6  * This source is free software; you can redistribute it and/or
      7  * modify it under the terms of EITHER:
      8  *   (1) The GNU Lesser General Public License as published by the Free
      9  *       Software Foundation; either version 2.1 of the License, or (at
     10  *       your option) any later version. The text of the GNU Lesser
     11  *       General Public License is included with this source in the
     12  *       file LICENSE-LGPL.txt.
     13  *   (2) The BSD-style license that is included with this source in
     14  *       the file LICENSE-BSD.txt.
     15  *
     16  * This source is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
     19  * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
     20  *
     21  * $Id: app-win32.c,v 1.6 2005/02/24 20:29:00 tonic Exp $
     22  * $Revision: 1.6 $
     23  */
     24 
     25 #define WIN32_LEAN_AND_MEAN
     26 #include <windows.h>
     27 #include <tchar.h>
     28 #ifdef UNDER_CE
     29 #include <aygshell.h>
     30 #endif
     31 
     32 #include <stdio.h>
     33 
     34 #include "importgl.h"
     35 
     36 #include "app.h"
     37 
     38 
     39 int gAppAlive = 1;
     40 
     41 static HINSTANCE sInstance;
     42 
     43 static const _TCHAR sAppName[] =
     44     _T("San Angeles Observation OpenGL ES version example (Win32)");
     45 static HWND sWnd;
     46 static int sWindowWidth = WINDOW_DEFAULT_WIDTH;
     47 static int sWindowHeight = WINDOW_DEFAULT_HEIGHT;
     48 static EGLDisplay sEglDisplay = EGL_NO_DISPLAY;
     49 static EGLConfig sEglConfig;
     50 static EGLContext sEglContext = EGL_NO_CONTEXT;
     51 static EGLSurface sEglSurface = EGL_NO_SURFACE;
     52 
     53 
     54 static void checkGLErrors()
     55 {
     56     GLenum error = glGetError();
     57     if (error != GL_NO_ERROR)
     58     {
     59         _TCHAR errorString[32];
     60         _stprintf(errorString, _T("0x%04x"), error);
     61         MessageBox(NULL, errorString, _T("GL Error"), MB_OK);
     62     }
     63 }
     64 
     65 
     66 static void checkEGLErrors()
     67 {
     68     EGLint error = eglGetError();
     69     if (error != EGL_SUCCESS)
     70     {
     71         _TCHAR errorString[32];
     72         _stprintf(errorString, _T("0x%04x"), error);
     73         MessageBox(NULL, errorString, _T("EGL Initialization Error"), MB_OK);
     74     }
     75 }
     76 
     77 
     78 static BOOL initEGL(HWND wnd)
     79 {
     80     static const EGLint configAttribs[] =
     81     {
     82 #if (WINDOW_BPP == 16)
     83         EGL_RED_SIZE,       5,
     84         EGL_GREEN_SIZE,     5,
     85         EGL_BLUE_SIZE,      5,
     86 #elif (WINDOW_BPP == 32)
     87         EGL_RED_SIZE,       8,
     88         EGL_GREEN_SIZE,     8,
     89         EGL_BLUE_SIZE,      8,
     90 #else
     91 #error WINDOW_BPP must be 16 or 32
     92 #endif
     93         EGL_DEPTH_SIZE,     16,
     94         EGL_ALPHA_SIZE,     EGL_DONT_CARE,
     95         EGL_STENCIL_SIZE,   EGL_DONT_CARE,
     96         EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
     97         EGL_NONE
     98     };
     99     EGLBoolean success;
    100     EGLint numConfigs;
    101     EGLint majorVersion;
    102     EGLint minorVersion;
    103 #ifdef PVRSDK
    104     HDC dc;
    105 #endif // PVRSDK
    106 
    107 #ifndef DISABLE_IMPORTGL
    108     int importGLResult;
    109     importGLResult = importGLInit();
    110     if (!importGLResult)
    111         return FALSE;
    112 #endif // !DISABLE_IMPORTGL
    113 
    114 #ifdef PVRSDK
    115     dc = GetDC(sWnd);
    116     sEglDisplay = eglGetDisplay(dc);
    117 #else
    118     sEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    119 #endif // !PVRSDK
    120     success = eglInitialize(sEglDisplay, &majorVersion, &minorVersion);
    121     if (success != EGL_FALSE)
    122         success = eglGetConfigs(sEglDisplay, NULL, 0, &numConfigs);
    123     if (success != EGL_FALSE)
    124         success = eglChooseConfig(sEglDisplay, configAttribs,
    125                                   &sEglConfig, 1, &numConfigs);
    126     if (success != EGL_FALSE)
    127     {
    128         sEglSurface = eglCreateWindowSurface(sEglDisplay, sEglConfig,
    129                                              wnd, NULL);
    130         if (sEglSurface == EGL_NO_SURFACE)
    131             success = EGL_FALSE;
    132     }
    133     if (success != EGL_FALSE)
    134     {
    135         sEglContext = eglCreateContext(sEglDisplay, sEglConfig, NULL, NULL);
    136         if (sEglContext == EGL_NO_CONTEXT)
    137             success = EGL_FALSE;
    138     }
    139     if (success != EGL_FALSE)
    140         success = eglMakeCurrent(sEglDisplay, sEglSurface,
    141                                  sEglSurface, sEglContext);
    142 
    143     if (success == EGL_FALSE)
    144         checkEGLErrors();
    145 
    146     return success;
    147 }
    148 
    149 
    150 static void deinitEGL()
    151 {
    152     eglMakeCurrent(sEglDisplay, NULL, NULL, NULL);
    153     eglDestroyContext(sEglDisplay, sEglContext);
    154     eglDestroySurface(sEglDisplay, sEglSurface);
    155     eglTerminate(sEglDisplay);
    156 #ifndef DISABLE_IMPORTGL
    157     importGLDeinit();
    158 #endif // !DISABLE_IMPORTGL
    159 }
    160 
    161 
    162 static LRESULT CALLBACK wndProc(HWND wnd, UINT message,
    163                                 WPARAM wParam, LPARAM lParam)
    164 {
    165     RECT rc;
    166     int useDefWindowProc = 0;
    167 
    168     switch (message)
    169     {
    170     case WM_CLOSE:
    171         DestroyWindow(wnd);
    172         gAppAlive = 0;
    173         break;
    174 
    175     case WM_DESTROY:
    176         PostQuitMessage(0);
    177         gAppAlive = 0;
    178         break;
    179 
    180     case WM_KEYDOWN:
    181         if (wParam == VK_ESCAPE || wParam == VK_RETURN)
    182             gAppAlive = 0;
    183         useDefWindowProc = 1;
    184         break;
    185 
    186     case WM_KEYUP:
    187         useDefWindowProc = 1;
    188         break;
    189 
    190     case WM_SIZE:
    191         GetClientRect(sWnd, &rc);
    192         sWindowWidth = rc.right;
    193         sWindowHeight = rc.bottom;
    194         break;
    195 
    196     default:
    197         useDefWindowProc = 1;
    198     }
    199 
    200     if (useDefWindowProc)
    201         return DefWindowProc(wnd, message, wParam, lParam);
    202     return 0;
    203 }
    204 
    205 
    206 int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance,
    207                    LPTSTR cmdLine, int cmdShow)
    208 {
    209     MSG msg;
    210     WNDCLASS wc;
    211     DWORD windowStyle;
    212     int windowX, windowY;
    213 
    214     // not referenced:
    215     prevInstance = prevInstance;
    216     cmdLine = cmdLine;
    217 
    218 
    219     sInstance = instance;
    220 
    221     // register class
    222     wc.style = CS_HREDRAW | CS_VREDRAW;
    223     wc.lpfnWndProc = (WNDPROC)wndProc;
    224     wc.cbClsExtra = 0;
    225     wc.cbWndExtra = 0;
    226     wc.hInstance = sInstance;
    227     wc.hIcon = NULL;
    228     wc.hCursor = 0;
    229     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
    230     wc.lpszMenuName = NULL;
    231     wc.lpszClassName = sAppName;
    232     if (!RegisterClass(&wc))
    233         return FALSE;
    234 
    235     // init instance
    236     windowStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
    237 #ifdef UNDER_CE
    238     sWindowWidth = GetSystemMetrics(SM_CXSCREEN);
    239     sWindowHeight = GetSystemMetrics(SM_CYSCREEN);
    240     windowX = windowY = 0;
    241 #else
    242     windowStyle |= WS_OVERLAPPEDWINDOW;
    243     windowX = CW_USEDEFAULT;
    244     windowY = 0;
    245 #endif
    246     sWnd = CreateWindow(sAppName, sAppName, windowStyle,
    247                         windowX, windowY,
    248                         sWindowWidth, sWindowHeight,
    249                         NULL, NULL, instance, NULL);
    250     if (!sWnd)
    251         return FALSE;
    252 
    253     ShowWindow(sWnd, cmdShow);
    254 
    255 #ifdef UNDER_CE
    256     SHFullScreen(sWnd,
    257                  SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON | SHFS_HIDESTARTICON);
    258     MoveWindow(sWnd, 0, 0, sWindowWidth, sWindowHeight, TRUE);
    259 #endif
    260 
    261     UpdateWindow(sWnd);
    262 
    263     if (!initEGL(sWnd))
    264         return FALSE;
    265 
    266     appInit(sWindowWidth, sWindowHeight);
    267 
    268     while (gAppAlive)
    269     {
    270         while (PeekMessage(&msg, sWnd, 0, 0, PM_NOREMOVE))
    271         {
    272             if (GetMessage(&msg, sWnd, 0, 0))
    273             {
    274                 TranslateMessage(&msg);
    275                 DispatchMessage(&msg);
    276             }
    277             else
    278                 gAppAlive = 0;
    279         }
    280 
    281         if (gAppAlive)
    282         {
    283             appRender(GetTickCount(), sWindowWidth, sWindowHeight);
    284             checkGLErrors();
    285             eglSwapBuffers(sEglDisplay, sEglSurface);
    286             checkEGLErrors();
    287         }
    288     }
    289 
    290     appDeinit();
    291     deinitEGL();
    292 
    293     return 0;
    294 }
    295