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

Author Topic: Help with Sprites and Vectors [Newbie]  (Read 2487 times)

0 Members and 1 Guest are viewing this topic.

stubbs

  • Newbie
  • *
  • Posts: 6
    • View Profile
Help with Sprites and Vectors [Newbie]
« on: February 02, 2011, 06:59:44 am »
Ive been having alot of trouble getting sprites into a vector , this is the code ive been using ,and i keep getting an error as i try and access clipped[60] to set the position of that sprite, and also when trying to draw it.
Code: [Select]
#include "tile.h"

Tile::Tile()
{
sf::Image tileset;
        sf::Sprite sprit;
        vector<sf::Sprite> clipped;

tileset.LoadFromFile("C:/Users/stubbs/Desktop/tilesheet.png");
tileset.SetSmooth(false);
sprit.SetImage(tileset);

for(int x = 0; x <= 7; x++)
{
for(int y = 0; y <= 20; y++)
{
sprit.SetSubRect(sf::IntRect(x,y*16,x+16,y+16));
clipped.push_back(sprit);
}
}


}
void Tile::SetType(sf::RenderWindow &App)
{
for(int x = 0; x <= 30; x++)
{
for(int y = 0; y <= 30; y++)
{
clipped[60].SetPosition((x*16),(y*16));
App.Draw(clipped[60]);
}
}
}


so what im trying to do is cut my sprite sheet into 16x16 squares and load each into a vector. so i will be able to assign what type of tile goes where.

any help would be great.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Help with Sprites and Vectors [Newbie]
« Reply #1 on: February 02, 2011, 07:51:10 am »
What error do you get?
Laurent Gomila - SFML developer

stubbs

  • Newbie
  • *
  • Posts: 6
    • View Profile
Help with Sprites and Vectors [Newbie]
« Reply #2 on: February 02, 2011, 08:15:07 am »
its an error at runtime .
Code: [Select]
Unhandled exception at 0x75569617 in Game.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0028f164..

the debugger breaks at the return of this function in the vector class.

Code: [Select]
reference at(size_type _Pos)
{ // subscript mutable sequence with checking
if (size() <= _Pos)
_Xran();
here-> return (*(begin() + _Pos));
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Help with Sprites and Vectors [Newbie]
« Reply #3 on: February 02, 2011, 08:39:08 am »
This is an easy one.

-> "out_of_range" means that  you access an element N in the vector, with N >= size
-> when the debugger breaks, look at the "_Pos" argument to find out the value of N
-> walk back in your call stack to find out the guilty line in your source code
Laurent Gomila - SFML developer

stubbs

  • Newbie
  • *
  • Posts: 6
    • View Profile
Help with Sprites and Vectors [Newbie]
« Reply #4 on: February 02, 2011, 08:58:14 am »
whoops , messed with code , not the same as what i had in the first post. anyway now the debugger breaks in the setType function , when i try and set the position of the sprite in clipped[60]. Im guessing that the sprites are getting messed up when being pushed onto the vect, how would i fix?
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Help with Sprites and Vectors [Newbie]
« Reply #5 on: February 02, 2011, 09:14:19 am »
this == 0x00000000 (the debugger even shows it in red, you can't miss it).

Your tile object is null, the error is one level up.
Laurent Gomila - SFML developer

stubbs

  • Newbie
  • *
  • Posts: 6
    • View Profile
Help with Sprites and Vectors [Newbie]
« Reply #6 on: February 02, 2011, 08:31:32 pm »
Im not great at c++ and have only had an intro to c++ class that barely touched on classes . everything else i know is what i learn from books or browsing programming forums. So even with your advice i dont know what you mean by your title object is null , a more in-depth explanation would be amazing . but anyway thanks for your help so far.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Help with Sprites and Vectors [Newbie]
« Reply #7 on: February 02, 2011, 08:35:21 pm »
You have something like this in your code:
Code: [Select]
Tile* tile = 0;
tile->SetType(window);
Laurent Gomila - SFML developer

stubbs

  • Newbie
  • *
  • Posts: 6
    • View Profile
Help with Sprites and Vectors [Newbie]
« Reply #8 on: February 02, 2011, 10:00:39 pm »
Quote from: "Laurent"
You have something like this in your code:
Code: [Select]
Tile* tile = 0;
tile->SetType(window);


yeah , should it be something like

Code: [Select]

Tile * tile = new Tile();
tile->SetType(window);


im not used to working with pointers and vectors , so in tile.cpp should sprit also be a pointer , and the vector should accept a pointer of sf::Sprite?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Help with Sprites and Vectors [Newbie]
« Reply #9 on: February 03, 2011, 07:42:49 am »
No, this is not a problem inside the Tile class. The problem is outside, the Tile object that you're manipulating doesn't exist since you have a null pointer. A don't know how you get this null pointer, but that's what needs to be fixed.

Seems like you should learn and experiment a little more the C++ basic concepts before trying to use SFML and make games ;)
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Help with Sprites and Vectors [Newbie]
« Reply #10 on: February 03, 2011, 08:00:10 am »
I have a great book to recommend. C++ Primer. Though programming is a craft work so in the end you'll learn more trough practice.

What happens in this example is that you have a pointer to NULL. NULL in C++ are in reality the number zero. And since we are working with pointers which are addresses to places in memory, we are saying that the pointer is pointing to nothing as memory 0 do not exist. Though NULL pointers are okay, you usually use it to say that something does not exist.

What is wrong is that somewhere in your code, you use the pointer like if it is pointing to an object. That's why the debugger says that the pointer called this has the value 0,, also known as NULL.

Also you want to avoid pointers, especially pointers to heap memory(memory gained through the new operator.)

Where do you do the call to the method of Tile that crashes? That pointer to the object used in the call where do you assign a value to it?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

stubbs

  • Newbie
  • *
  • Posts: 6
    • View Profile
Help with Sprites and Vectors [Newbie]
« Reply #11 on: February 03, 2011, 05:40:55 pm »
Quote from: "Groogy"
Also you want to avoid pointers, especially pointers to heap memory(memory gained through the new operator.)

Where do you do the call to the method of Tile that crashes? That pointer to the object used in the call where do you assign a value to it?

i wasnt using pointers at first , all i did was create a new Tile object , and in Tile.cpp in my constructor i initialize a vector<sf::Sprite> and i have a sprite object called sprit , i would iterate through my spritesheet cutting it into 16x16 tiles and storing each tile sprite into the vector .

Then in settype(should be called DrawTiles()) i tried to fill the screen with the tile in vector[60].

also i read somwhere that you had to push pointers to the sprites onto the vector , or it might remove everything in previous vector positions.

Silvah

  • Guest
Help with Sprites and Vectors [Newbie]
« Reply #12 on: February 03, 2011, 09:39:48 pm »
Quote from: "Groogy"
NULL in C++ are in reality the number zero.
Not always (it's about C, but it's true for null pointers in C++, too).

 

anything