[英]How do I run an entire CPP file from another?
没错,我在这里泡菜很认真。
我正在为University做2D游戏,而不必做任何复杂的事情。 我有使用C#XNA制作游戏的经验,并且正在尝试使用类似的技术。 我可能与你们之间的唯一区别是,我使用的是我的讲师编写的名为S2D的游戏引擎,该引擎与XNA相似。
本质上我的问题是,到目前为止,我有两个头文件(EscapeThePrison.h和Player.h)和三个cpp文件(main.cpp,World.cpp和Player.cpp)。
在main.cpp中,我只是拥有游戏的入口点。
但是,EscapeThePrison.cpp是唯一正在运行的其他文件。 该文件应该(到目前为止)绘制背景。 这可行。
Player.cpp应该绘制我的播放器图像,但是不会这样做。 我什至使用cout只是输出文本来查看它是否正在运行,但是我的代码只是derpy,但我什么也没得到。
无论如何,我基本上都需要能够运行整个player.cpp文件,这样该死的东西才能正常工作。 下面是我的代码。
EscapeThePrison.h:
#include "S2D/S2D.h"
#include "Player.h"
using namespace S2D;
class EscapeThePrison : public Game
{
private:
Vector2* BGPosition;
Rect* BGRect;
Texture2D* BGTexture;
public:
EscapeThePrison(int argc, char* argv[]);
virtual ~EscapeThePrison();
void virtual LoadContent();
void virtual Update(int elapsedTime);
void virtual Draw(int elapsedTime);
};
Player.h:
#pragma once
#include "S2D\S2D.h"
using namespace S2D;
struct User
{
Vector2* pos;
Rect* rect;
Texture2D* texture;
};
class Player
{
private:
User* user;
public:
Player(int argc, char*argv[]);
virtual ~Player();
void virtual LoadContent();
void virtual Update(int elapsedTime);
void virtual Draw(int elapsedTime);
};
Main.cpp(游戏入口)
#include "EscapeThePrison.h"
#include "Player.h"
int main(int argc, char* argv[])
{
EscapeThePrison* game = new EscapeThePrison(argc, argv);
}
World.cpp(工作文件)
#pragma once
#include "EscapeThePrison.h"
#include "Player.h"
EscapeThePrison::EscapeThePrison(int argc, char*argv[]) : Game(argc, argv)
{
Graphics::Initialise(argc, argv, this, 1080, 720 , false, 25, 25, "EscapeThePrison", 60);
Input::Initialise();
Graphics::StartGameLoop();
}
EscapeThePrison::~EscapeThePrison()
{
delete BGTexture;
delete BGRect;
}
void EscapeThePrison:: LoadContent()
{
BGTexture = new Texture2D();
BGTexture->Load("Images/tiles.jpg", false);
BGPosition = new Vector2(0.0f,0.0f);
BGRect = new Rect(0.0f, 0.0f, 1080, 720);
}
void EscapeThePrison:: Update(int elapsedTime)
{
}
void EscapeThePrison:: Draw(int elapsedTime)
{
SpriteBatch::BeginDraw();
SpriteBatch::Draw(BGTexture, BGPosition, BGRect);
SpriteBatch::EndDraw();
}
Player.cpp(无效)
#include "Player.h"
#include <iostream>
using namespace std;
Player::Player(int argc, char* argv[])
{
user = new User();
}
Player::~Player()
{
delete user->texture;
delete user->rect;
}
void Player:: LoadContent()
{
user->texture = new Texture2D();
user->texture->Load("Images/PlayerImage.png", false);
user->pos = new Vector2(100.0f, 100.0f);
user->rect = new Rect(0.0f, 0.0f, 39, 79);
}
void Player:: Update(int elapsedTime)
{
cout<< "output";
}
void Player:: Draw(int elapsedTime)
{
SpriteBatch::BeginDraw();
SpriteBatch::Draw(user->texture, user->pos, user->rect);
SpriteBatch::EndDraw();
}
正确,现在代码已无法使用,我将告诉您我已经尝试过的内容。
我联系了我的讲师,他没有明确地说出的全部内容只是“使用Player-> Update();”。 我尝试过,但是Intellisense突出显示了'->'并说它需要一个标识符。
我尝试使用“ Player :: Update();” 主动行动 但是它告诉我我不能从静态上下文中引用它。 我卸下了支架并允许使用,但是构建应用程序仍然没有执行任何操作。
我也不是一个完全白痴,我通过注释掉背景的绘制代码,使用户不会被绘制在背景后面。 依然没有。
我真的需要帮助,所以请不要讽刺。 希望这足够清楚。
您没有任何代码来创建Player
或在其上调用函数 ; 您将需要添加该代码。 函数不仅会魔术般地对不存在的对象进行自动调用。
顺便说一句,您正在打破三个规则,因此您的类都具有破坏性/危险的双重释放错误。 添加复制构造函数,或者更好的是删除所有此手动动态分配。
我联系了我的讲师,他没有明确地说出的全部内容只是“使用Player-> Update();”。 [..]我去掉了方括号并允许使用,但是构建应用程序仍然没有执行任何操作
通过猜测编程不起作用。 再次联系您的教授,这次与他进行了三行以上的电子邮件对话:坐了半小时或更长时间,然后让他向您解释如何构造C ++程序。 那就是他的报酬。
在Player实例而不是Player类上调用更新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.