I know this question has been asked before. But I thought I tried the common solution and it didn't work. I did my research, lol.
I have created a class that will handle sprites. Once implemented, it'll be used to animate sprite-sheets. (in case you're wondering why I need a class that stores a sprite). I am storing both the sprite and it's texture as variables, and I'm using a function to access the stored sprite.
Here's gameSprite.h
#ifndef _GAMESPRITE_H
#define _GAMESPRITE_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <string>
class gameSprite
{
public:
gameSprite(std::string spriteSheet, int columns = 1, int rows = 1, int x = 0, int y = 0);
sf::Sprite getSpritesheet();
private:
sf::Sprite sprite;
sf::Texture texture;
};
#endif
Here's gameSprite.cpp
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include "gameSprite.h"
gameSprite::gameSprite(std::string spriteSheet, int columns, int rows, int x, int y)
{
// temporarily hard coding the texture to be loaded for testing purposes
texture.loadFromFile("img/MrZOMG.png");
sprite.setTexture(texture);
}
sf::Sprite gameSprite::getSpritesheet()
{
return sprite;
}
and here's the bit of code in my main loop where I try to draw the sprite
graphics.gameWindow.clear(sf::Color(0,255,255));
graphics.gameWindow.draw( (sprites[0]).getSpritesheet() );
graphics.gameWindow.display();
sprites is a vector containing instances of gameSprite. I'm hardcoding which one to draw for testing purposes.
I know both my posts have been asking for help. I'm sorry about that. Thank you so much for reading this, and any help or direction you can give would be great. (meanwhile, I'm going to try and make a quick and dirty workaround that'll re-load the texture over the sprite whenever the sprite is called up. I hope that's not the only option, as that sounds like it'd take more memory than just loading the texture once)
(UGH! I'm so used to hitting f5 to compile that I accidentally hit f5 when I wanted to post and it refreshed the page. So I had to write this twice)
-edit-
More code was asked for, so here's the code of my game loop.
game.h
#ifndef _GAME_H
#define _GAME_H
#include <vector>
#include "graphic.h"
#include "input.h"
#include "setting.h"
#include "gameSprite.h"
class game
{
public:
game();
void gameLoop();
graphic graphics;
std::vector<gameSprite> sprites;
std::vector<input> inputs;
setting settings;
};
#endif
game.cpp
#include <vector>
#include <iostream>
#include "game.h"
#include "global.h"
#include <SFML/Graphics.hpp>
game::game()
{
inputs.push_back(input(1));
sprites.push_back(gameSprite("img/MrZOMG.png"));
}
void game::gameLoop()
{
while(graphics.gameWindow.isOpen())
{
for (std::size_t i=0; i<inputs.size(); i++) {
(inputs.at(i)).update();
}
sf::Event Event;
while(graphics.gameWindow.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
std::cout << "The window was closed." << std::endl;
graphics.gameWindow.close();
break;
default:
break;
}
graphics.gameWindow.clear(sf::Color(0,255,255));
graphics.gameWindow.draw( (sprites[0]).getSpritesheet() );
graphics.gameWindow.display();
}
}
std::cout << "Loop has ended." << std::endl;
}