Porting a Windows.h/OpenGLES program

Member
Posts: 312
Joined: 2006.10
Post: #1
Hey, I'm trying to port a a program in windows that uses window.h. Unfortunately, I don't have any experience with it Sad

If someone could provide the documentation for it, or explain what the following code does (psuedo, or plain english Rasp)

Code:
#include <windows.h>
#include <stdio.h>

bool g_initialized = false;
HWND g_hWnd;
HDC g_hDC;
HGLRC g_hRC;

void displayLastError (void)
{
    LPTSTR lpMsgBuf = NULL;
    
    int err = GetLastError();

    // format the error message string
    if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL, err,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR) &lpMsgBuf, 0,  NULL))
    {
        // display the message string
        printf("error: %i: %s", err, lpMsgBuf);
    }
    
    // free the message buffer
    LocalFree (lpMsgBuf);
    
    return;
}

LRESULT CALLBACK wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch (message) {
        case WM_CREATE:
            return 0;
    
        case WM_CLOSE:
            exit(0);
            return 0;
    
        case WM_DESTROY:
            return 0;
    
        case WM_KEYDOWN:
            switch (wParam) {
                case VK_ESCAPE:
                    exit(0);
                    break;
                }
            return 0;
    
        default:
            return DefWindowProc( hWnd, message, wParam, lParam );
    }
}

DWORD WINAPI guiThread(LPVOID lpParam)
{
    WNDCLASS wc;
    const char* windowClass = "luaplayerWindowclass";

    // create window
    ZeroMemory(&wc, sizeof(wc));
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = wndProc;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszClassName = windowClass;
    RegisterClass(&wc);
    g_hWnd = CreateWindow(windowClass, "Lua Player", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 100, 100, 500, 320, NULL, NULL, 0, NULL);

    // create OpenGL context
    PIXELFORMATDESCRIPTOR pfd;
    int format;
    g_hDC = GetDC(g_hWnd);
    ZeroMemory(&pfd, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    format = ChoosePixelFormat( g_hDC, &pfd );
    SetPixelFormat(g_hDC, format, &pfd);

    // create and enable the render context (RC)
    g_hRC = wglCreateContext(g_hDC);

    g_initialized = true;

    BOOL bRet;
    MSG msg;
    while ((bRet = GetMessage(&msg, NULL, 0, 0 )) != 0) {
        if (bRet == -1) {
            break;
        } else {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    // delete OpenGL context
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(g_hRC);
    ReleaseDC(g_hWnd, g_hDC);

    exit(0);
    return 0;
}

void initGui()
{
    if (!g_initialized) {
        DWORD threadId;
        CreateThread(NULL, 0, guiThread, 0, 0, &threadId);
        while (!g_initialized) Sleep(100);
        if (!wglMakeCurrent( g_hDC, g_hRC )) {
            displayLastError();
            printf("wglMakeCurrent failed\n");
            exit(-1);
        }
    }
}

int getFrameWidth()
{
    RECT r;
    GetClientRect(g_hWnd, &r);
    return r.right - r.left;
}

int getFrameHeight()
{
    RECT r;
    GetClientRect(g_hWnd, &r);
    return r.bottom - r.top;
}

void flipFrame()
{
    SwapBuffers( g_hDC );
    Sleep(10);
}

void releaseGui()
{
    if (g_initialized) {
        SendMessage(g_hWnd, WM_CLOSE, 0, 0);
        while (g_initialized) Sleep(100);
    }
}
Fortunately I only need to port these
Code:
extern void initGui();
extern int getFrameWidth();
extern int getFrameHeight();
extern void flipFrame();
extern void releaseGui();

Also, there is a OpenGLES equivalent, here:
Code:
static const EGLint attrib_list [] = {
    EGL_RED_SIZE, 1,
    EGL_GREEN_SIZE, 1,
    EGL_BLUE_SIZE, 1,
    EGL_ALPHA_SIZE, 0,
    EGL_DEPTH_SIZE, 0,
    EGL_NONE
};

static bool g_initialized = false;
static int g_width = 480;
static int g_height = 272;
static EGLDisplay dpy;
static EGLSurface surface;

void initGui()
{
    if (!g_initialized) {
        EGLConfig config;
        EGLint num_configs;
        EGLContext ctx;
        
        dpy = eglGetDisplay(0);
        eglInitialize(dpy, NULL, NULL);
        
        eglChooseConfig(dpy, attrib_list, &config, 1, &num_configs);
        
        ctx = eglCreateContext(dpy, config, NULL, NULL);
        surface = eglCreateWindowSurface(dpy, config, 0, NULL);
        eglMakeCurrent(dpy, surface, surface, ctx);

        g_initialized = true;
    }
}

int getFrameWidth()
{
    return g_width;
}

int getFrameHeight()
{
    return g_height;
}

void flipFrame()
{
    eglSwapBuffers(dpy, surface);
}

void releaseGui()
{
    if (g_initialized) {
        eglTerminate(dpy);
        g_initialized = false;
    }
}

So, are these just; creat a Window context, get the width of the window, get the height of the window, swap buffers, and releasing the window? So, could this all be easily achieved with Cocoa and a OpenGL (must use OpenGL) context?
Quote this message in a reply
Moderator
Posts: 592
Joined: 2002.12
Post: #2
bronxbomber92 Wrote:So, are these just; creat a Window context, get the width of the window, get the height of the window, swap buffers, and releasing the window? So, could this all be easily achieved with Cocoa and a OpenGL (must use OpenGL) context?

Yes you are correct.
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #3
Thanks. Could you tell me how I would swap the buffers?

edit - Also, the application is in C++, so could I keep everything else in c++, and just create a window and OpenGL context with Cocoa? Or would using GLUT just be best because of the ease?

Thanks
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
you should be able to find out how to swap buffers for the given API you are using.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #5
yeah, I've got it now. I decided to use GLUT and C++ Rasp
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
This is from here; I've done the Mac port.

http://forums.ps2dev.org/viewtopic.php?p=49209#49209
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #7
Yes, OneSadCookie, Shine (from PS2dev) and myself have all made a port, but OneSadCookie's is the best, so I won't bother relesing mine Rasp
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGLES video play back prathap 0 1,174 May 9, 2014 03:45 AM
Last Post: prathap