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

Author Topic: Problems using textures in classes  (Read 2722 times)

0 Members and 1 Guest are viewing this topic.

C0dedSurvivor

  • Newbie
  • *
  • Posts: 10
    • View Profile
Problems using textures in classes
« on: November 23, 2015, 07:25:17 pm »
I didn't even fully understand what the problem is. It was fixed when I commented out the texture and sprite declaration, but I need those for what I am using it for. The code I used was:

Main:

enemy enemies[4];

int main(){
   for (int a = 0; a < enemyListL; a++){
      enemies[a].name = "dead";
      if (!enemies[a].texture.create(32, 32)){}
   }
   enemies[0].sprite.setPosition(50, 50);

   generateEnemy(enemies, "link", 15);
}

Class:

#include <SFML/Graphics.hpp>

using namespace sf;

const int enemyListL = 5;

string enemiesNames[enemyListL] = { "link", "wyrm", "armored wyrm", "glitching wyrm", "evil guard" };

//health, pAttack, aAttack, pDefense, aDefense, speed, luck
int enemyBaseStat[7][enemyListL] = { 5, 5, 5, 5, 5, 5, 5};

class enemy{
public:
   string name;
   int health;
   int level;
   int pAtk;
   int aAtk;
   int pDef;
   int aDef;
   int speed;
   int luck;
   Texture texture;
   Sprite sprite;
   enemy(){               //This is the line it points to with the error
   }
};

int takeDamageDisk(Disks disk, enemy enemy1){
   srand(time(NULL));
   return ((disk.damage * 2) / (enemy1.aDef)) * ((rand() % 15) + 91) / 100;
}

void generateEnemy(enemy enemiesT[4], string name, int level){
   for (int a = 0; a < 4; a++){
      if (enemiesT[a].name == "dead"){
         for (int b = 0; b < enemyListL; b++){
            if (name == enemiesNames){
               enemiesT[a].level = level;
               srand(time(NULL));
               enemiesT[a].health = enemyBaseStat[0] * level + (enemyBaseStat[0] * level * (((rand() % 30) - 15) + 100) / 100);
               srand(rand() ^ time(NULL));
               enemiesT[a].pAtk = enemyBaseStat[1] * level + (enemyBaseStat[1] * level * (((rand() % 30) - 15) + 100) / 100);
               srand(rand() * time(NULL));
               enemiesT[a].aAtk = enemyBaseStat[2] * level + (enemyBaseStat[2] * level * (((rand() % 30) - 15) + 100) / 100);
               srand(rand() ^ time(NULL));
               enemiesT[a].pDef = enemyBaseStat[3] * level + (enemyBaseStat[3] * level * (((rand() % 30) - 15) + 100) / 100);
               srand(rand() * time(NULL));
               enemiesT[a].aDef = enemyBaseStat[4] * level + (enemyBaseStat[4] * level * (((rand() % 30) - 15) + 100) / 100);
               srand(rand() ^ time(NULL));
               enemiesT[a].speed = enemyBaseStat[5] * level + (enemyBaseStat[5] * level * (((rand() % 30) - 15) + 100) / 100);
               srand(rand() * time(NULL));
               enemiesT[a].luck = enemyBaseStat[6] * level + (enemyBaseStat[6] * level * (((rand() % 10) - 5) + 100) / 100);
               sf::Image image;
               image.loadFromFile("Images\Enemies\\" + name + ".png");
               enemiesT[a].texture.update(image);
               enemiesT[a].sprite.setTexture(enemiesT[a].texture);
            }
         }
      }
   }
}


The window compiles correctly but crashes immediately after launch, before the window can even register.

The error is this:
Unhandled exception at 0x777522D2 (ntdll.dll) in RPGFramework.exe: 0xC0000005: Access violation writing location 0x00000004.

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Problems using textures in classes
« Reply #1 on: November 23, 2015, 07:36:47 pm »
Did you use a debugger to narrow down the line in which the error occurs (stacktrace, where it gets called from)?

Apart of that, I'm not sure you should use an sf::Texture as a value-member of your enemy class. It will get loaded multiple times redundantly and if you're not careful, it will get copied, which is bad for performance and memory footprint.
« Last Edit: November 23, 2015, 07:38:50 pm by BlueCobold »

C0dedSurvivor

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problems using textures in classes
« Reply #2 on: November 23, 2015, 07:53:51 pm »
As far as I can find, that is where the stacktrace starts. As for whether I should do this, I can't find any other way to check the name of a texture to attach it to a sprite. I don't want to have to create separate classes for each enemy. As you can see from the second part of the code, I am attaching the texture by comparing the file name to the name of the enemy. If there is a way to check the the name of a texture, compare it to the name of an enemy and attach it if it matches, that would be great. But I don't know ow to do that.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problems using textures in classes
« Reply #3 on: November 23, 2015, 10:11:51 pm »
Globals make me cry  :'(

I think I can point you in the direction of the source of your error:
const int enemyListL = 5;
// ...
enemy enemies[4];
// ...
for (int a = 0; a < enemyListL; a++){
  enemies[a].name = "dead";
  if (!enemies[a].texture.create(32, 32)){}
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

C0dedSurvivor

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problems using textures in classes
« Reply #4 on: November 30, 2015, 05:13:24 pm »
Thanks for your help, because it seems SFML has the same problem you have, it hates being Global. As soon as a moved it into being a non-global texture, it fixed.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problems using textures in classes
« Reply #5 on: November 30, 2015, 06:19:30 pm »
Did you not see the error in the code I posted?  :(
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything