78

I'm recently starting to pick up C++ and the SFML library, and I was wondering if I defined a Sprite on a file appropriately called "player.cpp" how would I call it on my main loop located at "main.cpp"?

Here is my code (Be aware that this is SFML 2.0, not 1.6!).

main.cpp

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "player.cpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Skylords - Alpha v1");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw();
        window.display();
    }

    return 0;
}

player.cpp

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int playerSprite(){
    sf::Texture Texture;
    if(!Texture.loadFromFile("player.png")){
        return 1;
    }
    sf::Sprite Sprite;
    Sprite.setTexture(Texture);
    return 0;
}

Where I need help is in the main.cpp where it says window.draw(); in my draw code. In that parenthesis, there should be the name of the Sprite that I want to load onto the screen. As far as I've searched, and tried by guessing, I have not succeeded into making that draw function work with my sprite on the other file. I feel like I'm missing something big, and very obvious (on either files), but then again, every pro was once a newb.

Krzysztof Raciniewski
  • 4,057
  • 2
  • 16
  • 39

2 Answers2

110

You can use header files.

Good practice.

You can create a file called player.h declare all functions that are need by other cpp files in that header file and include it when needed.

player.h

#ifndef PLAYER_H    // To make sure you don't declare the function more than once by including the header multiple times.
#define PLAYER_H

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int playerSprite();

#endif

player.cpp

#include "player.h"  // player.h must be in the current directory. or use relative or absolute path to it. e.g #include "include/player.h"

int playerSprite(){
    sf::Texture Texture;
    if(!Texture.loadFromFile("player.png")){
        return 1;
    }
    sf::Sprite Sprite;
    Sprite.setTexture(Texture);
    return 0;
}

main.cpp

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "player.h"            //Here. Again player.h must be in the current directory. or use relative or absolute path to it.

int main()
{
    // ...
    int p = playerSprite();  
    //...

Not such a good practice but works for small projects. declare your function in main.cpp

#include "stdafx.h"
#include <SFML/Graphics.hpp>
// #include "player.cpp"


int playerSprite();  // Here

int main()
{
    // ...   
    int p = playerSprite();  
    //...
  • 3
    Alright, you sir, are close to saving my arse. I've always heard of these damn header files, but never really looked into them, they're pretty important, eh? But here's another question, once I've done this, and I imported it into the "main.cpp", what would my "main.cpp" file look like in the window.draw area, how would that go about? –  Apr 09 '13 at 01:32
  • 1
    @Safixk Yes. When your code starts to get bigger they become extremely important. I suggest you do create header files if you have more than cpp file –  Apr 09 '13 at 01:34
  • 1
    Alright, trying your new code that you've just sent me, check back in 5 –  Apr 09 '13 at 01:34
  • 1
    Question, in my player.h file - "#idndef PLAYER_H" "#idndef" is not recognized preprocessing device - Would you by chance have meant "#ifndef"? –  Apr 09 '13 at 01:36
  • 1
    Also, how would my "main.cpp" file look in the "window.draw" area? –  Apr 09 '13 at 01:39
  • 1
    @Safixk You just call it like as if it was in the main file `int p = playerSprite()` see the edit. –  Apr 09 '13 at 01:41
  • 1
    @startdust_ Alright, I'm sorry, I'm completely lost. Could you please just show me, or explain in depth a bit more? –  Apr 09 '13 at 01:45
  • 1
    @Safixk I think you need to do a lil bit of reading up :). I can't explain everything in here. Just look for some C++ tutorials. You will catch up fast. It is not as complicated as it seems. At least not this part. :) –  Apr 09 '13 at 01:47
  • 1
    I did everything as mentioned but my android application is crashing when it is trying to call cpp method from another class – AdiAtAnd Sep 26 '16 at 10:25
  • 1
    How does `main.cpp` know to look for code in `player.cpp`? – Aaron Franke Feb 27 '19 at 10:05
  • 1
    isn't it bad practice to have includes in the header file? – sorh Mar 06 '20 at 20:13
  • 1
    pardon me for being basic, so basically the .h file act similar to interface in java? – A N Syafiq. May 13 '21 at 04:43
17

Small addition to @user995502's answer on how to run the program.

g++ player.cpp main.cpp -o main.out && ./main.out

rahuljain1311
  • 1,062
  • 10
  • 18