Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:56:47+00:00 2026-05-31T12:56:47+00:00

I have an Application object that has a vector of GameChar objects as one

  • 0

I have an Application object that has a vector of GameChar objects as one of its member variables. This GameChar objects contain a GLbatch that is initialized in the constructor and drawn when you call the draw method of the GameChar object. When the program starts I create an application object and three GameChar objects, and I push the GameChar’s into the Application object’s vector with the push_back() method. Then, inside a game loop, I iterate through the vector and call the draw method of all I’ts objects. The problem is that only the object that was last put in the vector gets drawn. I have assured that the vector is iterated correctly, and that the draw method is called on all the objects, but no matter in what order I call this draw methods (I have tried to iterate the vector from end to start to assure this), only the object that was last inserted to the vector gets drawn.

Here is the code that creates the application and GameChar objects (the constructor gets the arguments in this order: width,height,x,y):

Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);
app.characters.push_back(character3);

And this is the loop:

while (1)
{
    vector<GameChar>::size_type sz=characters.size();
    unsigned int i;
    for (i=0;i<sz;i++)
    {
        characters[i].update();
    }
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    for (i=0;i<sz;i++)
    {

        characters[i].draw();
    }

    glfwSwapBuffers();
}

UPDATE:
Here is the constructor of GameChar:

GameChar::GameChar(GLfloat lWidth,GLfloat lHeight,GLfloat lX,GLfloat lY)
{
    xPos=lX;
    yPos=lY;
    height=lHeight;
    width=lWidth;
    if (xPos<-1)
    {
        xPos=-1;
    }
    if (xPos+width>1)
        xPos=1-width;
    if (yPos<-1)
        yPos=-1;
    if (yPos+height>1)
        yPos=1-height;
    verts[0]=xPos;
    verts[1]=yPos;
    verts[2]=0.0f;
    verts[3]=xPos+width;
    verts[4]=yPos;
    verts[5]=0.0f;
    verts[6]=xPos;
    verts[7]=yPos+height;
    verts[8]=0.0f;
    verts[9]=xPos+width;
    verts[10]=yPos+height;
    verts[11]=0.0f;

    batch.Begin(GL_TRIANGLE_STRIP, 4);
    batch.CopyVertexData3f(verts);
    batch.End();
}

The draw method:

void GameChar::draw()
{
    batch.Draw();
}

And I’ve updated the game loop with the complete code.

Also, If i try to change the code:

for (i=0;i<sz;i++)
{
    characters[i].draw();
}

with this code:

for (i=0;i<sz;i++)
{
    GLBatch batch;
    batch=characters[i].batch;
    batch.Draw();
}

No square gets drawn. I really find all this a nonsense

ANOTHER UPDATE:

If I pop_back the third object after pushing it in, like this:

Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);
app.characters.push_back(character3);
app.characters.pop_back();

nothing is drawn, but if don’t push it in, like this:

Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);

The second character is drawn. So I think the problem is that the characters that are already in the vector are invalidated when a new one is pushed, but I don’t know why this happens, or how to fix it

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:56:48+00:00Added an answer on May 31, 2026 at 12:56 pm

    I finally managed to fix it! I still don’t know what’s the cause of the problem, but I’ve changed the vector of GameChar to a vector of pointers to GameChar objects, and now it works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses Hibernate for its domain objects. One part of
I have an application that has many different types of objects that each persist
I have an application that has several objects (about 50 so far, but growing).
I have a Winforms application that generates its own PrintDocument object for printing. It
We have an application that performs comparisons on data objects to determine if one
I'm developing a Java application that has performance at its core. I have a
I have a GWT application that has RPC service on its back end. I'm
I am building a web application and have been told that using object oriented
I have an AJAX application that downloads a JSON object and uses the data
I have a web application that uses the CDO Message object to email reports.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.