Hello all,
I've recently started picking up C++ programming again to make a very simple game for my daughter. I know that my code is not elegant and I am probably missing out on some of the best practices but I'm hoping someone might be able to help me with an issue I've been having. I've been trying to set up loading in of sprites so that it can be a bit more flexible than how I had it (with the filenames hardcoded into the program, now accessing the filenames from a resources text file).
However, since I've swapped to that it will only render one Sprite and no more. I've included what I think are the relevant bits of code below but I am aware I've probably missed some!
I'm using Thor library to load in and store the textures so they only get loaded in once.
Cutscene.cpp (this is the section for loading in the sprites - m_sprites is a linked list of type sf::Sprite)
//Sets up a file reader and variables needed to read in from it
MyFileReader* sreader = new MyFileReader;
char* buffer = new char;
unsigned int size = 0;
//Reads all the information into a buffer to scan from later
const char* fileName = "Assets/cutscenes/Cutscene1Sprites.txt";
if (sreader->Exists(fileName))
{
sreader->OpenFile(fileName, "r");
sreader->ReadBuffer(buffer, size);
sreader->CloseFile();
}
else
{
MessageBox(nullptr, TEXT("There was an error with the file, please check the game assets"), TEXT("File Error"), MB_OK);
delete sreader;
}
unsigned int counter = 0;
int t = 0;
while (counter < size)
{
sf::Sprite* sprite = new sf::Sprite;
sf::Texture* tex = new sf::Texture;
//Reads the type of sprite (background, character or object)
const char* type = sreader->Scanfile(buffer, counter, size);
//Reads the name of the sprite
const char* name = sreader->Scanfile(buffer, counter, size);
//Reads in the path for this sprtie
const char* spath = sreader->Scanfile(buffer, counter, size);
//Reads in the position (x then y) for this sprite
float posX = atof(sreader->Scanfile(buffer, counter, size));
float posY = atof(sreader->Scanfile(buffer, counter, size));
//Reads in the scale (x then y) for this sprite
float scaleX = atof(sreader->Scanfile(buffer, counter, size));
float scaleY = atof(sreader->Scanfile(buffer, counter, size));
//Loads in the texture and sets it to the sprite
try
{
tex = &m_holder.acquire(name, thor::Resources::fromFile<sf::Texture>(spath));
}
catch (thor::ResourceAccessException)
{
tex = &m_holder[name];
}
sprite->setTexture(*tex);
sprite->setPosition(sf::Vector2f(posX, posY));
sprite->setScale(sf::Vector2f(scaleX, scaleY));
m_sprites.Insert(sprite);
}
Cutscene.cpp (this is when it enters this GameState
//Adds the sprites for this GameState
LinkedListNode<sf::Sprite*>* spritenode = m_sprites.GetFirst();
while (spritenode)
{
SpriteManager::GetInstance()->AddSprite(spritenode->GetData());
spritenode = spritenode->GetNext();
}
SpriteManager.cpp (this is where it adds the sprites - m_currentSprites is a Linked List which is cleared every time a GameState swaps)
void SpriteManager::AddSprite(sf::Sprite* sprite)
{
m_currentSprites.Insert(sprite);
}
WindowManager.cpp (this is the main draw function that is called each update loop - m_window is a sf::RenderWindow, GetSprites() returns the first in the Linked List of m_currentSprites referred to earlier)
m_window.clear(sf::Color::Green);
LinkedListNode<sf::Sprite*>* node;
node = SpriteManager::GetInstance()->GetSprites();
sf::RenderTexture renderTex, blurTex;
renderTex.create(m_window.getSize().x, m_window.getSize().y);
renderTex.clear(sf::Color::Green);
while (node)
{
renderTex.draw(*node->GetData());
node = node->GetNext();
}
renderTex.display();
//Draws the render texture to the window
const sf::Texture& texture = renderTex.getTexture();
sf::Sprite windowS(texture);
//Draw the render texture to the window
m_window.draw(windowS);
m_window.display();
I'm aware that I might not have included some code which might be helpful but this is my first time posting and I didn't want to upload half a Visual Studio Solution and make it too long!
Thanks for your time,
Tom