Tuesday, September 29, 2009

Embedded C++ (A better Hello World and OpenGLES)

This one has support for fullscreen mode (Make sure you link to aygshell.lib for this to work)

Also make sure you setup the environment for Standard SDK 500.

The example shows a rotating triangle.

You can toggle fullscreen mode by touching the screen (or using a stylus).

// // MainApp.cpp // #include "stdafx.h" #include #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // The current instance HWND hWnd; bool running; bool fullscreen; //OpenGLES variables bool opengl_loaded; HDC hdc; EGLDisplay display; EGLSurface surface; EGLContext context; GLfloat colors[3][4] = { {1.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} }; GLfloat vertices[3][3] = { {0.0f, 0.7f, 0.0f}, {-0.7f, -0.7f, 0.0f}, {0.7f, -0.7f, 0.0f} }; float rotation; //Declare the function for the windows messages LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); bool LoadOpenGLES() { opengl_loaded = false; hdc = GetDC(hWnd); // the screen or window device context, for example display = eglGetDisplay(hdc); EGLint major, minor; if (!eglInitialize(display, &major, &minor)) { // could not initialize display eglTerminate(display); ReleaseDC(hWnd, hdc); return false; } EGLConfig* configs = NULL; EGLint matchingConfigs = 0; EGLint attribList[] = { EGL_ALPHA_SIZE, 0, EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_BLUE_SIZE, 5, EGL_DEPTH_SIZE, 16 , EGL_SURFACE_TYPE, EGL_WINDOW_BIT,//|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT, EGL_NONE }; // extend this bool returnval = false; wchar_t* info = new wchar_t[100]; int num_configs = 0; if (eglGetConfigs(display,NULL,0,&num_configs)) { EGLConfig config = NULL; if (num_configs>0) { configs = new EGLConfig[num_configs]; eglGetConfigs(display,configs,sizeof(EGLConfig),&num_configs); eglChooseConfig(display, attribList, &config, 1, &matchingConfigs); } if (matchingConfigs>0) { surface = eglCreateWindowSurface(display, config, hWnd, NULL); context = eglCreateContext(display, config, 0, NULL); eglMakeCurrent(display, surface, surface, context); swprintf(info,L"Version: %i.%i\n%S\n%S",major,minor,(char*)glGetString(GL_RENDERER),(char*)glGetString(GL_VERSION)); MessageBox(NULL,info,L"OPENGLES",MB_OK); opengl_loaded = true; returnval = true; } else MessageBox(hWnd,L"No matching configs found",L"OPENGLES",MB_OK|MB_ICONERROR); } else MessageBox(hWnd,L"No configs found",L"OPENGLES",MB_OK|MB_ICONERROR); delete[] info; return returnval; } bool UnloadOpenGLES() { if (opengl_loaded) { opengl_loaded = false; eglDestroyContext(display, context); eglDestroySurface(display, surface); eglTerminate(display); ReleaseDC(hWnd, hdc); return true; } return false; } void UpdateApp() { rotation = (GetTickCount()%5000)*360.0f/5000.0f; //one full rotation every 5 sec InvalidateRect(hWnd, NULL, FALSE); UpdateWindow(hWnd); } bool ToggleFullscreen() { if (fullscreen) { //change to not fullscreen HWND desktop = GetDesktopWindow(); RECT r = {0,0,0,0}; GetWindowRect(desktop, &r); SetWindowPos(hWnd,HWND_NOTOPMOST,0,GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYFIXEDFRAME),r.right-r.left,r.bottom-r.top,0); SHFullScreen(hWnd,SHFS_SHOWTASKBAR|SHFS_SHOWSIPBUTTON); fullscreen = !fullscreen; } else { //change to fullscreen SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),0); SHFullScreen(hWnd,SHFS_HIDETASKBAR|SHFS_HIDESIPBUTTON); fullscreen = true; } UpdateWindow(hWnd); return true; } //This is the main function that is called when the app starts int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { //Create a message object to handle the windows messages MSG msg; running = false; opengl_loaded = false; fullscreen = false; //Setup the variables for the window wchar_t* szTitle = L"MainApp"; // The title bar text wchar_t* szWindowClass = L"Example1Window"; // The window class name hInst = hInstance; //Register window classs WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = 0; wc.hCursor = 0; wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszMenuName = 0; wc.lpszClassName = szWindowClass; RegisterClass(&wc); //Create the window hWnd = CreateWindowEx(WS_EX_CAPTIONOKBTN, szWindowClass, szTitle, WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; fullscreen=false; ToggleFullscreen(); //make it fullscreen ShowWindow(hWnd, SW_SHOWNORMAL); //Load OpenGLES if (LoadOpenGLES()) { //setup state variables glClearColor(0, 0, 0, 0); glClearDepthf(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_RGBA); glEnable(GL_BLEND); glEnable(GL_ALPHA_TEST); glShadeModel(GL_SMOOTH); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //DEBUG testing for performance glDisable(GL_DEPTH_TEST); glDisable(GL_BLEND); glDisable(GL_ALPHA_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //setup viewport RECT rect; GetWindowRect(hWnd,&rect); glViewport(rect.top+32,rect.left,rect.bottom,rect.right); //RED glClearColor(1.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); eglSwapBuffers(display, surface); Sleep(500); //GREEN glClearColor(0.0f,1.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); eglSwapBuffers(display, surface); Sleep(500); //BLUE glClearColor(0.0f,0.0f,1.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); eglSwapBuffers(display, surface); Sleep(500); //BLACK (with Triangle) glClearColor(0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); eglSwapBuffers(display, surface); } // Main message loop: running = true; while (running) { if (PeekMessage(&msg, hWnd,0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } UpdateApp(); } //Free up OpenGLES UnloadOpenGLES(); return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_ERASEBKGND: if (running) return 0; break; case WM_COMMAND: { if (MessageBox(NULL,L"Are you sure?",L"Exiting",MB_YESNO)==IDYES) { running = false; return 0; } break; } case WM_PAINT: { if (opengl_loaded) { RECT rect; GetWindowRect(hWnd,&rect); //BLACK (with Triangle) glViewport(0,0,rect.right-rect.left,rect.bottom-rect.top); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(rotation, 0.0f, 1.0f, 0.0f); glClearColor(0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glColorPointer(4, GL_FLOAT, 0, colors); glVertexPointer(3, GL_FLOAT, 0, vertices); // Draw the triangle (3 vertices) glDrawArrays(GL_TRIANGLES, 0, 3); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); eglSwapBuffers(display, surface); eglWaitGL(); } else if (running) { PAINTSTRUCT ps; RECT rt; HDC hdc = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &rt); wchar_t* temp = new wchar_t[25]; swprintf(temp,L"OpenGLES not loaded (%i)", GetTickCount()); DrawText(hdc, temp, wcslen(temp), &rt, DT_SINGLELINE | DT_VCENTER | DT_CENTER); delete[] temp; EndPaint(hWnd, &ps); return 0; } } break; case WM_LBUTTONDOWN: { ToggleFullscreen(); } break; case WM_DESTROY: running = false; break; } return DefWindowProc(hWnd, message, wParam, lParam); }

No comments:

Post a Comment