Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing Images problem  (Read 7607 times)

0 Members and 1 Guest are viewing this topic.

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« on: January 25, 2010, 07:49:33 pm »
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.

Code: [Select]
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.

Code: [Select]
#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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #1 on: January 25, 2010, 07:56:09 pm »
Hi

Don't include a .cpp file, it's meant to be compiled and linked, not included. You must split the declaration of your class and its implementation into a header (.h) and a .cpp file.

Quote
The compiler says that Draw it´s no a function to the class

Draw wants a parameter (the object to draw). And you should call it after Clear if you want to see something ;)
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #2 on: January 25, 2010, 08:04:30 pm »
Quote from: "Laurent"
Hi

Don't include a .cpp file, it's meant to be compiled and linked, not included. You must split the declaration of your class and its implementation into a header (.h) and a .cpp file.

Quote
The compiler says that Draw it´s no a function to the class

Draw wants a parameter (the object to draw). And you should call it after Clear if you want to see something ;)


okey, I will do that, but what should it be as parameter? If I remove the Draw function it will only render a blue background.

So I have no clue right now what it´s should be in the draw function.

map.Draw(&App)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #3 on: January 25, 2010, 08:16:17 pm »
Have you read the tutorials? ;)

Code: [Select]
App.Draw(map);
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #4 on: January 25, 2010, 08:19:04 pm »
Quote from: "Laurent"
Have you read the tutorials? ;)

Code: [Select]
App.Draw(map);


Yes, of course. A lot of times :)

But I have tried App.Draw(map); But maybe I should check if all images loads correct.

See if I can solve the problem :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #5 on: January 25, 2010, 08:31:11 pm »
Do you have another problem when you correct this? What is it?
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #6 on: January 25, 2010, 08:39:27 pm »
Quote from: "Laurent"
Do you have another problem when you correct this? What is it?


They are linked together, I want to App.Draw(groundImage); for example. Where
Code: [Select]
if(num=='1')
{
this->sprite.SetPosition(x*50, y*50);
this->sprite.SetImage(groundImage);
App.Draw();
}



But it doesn´t work.

But if I do like I did first I only get a white dot in the left upper corner. I guess thats a image that failed to load.

But how do I do to draw a Image in that case. It´s a tiled map, but it doesn´t draw any images. :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #7 on: January 25, 2010, 08:47:59 pm »
Hum, in fact there are many errors in your code ;)

Code: [Select]
void Map::loadAllImages()
{
   //Create Sprite
   Sprite blockTile(blockImage);
   Sprite groundTile(groundImage);
   Sprite wallTile(wallImage);
}

Here you just create temporary sprites using your images. These sprites will be destroyed when the function returns, so it's basically doing nothing at all. I guess you want to load your images from files?
Code: [Select]
void Map::loadAllImages()
{
   if (!blockImage.LoadFromFile("path/to/block-image") ||
       !groundImage.LoadFromFile("path/to/ground-image") ||
       !wallImage.LoadFromFile("path/to/wall-image"))
    {
        // error...
    }
}

Then you inherit from Sprite but you never use your base class. My advice would be to avoid inheriting from sf::Sprite to start. Just have a sprite as a member, and create a function to draw your map. This is what you tried to do with updateMap, all you have to do is to pass a reference to the window because you'll need it to draw something.
Code: [Select]
void Map::draw(sf::RenderWindow& window)
{

    //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);
            }
            window.Draw(this->sprite);
         }
      }
}

Code: [Select]
// In your main:
map.draw(App);


When everything works, I suggest that you create one sprite per tile rather than reusing a single sprite to draw them all. This is more efficient, you only need to set the position/image/etc of tiles once rather than every time you draw them.
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #8 on: January 25, 2010, 09:17:39 pm »
Quote from: "Laurent"
Hum, in fact there are many errors in your code ;)

When everything works, I suggest that you create one sprite per tile rather than reusing a single sprite to draw them all. This is more efficient, you only need to set the position/image/etc of tiles once rather than every time you draw them.


Thanks a lot :) I think just like you said before it´s something wrong with the linking, it doesn´t work now to link. I put them both in a project, one is main and one is called view.cpp.

So i guess I have to check the linkings out. But I am very grateful:D I hope it vill work soon.

I have two days now to complete a lot of things.

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #9 on: January 25, 2010, 09:47:15 pm »
Quote from: "Jimicro"
Quote from: "Laurent"
Hum, in fact there are many errors in your code ;)

When everything works, I suggest that you create one sprite per tile rather than reusing a single sprite to draw them all. This is more efficient, you only need to set the position/image/etc of tiles once rather than every time you draw them.


Thanks a lot :) I think just like you said before it´s something wrong with the linking, it doesn´t work now to link. I put them both in a project, one is main and one is called view.cpp.

So i guess I have to check the linkings out. But I am very grateful:D I hope it vill work soon.

I have two days now to complete a lot of things.


OMG. I don´t what to say, It works :D :D, It actually works. I´m very happy. because I´m so tired, school all day and it´s finally works.

Thank You very much :D!!!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #10 on: January 25, 2010, 09:54:57 pm »
I'm glad you made it work :)
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #11 on: January 26, 2010, 03:26:29 pm »
Is there anyway to compare the sprite with another image who lies in another file? like
Code: [Select]
if(character.getPosition().x <= tileSprite.GetPosition().x){
character.Move(0, 0);
}


?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #12 on: January 26, 2010, 06:36:30 pm »
Sure.
Laurent Gomila - SFML developer

Jimicro

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing Images problem
« Reply #13 on: January 26, 2010, 06:48:26 pm »
Quote from: "Laurent"
Sure.


But what should I write to get the images to a class so I can compare them?

How should I do to make the sprites so I can write like I did before, so the sprite has names like wallTile.getPosition().x ? is there some way? they are like this->sprite.wallTile, and I can´t just write this->sprite(wallTile).getPosition().x

Any Ideas?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Images problem
« Reply #14 on: January 26, 2010, 06:50:04 pm »
Sorry, I don't understand what you want to compare.
Laurent Gomila - SFML developer

 

anything