Sign in to follow this  
TheShadow344

Problem with display list class

Recommended Posts

I have a problem with my display list construction class. I can successfully load a display list into the class, and it will display no problem. However, anytime I try to load an additional display list and render it, it will not be rendered, as in the following...
// Just a sample of usage
#include <GL/GL.h>
#define DSPL cDisplayList::GetInstance()

Gluint DisplayList[2];

// The following will render correctly
DSPL->Create(DisplayList[0], "Resource/TitleDSPL.dspl");
DSPL->Use(DisplayList[0]);

// The following will not be rendered
DSPL->Create(DisplayList[1], "Resource/LoadDSPL.dspl");
DSPL->Use(DisplayList[1]);

I have tried the following methods of solving my problem... 1. Checked the filename of the file I'm loading. It is correct. 2. Tried loading into DisplayList[1] first, and then DisplayList[0]. Using this method, DisplayList[1] will now render correctly and DisplayList[0] will not. 3. Tried loading the same file into each index of the DisplayList array. Again, only DisplayList[0] will show. Here's the source code to my singleton class template, as well as the cDisplayList class. Feel free to critique it in any other ways you see fit. SINGLETON.H
#ifndef SINGLETON_H
#define SINGLETON_H

template<class DataType> class singleton
{
  protected:
    static DataType *m_Instance;
    singleton() { }
    ~singleton() { ReleaseInstance(); }
    singleton(const singleton &Copy) { }
    singleton &operator = (const singleton &Copy) { }
    
    inline void ReleaseInstance()
    { 
      if(m_Instance != NULL)
      {
        delete m_Instance;
        m_Instance = NULL;
      }
    }
    
  public:
    static DataType* GetInstance()
    {
      if(m_Instance == NULL) { m_Instance = new DataType; }
      return(m_Instance);
    }
};

template<class DataType> DataType *singleton<DataType>::m_Instance = NULL;
#endif


DISPLAY_LIST.H
#include <GL/GL.h>
#include <list>
#include <string>
#include <fstream>

///////////////////////////////////////////////////////////////////////////////////////
// cDisplayList Class (SINGLETON)                                                    //
// Provides an interface for display list creation, usage, and deletion.             // 
///////////////////////////////////////////////////////////////////////////////////////

class cDisplayList : public singleton<cDisplayList>
{
  struct OPENGL_CMD
  {
    std::string Cmd;
    GLfloat FloatNum[4];
    GLint IntNum[2];
    
    OPENGL_CMD(std::string NewCmd) : Cmd(NewCmd) { }
    
    OPENGL_CMD(std::string NewCmd, GLfloat A, GLfloat B, GLfloat C, GLfloat D)
    : Cmd(NewCmd)
    {
      FloatNum[0] = A;
      FloatNum[1] = B;
      FloatNum[2] = C;
      FloatNum[3] = D;
    }
    
    OPENGL_CMD(std::string NewCmd, GLint A, GLint B)
    : Cmd(NewCmd)
    {
      IntNum[0] = A;
      IntNum[1] = B;
    }
    
    ~OPENGL_CMD()
    {
      Cmd.clear();
      FloatNum[0] = FloatNum[1] = FloatNum[2] = FloatNum[3] = 0;
      IntNum[0] = IntNum[1] = 0;
    }
  };
  
  friend class singleton<cDisplayList>;
  private:
    std::list<OPENGL_CMD> m_CmdList;
    
  protected:
    cDisplayList() { m_CmdList.clear(); }
    ~cDisplayList() { m_CmdList.clear(); ReleaseInstance(); }
    
  public:
    bool Create(GLuint &p_DisplayList, std::string p_FileName);
    inline void Use(GLuint &p_DisplayList);
    void Free(GLuint &p_DisplayList);
};

DISPLAY_LIST.CPP
///////////////////////////////////////////////////////////////////////////////////////
// cDisplayList::Create()                                                            //
// Creates a display list from a valid DSPL file.                                    // 
///////////////////////////////////////////////////////////////////////////////////////

