C++ Game Class Object Code Help - Pygamer

Hello,

I am trying to code a spaceship shooter on the Adafruit Pygamer using C++ and the Arduino IDE. I am using the Adafruit Arcada library to abstract the user inputs, and using the Adafruit GFX and Adafruit ST7735 libraries to draw to the screen so I can use the GFXCanvas16 as a framebuffer.

Writing the code in one sketch INO file (using a header file just to store the bitmap for the spaceship as the only other file) seems to work fine and I get the spaceship moving around the screen. The INO file is attached.

Before going farther I was trying to break the code up and create a game class (my first time learning classes). I want the game class to have functions that 1. sets up the game, 2. reads the inputs, 3. updates the game data, 4. draws the new frame on the screen.

I created the Game.h and Game.cpp files and skeletonized the Class as follows:

//
    //  Game.h
    //
    //
#ifndef Game_h
#define Game_h

class Game
{
public:
    Game();
    void run();
    
    void setup();
    void processEvents();
    void update();
    void render();
    
};

#endif /* Game_h */

and

// Game.cpp

#include "Game.h"

Game::Game()
{
}

void Game::run()
{
    setup();
    processEvents();
    update();
    render();
}

void Game::setup()
{

}

void Game::processEvents()
{

}

void Game::update()
{

}

void Game::render()
{

}
// end Game.cpp

The trouble I am having is when I copy one section (like just starting with the processEvents section of the INO file) from the original INO file to the appropriate function in the Game.cpp file, I get compile errors related to the variable types not previously been defined, AND I get compile errors that the arcada object has not been defined. So I added to the top of the Game.cpp file an extern reference for the variables (originally defined in the INO file) and that seems to fix the compile error for the variables… but I cannot figure out how to do the same with the arcada object. So the Game.cpp file as an example becomes:

// Game.cpp

#include "Game.h"

extern mJoystickX, mJoystickY;

Game::Game()
{
}

void Game::run()
{
    setup();
    processEvents();
    update();
    render();
}

void Game::setup()
{

}

void Game::processEvents()
{
        // read inputs
    mJoystickX = arcada.readJoystickX();
    mJoystickY = arcada.readJoystickY();
    
    Serial.print("X: "), Serial.print(mJoystickX), Serial.print("   Y: "), Serial.println(mJoystickY);
}

void Game::update()
{

}

void Game::render()
{

}
// end Game.cpp

And now I can’t figure out how to get the compiler to understand that the arcada object was already created elsewhere and to use that same already created instance of the class object. I tried using extern for that but it didn’t resolve the issue. No matter which function of the Game class I try to first convert over I get the same error trying to use the arcada or screen objects.

Is there a way to achieve this with already created objects… having multiple files and functions use the same objects globally?

(Original INO attached)

Pygamer_Space_Shooter.ino (2.83 KB)

nebinarduino:
Hello,

Yo.

nebinarduino:
...I get compile errors that the arcada object has not been defined...

-- ? --

FYI I did resolve my issue. I was not declaring the objects / pointers in the constructor. Between that and the use of pointers to manipulate the objects I was able to get this running.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.