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

Author Topic: Getting Sprites to Work  (Read 3699 times)

0 Members and 1 Guest are viewing this topic.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Sprites to Work
« on: August 14, 2010, 12:34:47 am »
I'm gonna sound like a total idiot, but I can't get my sprites to work on my SFML program.

I am trying to make a program that displays a map on the left side of the window and a text input/output on the right side of the window.
(I'm still a beginner and the program isn't complete yet, because I don't know how to go about it)
Anyhow, the sprite of my map won't display to the screen.

Please help.

tyler.is.number.one@gmail.com
-Wander
-Wander

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Getting Sprites to Work
« Reply #1 on: August 14, 2010, 12:42:44 am »
There is no way anyone can help you without further information.
Listen to my band: pencilcase.bandcamp.com

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Sprites to Work
« Reply #2 on: August 14, 2010, 12:46:27 am »
OOPS! I meant to paste the code.... :/ Got distracted.

Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using std::endl; // Ends the line
using std::cout; // Basic Output
using sf::Clock; // Measures time
using sf::Sleep; // Causes the program to sleep
using sf::Thread; // Lightweight process (multiples can run at the same time)
using sf::Mutex; // Locks and unlocks chunks of code
using sf::Window; // Creates a window

class MyPicture
{
public :

    MyPicture(const MyPicture& Copy) : Image(Copy.Image), Sprite(Copy.Sprite)
    {
        Sprite.SetImage(Image);
    }

private :

    sf::Image  Image;
    sf::Sprite Sprite;
};

int main()
{
    Window App(sf::VideoMode::GetMode(1), "SFML Window", sf::Style::Resize | sf::Style::Close);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            const sf::Input& Input = App.GetInput();

            bool LeftButtonDown = Input.IsMouseButtonDown(sf::Mouse::Left);
            bool RightButtonDown = Input.IsMouseButtonDown(sf::Mouse::Right);
            unsigned int MouseX = Input.GetMouseX();
            unsigned int MouseY = Input.GetMouseY();

            cout << MouseX << " " << MouseY << endl;
            cout << LeftButtonDown << RightButtonDown << endl;

            sf::Image Image;
            if (!Image.LoadFromFile("MapBMP.bmp"))
            {
                cout << "Error... Couldn't load file..." << endl;
            }

            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
            }

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            {
                App.Close();
            }

            App.Display();
        }

    }

    return EXIT_SUCCESS;
}
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Sprites to Work
« Reply #3 on: August 14, 2010, 08:44:01 am »
Two things :
1) DON'T LOAD AN IMAGE IN A LOOP! (print this in your brain hard) You will just kill any performance left.
2) You're not displaying anything here.  :wink:

You have this class, MyPicture, but you don't have any object of this class.
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Sprites to Work
« Reply #4 on: August 14, 2010, 08:46:28 am »
Code: [Select]
sf::Image Image;
            if (!Image.LoadFromFile("MapBMP.bmp"))
            {
                cout << "Error... Couldn't load file..." << endl;
            }

This is what is loading the image, correct?

Also, I never found a command in the tutorials that actually displays the picture. What command is it? How is it used?
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Sprites to Work
« Reply #5 on: August 14, 2010, 08:51:24 am »
Yes, it is. But you're loading the pict in a loop, so each time the loop restart you load it again, and again, and again, … This is a bad idea. Just load it once before the loop.

You can find all the information you need in the tutorials. Here : http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php . Read it all once and then experiments some stuff yourself.  :wink:
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Sprites to Work
« Reply #6 on: August 14, 2010, 09:17:23 am »
Thank you very much! :D

I got the map to display and now I'm moving around the map to see different parts of it. I need to know how to split the screen in half to have the map draw on one half and text input on the other side.

Can you help me, or do I need to make a new topic?
-Wander