Я пытаюсь создать систему динамических массивов для хранения игровых объектов. Какой способ реализовать это лучше, чем тC++

Программы на C++. Форум разработчиков
Ответить
Гость
 Я пытаюсь создать систему динамических массивов для хранения игровых объектов. Какой способ реализовать это лучше, чем т

Сообщение Гость »


Я новичок в разработке игр и 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);
};
CPP File:

Код: Выделить всё

    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;
}
The entity array system for storing this type is made up of an unordered_map that stores pointers to the Entity Objects as well as a

Код: Выделить всё

const char*
for the map key, which is assigned upon calling the EntityArray::Add() function. to add an entity to the map, you would call:

Код: Выделить всё

EntityArray* arr = new EntityArray();
arr->Add("Player", new Entity("../assets/test/player.png", renderer));
... EOF ...

Код: Выделить всё

delete arr;
The heap allocated pointers associated with the unordered_map are deleted and set to in the destructor, via a for loop.
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);
};
CPP File:

Код: Выделить всё

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);
}
My Question:
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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»