Im trying to practice with sfml, but it's not working out to well. I created a class to handle the image and sprite for my "Tank" which i am trying to display. When i run the program i get no errors, the window pops up as normally but nothing is displayed.... Any help would be greatly appreciated.
#ifndef TANK_H
#define TANK_H
#include <string>
#include <SFML/Graphics.hpp>
class Tank
{
public:
Tank();
Tank(const Tank& Copy);
bool LoadFile(const std::string ImageFile); //loads image
sf::Sprite DrawFile(Tank& Copy); //function returns sprite for main
//loop in main can draw using this function
protected:
private:
static sf::Image Image; //same image for all tanks
sf::Sprite Sprite;
};
#endif // TANK_H
#include <iostream>
#include "Tank.h"
#include <SFML/Graphics.hpp>
class Tank; //compile Tank first
int main()
{
sf::RenderWindow Window(sf::VideoMode(800,600,32), "TANKWARS");
Tank Copy;
Tank Playerone(Copy);
if(!Playerone.LoadFile("Tank1.tga"))
{
return EXIT_FAILURE; //end if file cannot be loaded
}
while(Window.IsOpened())
{
sf::Event Event;
while(Window.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
Window.Close(); //close if X is hit
}
Window.Clear(); //clear
Window.Draw(Playerone.DrawFile(Playerone)); //use function to return
//the sprite
Window.Display(); //display contents
}
return EXIT_SUCCESS;
}
You don't seem to be good with classes yet. The problem is that you never set Sprite to whatever you loaded in Tank::LoadFile.
In general a class is a blueprint for an object. Your object is a Tank. It contains a Sprite, it can be loaded and drawn. Methods load and draw are made part of the class so that they can work with Sprite. Load should create a new sprite and assign it to Sprite. Draw should do Window.Draw(Sprite); Window could be passed as a parameter.
Thanks for the input, i feel i need to immerse myself further into c++ before i can start learning more complex things like SFML. I have basic understanding of classes and most syntax, but i fall off in more complex programs because of lack of experience with programs.