Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C++ compilation problem [SOLVED!!! - stupid typos]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
tenspd137
Guru
Guru


Joined: 22 Aug 2006
Posts: 375

PostPosted: Wed Sep 19, 2012 4:11 am    Post subject: C++ compilation problem [SOLVED!!! - stupid typos] Reply with quote

Hi all,

I was trying to compile some files using C++0x. From what I can tell the following files should compile with:
g++ -std=c++0x -c GLWindowX11.cpp

interfaces.hpp:
Code:

#ifndef _INTERFACES_HPP_
#define _INTERFACES_HPP_

#include <memory>

using std::shared_ptr;
using std::weak_ptr;


class IWindow
{
  public:
    virtual ~IWindow() {}
    virtual bool CreateWindow(int width, int height, int bpp, bool fullscreen) = 0;
    virtual void DestroyWindow() = 0;
    virtual void SwapBuffers() = 0;
    virtual Display* GetDisplay() = 0;
};

#endif


GLXWindow.hpp
Code:

#ifndef _GLWINDOWX11_HPP
#define _GLWINDOWX11_HPP

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/xf86vmode.h>
#include <GL/gl.h>
#include <GL/glx.h>
//don't know if we needd these for extensions...
//#include <GL/glext.h>
//#include <GL/glxext.h>
#include <ctime>

#include "interfaces.hpp"


class GLWindowX11 : public IWindow
{
  public:
    virtual ~GLWindowX11();
    GLWindowX11();
    virtual bool CreateWindow(int width, int height, int bpp, bool fullscreen);
    virtual void DestroyWindow();
    virtual void SwapBuffers() { glXSwapBuffers(m_display, m_XWindow); }
    Display* GetDisplay() { return m_display; );

  private:
    Display* m_display;
    Window m_XWindow;
    GLXContext m_glContext;
    XF86VidModeModeInfo m_XF86DeskMode;
    XSetWindowAttributes m_XSetAttr;
    int m_screenID;

    bool m_idFullscreen;
    unsigned int m_width;
    unsigned int m_height;
    unsigned int m_bpp;

    bool m_GL3Supported;

};


#endif


GLXWindowX11.cpp
Code:

#include <iostream>
#include <string>
#include <ctime>
#include <memory>
#include "GLWindowX11.hpp"

using std::string;

typedef GLXContext ( * PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display *dpy, GLXFBConfig config, GLXContext share_context, bool direct, const int *attrib_list);

GLWindowX11():
    m_display(NULL),
    m_XWindow(0),
    m_glContext(0),
    m_screenID(0),
    m_isFullscreen(false),
    m_width(0),
    m_height(0),
    m_bpp(0),
    m_GL3Supported(false)
{
}

~GLWindowX11()
{
}

