如何调用另一个文件中的函数?

时间:2021-07-22 16:00:24

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"?

我最近开始学习c++和SFML库,我想知道我是否在一个名为“player”的文件中定义了一个精灵。cpp“我怎么把它命名为我的主回路,位于“main.cpp”?

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

这是我的代码(请注意这是SFML 2.0,而不是1.6!)

main.cpp

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

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;
}

How 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.

我在哪里需要帮助是最重要的。cpp,它写的是window。draw();在我画的代码。在这个括号中,应该有我想要加载到屏幕上的精灵的名字。据我所知,我试着猜测。我还没有成功地使绘制函数与另一个文件上的精灵一起工作。我觉得我错过了一些重要的东西,而且非常明显(在任何一个文件上),但话说回来,每个专业人士都曾经是一个新手。

2 个解决方案

#1


54  

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声明头文件中其他cpp文件需要的所有函数,并在需要时包含它。

player.h

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

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

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

这不是一个好的做法,但对小型项目有效。在main.cpp中声明函数

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


int playerSprite();  // Here

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

#2


0  

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.

你的精灵是通过playerSprite函数创建的…它也超出了范围,并在相同的功能结束时停止存在。必须创建精灵,您可以将它传递给playerSprite以初始化它,也可以将它传递给绘制函数。

Perhaps declare it above your first while?

也许在你第一次申报之前申报?

#1


54  

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声明头文件中其他cpp文件需要的所有函数,并在需要时包含它。

player.h

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

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

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

这不是一个好的做法,但对小型项目有效。在main.cpp中声明函数

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


int playerSprite();  // Here

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

#2


0  

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.

你的精灵是通过playerSprite函数创建的…它也超出了范围,并在相同的功能结束时停止存在。必须创建精灵,您可以将它传递给playerSprite以初始化它,也可以将它传递给绘制函数。

Perhaps declare it above your first while?

也许在你第一次申报之前申报?