Hello
I´m on a school projekt to do a little game. But I´m stuck. I don´t know how to draw images when they are in another file. The compiler says that Draw it´s no a function to the class. Here is the soruce for better description.
view.cpp #include <SFML/Graphics.hpp>
#include <fstream>
#include <iostream>
using namespace sf;
using namespace std;
class Map: public Sprite
{
private:
sf::Sprite sprite;
char num;
char level[12][12];
int height;
//Create image
sf::Image blockImage;
sf::Image groundImage;
sf::Image wallImage;
public:
void openFile();
void updateMap();
void loadAllImages();
void update();
Map();
};
void Map::openFile()
{
height = 0;
//Read textfle (2D char array)
ifstream file;
file.open("level.txt");
std::string line;
while(getline(file, line))
{
for(int width = 0; width < 12; width++)
{
level[height][width] = line.at(width);
}
height++;
}
//Close file stream
file.close();
}
void Map::loadAllImages()
{
//Create Sprite
Sprite blockTile(blockImage);
Sprite groundTile(groundImage);
Sprite wallTile(wallImage);
}
void Map::updateMap()
{
//Scroll through level and display corresponding image of tile
for(int y = 0; y < 12; y++)
{
for(int x = 0; x < 12; x++)
{
num = level[y][x];
if(num == '1')
{
this->sprite.SetPosition(x*50, y*50);
this->sprite.SetImage(blockImage);
}
else if(num == '2')
{
this->sprite.SetPosition(x*50, y*50);
this->sprite.SetImage(groundImage);
}
else if(num == '3')
{
this->sprite.SetPosition(x*50, y*50);
this->sprite.SetImage(wallImage);
}
}
}
}
and the other file that contains main function.
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
#include "view.cpp"
using namespace sf;
using namespace std;
int main()
{
Map map;
map.openFile();
map.updateMap();
//Display using SFML
//Render Window
sf::RenderWindow App(sf::VideoMode(640, 480), "Shoot");
//Game loop
while (App.IsOpened())
{
sf::Event Event;
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
{
App.Close();
}
}
map.loadAllImages();
App.Draw();
// Clear the screen with white color
App.Clear(sf::Color(0, 0, 255));
//Display sprites
App.Display();
}
return EXIT_SUCCESS;
//*/
}
Hope you can help me