bool GLWindowX11::CreateWindow(int width, int height, int bpp, bool fullscreen)
{
    m_display = XOpenDisplay(0);
    if (m_display == NULL)
    {
   std::cerr << "Could not open the display" << std::endl;
   return false;
    }
   
    m_screenID = DefaultScreen(m_display);
    int n = 0, modeNum = 0;
   
    //Get a framebuffer config using default attributes
    GLXFBConfig framebufferConfig = (*glxChooseFBConfig(m_display, m_screenID, 0, &n));
    XF86VidModeModeInfo **modes;

    if (!XF86VidModeGetAllModeLines(m_display, m_screenID, &modeNum, &modes))
    {
   std::cerr << "Could not query the video modes..." << std::endl;
   return false;
    }

    m_XF86DeskMode = *modes[0];
    int bestMode = -1;
    for(int i = 0; i<modeNum, i++)
    {
   if((modes[i]->hdisplay == width) && (modes[i]->vdisplay == height))
   {
       bestMode = i;
   }
    }

    if (bestMode == -1)
    {
   std::cerr << "Could not find a suitable graphics mode" << std::endl;
   return false;
    }

    int doubleBufferedAttribList [] = {
   GLX_RGBA, GLX_DOUBLEBUFFER,
   GLX_RED_SIZE, 4,
   GLX_GREEN_SIZE, 4,
   GLX_BLUE_SIZE, 4,
   GLX_DEPTH_SIZE, 16,
   None
    };

    XVisualInfo* vi = NULL;
    //Attempt to create a double buffered window
    vi = glXChooseVisual(m_display, m_screenID, doubleBufferedAttribList);

    if(vi == NULL)
    {
   std::cerr << "Could not create a double buffered window..." << std::endl;
   return false;
    }

    //Create a GL 2.1 context
    GLXContext gl2context = glXCreateContext(m_display, vi, 0, GL_TRUE);
   
    if (gl2context == NULL)
    {
   std::cerr << "Could not create a GL 2.1 context - please check your graphics drivers..." << std::endl;
   return false;
    }

    //Get pointer to GL 3.0+ context
    PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB");

    if(glXCreateContextAttribs == NULL)
    {
   std::cerr << "OpenGL 3 not supported." << std::endl;
   m_GL3Supported = false;
    }
    else
    {
   //Create the 3.0 context
   int attribs[] = {
       GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
       GLX_CONTEXT_MINOR_VERSION_ARB, 0,
       0 //end of array
   };
   
   m_glContext = glXCreateContextAttribs(m_display, frambufferConfig, 0, true, &attribs[0]);
   glXDestroyContext(m_display, gl2Context);
   m_GL3Supported = true;
    }

    Colormap cmap = XCreateColormap(m_display, RootWindow(m_display, vi->screen), vi->visual,AllocNone);
    m_XSetAttr.colormap = cmap;
    m_XSetAttr.bordr_pixel = 0;
    m_XSetAttr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | StructureNotifyMask;
    m_XSetAttr.override_redirect = False;
   
    unsigned long windowAttributes = CWBorderPixel | CWColormap | CWEventMask;

    if(fullscreen)
    {
   windowAttributes = CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
   
   XF86VidModeSwitchToMode(m_display, m_screenID, modes[bestMode]);
   XF86VidModeSetViewPort(m_display, m_screenID, 0, 0);
   m_XSetAttr.override_redirect = True;
    }

    m_XWindow = XCreateWindow(m_display, RootWindow(m_display, vi->screen),
               0, 0, width, height, 0, vi->depth, InputOutput, vi->visual,
               CWBorderPixel | CWColorMap | CWEventMask, &m_XSetAttr);

    std::string title = "test";

    if (fullscreen)
    {
        XWarpPointer(m_display, None, m_XWindow, 0, 0, 0, 0, 0, 0);
        XMapRaised(m_display, m_XWindow);
        XGrabKeyboard(m_display, m_XWindow, True, GrabModeAsync, GrabModeAsync, CurrentTime);
        XGrabPointer(m_display, m_XWindow, True, ButtonPressMask,
                     GrabModeAsync, GrabModeAsync, m_XWindow, None, CurrentTime);

        m_isFullscreen = true;
    }
    else
    {
        Atom wmDelete = XInternAtom(m_display, "WM_DELETE_WINDOW", True);
        XSetWMProtocols(m_display, m_XWindow, &wmDelete, 1);
        XSetStandardProperties(m_display, m_XWindow, title.c_str(), None, NULL, NULL, 0, NULL);
        XMapRaised(m_display, m_XWindow);
    }

    XFree(modes);

    glXMakeCurrent(m_display, m_XWindow, m_glContext);

    int posx = 0;
    int posy = 0;

    Window winDummy;
    unsigned int borderDummy;
   
    m_width = (unsigned) width;
    m_height = (unsigned) height;
    m_bpp = (unsigned) bpp;

    XGetGeometry(m_display, m_XWindow, &winDummy, &posx, &posy, &m_width, &m_height, &borderDummy, &m_bpp);

    return true;
}

void GLWindowX11::DestroyWindow()
{
    if (m_glContext)
    {
   glXMakeCurrent(m_display, None, NULL);
   glXDestroyContext(m_displayt, m_glContext);
   m_glContext = NULL;
    }

    if (m_isFullScreen)
    {
   XF86VidModeSwitchToMode(m_display, m_screenID, &m_XF86DeskMode);
   XF86VidModeSetViewPort(m_display, m_screenID, 0, 0);
    }

    XCloseDisplay(m_display);
}



