简体   繁体   中英

How do I run an entire CPP file from another?

Right guys, I'm in a serious pickle here.

I'm doing a 2D game for University and it doesn't have to be complicated by any means. I have experience making games in C# XNA and am trying to use similar techniques. The only difference I might have to you guys is that I'm using a game engine called S2D, written by my lecturer, which bears similarities to XNA.

Essentially my problem is that I have two header files (EscapeThePrison.h and Player.h) and three cpp files (main.cpp, World.cpp and Player.cpp) so far.

In main.cpp I simply have the entry point for the game.

EscapeThePrison.cpp is the only other file being run, however. This file is supposed to (as of yet) draw a background. This works.

Player.cpp is supposed to draw my player image, however it does not do this. I even used cout to just output text to see if it's being run but my code is just derpy, but I get nothing.

Anyway I basically need to be able to run the entire player.cpp file so the damn thing will work. Below is my code.

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(entry for game)

#include "EscapeThePrison.h"
#include "Player.h"
int main(int argc, char* argv[]) 
{
    EscapeThePrison* game = new EscapeThePrison(argc, argv);

}

World.cpp (the working file)

#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 (the non-working one)

#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();
}

Right, now the code is out of the way, I'll tell you about what I've tried.

I contacted my lecturer about it and all he managed to say without being specific was pretty much 'use Player->Update();' which I tried but Intellisense highlights the '->' and says it expected an identifier.

Acting on initiative I tried using 'Player::Update();' but it told me I can't reference it from a static context. I removed the brackets and it allowed it, but building the application still did nothing.

I'm also not a complete idiot and I made user the player wasn't being drawn behind the background by commenting out the draw code for the background. Still nothing.

I really need help with this so no sarcastic answers please. Hope this is clear enough.

You don't have any code to create a Player , or invoke functions on it ; you will need to add that code. Functions don't just magically get called autonomously on non-existent objects.

By the way, you are breaking the rule of three so your classes all have destructive/dangerous double-free bugs. Either add copy constructors or, better yet, drop all of this manual dynamic allocation.

I contacted my lecturer about it and all he managed to say without being specific was pretty much 'use Player->Update();' [..] I removed the brackets and it allowed it, but building the application still did nothing

Programming by guessing doesn't work. Contact your professor again and this time have more than a three-line email conversation with him: sit down for half an hour or more, and have him explain to you how to structure your C++ program. That's what he's paid for.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM