Jump to content
  • Advertisement
Sign in to follow this  
Vectormycalculus

WinMain and Header

This topic is 3750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think im missing something in my Class OpenGLCube. Im getting an error Regarding the global pointers, setupProjection, render, and prepare. I dont receive these errors until within the LRESULT CALLBACK, and WinMain function, both having to do with global varibles. Heres the source for the basic OpenGL-Enabled Framework Window
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include "OpenGLCube.h"

bool exiting = false;
long windowWidth = 800;
long windowHeight = 600;
long windowBits = 32;
bool fullscreen = false;
HDC hDC;

OpenGLCube *g_glRender = NULL;

void SetupPixelFormat(HDC hDC)
{
    int pixelFormat;

    PIXELFORMATDESCRIPTOR pfd =
    {   
        sizeof(PIXELFORMATDESCRIPTOR),  // size
            1,                          // version
            PFD_SUPPORT_OPENGL |        // OpenGL window
            PFD_DRAW_TO_WINDOW |        // render to window
            PFD_DOUBLEBUFFER,           // support double-buffering
            PFD_TYPE_RGBA,              // color type
            32,                         // prefered color depth
            0, 0, 0, 0, 0, 0,           // color bits (ignored)
            0,                          // no alpha buffer
            0,                          // alpha bits (ignored)
            0,                          // no accumulation buffer
            0, 0, 0, 0,                 // accum bits (ignored)
            16,                         // depth buffer
            0,                          // no stencil buffer
            0,                          // no auxiliary buffers
            PFD_MAIN_PLANE,             // main layer
            0,                          // reserved
            0, 0, 0,                    // no layer, visible, damage masks
    };

    pixelFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, pixelFormat, &pfd);
}

LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static HDC hDC;
    static HGLRC hRC;
    int height, width;

    // dispatch messages
    switch (uMsg)
    {   
    case WM_CREATE:         // window creation
        hDC = GetDC(hWnd);
        SetupPixelFormat(hDC);
        //SetupPalette();
        hRC = wglCreateContext(hDC);
        wglMakeCurrent(hDC, hRC);
        break;

    case WM_DESTROY:            // window destroy
    case WM_QUIT:
    case WM_CLOSE:                  // windows is closing

        // deselect rendering context and delete it
        wglMakeCurrent(hDC, NULL);
        wglDeleteContext(hRC);

        // send WM_QUIT to message queue
        PostQuitMessage(0);
        break;

    case WM_SIZE:
        height = HIWORD(lParam);        // retrieve width and height
        width = LOWORD(lParam);

        g_glRender->SetupProjection(width, height);

        break;

    case WM_ACTIVATEAPP:        // activate app
        break;

    case WM_PAINT:              // paint
        PAINTSTRUCT ps;
        BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;

    case WM_LBUTTONDOWN:        // left mouse button
        break;

    case WM_RBUTTONDOWN:        // right mouse button
        break;

    case WM_MOUSEMOVE:          // mouse movement
        break;

    case WM_LBUTTONUP:          // left button release
        break;

    case WM_RBUTTONUP:          // right button release
        break;

    case WM_KEYUP:
        break;

    case WM_KEYDOWN:
        int fwKeys;
        LPARAM keyData;
        fwKeys = (int)wParam;    // virtual-key code 
        keyData = lParam;          // key data 

        switch(fwKeys)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            break;
        default:
            break;
        }

        break;

    default:
        break;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    WNDCLASSEX windowClass;     // window class
    HWND       hwnd;            // window handle
    MSG        msg;             // message
    DWORD      dwExStyle;       // Window Extended Style
    DWORD      dwStyle;         // Window Style
    RECT       windowRect;

    g_glRender = new OpenGLCube;

    windowRect.left=(long)0;                        // Set Left Value To 0
    windowRect.right=(long)windowWidth; // Set Right Value To Requested Width
    windowRect.top=(long)0;                         // Set Top Value To 0
    windowRect.bottom=(long)windowHeight;   // Set Bottom Value To Requested Height

    // fill out the window class structure
    windowClass.cbSize          = sizeof(WNDCLASSEX);
    windowClass.style           = CS_HREDRAW | CS_VREDRAW;
    windowClass.lpfnWndProc     = MainWindowProc;
    windowClass.cbClsExtra      = 0;
    windowClass.cbWndExtra      = 0;
    windowClass.hInstance       = hInstance;
    windowClass.hIcon           = LoadIcon(NULL, IDI_APPLICATION);  // default icon
    windowClass.hCursor         = LoadCursor(NULL, IDC_ARROW);      // default arrow
    windowClass.hbrBackground   = NULL;                             // don't need background
    windowClass.lpszMenuName    = NULL;                             // no menu
    windowClass.lpszClassName   = "GLClass";
    windowClass.hIconSm         = LoadIcon(NULL, IDI_WINLOGO);      // windows logo small icon

    // register the windows class
    if (!RegisterClassEx(&windowClass))
        return 0;

    if (fullscreen)                             // fullscreen?
    {
        DEVMODE dmScreenSettings;                   // device mode
        memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
        dmScreenSettings.dmSize = sizeof(dmScreenSettings); 
        dmScreenSettings.dmPelsWidth = windowWidth;         // screen width
        dmScreenSettings.dmPelsHeight = windowHeight;           // screen height
        dmScreenSettings.dmBitsPerPel = windowBits;             // bits per pixel
        dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

        // 
        if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
        {
            // setting display mode failed, switch to windowed
            MessageBox(NULL, "Display mode failed", NULL, MB_OK);
            fullscreen = FALSE; 
        }
    }

    if (fullscreen)                             // Are We Still In Fullscreen Mode?
    {
        dwExStyle=WS_EX_APPWINDOW;                  // Window Extended Style
        dwStyle=WS_POPUP;                       // Windows Style
        ShowCursor(FALSE);                      // Hide Mouse Pointer
    }
    else
    {
        dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;   // Window Extended Style
        dwStyle=WS_OVERLAPPEDWINDOW;                    // Windows Style
    }

    AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);     // Adjust Window To True Requested Size

    // class registered, so now create our window
    hwnd = CreateWindowEx(0,                                 // extended style
        "GLClass",                          // class name
        "OpenGL Cube",              // app name
        dwStyle | WS_CLIPCHILDREN |
        WS_CLIPSIBLINGS,
        0, 0,                               // x,y coordinate
        windowRect.right - windowRect.left,
        windowRect.bottom - windowRect.top, // width, height
        NULL,                               // handle to parent
        NULL,                               // handle to menu
        hInstance,                          // application instance
        0);                              // no extra params

    hDC = GetDC(hwnd);

    // check if window creation failed (hwnd would equal NULL)
    if (!hwnd)
        return 0;

    ShowWindow(hwnd, SW_SHOW);          // display the window
    UpdateWindow(hwnd);                 // update the window

    g_glRender->Initialize();

    while (!exiting)
    {
        g_glRender->Prepare(0.0f);
        g_glRender->Render();
        SwapBuffers(hDC);

        while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
        {
            if (!GetMessage (&msg, NULL, 0, 0))
            {
                exiting = true;
                break;
            }

            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }

    delete g_glRender;

    if (fullscreen)
    {
        ChangeDisplaySettings(NULL,0);          // If So Switch Back To The Desktop
        ShowCursor(TRUE);                       // Show Mouse Pointer
    }

    return (int)msg.wParam;
}

