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

Author Topic: Thor ResourceCache access  (Read 1747 times)

0 Members and 1 Guest are viewing this topic.

Tommas

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Thor ResourceCache access
« on: August 30, 2014, 04:27:47 am »
I have a question about the proper way to use the resource management system found in the Thor library. The examples I have found have used ResourceCache and ResourceKey in the same scope- but what if I want to be able to access the resources stored in the ResourceCache in a different function?

For example, I have a class that has a private member mTextures, which is a thor::ResourceCache<sf::Texture>.

I have a function, loadResources, that is:
void Round::loadResourses()
{
thor::ResourceKey<sf::Texture> TextureBack = thor::Resources::fromFile<sf::Texture>("Media/Textures/Back.png");

mTextures.acquire(TextureBack);
}
 

And then, in the buildScene function, I want to access the texture referenced by TextureBack, like so:

std::shared_ptr<sf::Texture> background = mTextures.search(TextureBack);
 

But the compiler doesn't know what TextureBack is, because it is in a different scope. What is the proper way to access the resource management system from other parts of the function, or from other functions, if I have passed a reference to the ResourceCache through?

Thank you for your help in advance (and also thanks to those who work so hard to make us such cool libraries!)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thor ResourceCache access
« Reply #1 on: August 30, 2014, 10:20:46 am »
You have to store the resource keys in a way so that they are accessible in the function ;)

The straightforward way would be to use a member variable. If you have very few keys, you can store them directly:
thor::ResourceKey<sf::Texture> mTextureBack;

Otherwise you should think about a mapping, e.g. from enumerators to keys.
// inside class definition, private section:
enum struct TextureType
{
    Back,
    ...
};

std::map<TextureType, thor::ResourceKey<sf::Texture>> mKeys;

// in a member function that is called by the constructor:
mKeys[TextureType::Back] = thor::Resources::fromFile<sf::Texture>(...);

That's just one possibility. For contiguous enumerators, you can also use std::vector instead of std::map. Or std::unordered_map if you have very many resource keys and trees are effectively too slow (very unlikely).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tommas

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Thor ResourceCache access
« Reply #2 on: August 30, 2014, 06:25:11 pm »
Thanks, Nexus! That was exactly what I needed.

Tommas

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Thor ResourceCache access
« Reply #3 on: August 30, 2014, 08:46:32 pm »
Hmm... When I try and do as you suggested, I am getting a strange error. In Visual Studio 2013 express, I get an "error C2512: 'thor::ResourceKey<sf::Texture>': no appropriate default constructor available."

The output is:
1>------ Build started: Project: Hand and Foot, Configuration: Debug Win32 ------
1>  Round.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(746): error C2512: 'thor::ResourceKey<sf::Texture>' : no appropriate default constructor available
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(762) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<std::tuple<Round::TextureType &&>,std::tuple<>,0,>(_Tuple1 &,_Tuple2 &,std::_Arg_idx<0>,std::_Arg_idx<>)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>  ,            _Tuple1=std::tuple<Round::TextureType &&>
1>  ,            _Tuple2=std::tuple<>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(762) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<std::tuple<Round::TextureType &&>,std::tuple<>,0,>(_Tuple1 &,_Tuple2 &,std::_Arg_idx<0>,std::_Arg_idx<>)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>  ,            _Tuple1=std::tuple<Round::TextureType &&>
1>  ,            _Tuple2=std::tuple<>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<Round::TextureType&&,>(std::piecewise_construct_t,std::tuple<Round::TextureType &&>,std::tuple<>)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<Round::TextureType&&,>(std::piecewise_construct_t,std::tuple<Round::TextureType &&>,std::tuple<>)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,const std::piecewise_construct_t&,_Ty,std::tuple<>>(_Objty *,const std::piecewise_construct_t &,_Ty &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>
1>  ,            _Objty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>  ,            _Ty=std::tuple<Round::TextureType &&>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,const std::piecewise_construct_t&,_Ty,std::tuple<>>(_Objty *,const std::piecewise_construct_t &,_Ty &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>
1>  ,            _Objty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>  ,            _Ty=std::tuple<Round::TextureType &&>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(872) : see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(std::allocator<_Other> &,_Objty *,const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>>
1>  ,            _Ty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>  ,            _Other=std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>
1>  ,            _Objty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(872) : see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(std::allocator<_Other> &,_Objty *,const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>>
1>  ,            _Ty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>  ,            _Other=std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>
1>  ,            _Objty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(933) : see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(_Ty *,const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>
1>  ,            _Ty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(933) : see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(_Ty *,const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>,void *>
1>  ,            _Ty=std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1184) : see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_buy<std::pair<const _Kty,_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::_Buynode<const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1184) : see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_buy<std::pair<const _Kty,_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::_Buynode<const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\map(181) : see reference to function template instantiation 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace_hint<const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>  ,            _Pr=std::less<Round::TextureType>
1>  ,            _Alloc=std::allocator<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\map(181) : see reference to function template instantiation 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace_hint<const std::piecewise_construct_t&,std::tuple<Round::TextureType &&>,std::tuple<>>(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,const std::piecewise_construct_t &,std::tuple<Round::TextureType &&> &&,std::tuple<> &&)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>  ,            _Pr=std::less<Round::TextureType>
1>  ,            _Alloc=std::allocator<std::pair<const Round::TextureType,thor::ResourceKey<sf::Texture>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\map(173) : while compiling class template member function 'thor::ResourceKey<sf::Texture> &std::map<Round::TextureType,thor::ResourceKey<sf::Texture>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator [](Round::TextureType &&)'
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>          ]
1>          c:\users\jordan w\documents\visual studio 2013\projects\hand and foot - copy\hand and foot\round.cpp(51) : see reference to function template instantiation 'thor::ResourceKey<sf::Texture> &std::map<Round::TextureType,thor::ResourceKey<sf::Texture>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator [](Round::TextureType &&)' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>          ]
1>          c:\users\jordan w\documents\visual studio 2013\projects\hand and foot - copy\hand and foot\round.h(62) : see reference to class template instantiation 'std::map<Round::TextureType,thor::ResourceKey<sf::Texture>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
1>          with
1>          [
1>              _Kty=Round::TextureType
1>  ,            _Ty=thor::ResourceKey<sf::Texture>
1>          ]
 

The out put references two lines of code from my file.

Line 62 from Round.h, in the "private:" section:
std::map<TextureType, thor::ResourceKey<sf::Texture>>   mKeys;
 

and line 51 from Round.cpp, in the function "buildScene()", which is called after "loadTextures()":
std::shared_ptr<sf::Texture> tableTexture = mTextures.search(mKeys[TextureType::Table]);
 

Any ideas what might be going on? I am running Windows 7, and have the libraries statically linked, if that is helpful information at all.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thor ResourceCache access
« Reply #4 on: August 31, 2014, 11:40:46 am »
As the message says, the object needs a default constructor for std::map::operator[]. I forgot that thor::ResourceKey has none.

You can use insert() or emplace() (for insertion) and find() (for lookup) instead of operator[].
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything