1

System Specs and task

I am using Code::Blocks on Ubuntu 10.10 and playing around with OpenGL and glx. I'm in the process of learning C++(from a background in C and Java), so the style of any code doesn't conform to any real good standards (but I'm open to suggestions on how to improve, even if you don't have an answer to the question)

Edit:

Huge Realization: The default OpenGL Project Code::Blocks creates is C, not C++. I'm looking into this now.

I'm currently trying to modify the default OpenGL project on Code::Blocks into a simple 3d engine. I am currently getting the error:

expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before 'Draw'

Which disappears as soon as I comment out the #include for < GL/glx.h>

I read on a forum somewhere that Code::Blocks doesn't look in usr/include/ by default, but I added that to the search directories for the compiler in the project build options and it didn't seem to fix anything.

Code:

main.cpp: main.c:

#include <time.h>
#include "Draw.h"

#include <stdio.h>
#include <stdlib.h>

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

/*draw init here*/

Draw::Draw renderer = Draw::Draw.getDraw();

printf( "Press left mouse button to rotate around X axis\n" );
printf( "Press middle mouse button to rotate around Y axis\n" );
printf( "Press right mouse button to rotate around Z axis\n" );
printf( "Press ESC to quit the application\n" );

/* timing variable*/
/* Set it to delay half a second before rendering the first frame*/
clock_t flip_time = clock() + 0.5f * CLOCKS_PER_SEC;

while (1)
{


    /* Update models */


    /* Draw scene */

    /*  wait until it's been 1/60th of a second*/
    while(clock() < flip_time){}

    flip_time = clock() + (1.0f/60.0f) * CLOCKS_PER_SEC;
    /* Actually flip the frame */

}
}

Draw.h:

#ifndef DRAW_H
#define DRAW_H

#include <GL/glx.h>    /* This is the problem line */
#include <GL/gl.h>

#include <X11/X.h>    /* X11 constant (e.g. TrueColor) */
#include <X11/keysym.h>

class Draw
{
public:
    static Draw getDraw();
    virtual ~Draw();
    void update();
    void render();

protected:
private:
    Draw();
    bool init();

    /* The singleton*/
    static Draw *instance;
    static bool exists;

    /* X Window values */
    Display             *dpy;
    Window               win;
    GLboolean            doubleBuffer;

    /* X Parameters*/
    XVisualInfo         *vi;
    Colormap             cmap;
    XSetWindowAttributes swa;
    GLXContext           cx;
    XEvent               event;
    int                  dummy;

};

#endif // DRAW_H

Last, but not least Draw.cpp:

#include "Draw.h"

/* Set up the singleton*/
bool Draw::exists = false;
Draw* Draw::instance = NULL;

Draw::Draw()
{
/*TODO: make this constructor */
}

Draw::~Draw()
{
//dtor
}

Draw Draw::getDraw()
{
if(!exists)
{
    instance = new Draw();
    instance->init();
    exists = true; //Thanks mat, This line was accidentally removed with extraneous comments
}

return *instance;
}

bool Draw::init()
{
/* Get the buffers ready */
static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};

/* Double Buffered is best*/
doubleBuffer = GL_TRUE;

/*TODO: add constructor if it hasn't been constructed already*/

dpy = XOpenDisplay(NULL);
if (dpy == NULL)
{
    return false;
}

/* make sure OpenGL's GLX extension supported */
if(!glXQueryExtension(dpy, &dummy, &dummy))
{
    return false;
}

/* find an appropriate visual */
/* find an OpenGL-capable RGB visual with depth buffer */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);

if (vi == NULL)
{
    vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
    if (vi == NULL)
    {
        return false;
    }

    doubleBuffer = GL_FALSE;
}


/*
TODO: Fix or remove this
if(vi->class != TrueColor)
{
    return false;
}
*/
/* create an OpenGL rendering context  */

/* create an OpenGL rendering context */
cx = glXCreateContext(dpy, vi, /* no shared dlists */ None,
                    /* direct rendering if possible */ GL_TRUE);
if (cx == NULL)
{
    return false;
}

/* create an X window with the selected visual */

/* create an X colormap since probably not using default visual */
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = KeyPressMask    | ExposureMask
             | ButtonPressMask | StructureNotifyMask;
win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0,
                  300, 300, 0, vi->depth, InputOutput, vi->visual,
                  CWBorderPixel | CWColormap | CWEventMask, &swa);
XSetStandardProperties(dpy, win, "main", "main", None,
                     NULL, NULL, NULL);

/* bind the rendering context to the window */
glXMakeCurrent(dpy, win, cx);

/* request the X window to be displayed on the screen */
XMapWindow(dpy, win);

/* configure the OpenGL context for rendering  */
glEnable(GL_DEPTH_TEST); /* enable depth buffering */
glDepthFunc(GL_LESS);    /* pedantic, GL_LESS is the default */
glClearDepth(1.0);       /* pedantic, 1.0 is the default */

/* frame buffer clears should be to black */
glClearColor(0.0, 0.0, 0.0, 0.0);

/* set up projection transform */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
/* establish initial viewport */

/* pedantic, full window size is default viewport */
glViewport(0, 0, 300, 300);

return true;
}

void Draw::update()
{
/*TODO: Add things to draw here*/
}

void Draw::render()
{
    /* actually flip buffers here */
}

I removed a ton of comments before posting this here, but that shouldn't affect whether or not it compiles.

Thanks!

Jeremy
  • 11
  • 4

2 Answers2

0

This line in your main file is wrong:

Draw::Draw renderer = Draw::Draw.getDraw();

Draw::Draw is not a type. To get this to compile, you just need:

Draw renderer = Draw.getDraw();

It looks like you're trying to build a singleton. You code does not do that at all, you'll get a copy each time. (Note that you're not setting exists anywhere, but that's just an extra bug.) You should be returning a pointer or a reference to the shared instance. See for instance this article to get the syntax right: C++ Singleton design pattern.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Thanks, I've corrected the first mistake and added a line I omitted while copying the code over. I took a look at that page earlier, but was recommended instead to use [this one](http://www.codeproject.com/KB/cpp/singletonrvs.aspx). I'll take another look to make sure that my pattern works correctly, but I'm pretty sure that won't be too difficult to figure out once I can compile and test what I'm working on. – Jeremy Apr 06 '11 at 06:36
  • Instead of a pointer he could also use a reference; same effect, in most cases identical binary code, but makes some things a little nicer. Change "static Draw getDraw()" to "static Draw &getDraw()". Also since your constructor is private, you may call init from just there. -- On a side note: Personally I consider the singletons as an anti pattern, there's really little one can they use for. Class factories maybe, but those are IMHO antipatterns, too. – datenwolf Apr 06 '11 at 07:37
0

I found the issue with the linking.

The default project for OpenGL in Code::Blocks is NOT C++, it's C. I configured it to use g++ and it fixed the issue with glx not linking in correctly. I revised my singleton to look a little more like this and it works correctly now too. I have an issue now with the window not appearing, but I should be able to figure that out.

Thanks!

Jeremy
  • 11
  • 4