Errors are as follows ( trying to switch to MSVC++ ) CubeMain.cpp:82: error: 'class OpenGLCube' has no member named 'SetupProjection' CubeMain.cpp: In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)': CubeMain.cpp:230: error: 'class OpenGLCube' has no member named 'Prepare' CubeMain.cpp:231: error: 'class OpenGLCube' has no member named 'Render' and the header file, hopefully correct
#ifndef __GL_COMPONENT
#define __GL_COMPONENT

#include <windows.h>


class OpenGLCube
{

private:
        int width;
        int height;

public:
   OpenGLCube();
    virtual ~OpenGLCube();
       
       void Initialize();
       void Display();
       void Reshape(int w, int h);
       void SetupProjection(int width, int height);
       void Prepare(float dt);
       void Render();
       
};

#endif

Cube Source
#include <windows.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <gl/glut.h>

#include "OpenGLCube.h"

OpenGLCube::OpenGLCube()
{
}

OpenGLCube::~OpenGLCube()
{
}

void OpenGLCube::Initialize()
{
     glClearColor(0.0, 0.0, 0.0, 0.0);
     glShadeModel(GL_FLAT);
}

void SetupProjection(int width, int height)
{
}

void OpenGLCube::Prepare(float dt)
{
}

void OpenGLCube::Render()
{
}

void OpenGLCube::Display()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0, 1.0, 1.0);
     glLoadIdentity();        // clear the matrix stack
     
     // these are our viewing transofrmations
     gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
     glScalef(1.0, 2.0, 1.0);
     glutWireCube(1.0);
     glFlush();
}

void OpenGLCube::Reshape(int w, int h)
{
     glViewport(0, 0, (GLsizei) w, (GLsizei) h);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
     glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    //Initialize();
    //glutDisplayFunc(Display); // all 4 of the comment calls are undeclared
    //glutReshapeFunc(Reshape);
    //glutMainLoop();
    return 0;
}
   

if you noticed in the cube source file, the examples that im running have a 'main' function. Most of the calls are made using the GLUT library. Could someone explain what this function would do if it worked for me? Im expecting it to omit the use of my 'cubemain' producing a similar window. But 4 of these calls are undeclared, could someone be nice and help me out? :]

