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

Author Topic: Rendering private sprite of class  (Read 2655 times)

0 Members and 1 Guest are viewing this topic.

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Rendering private sprite of class
« on: May 27, 2014, 09:09:27 pm »
Hi guys
I am writing here, because i need Your help.
The problem is how to excatly draw a sprite of a class?

Quote
main.cpp
#include <SFML/Window.hpp>
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>


#include "Mob.h"

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 1000), "Title");
    window.setPosition(sf::Vector2i(320, 0));
    window.setVerticalSyncEnabled(true); // call it once, after creating the window

    Mob mob1;
    mob1.attack(10);
    mob1.setskin("zombie");

    while (window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        mob1.render(window);
        window.display();
    }
    return 0;
}
 

Quote
Mob.h
#ifndef MOB_H_INCLUDED
#define MOB_H_INCLUDED

#include <SFML/Window.hpp>
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <sstream>
#include <windows.h>
#include <ctime>
#include <cstdlib>

using namespace std;

class Mob
{
    private:
        int hp;
        int mana;
        int ad;
        sf::Texture T_skin;
        sf::Sprite skin;
    public:
        void attack(int amount);
        void sethp();
        void setskin(std::string name);
        void render(sf::RenderWindow& i);
};

#endif // MOB_H_INCLUDED
 

Quote
mob.cpp
#include <iostream>
#include "Mob.h";

void Mob::attack(int amount)
{
    std::cout << "You attacked someone\n";
}


void Mob::setskin(string name)
{
    if(T_skin.loadFromFile(name+".png"))
    {
        skin.setTexture(T_skin);
        skin.setTextureRect(sf::IntRect(0, 0, T_skin.getSize().x, T_skin.getSize().y));
        skin.setPosition(sf::Vector2f(640, 290));
        cout << "I'm setting the \"Skin\" option\n";
    }
}

void Mob::render(sf::RenderWindow& i)
{
    i.draw(skin);
}
 

The problem is exactly in Mob::render() and main loop in main() - no error(img loaded, run as admin)
I would really appreciate Your help(explanations, examples).
« Last Edit: May 27, 2014, 10:48:02 pm by marcin107 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Rendering private sprite of class
« Reply #1 on: May 27, 2014, 09:21:22 pm »
Are you sure there's no error?  "window" isn't defined anywhere in Mob so Mob::render() shouldn't even compile.

If I understand the problem correctly, all you need to do is pass the window (preferably by const reference) from main() into your render() function.

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Rendering private sprite of class
« Reply #2 on: May 27, 2014, 09:40:26 pm »
I have updated the Mob.cpp to show msg.

I have a spam in console, that the "skin" is shown, so the program works, but i have only the white screen.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Rendering private sprite of class
« Reply #3 on: May 27, 2014, 09:48:20 pm »
Ah, I didn't see the window you defined in Mob.cpp because no one ever should ever create windows in a place like that.

1) Never define things at global scope without a very good reason, especially heavy multimedia objects like windows, sounds and textures.  This isn't the source of your current problem, but it's still really bad.

2) The window you've defined in Mob.cpp is not the same window you defined in main.cpp.  You're probably drawing the sprite successfully, but not to the window that you create(), clear() and display() over in main.  What you want to do is draw the sprite to the window from main.cpp.  As I said before, the best solution is to make main() pass a const reference to its window into the render() function, so Mob can draw itself onto that one.
« Last Edit: May 27, 2014, 09:51:22 pm by Ixrec »

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Rendering private sprite of class
« Reply #4 on: May 27, 2014, 09:55:46 pm »
Okey, I saw i one "little" mistake in loadfromfile "!" exactly, but i have the same situation.

Next thing is that i wanted to start learning the OOP so i don't understand so much about it right in this examp.

So what's your proposition? 

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Rendering private sprite of class
« Reply #5 on: May 27, 2014, 10:00:48 pm »
If you're asking why you're still getting a white square, I explained the problem and solution in my last post.

If you're asking about learning general knowledge like OOP, find a good C++ book and read it cover to cover.  Most good OOP practices will inevitably be part of any decent introduction to C++.  You can find plenty of book recommendations by searching this forum or the internet, just don't go for online tutorials because almost all of them are too shallow and incomplete.

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Rendering private sprite of class
« Reply #6 on: May 27, 2014, 10:11:53 pm »
I have a "C++ Primer Plus" - Stephen Prata (not sure about translating the title) but i coudn't get through this. I read about the half and i stopped right on OOP, i was trying to get through this section but i coudn't. I see i have no choice now ;)

So the reference you say?
« Last Edit: May 27, 2014, 10:13:28 pm by marcin107 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Rendering private sprite of class
« Reply #7 on: May 27, 2014, 10:16:06 pm »
Yes, main() needs to pass its window to the render() function.  There's no way for Mob to draw itself to the right window otherwise.  Passing by reference is the best choice, since sf::Window is a non-trivial object and there's no need for pointer semantics.

The differences between passing by value, by pointer and by reference are very important in C++.  They're probably also in some part of the book you haven't read yet.
« Last Edit: May 27, 2014, 10:17:42 pm by Ixrec »

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Rendering private sprite of class
« Reply #8 on: May 27, 2014, 10:48:27 pm »
I updated the code so it works now.
Thanks for your help.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rendering private sprite of class
« Reply #9 on: May 28, 2014, 12:20:34 am »
I updated the code so it works now.
Don't do that. Now it's impossible to reconstruct the original problem of this thread.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: