How to call on a function found on another file?

我怕爱的太早我们不能终老 提交于 2019-11-26 11:59:46

问题


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.


回答1:


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();  
    //...



回答2:


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

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




回答3:


Your sprite is created mid way through the playerSprite function... it also goes out of scope and ceases to exist at the end of that same function. The sprite must be created where you can pass it to playerSprite to initialize it and also where you can pass it to your draw function.

Perhaps declare it above your first while?



来源:https://stackoverflow.com/questions/15891781/how-to-call-on-a-function-found-on-another-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!