mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-23 10:26:30 +01:00
feat: Adding error handling for resources
This commit is contained in:
parent
83fc84ffb7
commit
983dafa3a0
Binary file not shown.
@ -22,4 +22,7 @@ const float MOVING_LEFT_SPEED = -MOVING_RIGHT_SPEED;
|
|||||||
// Time constants
|
// Time constants
|
||||||
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f); // 1 frame is 1 / 60 of the second so we get 60 frames in a second
|
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f); // 1 frame is 1 / 60 of the second so we get 60 frames in a second
|
||||||
|
|
||||||
|
// Error strings
|
||||||
|
const std::string TEXTURE_LOAD_ERROR = "TextureHolder::load - Failed to load ";
|
||||||
|
|
||||||
#endif // CONSTANTS_HPP
|
#endif // CONSTANTS_HPP
|
||||||
|
|||||||
Binary file not shown.
@ -1,5 +1,7 @@
|
|||||||
#ifndef RESOURCES_CPP
|
#ifndef RESOURCES_CPP
|
||||||
#define RESOURCES_CPP
|
#define RESOURCES_CPP
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
// Mostly Chapter 2
|
// Mostly Chapter 2
|
||||||
// Handles resource management
|
// Handles resource management
|
||||||
namespace Textures // This gives us a scope for the enumerators which allows us to write Textures::Airplane instead of just Airplane to avoid name collisions in the global scope
|
namespace Textures // This gives us a scope for the enumerators which allows us to write Textures::Airplane instead of just Airplane to avoid name collisions in the global scope
|
||||||
@ -22,13 +24,18 @@ void TextureHolder::load(Textures::ID id, const std::string& filename)
|
|||||||
// Function to load a resource, it takes one parameter for filename and one for identifier
|
// Function to load a resource, it takes one parameter for filename and one for identifier
|
||||||
{
|
{
|
||||||
std::unique_ptr<sf::Texture> texture(new sf::Texture()); // Create sf:Texture and store it in the unique pointer
|
std::unique_ptr<sf::Texture> texture(new sf::Texture()); // Create sf:Texture and store it in the unique pointer
|
||||||
texture -> loadFromFile(filename); // Load the texture from the filename
|
if(!texture -> loadFromFile(filename))// Load the texture from the filename
|
||||||
mTextureMap.insert(std::make_pair(id, std::move(texture))); // Insert texture into map mTextureMap, std::move used to take ownership from texture variable and transfer it to std::make_pair(), std::move moves the resource into a new place and removes it from earlier place https://en.cppreference.com/w/cpp/utility/move
|
{
|
||||||
|
throw std::runtime_error(TEXTURE_LOAD_ERROR + filename);
|
||||||
|
}
|
||||||
|
auto inserted = mTextureMap.insert(std::make_pair(id, std::move(texture))); // Insert texture into map mTextureMap, std::move used to take ownership from texture variable and transfer it to std::make_pair(), std::move moves the resource into a new place and removes it from earlier place https://en.cppreference.com/w/cpp/utility/move
|
||||||
|
assert(inserted.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Texture& TextureHolder::get(Textures::ID id) // returns a reference to a texture
|
sf::Texture& TextureHolder::get(Textures::ID id) // returns a reference to a texture
|
||||||
{
|
{
|
||||||
auto found = mTextureMap.find(id); // find returns an iterator to the found element or end() if nothing was found
|
auto found = mTextureMap.find(id); // find returns an iterator to the found element or end() if nothing was found
|
||||||
|
assert(found != mTextureMap.end());
|
||||||
return *found -> second; // We have to access the second member of the pointer, then we deference it and get a texture
|
return *found -> second; // We have to access the second member of the pointer, then we deference it and get a texture
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user