When I try to compile these, I get the following error:
GLWindowX11.cpp:8:12: error: using-declaration for non-member at class scope
....many more errors.....



If I remove the using std::string; line,
GLWindowX11.cpp:12:1: error: ‘GLWindowX11::GLWindowX11()’ cannot be overloaded

I don't think I have defined classes incorectly, so I don't know what I am missing....

Also, in my .cpp file, if I use GLXWindowX11::GLXWindowX11(): ......., it complains about an extra qualification. this would make sense if it were in the function or header, but form everything I can see, this should be correct also.

Any hints would be appreciated - this is really bothering me *sigh*


Last edited by tenspd137 on Wed Sep 19, 2012 8:01 am; edited 1 time in total
Back to top
View user's profile Send private message
schorsch_76
Apprentice
Apprentice


Joined: 19 Jun 2012
Posts: 212

PostPosted: Wed Sep 19, 2012 5:29 am    Post subject: Reply with quote

Could you please mark the line "12" in your cpp file...
Back to top
View user's profile Send private message
aderesch
Tux's lil' helper
Tux's lil' helper


Joined: 06 Mar 2010
Posts: 123
Location: Berlin, Germany

PostPosted: Wed Sep 19, 2012 5:56 am    Post subject: Re: C++ compilation problem Reply with quote

tenspd137 wrote:

When I try to compile these, I get the following error:
GLWindowX11.cpp:8:12: error: using-declaration for non-member at class scope
....many more errors.....

Well, this pretty much tells you the compiler believes it is still inside a class definition instead of at global scope. Check for correct braces/parentheses/semicolons.
Also, constructor and destructor need a GLXWindowX11::. Your error message about this is also due to wrong scoping.

Although probably irrelevant right now, you should include the necessary X11 headers for Display in interfaces.hpp.

ad
Back to top
View user's profile Send private message
^marcs
Apprentice
Apprentice


Joined: 09 Mar 2005
Posts: 169

PostPosted: Wed Sep 19, 2012 6:23 am    Post subject: Reply with quote

Constructor should be defined this way in .cpp
Code:
GLWindowX11::GLWindowX11():
    m_display(NULL),
    m_XWindow(0),
    m_glContext(0),
    m_screenID(0),
    m_isFullscreen(false),
    m_width(0),
    m_height(0),
    m_bpp(0),
    m_GL3Supported(false)
{
}

error he is issuing about scoping is a result of something else.

Code:
    Display* GetDisplay() { return m_display; );

is a main bugger here, correct the bracers around return m_display and that should throw You way ahead.

also, You have
Code:
 bool m_idFullscreen;

in declaration but
Code:
m_isFullscreen(false),

in constructor list.

There is more after that, but You should figure it out.
Back to top
View user's profile Send private message
tenspd137
Guru
Guru


Joined: 22 Aug 2006
Posts: 375

PostPosted: Wed Sep 19, 2012 8:01 am    Post subject: Reply with quote

GAHHHHH! Stupid typos and small screen font!

Thank you to everyone who looked at this for me. It was one of those cases where I was reading what I wanted to instead of what was there - you know, when you have looked at the same thing 100 times, so you need someone else to point out your stupidly obvious error.... Thanks for being a fresh set of eyes - much appreciated!

Once I cleaned up these:
Code:

 Display* GetDisplay() { return m_display; );
......
 bool m_idFullscreen;


all my other typos came out as error messages I would expect and I cleaned it all up to compile with no probs.

Also, thank you for confirming that I was indeed writing my constructor and destructor correctly when I qualified them in my .cpp file with GLXWindowX11:: - I was trying to appease the compiler to see if it would move forward because I kept missing the brace above.

I was just up too late trying to do this and missing the obvious.

I think I can actually get to sleep now - I won't be up trying to do this over and over in my head.

Thanks again!!!! :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum