I've been trying to get this to build for hours, I have no idea what's wrong. Any help is appreciated, thank you.
Here's the code:
main.cpp
#include "SDL/SDL.h"
#include "Core.h"
#include "GameStates.h"
#include "globals.h"
int main(int argc, char** args)
{
if(core.Initilization(640, 480, 32, SDL_SWSURFACE) == -1)
{
SDL_Quit();
}
while(core.desiredstate != core.quit)
{
::currentstate->EventHandling();
::currentstate->Logic();
core.ChangeState();
::currentstate->Render();
::currentstate->Update();
}
SDL_FreeSurface(core.screen);
SDL_Quit();
}
Core.h:
#ifndef CORE_H
#define CORE_H
#include "SDL/SDL.h"
#include <string>
class Core
{
public:
SDL_Surface* Load(std::string filename);
SDL_Surface* screen;
bool Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS);
void ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
void SetState(int newstate);
void ChangeState();
enum state { intro, state_null, quit };
int desiredstate, stateID;
} core;
#endif
Core.cpp
#include "Core.h"
#include "GameStates.h"
#include "SDL/SDL.h"
#include "Intro.h"
#include "globals.h"
#include <string>
/* Starts SDL subsystems and sets screen attributes */
bool Core::Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS)
{
//starts SDL subsystems, returns false upon error
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
//The screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
//Returns false if there was an error
if(screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Game", NULL);
return true;
}
/* Loads an image and optimizes it */
SDL_Surface* Core::Load(std::string filename)
{
//original loaded image
SDL_Surface* original = SDL_LoadBMP(filename.c_str());
SDL_Surface* optimized = NULL;
if(original != NULL)
{
//Sets optimized to optimized version of original
optimized = SDL_DisplayFormat(original);
SDL_FreeSurface(original);
}
return optimized;
}
/* Blits surfaces */
void Core::ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//holds the x y coordinates
SDL_Rect location;
location.x = x;
location.y = y;
if(destination != NULL)
{
SDL_BlitSurface(source, NULL, destination, &location);
}
}
Intro.h
#ifndef INTRO_H
#define INTRO_H
#include "SDL/SDL.h"
#include "GameStates.h"
class Intro : public GameStates
{
private:
SDL_Surface* test;
public:
Intro();
void EventHandling();
void Logic();
void Render();
void Update();
~Intro();
};
#endif
/* Sets desiredstate to be used in ChangeState(); */
void Core::SetState(int newstate)
{
if(desiredstate != state_null && desiredstate != quit)
{
desiredstate = newstate;
}
}
/* Changes the game state */
void Core::ChangeState()
{
if(desiredstate != state_null && desiredstate != quit)
{
//frees old state memory
delete ::currentstate;
switch(desiredstate)
{
case intro:
//allocates new state memory
::currentstate = new Intro();
break;
}
stateID = desiredstate;
desiredstate = state_null;
}
}
GameStates.h
#ifndef GAMESTATES_H
#define GAMESTATES_H
class GameStates
{
public:
virtual void EventHandling() = 0;
virtual void Logic() = 0;
virtual void Render() = 0;
virtual void Update() = 0;
};
#endif
Intro.cpp
#include "Intro.h"
#include "GameStates.h"
#include "Core.h"
#include "SDL/SDL.h"
Intro::Intro()
{
test = core.Load("test.bmp");
}
void Intro::EventHandling()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
core.SetState(core.quit);
break;
}
}
}
void Intro::Logic()
{
//to be coded when it finally builds...
}
void Intro::Render()
{
core.ApplySurface(30, 30, test, core.screen);
}
void Intro::Update()
{
SDL_Flip(core.screen);
}
Intro::~Intro()
{
SDL_FreeSurface(test);
}
globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
#include "GameStates.h"
#include "SDL/SDL.h"
extern GameStates* currentstate;
#endif;
globals.cpp
#include "globals.h"
#include "GameStates.h"
currentstate = NULL;
And finally, the error:
/home/lincoln/Desktop/test/src/Intro.cpp:6: multiple definition of core' main.o:/home/lincoln/Desktop/test/src/main.cpp:7: first defined here Core.o: In function
Core::Initilization(int, int, int, int)':
/home/lincoln/Desktop/test/src/Core.cpp:10: multiple definition of core' main.o:/home/lincoln/Desktop/test/src/main.cpp:7: first defined here main.o: In function
main':
main.cpp:(.text+0x49): undefined reference to currentstate' main.cpp:(.text+0x56): undefined reference to
currentstate'
main.cpp:(.text+0x62): undefined reference to currentstate' main.cpp:(.text+0x73): undefined reference to
currentstate'
main.cpp:(.text+0x89): undefined reference to currentstate' main.o:main.cpp:(.text+0x9a): more undefined references to
currentstate' follow
collect2: ld returned 1 exit status
make: *** [game] Error 1
Thanks for all of your help!