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

Author Topic: 'Tiled' Tile-map Loader  (Read 200921 times)

0 Members and 4 Guests are viewing this topic.

Loopback

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #150 on: April 17, 2014, 04:57:43 pm »
So... trying to do this will be usless, wont it?

if(layer->name == "Capa de Patrones 1"){ //tile layer
       
       
        for(auto tile = layer->tiles.begin(); tile != layer->tiles.end(); ++tile)
        {
           
            //collision = object.sprite->getGlobalBounds()->Intersects();
       
            if(collision){
               
                //something
           
                }   
          } 
    }
}



/* */

And what about if I wanted to do destroy an object when collision is detected?:

                for(auto layer = ml2.GetLayers().begin(); layer != ml2.GetLayers().end(); ++layer)
{
    if(layer->name == "Destructible")
       
       
    {
        for(auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
        {
            collision = object->GetAABB().intersects(pj->getSprite().getGlobalBounds());
           
           
           
            if(collision){
               

                //wanna destroy object here, how could I do it?

                }
               
          }
    }

}
« Last Edit: April 17, 2014, 05:13:36 pm by Loopback »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: 'Tiled' Tile-map Loader
« Reply #151 on: April 17, 2014, 09:45:08 pm »
Rather than make the map objects themselves game entities, you could use them as spawn points. For example:

for(const auto& o : layer.objects)
    m_items.push_back(std::make_unique<Collectable>(o.GetPosition()));
 

then for collision testing and destruction you don't even need to query the map:

for(auto& i : m_items)
{
    if(i.contains(player.bounds))
        i.setCollected(true);
}

m_items.erase(std::remove_if(m_items.begin(),
            m_items.end(),
            [](const Item::Ptr& p)->bool
            {
                return (p->Collected());
            }), m_items.end());
 

Conor

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #152 on: April 18, 2014, 01:07:22 am »
Fallahn, you've really helped me. Thanks I appreciate it! I have collision working from just making levels in TileD now too. :)

Loopback

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #153 on: April 18, 2014, 06:59:39 pm »
Rather than make the map objects themselves game entities, you could use them as spawn points. For example:

for(const auto& o : layer.objects)
    m_items.push_back(std::make_unique<Collectable>(o.GetPosition()));
 

then for collision testing and destruction you don't even need to query the map:

for(auto& i : m_items)
{
    if(i.contains(player.bounds))
        i.setCollected(true);
}

m_items.erase(std::remove_if(m_items.begin(),
            m_items.end(),
            [](const Item::Ptr& p)->bool
            {
                return (p->Collected());
            }), m_items.end());
 

how would I declare "m_items"? tmx::MapObject array?  std::Vector<tmx::MapObject>?

thx in advance :)

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: 'Tiled' Tile-map Loader
« Reply #154 on: April 18, 2014, 08:26:36 pm »
This is just pseudo code really. What I'm suggesting is that you make your own class which represents collectable items (although the same can be done for things like enemies)
class Collectable
{
public:
    typedef std::unique_ptr<Collectable> Ptr;
    Collectable(sf::vector2f spawnPosition);
    void SetCollected(bool b){m_collected = b;}
    bool Collected() const {return m_collected;}
private:
    m_collected = false;
};
 

then in your game class (or whichever loads the map) have a container (m_items) of Collectable::Ptr which can be populated when loading the map. Then when you update your game you test the container for collisions (not the map itself), and remove items once they are collected.

Loopback

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #155 on: April 18, 2014, 09:27:04 pm »
Sounds a great idea! but there's something I'm missing. If I do it that way, when it comes to the point in which I have to draw the map (in the main iteration), yeah, I do erase the object from the vector but, not from the map?

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: 'Tiled' Tile-map Loader
« Reply #156 on: April 18, 2014, 09:39:40 pm »
once the map is loaded and collectables / npcs / whatever are created the map objects become moot, so you just ignore them. All you need worry about is drawing the map (and any sprites you may have, of course).

