Hey guys,

Here's my problem:

I've been following ZeusCMD tutorials on OpenGL, and when I tried compiling main.cpp , I get an initializer error.

I am not sure what caused it, since it seems exactly the same as the example. I linked all libraries it tells me to (-lglu32 -lopengl32).

I'm using Code::Blocks on Windows Vista by the way.

Here's the clipboard error:

C:\Users\Renato\Desktop\Win32\glutil\main.cpp|3|error: expected initializer before '*' token|
C:\Users\Renato\Desktop\Win32\glutil\main.cpp||In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':|
C:\Users\Renato\Desktop\Win32\glutil\main.cpp|26|error: `opengl' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|

Can we see the line that causes the errors, and a few lines either side of them?

#include "glutil.h"

OpenGL *opengl;         //Here's where it asks for an initializer

bool init()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    return true;
}

Where it asks for an initializer.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    opengl = OpenGL::create();        //opengl was not declared

    opengl -> setInitFunc(init);
    opengl -> setDisplayFunc(display);
    opengl -> setIdleFunc(idle);

    if(!opengl -> initGL("My OpenGL Window", 500, 500, false, 32))
        return 1;

    opengl -> runMessageLoop();

    return 0;
}

Where it says opengl was not declared on this scope.

Post "glutil.h" code.

Are you sure that "glutil.h" is actually getting included correctly (i.e. have you set the include path). This is the only source of error I can see here.

BTW: you are aware that this glutil library that you are using is far from unique. Just google glutil and you will realize that there are an enormous amount of people who have made libraries called glutil.h (a very lame attempt at making their library "the standard one"..). You should stick with proper standard libraries (like opengl, GLUT, and GLUI, and cross-platform renderers like Ogre3D or VTK).

I'm following his guide.

Here's glutil.h and glutil.cpp respectively.

#ifndef GLUTIL_H

#define GLUTIL_H

#pragma comment(lib, "opengl21.lib")
#pragma comment(lib, "glu32.lib")

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

class OpenGL
{
    public:
        static OpenGL *create()
        {return instance = new OpenGL;}

        ~OpenGL(){if(instance) delete instance;}

        bool initGL(char *t, int w=640, int h=480,
                    bool full = false, int bpp=32);

        bool changeDisplaySettings(int w, int h, bool full, int bpp);

        void runMessageLoop();
        void endMessageLoop() const {finished = true;}

        void displayError(char *title);

        void setInitFunc(bool(*i)())
        {
            init = i;
        }
        void setDisplayFunc(void(*d)())
        {
            display = d;
        }
        void setIdleFunc(void(*i)())
        {
            idle = i;
        }
        void setResizeFunc(void(*r)(int w, int h))
        {
            resize = r;
        }

        HWND getHWND() const
        {return hwnd;}

        bool isKeyDown(int key) const
        {return keys[key];}
        bool keyUp(int key)
        {keys[key] = false;}

        bool isMouseDownL() const
        {return mouseDownL;}

        int getMouseX() const
        {return mouseX;}
        int getMouseY() const
        {return mouseY;}

        int getWidth() const {return width;}
        int getHeight() const {return height;}

    private:
        OpenGL(){}

        void killGL();

        static LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

        static OpenGL *instance;

        static char *title;
        static int width;
        static int height;
        static int bits;
        static bool fullscreen;

        static bool active;
        static bool finished;
        static bool keys[256];
        static int mouseX;
        static int mouseY;
        static bool mouseDownL;

        static HDC hdc;
        static HWND hwnd;
        static HGLRC hrc;

        static bool (*init)();
        static void (*display)();
        static void (*idle)();
        static void (*resize)(int w, int h);
}

#endif

glutil.cpp

#include "glutil.h"

OpenGL *OpenGL::instance = NULL;

char *OpenGL::title = "";
int OpenGL::width = 640;
int OpenGL::height = 480;
int OpenGL::bits = 16;
bool OpenGL::fullscreen = false;

bool OpenGL::active = false;
bool OpenGL::finished = false;
bool OpenGL::keys[256];
int OpenGL::mouseX = 0;
int OpenGL::mouseY = 0;
bool OpenGL::mouseDownL = false;

HDC OpenGL::hdc = NULL;
HWND OpenGL::hwnd = NULL;
HGLRC OpenGL::hrc = NULL;

bool (*OpenGL::init)() = NULL;
void (*OpenGL::display)() = NULL;
void (*OpenGL::idle)() = NULL;
void (*OpenGL::resize)(int w, int h) = NULL;

bool OpenGL::initGL(char *t, int w, int h, bool full, int bpp)
{
    title = t;
    width = w;
    height = h;
    fullscreen = full;
    bits = bpp;

    HINSTANCE hInstance = GetModuleHandle(NULL);

    WNDCLASSEX wc;
    unsigned int pixelFormat;

    DWORD style;

    wc.cdSize           = sizeof(wc);
    wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc      = (WNDPROC)WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm          = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground    = NULL;
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = "myClass";

    if(!RegisterClassEx(&wc))
    {
        displayError("Registering Window Class");
        return false;
    }

    if(fullscreen)
    {
        DEVMODE displaySettings;

        memset(&displaySettings, 0, sizeof(displaySettings));

        displaySettings.dmSize          = sizeof(displaySettings);
        displaySettings.dmPelsWidth     = width;
        displaySettings.dmPelsHeight    = height;
        displaySettings.dmBitsPerPel    = bits;

        displaySettings.dmFields        = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;

        if(ChangeDisplaySettings(&displaySettings,
                                 CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
        {
            displayError("Cannot change to requested display mode");
            fullscreen = false;
            killGL();
            return false;
        }
    }

    if(fullscreen)
    {
        style = WS_POPUP;
        ShowCursor(false);
    }

    else style = WS_OVERLAPPEDWINDOW;

    RECT rect;
    rect.left = 0; rect.top = 0;
    rect.right = width; rect.bottom = height;

    AdjustWindowRectEx(&rect, style, FALSE, 0);

    if(!(hwnd = CreateWindowEx(
                WS_EX_APPWINDOW,
                "myClass", title,
                WS_CLIPSIBLINGS | WS_CLIPCHILDREN | style,
                0, 0,
                rect.right - rect.left,
                rect.bottom - rect.top,
                NULL, NULL, hInstance, NULL)))
    {
        killGL();
        displayError("Creating Window");
        return false;
    }

    PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(pfd), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
                        PFD_TYPE_RGBA,
                        bits, 0, 0, 0, 0, 0, 0,
                        0, 0, 16, 0, 0, PFD_MAIN_PLANE,
                        0, 0, 0, 0
    };

    if(!(hdc = GetDC(hwnd)))
    {
        killGL();
        displayError("Retrieving DC");
        return false;
    }

    if(!(pixelFormat = ChoosePixelFormat(hdc, &pfd)))
    {
        killGL();
        displayError("Choosing Pixel Format");
        return false;
    }

    if(!SetPixelFormat(hdc, pixelFormat, &pfd))
    {
        killGL();
        displayError("Setting Pixel Format");
        return false;
    }

    if(!(hrc = wglCreateContext(hdc)))
    {
        killGL();
        displayError("Creating Rendering Context");
        return false;
    }

    if(!wglMakeCurrent(hdc, hrc))
    {
        killGL();
        displayError("Activating Rendering Context");
        return false;
    }

    ShowWindow(hwnd, SW_SHOW);

    SetForegroundWindow(hwnd);
    SetFocus(hwnd);

    if(resize)
        resize(width, height);

    if(init)
        return init();

    return true;
}

bool OpenGL::changeDisplaySettings(int w, int h, bool full, int bpp)
{
    killGL();

    int oldWidth    = width;
    int oldHeight   = height;
    bool oldFull    = fullscreen;
    int oldBpp      = bits;

    if(!initGL(title, w, h, full, bpp))
    {
        initGL(title, oldWidth, oldHeight, oldFull, oldBpp);
        return false;
    }

    if(init)
        return init();

    return true;
}

void OpenGL::killGL()
{
    if(fullscreen)
    {
        ChangeDisplaySettings(NULL, 0);
        ShowCursor(true);
    }

    if(hrc)
    {
        if(!wglMakeCurrent(NULL, NULL))
            displayError("Deactivating Rendering Context");

        if(!wglDeleteContext(hrc))
            displayError("Deleting Rendering Context");

        hrc = NULL;
    }

    if(hdc && !ReleaseDC(hwnd, hdc))
            displayError("Releasing Device Context");

    hdc = NULL;

    if(hdc && !DestroyWindow(hwnd))
            displayError("Destroying Window");

    hwnd = NULL;

    if(!UnregisterClass("myClass", GetModuleHandle(NULL)))
            displayError("Unregistering Class");
}

LRESULT OpenGL::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_ACTIVATE:
            active = HIWORD(wParam)? false:true;
        break;
        case WM_CLOSE:
            PostQuitMessage(0);
        break;
        case WM_SIZE:
            width   = LOWORD(lParam);
            height  = HIWORD(lParam);

            if(resize)
                resize(LOWORD(lParam), HIWORD(lParam));
        break;
        case WM_KEYDOWN:
            keys[wParam] = true;
        break;
        case WM_KEYUP:
            keys[wParam] = false;
        break;
        case WM_MOUSEMOVE:
            mouseX = LOWORD(lParam);
            mouseY = height - (HIWORD(lParam) + 1);
        break;
        case WM_LBUTTONDOWN:
            mouseDownL = true;
        break;
        case WM_LBUTTONUP:
            mouseDownL = false;
        break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

void OpenGL::displayError(char *title)
{
    LPVOID lpMsgBuf;

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL, GetLastError(), 0, (LPSTR) &lpMsgBuf, 0, NULL);

    MessageBox(NULL, (LPCTSTR)lpMsgBuf, title, MB_OK | MB_ICONERROR);

    LocalFree(lpMsgBuf);
}

void OpenGL::runMessageLoop()
{
    MSG msg;

    while(!finished)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                break;

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

        if(active)
        {
            if(idle)
            {
                idle();

                if(keys[VK_F1])
                {
                    keys[VK_F1] = false;

                    fullscreen = !fullscreen;

                    if(fullscreen)
                        fullscreen = this -> changeDisplaySettings(1024, 768, true, 32);
                    else
                        this -> changeDisplaySettings(500, 500, false, 32);
                }
                else if(keys[VK_ESCAPE])
                    this -> endMessageLoop();
            }
            display();
            SwapBuffers(hdc);
        }
    }
    killGL();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.