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

Author Topic: Tilemap problem  (Read 1478 times)

0 Members and 1 Guest are viewing this topic.

Rein

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Tilemap problem
« on: May 18, 2013, 06:34:57 pm »
Hi all,

I'm new to SFML and C++ in general. I've been trying to make a tilemap for a simple game, but so far without success.

Below is the class so far. (which is not a lot) My code compiles, until it reaches the draw method. There I get "Thread 1: EXC_BAD_ACCESS (code=13, address=0x0)".

I guess that would mean the pointer to the tile is not available anymore, but I don't see why...I just added those tiles...

Any help is welcome for this beginner.

Thank you!

#include "Includes.h";
#include "Tile.h";

#ifndef Monstrum_TileMap_h
#define Monstrum_TileMap_h

class TileMap
{
public:
    TileMap();
    ~TileMap();
   
    int mWidth;
    int mHeight;
   
    void addTile(int x, int y, Tile* tile);
    Tile* getTile( int x, int y );
    void draw( sf::RenderWindow& renderWindow );
   
    bool create( int width, int height );
   
    std::vector<std::vector<Tile*> > mTiles;

};

#endif
 

#include "Includes.h"
#include "TileMap.h"

TileMap::TileMap()
{
}

TileMap::~TileMap()
{
}

bool TileMap::create( int width, int height)
{
    mWidth = width;
    mHeight = height;
   
    mTiles.resize( width );
   
    for(int i = 0; i < width; i++)
    {
        mTiles.at(i).resize( height, 0 );
    }

    Tile* tile;
   
    for ( int i = 0; i < width; ++i )
    {
        for ( int j = 0; j < height; ++j )
        {
            addTile( i, j, tile );
        }
    }
    return true;
}

void TileMap::addTile( int x, int y, Tile* tile )
{
    mTiles[x][y] = tile;
}

Tile* TileMap::getTile(int x, int y)
{
    return mTiles[x][y];
}

void TileMap::draw( sf::RenderWindow& renderWindow )
{
    for( int x = 0; x<mWidth; x++) {
        for( int y = 0; y<mHeight; y++) {
            getTile( x, y )->draw( renderWindow );
        }
    }
}
 

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Tilemap problem
« Reply #1 on: May 18, 2013, 06:47:22 pm »
you should read this before you post here: http://en.sfml-dev.org/forums/index.php?topic=5559.0

Also, I recommend(like many others here will) that you start learning the basics of C++ before jumping into SFML. Learn  how to use the various data structures and algorithms that are available to you.

That being said, what's the tile class look like?

Also here:

 
Tile* tile;
   
    for ( int i = 0; i < width; ++i )
    {
        for ( int j = 0; j < height; ++j )
        {
            addTile( i, j, tile );
        }
    }
in your create you're just making a pointer to a tile but not initializing it at all. Which would result in a bad (null) pointer, ie nothing to draw when it gets there.
« Last Edit: May 18, 2013, 06:51:48 pm by Raphman »

Cornstalks

  • Full Member
  • ***
  • Posts: 180
    • View Profile
    • My Website
Re: Tilemap problem
« Reply #2 on: May 18, 2013, 08:36:53 pm »
you should read this before you post here: http://en.sfml-dev.org/forums/index.php?topic=5559.0
I want to highlight this point, just to be sure the OP doesn't miss it:
Quote
Have you ran the debugger? What does the call stack look like? Don't know how the debugger works? Find a tutorial, seriously, this is the most important tool for a developer