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

Author Topic: std::vector or std::map for texture manager  (Read 4337 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
std::vector or std::map for texture manager
« on: July 23, 2012, 11:54:51 pm »
Hey guys, I'm making a tilemap game, and for the textures (player, tiles, etc) should I use a vector or a map to hold them? I want to assign all the tiles a specific value, like for example water's key would be 1. Which would be better for what I'm doing? I'm thinking map because you can hold the textures with a value, am I right?
Current Projects:
Technoport

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: std::vector or std::map for texture manager
« Reply #1 on: July 24, 2012, 12:06:41 am »
There's no better or worse it's completly depends one your code design and what fits your needs best.

The advantages of a std::map over a std::vector is that the map isn't fixed to linearity (i.e. 0, 1, 2, 3, ...) but you can insert and remove elements whenever, where ever you want without creating any 'holes'. Additionally you can choose a key as you want (e.g. instead of int you can use std::string).

So that 'list' shows that a std::map has some nice features you maybe want to use, but if your game can deal better with a std:vector go with that one.

Additionally there's the std::unordered_map (needs a compiler that supports this part of C++11), which doesn't sort the objects after the keys and thus guarantees an insert and lookup of O(1) = constant (i.e. that is good!). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: std::vector or std::map for texture manager
« Reply #2 on: July 24, 2012, 12:12:23 am »
Also, it's really easy to avoid duplicates with maps.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::vector or std::map for texture manager
« Reply #3 on: July 24, 2012, 09:28:43 am »
By the way: If you don't want to reinvent the wheel, you can use thor::ResourceCache of my library Thor. I also use maps internally.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: std::vector or std::map for texture manager
« Reply #4 on: July 24, 2012, 05:27:18 pm »
I'll look into thor. Do you mean by "maps" tilemaps?
Current Projects:
Technoport

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: std::vector or std::map for texture manager
« Reply #5 on: July 24, 2012, 05:35:27 pm »
I'm developing on my mac, on xcode, do you know exactly how to link thor in? I have successfully made the libraries  in Cmake.
Current Projects:
Technoport

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::vector or std::map for texture manager
« Reply #7 on: July 24, 2012, 11:12:03 pm »
Do you mean by "maps" tilemaps?
No, I mean std::maps that map keys like a filename to the actual resources (sf::Texture). I suggest to read the tutorial and the API documentation.

I'm developing on my mac, on xcode, do you know exactly how to link thor in? I have successfully made the libraries  in Cmake.
I don't know XCode, but I don't think there is something specific to Thor. Try it the way you linked SFML (and see Hiura's post) ;)

By the way, there are very few Mac users of Thor, so don't hesitate to give feedback when something doesn't work as expected.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything