Hello there,
I have myself a ImageLoader which stores a vector of sf::Textures. now here is the odd problem I am having, I have myself a class known as UserInterface.
Here is the header file
#pragma once
#include "SFML\Graphics.hpp"
#include "ImageLoader.h"
#include <vector>
class UserInterface
{
private:
ImageLoader& _imageLoader;
public:
int SelectedPower;
int Powers[9];
sf::Sprite Bar;
std::vector<sf::Sprite> PowerUpIcons;
sf::Sprite PowerSelected;
UserInterface(ImageLoader& imageLoader);
~UserInterface();
void Setup();
void SetupPowerUps();
void Draw(sf::RenderWindow& window);
void Update();
void AddPower();
};
Here is the cpp file
#include "UserInterface.h"
UserInterface::UserInterface(ImageLoader& imageLoader) :
_imageLoader(imageLoader)
{
SelectedPower = 0;
Powers[1] = 0;
Powers[2] = 0;
Powers[3] = 0;
Powers[4] = 0;
Powers[5] = 0;
Powers[6] = 0;
Powers[7] = 0;
Powers[8] = 0;
Powers[9] = 0;
}
UserInterface::~UserInterface()
{
}
void UserInterface::Setup()
{
Bar.setTexture(_imageLoader.Images[ImageNames::UI_BottomBar]);
Bar.setPosition(0, 675);
PowerSelected.setTexture(_imageLoader.Images[ImageNames::UI_Power_Selected]);
PowerSelected.setPosition(-100, -100);
SetupPowerUps();
}
void UserInterface::SetupPowerUps()
{
for (int i = 0; i < 9; i++)
{
sf::Sprite newSprite;
switch (i)
{
case 0: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up1]); break;
case 1: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up2]); break;
case 2: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up3]); break;
case 3: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up4]); break;
case 4: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up5]); break;
case 5: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up6]); break;
case 6: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up7]); break;
case 7: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up8]); break;
case 8: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up9]); break;
}
newSprite.setPosition(328 + (i * 41), 700);
newSprite.setTextureRect(sf::IntRect(0, 0, 38, 38));
PowerUpIcons.push_back(newSprite);
}
}
void UserInterface::Update()
{
PowerSelected.setPosition(328 + ( (SelectedPower - 1) * 41), 700);
}
void UserInterface::Draw(sf::RenderWindow& window)
{
sf::View tempView(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
window.setView(tempView);
//Bar.setTexture(_imageLoader.Images[ImageNames::UI_Power_Selected]);
//Bar.setPosition(0, 640);
window.draw(Bar);
for (int i = 0; i < PowerUpIcons.size(); i++)
{
window.draw(PowerUpIcons.at(i));
}
window.draw(PowerSelected);
}
void UserInterface::AddPower()
{
SelectedPower++;
}
Now basically if you look at my Setup function it sets up the Bar sf::Sprite and sets up the PowerSelected sf::Sprite, it sets the texture and also sets the position, now in my Draw function, it sets up a view and then draws out the bar, my powers and powerselected sprite. however when it attempts to draw Bar it comes up with this error
Unhandled exception at 0x0F1BE57E (sfml-graphics-d-2.dll) in ColourGalaxy.exe: 0xC0000005: Access violation reading location 0x00000004.
this error is thrown in function
void RenderTarget::draw(const Drawable& drawable, const RenderStates& states)
{
drawable.draw(*this, states);
}
Look at the image attached to see my error.
Now here is the bit that confuses me. If I comment out my window.draw(Bar); everything works fine. so if though I am setting up my PowerSelected sf::Sprite in the same function as the Bar variable. PowerSelected has no problem at all being drawn. Can anyone help me understand why my Bar variable is causing the memory violation?