Я новичок в разработке игр и C++. Я пытаюсь реализовать способ динамического хранения игровых объектов. Пока что то, что у меня есть, похоже, работает, но я не уверен, что использую все так, как должен.
Мой конструктор класса сущности принимает путь для загрузки текстуры и ссылка на рендерер. Сам класс состоит из SDL_Rect для хранения позиции, геттеров для SDL_Rect и SDL_Texture и сеттера для изменения позиции.
Заголовок:
Код: Выделить всё
class Entity
{
public:
Entity(const std::string& p_path, SDL_Renderer* p_ren);
~Entity();
private:
SDL_Texture* texture;
public:
SDL_Texture& GetTex();
SDL_Rect& GetPos();
SDL_Rect position;
void SetPos(const int& x_pos, const int& y_pos);
};
Код: Выделить всё
Entity::Entity(const std::string& p_path, SDL_Renderer* p_ren)
{
SDL_Point size;
texture = IMG_LoadTexture(p_ren, p_path.c_str());
SDL_QueryTexture(texture, NULL, NULL, &size.x, &size.y);
position.x = size.x;
position.y = size.y;
position.w = size.x;
position.h = size.y;
}
Entity::~Entity()
{
SDL_DestroyTexture(texture);
}
SDL_Texture& Entity::GetTex()
{
return *texture;
}
SDL_Rect& Entity::GetPos()
{
return position;
}
void Entity::SetPos(const int& x_pos, const int& y_pos)
{
position.x = x_pos;
position.y = y_pos;
}
Код: Выделить всё
const char*
Код: Выделить всё
EntityArray* arr = new EntityArray();
arr->Add("Player", new Entity("../assets/test/player.png", renderer));
Код: Выделить всё
delete arr;
Код: Выделить всё
nullptr
Header:
Код: Выделить всё
class EntityArray
{
public:
EntityArray();
~EntityArray();
public:
std::unordered_map entity_container;
public:
Entity* Add(const char* p_key, Entity* p_ent);
void Sub(const char* p_key);
Entity& Find(const char* p_key);
};
Код: Выделить всё
EntityArray::EntityArray()
{
}
EntityArray::~EntityArray()
{
for(auto i = entity_container.begin(); i != entity_container.end(); i++) {
delete i->second;
i->second = nullptr;
}
entity_container.clear();
}
Entity* EntityArray::Add(const char* p_key, Entity* p_ent)
{
entity_container.emplace(p_key, p_ent);
return entity_container.at(p_key);
}
Is this an acceptable/efficient method to do what I'm trying to do? I apologize if anything isn't clear, I am relatively new to this and wanted to try making this system on my own without copying code, using what I've learned and trying to piece it together in my mind. Thanks in advance.
Источник: https://stackoverflow.com/questions/781 ... t-would-be