Share this post


Link to post
Share on other sites
Advertisement
I'm quite confused by your errors. Only thing I can spot, in the cube source, is this:


void SetupProjection(int width, int height)
{
}


should be this:


void OpenGLCube::SetupProjection(int width, int height)
{
}


But I've never seen that give a compiler error - that should be causing a linker error.

Regarding main() and WinMain(), as far as I know if you are building as a console application, WinMain() will be treated like any other function, and if you are building as a Windows application, main() will be treated like any other function. This is an assumption as I've never tested it.

So if that is correct and you are building as a Windows application, your main() function is doing nothing as it is never called.

Share this post


Link to post
Share on other sites
Quote:

Regarding main() and WinMain(), as far as I know if you are building as a console application, WinMain() will be treated like any other function, and if you are building as a Windows application, main() will be treated like any other function. This is an assumption as I've never tested it.

i'll take your word for it

What im thinking is that the main function does is create a window, and the PFD is 'behind' the glut function calls.

Removing the CubeMain.cpp leaves me with the following errors:

Cube.cpp: In function `int main(int, char**)':
Cube.cpp:63: error: `Initialize' undeclared (first use this function)
Cube.cpp:64: error: `Display' undeclared (first use this function)
Cube.cpp:65: error: `Reshape' undeclared (first use this function)

Since these funcs are within the main function it has no relevence with my OpenGLCube class.

glutDisplayFunc(Display); has the prototype:
GLUTAPI void APIENTRY glutDisplayFunc(void (GLUTCALLBACK *func)(void));

how do i declare 'display' the prototype says its void an is a pointer to an array..





Share this post


Link to post
Share on other sites
With the Source looking like this:

//#include <windows.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <gl/glut.h>

//#include "OpenGLCube.h"
/*
OpenGLCube::OpenGLCube()
{
}

OpenGLCube::~OpenGLCube()
{
}
*/

void Initialize()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
/*
void OpenGLCube::SetupProjection(int width, int height)
{
}

void OpenGLCube::Prepare(float dt)
{
}

void OpenGLCube::Render()
{
}
*/

void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity(); // clear the matrix stack

// these are our viewing transofrmations
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(1.0, 2.0, 1.0);
glutWireCube(1.0);
glFlush();
}

void Reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
Initialize();

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
Initialize();
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}



Im left with the linker errors you ( or someone else) might have seen before:

Cube.o(.text+0x26): In function `glutInit_ATEXIT_HACK':
C:/Dev-Cpp/include/gl/glut.h:486: undefined reference to `__glutInitWithExit@12'
Cube.o(.text+0x51): In function `glutCreateWindow_ATEXIT_HACK':
C:/Dev-Cpp/include/gl/glut.h:503: undefined reference to `__glutCreateWindowWithExit@8'
Cube.o(.text+0x7b): In function `glutCreateMenu_ATEXIT_HACK':
C:/Dev-Cpp/include/gl/glut.h:549: undefined reference to `__glutCreateMenuWithExit@8'

i guess these are not defined with glut.h or by linking glut32 in the linker options.

Share this post


Link to post
Share on other sites
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Folder\Cube.cpp" -o "C:\Folder\Cube.exe" -ansi -pg -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lopengl32 -lglu32 -lglut32 -lWinMM -lgdi32 -lglaux -lgmon -pg -g3
C:\DOCUME~1\Owner\LOCALS~1\Temp/ccYllaaa.o(.text+0x26): In function `glutInit_ATEXIT_HACK':
C:/Dev-Cpp/include/gl/glut.h:486: undefined reference to `__glutInitWithExit@12'
C:\DOCUME~1\Owner\LOCALS~1\Temp/ccYllaaa.o(.text+0x51): In function `glutCreateWindow_ATEXIT_HACK':
C:/Dev-Cpp/include/gl/glut.h:503: undefined reference to `__glutCreateWindowWithExit@8'
C:\DOCUME~1\Owner\LOCALS~1\Temp/ccYllaaa.o(.text+0x7b): In function `glutCreateMenu_ATEXIT_HACK':
C:/Dev-Cpp/include/gl/glut.h:549: undefined reference to `__glutCreateMenuWithExit@8'
collect2: ld returned 1 exit status

Execution terminated


hmm.

Share this post


Link to post
Share on other sites
could someone post what im missing? These are all linker errors involving glut. As you can see by my compiler log, i do have the -lglut32 linked! What can i do to fix this problem?

Share this post


Link to post
Share on other sites
<bump>
could someone post what im missing? These are all linker errors involving glut. As you can see by my compiler log, i do have the -lglut32 linked! What can i do to fix this problem?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!