[英]How to call on a function found on another file?
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”的文件上定义了一个 Sprite,我将如何在位于“main.cpp”的主循环中调用它?
Here is my code (Be aware that this is SFML 2.0, not 1.6!).这是我的代码(请注意,这是 SFML 2.0,而不是 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播放器.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();
我需要帮助的地方在
main.cpp
,它说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.
在那个括号中,应该有我想要加载到屏幕上的 Sprite 的名称。 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.
我觉得我错过了一些大的,非常明显的(在任何一个文件上),但话说回来,每个专业人士都曾经是新手。
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播放器.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播放器.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
在 main.cpp 中声明你的函数
#include "stdafx.h"
#include <SFML/Graphics.hpp>
// #include "player.cpp"
int playerSprite(); // Here
int main()
{
// ...
int p = playerSprite();
//...
对@user995502 关于如何运行程序的回答的补充。
g++ player.cpp main.cpp -o main.out && ./main.out
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”的文件上定义了Sprite,如何在位于“ 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播放器
#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();
我需要帮助的地方是在
main.cpp
,上面写着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.
在该括号中,应该有要加载到屏幕上的Sprite的名称。 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.
据我搜索并通过猜测尝试后,我还没有成功地使该draw函数与我的sprite一起作用于另一个文件。 I feel like I'm missing something big, and very obvious (on either files), but then again, every pro was once a newb.
我觉得我缺少大的东西,而且很明显(在两个文件中都很明显),但是每位专业人士再一次都是新手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.