Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Optymalizacja zapisu powtarzających się danych podczas tworzenia obiektów

Ostatnio zmodyfikowano wczoraj o godz. 23:11
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
Optymalizacja zapisu powtarzających się danych podczas tworzenia obiektów
» 2025-09-12 15:16:03
Gdy tworzy się wiele obiektów a każdy z nich jest opisany zmiennymi w podobny sposób dobrze jest korzystać z przestrzeni nazw w nawiasach klamrowych.

przykład 1
C/C++
Animation() {
   
   
currentFrame = 0;
   
currentLayer = 0;
   
   
int size = 64;
   
   
Frame * frame;
   
   
{
       
frame = new Frame();
       
std::vector < Layer * > & layers = frame->getLayers(); // powtarzająca się zmienna
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
frames.push_back( frame );
   
}
   
    {
       
frame = new Frame();
       
std::vector < Layer * > & layers = frame->getLayers(); // powtarzająca się zmienna
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
layers.push_back( new Layer( L"Layer " + std::to_wstring( layers.size() ), sf::Vector2i( size, size ) ) );
       
frames.push_back( frame );
   
}
   
   
lastLayer();
   
}

przykład 2
C/C++
void createMonsters() {
    {
       
Monster * monster = new Monster( L"monsters\\dziobak", 72, 36, 50, 10 );
       
monster->attributes[ Attribute::HP ] = 40;
       
monster->attributes[ Attribute::HP_max ] = 40;
       
monster->attributes[ Attribute::MP ] = 10;
       
monster->attributes[ Attribute::MP_max ] = 10;
       
monster->attributes[ Attribute::STRENGTH ] = 2;
       
monster->attributes[ Attribute::DEXTERITY ] = 2;
       
monster->attributes[ Attribute::INTELLIGENCE ] = 2;
       
prefabs.push_back( monster );
   
}
   
    {
       
Monster * monster = new Monster( L"monsters\\goblin", 32, 16, 50, 20 );
       
monster->attributes[ Attribute::HP ] = 80;
       
monster->attributes[ Attribute::HP_max ] = 80;
       
monster->attributes[ Attribute::MP ] = 10;
       
monster->attributes[ Attribute::MP_max ] = 10;
       
monster->attributes[ Attribute::STRENGTH ] = 8;
       
monster->attributes[ Attribute::DEXTERITY ] = 4;
       
monster->attributes[ Attribute::INTELLIGENCE ] = 2;
       
prefabs.push_back( monster );
   
}
   
    {
       
Monster * monster = new Monster( L"monsters\\wilczur", 70, 35, 60, 50 );
       
monster->attributes[ Attribute::HP ] = 120;
       
monster->attributes[ Attribute::HP_max ] = 120;
       
monster->attributes[ Attribute::MP ] = 10;
       
monster->attributes[ Attribute::MP_max ] = 10;
       
monster->attributes[ Attribute::STRENGTH ] = 12;
       
monster->attributes[ Attribute::DEXTERITY ] = 6;
       
monster->attributes[ Attribute::INTELLIGENCE ] = 3;
       
prefabs.push_back( monster );
   
}
}
P-183000
pekfos
» 2025-09-12 22:16:32
Jeśli kopiujesz kod, to pewnie robisz coś źle.
C/C++
Animation() {
   
   
currentFrame = 0;
   
currentLayer = 0;
   
   
int size = 64;
   
   
for( int fr = 0; fr < 2; ++fr )
   
{
       
Frame * frame = new Frame();
       
for( int i = 0; i < 4; ++i )
           
 frame->getLayers().push_back( new Layer( L"Layer " + std::to_wstring( i ), sf::Vector2i( size, size ) ) );
       
       
frames.push_back( frame );
   
}
   
   
lastLayer();
}
P-183001
tBane
Temat założony przez niniejszego użytkownika
» 2025-09-12 23:11:12
Racja, moje niedopatrzenie :-)
P-183002
« 1 »
  Strona 1 z 1