Loopback

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #157 on: April 20, 2014, 07:28:12 pm »
Still having problems to add, remove objects from layers. Tried the easiest way I could. I succesfully add the object to layer but seems to be having problems when drawing (seems it does not 'update' or refresh the layer objects at the time of drawing)

KevinK

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #158 on: April 20, 2014, 10:25:07 pm »
Hi I'm trying to get the tiled map loader up and running, I have all the libraries and includes done as well as the .cpp files added to the solution. I'm using VS 2012. The program runs just fine but it breaks when I run this command in the code down below. It says "A buffer overrun has occurred in "Time-Voyager.exe" which has corrupted the programs internal states. Press break to debug the program or continue to terminate.". The last call stack says this: "msvcr110.dll!_crt_debugger_hook(int _Reserved) Line 60   C" I'm not sure what to do I've tried everything.


#include <stdio.h>
#include <iostream>
#include <zlib.h>
#include <tmx/MapLoader.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <math.h>

using namespace sf;

int main(){

tmx::MapLoader myMap("maps\\");

}

 

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: 'Tiled' Tile-map Loader
« Reply #159 on: April 21, 2014, 09:06:59 am »
@loopback:
You're making copies of the layers, instead of referencing them (they should actually be non-copyable, I'll have to fix that). If you make references to the layers and modify them then you should only need to call window.draw(myMap)

@KevinK:
There's not much there to go on; a wild stab in the dark I'd suggest changing the path, either replace \\ with / or leave it off altogether  - the map loader should automatically add it if it's missing.

Loopback

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #160 on: April 21, 2014, 11:29:04 am »
and How can I make those references to layers instead of making copies?

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: 'Tiled' Tile-map Loader
« Reply #161 on: April 21, 2014, 01:40:07 pm »

KevinK

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #162 on: April 22, 2014, 06:25:29 am »
Well I messed around with it for a number of hours today, I was able to get my program to compile in debug only... what made debug work was changing from sf::String to std::string and adding the full path to my files, for whatever reason. But when I try to run my program in release it still crashes from a buffer overrun :/ I'm not sure what to do to get release mode working. I tried googling my problem to try and find similar issues and they were all not very useful. I had a lead to possibly the encoding that was causing the buffer overrun but I don't know anything about encoding.
const std::string pathToPlayer = "C:\\Users\\Kevin\\Documents\\Visual Studio 11\\Projects\\Time-Voyager2\\player.png";
const std::string myMapString = "C:\\Users\\Kevin\\Documents\\Visual Studio 11\\Projects\\Time-Voyager2\\maps";
const std::string filename = "test.tmx";
const std::string caption = "TileD Maps";

int main(){

        std::cout << myMapString.length();
        tmx::MapLoader myMap(myMapString);
       
       
        myMap.Load(filename);
}
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #163 on: April 22, 2014, 06:37:54 am »
KevinK,

It really sounds like you got something screwed up in your environment. Mismatched libs, libs for a different compiler, and many other things. I suggest you delete all SFML and Tiled files from your PC, then download SFML source from github and compile that yourself. Then download and compile the Tiled source to see if that fixes the issue.

Also use forward '/' slashes in your paths.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

simpl

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #164 on: April 30, 2014, 07:46:37 pm »
Hey, first of all thanks for sharing your work with us .. =).

Unfortunately I have a problem with creation of the project files with CMake.
Like Ricky (http://en.sfml-dev.org/forums/index.php?topic=3023.msg104256#msg104256)
the error CMake is providing is as follows:
CMake Error at CMakeLists.txt:32 (install):
  install Library TARGETS given no DESTINATION!

Apart from that there arent any errors.

I would be very happy, if you could provide a solution for this problem.
I read on the same page about commenting this line out, but this shouldnt be the final solution, should it?

Greetings!

 

anything