I'm writting a simple game and I've had it finished, except it had a bad file structure and every class was just an .h file.
I've decided to split the declarations and definitions into separate files, because I ran into circular inclusion issue (which was predictable).
Now that I've split them into separate files, my .h files contains:
- declaration of class, it's methods, enum and other variables.
- inclusion of neccessary files that .cpp or .h requires (so basically .cpp includes it's .h file and it get's all the inclusions it needs).
While my .cpp file contains:
- definition of methods.
Okay, now that you have a general idea on how my files are structured, here's the issue I'm having.
Spaceship.cpp requires cursorPos variable that exists in Core.h
Core.h includes Spaceship.h because it requires it for a method.
If I include Core.h inside Spaceship.h, I get double-declaration.
If I don't include Core.h inside Spaceship.h, I get no-declaration.
I'm clueless, should I create a 'Helper' class that contains variables I want to cross-share between classes?
EDIT Code as requested
Spaceship.h
#pragma once
#ifndef SPACESHIP_H
#define SPACESHIP_H
#include "Vector2D.h"
#include "Entity.h"
#include "Missile.h"
#include <SDL.h>
#include "Core.h"
extern const int SPACESHIP_SHOOT_DELAY = 50;
extern const int SPACESHIP_MAX_VELOCITY = 10;
extern const float SPACESHIP_VELOCITY_GAIN = 0.05;
extern const float SPACESHIP_VELOCITY_LOSS = -0.15;
class Spaceship : public Entity
{
public:
Vector2D position = Vector2D(0, 0);
Vector2D direction = Vector2D(0, 0);
SDL_Texture* texture;
//Required by rendering
SDL_Rect renderbox = { 0, 0, 32, 32 };
SDL_Rect boundingbox = { 0, 0, 32, 32 };
SDL_Point center = { 16, 16 };
int lastShot = SDL_GetTicks();
int angle = 0;
float velocity = 0;
void Update() override;
void Render(SDL_Renderer* renderer) override;
void Fire();
Spaceship();
~Spaceship();
};
#endif
Core.h
#pragma once
#ifndef CORE_H
#define CORE_H
#include "Vector2D.h"
#include <SDL.h>
#include "Spaceship.h"
extern Vector2D cursorPos = Vector2D();
class Core
{
private:
static bool run;
public:
static SDL_Window* window;
static SDL_Renderer* renderer;
static SDL_Cursor* cursor;
static int screenWidth;
static int screenHeight;
static void Initialize();
static void ProcessEvents(SDL_Event* e);
static void Update();
static void Render();
static int Execute();
static void Cleanup();
Core();
~Core();
};
#endif