Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I can't draw items inside a linked list?  (Read 2809 times)

0 Members and 1 Guest are viewing this topic.

NiteStriker

  • Newbie
  • *
  • Posts: 2
    • View Profile
I can't draw items inside a linked list?
« on: June 26, 2010, 07:43:31 pm »
Before I begin, I would like to point out that I'm using:

-Ubuntu 9.10
-Eclipse (Helios) as my IDE
-GCC as my compiler
-GDB as my debugger

Ok so as the title of this thread suggests, I'm not able to draw items that are inside a linked list. In the game that I'm currently working on, I have an EnemyManager class. This class spawns enemies. It holds each enemy that it creates inside a linked list. I'm using the STL list (#include <list>) to accomplish this.

I put some of my code below to demonstrate how I'm doing this. The "enemies" variable is my linked list.

Code: [Select]

//draw all  enemies on the screen
void EnemyManager::draw(sf::RenderWindow & screen){

//first check to see if there are enemies to draw
if( enemies.size() > 0 ){

//create an iterator to traverse the list
list<Enemy>::iterator enemy;

//go through the list of enemies from start to the end
for(enemy = enemies.begin(); enemy != enemies.end(); enemy++){
//draw each enemy inside the list
screen.Draw((*enemy).getSprite());
}

  }
}




Whenever I traverse the items inside the list and use the draw function on each of them, they all come out as white squares. I know that there isn't a problem with the image loading because I'm able to declare a single enemy object outside of the EnemyManager class and draw it just fine.

I tried debugging it and here's what I get:
Code: [Select]
warning: can't find linker symbol for virtual table for `std::vector<sf::Color, std::allocator<sf::Color> >' value
warning: can't find linker symbol for virtual table for `std::_Vector_base<sf::Color, std::allocator<sf::Color> >::_Vector_impl' value


I'm not exactly sure of what the problem is but I've already included the Graphics library into the EnemyManager class. I'm pretty sure that the code should work since it worked correctly when using SDL.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: I can't draw items inside a linked list?
« Reply #1 on: June 27, 2010, 03:56:57 am »
Probably, the initialization of the sf::Sprite instances is the problem. Could you show the function that takes care of that (probably the constructor)?

Your code is not wrong, but some things can be improved:
Code: [Select]
//first check to see if there are enemies to draw
if( enemies.size() > 0 )
This is not necessary, since the iterators begin() and end() in an empty container are equal, so the for loop body is never reached.

Code: [Select]
//create an iterator to traverse the list
list<Enemy>::iterator enemy;
I would declare it inside the for loop, so that it doesn't pollute the surrounding scope. This might be of advantage in more complex functions, however it's not a crucial issue.

Code: [Select]
enemy++Always use pre-increment if you don't evaluate the expression. While post-increment (enemy++) copies the variable, increments the original and returns the copy, pre-increment (++enemy) just increments the object, which is especially for non-trivial iterators much faster.

Code: [Select]
(*enemy).getSprite()There is a short syntax for that: enemy->getSprite()

So, a simplified version of your function might look like this:
Code: [Select]
void EnemyManager::draw(sf::RenderWindow & screen)
{
    //go through the list of enemies from start to the end
    for(list<Enemy>::iterator enemy = enemies.begin(); enemy != enemies.end(); ++enemy)
    {
         //draw each enemy inside the list
         screen.Draw(enemy->getSprite());
    }
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NiteStriker

  • Newbie
  • *
  • Posts: 2
    • View Profile
I can't draw items inside a linked list?
« Reply #2 on: June 27, 2010, 06:24:18 am »
Hi, thanks for replying  :)

I'll admit that I'm a bit of a beginner at C++ so I appreciate your commentary on how I could improve my code.

Here's the model that I used for each enemy constructor:

Code: [Select]

Enemy1(int x, int y):Enemy(x,y){
   getImage().LoadFromFile("src/Images/Enemy1.png");
   getSprite().SetImage(getImage());
}


The Image and sprite variables are contained in a super class. The functions that I used in this case return references to these variables.

The variables x and y simply set the coordinates (position) of the sprite.