bool cDisplayList::Create(GLuint &p_DisplayList, std::string p_FileName)
{
  if((glIsList(p_DisplayList)) || (p_FileName.empty())) { return(false); }
  
  // Clear command list
  m_CmdList.clear();
  
  // Open DSPL file
  std::ifstream DSPL_File(p_FileName.c_str());
  if(!DSPL_File.is_open()) { return(false); }
  
  std::string Buffer;
  
  // Check whether file opened is a valid DSPL file
  getline(DSPL_File, Buffer);
  if(Buffer != "START_DSPL") { return(false); }
  
  // Continue to read file
  while(!DSPL_File.eof())
  {
    // Read one line
    getline(DSPL_File, Buffer);
    
    std::istringstream Line(Buffer);
    std::string Cmd, Num1, Num2, Num3;
    
    // Process command
    if(Buffer.substr(0, 10) == "START_QUAD")
    {
      OPENGL_CMD NewCmd("START_QUAD");
      m_CmdList.push_back(NewCmd);
    }
    else if(Buffer.substr(0, 7) == "COLOR3F")
    {
      // Read data
      Line >> Cmd >> Num1 >> Num2 >> Num3;
      
      // Copy data
      OPENGL_CMD NewCmd("COLOR4F", atof(Num1.c_str()), atof(Num2.c_str()), atof(Num3.c_str()), 1.0f);
      m_CmdList.push_back(NewCmd);
    }
    else if(Buffer.substr(0, 8) == "VERTEX2I")
    {
      // Read data
      Line >> Cmd >> Num1 >> Num2;
      
      // Copy data
      OPENGL_CMD NewCmd("VERTEX2I", atoi(Num1.c_str()), atoi(Num2.c_str()));
      m_CmdList.push_back(NewCmd);
    }
    else if(Buffer.substr(0, 8) == "END_QUAD")
    {
      OPENGL_CMD NewCmd("END_QUAD");
      m_CmdList.push_back(NewCmd);
    }
  }
  
  // Close file
  DSPL_File.close();
  
  // Create display list from list of OpenGL commands
  p_DisplayList = glGenLists(1);
  glNewList(p_DisplayList, GL_COMPILE);
  
  // Proceed through command list
  for(std::list<OPENGL_CMD>::iterator x = m_CmdList.begin(); x != m_CmdList.end(); ++x)
  {
    if((*x).Cmd == "START_QUAD") { glBegin(GL_QUADS); }
    else if((*x).Cmd == "COLOR4F") { glColor4fv((*x).FloatNum); }
    else if((*x).Cmd == "VERTEX2I") { glVertex2iv((*x).IntNum); }
    else if((*x).Cmd == "END_QUAD") { glEnd(); }
  }
  
  // Finish display list
  glEndList();
  
  return(true);  
}

///////////////////////////////////////////////////////////////////////////////////////
// cDisplayList::Use()                                                               //
// Calls a valid display list.                                                       // 
///////////////////////////////////////////////////////////////////////////////////////

inline void cDisplayList::Use(GLuint &p_DisplayList)
{
  if(glIsList(p_DisplayList)) { glCallList(p_DisplayList); }
}

///////////////////////////////////////////////////////////////////////////////////////
// cDisplayList::Free()                                                              //
// Frees a display list.                                                             // 
///////////////////////////////////////////////////////////////////////////////////////

void cDisplayList::Free(GLuint &p_DisplayList)
{
  if(glIsList(p_DisplayList)) { glDeleteLists(p_DisplayList, 1); }
}

I've been trying to solve my problem for a few hours now, and I have had no success (hence why I'm posting in the first place). I need a fresh pair of eyes to look at the code and offer some advice. Thanks in advance.

Share this post


Link to post
Share on other sites
Wow do I feel stupid now. As it turns out, there was nothing wrong with my code at all (at least the code that I provided).

At least the above code will not go to waste. It works perfectly fine, so if you want to use it (in its entirety or in part), feel free.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this