如何从另一个CPP文件运行?

时间:2014-01-01 22:30:31

标签: c++

对,伙计们,我在这里发脾气。

我正在为大学做一个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-&gt; Update();'我试过,但Intellisense强调了' - &gt;'并说它期望一个标识符。

代理倡议我尝试使用'Player :: Update();'但它告诉我,我无法从静态上下文中引用它。我删除了括号并允许它,但构建应用程序仍然没有做任何事情。

我也不是一个完全白痴,我通过评论背景的绘制代码让玩家没有在后台绘制玩家。仍然没有。

我真的需要帮助,所以请不要讽刺。希望这很清楚。

2 个答案:

答案 0 :(得分:4)

您没有任何代码可以创建Player或在其上调用函数;您需要添加该代码。函数不仅可以神奇地在不存在的对象上自动调用。

顺便说一下,你正在打破the rule of three所以你的课程都有破坏性/危险的双重免费错误。添加复制构造函数,或者更好的是,删除所有这些手动动态分配。

  

我联系了我的讲师关于它的事情,他设法说的没有具体的说法就是'使用Player-&gt; Update();' [..] 我删除了括号并允许它,但构建应用程序仍然没有做任何事情

猜测编程不起作用。再次联系你的教授,这次与他进行三线以上的电子邮件对话:坐下半小时或更长时间,让他向你解释如何构建你的C ++程序。这就是他付出的代价。

答案 1 :(得分:1)

在Player的实例上调用更新,而不是Player类