In the process of going from GLUT to SDL for a little OpenGL program.
So far I'm working on initialising, and everything was working fine in a single main.cpp file, but when I started breaking up the program into separate header and source files I'm running into the problems I had when i tried to modularize my last project: how to handle global variables.
My code is pretty small so I can post all of it here except 2 files which aren't involved in the problem.
///////////////main.cpp
#include <windows.h>
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "system.h"
#include "game.h"
using namespace std;
int main(int argc, char **argv)
{
cout << "Initializing...\n\n";
if( init_SDL() ) {
cout << "...SDL Initialized\n";
}
if( init_GL() ) {
cout << "...OpenGL Initialized\n";
}
gameRunning = true;
mainLoop();
atexit(SDL_Quit);
return 0;
}
//////////////game.h
#ifndef GAME_H
#define GAME_H
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
SDL_Event myEvent;
bool gameRunning;
void mainLoop();
#endif
///////////////game.cpp
#include "game.h"
void mainLoop()
{
while (gameRunning) {
if (SDL_PollEvent(&myEvent)) {
if (myEvent.type == SDL_QUIT) {
gameRunning = false;
}
}
if (myEvent.type == SDL_KEYDOWN) {
SDLKey keyPressed = myEvent.key.keysym.sym;
switch (keyPressed) {
case SDLK_ESCAPE:
gameRunning = false;
break;
}
}
glClear( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapBuffers();
}
}
In this current state I get the following errors when trying to build with MSVC 2008 Express:
Linking...
game.obj : error LNK2005: "bool gameRunning" (?gameRunning@@3_NA) already defined in main.obj
game.obj : error LNK2005: "union SDL_Event myEvent" (?myEvent@@3TSDL_Event@@A) already defined in main.obj
C:\Documents and Settings\S\Desktop\P\SDLGL00\SDLGL00\Debug\SDLGL00.exe : fatal error LNK1169: one or more multiply defined symbols found
I tried moving the definition gameRunning = true; into the game.cpp file but that didn't work and gave me a different set of errors:
c:\documents and settings\s\desktop\p\sdlgl00\sdlgl00\game.cpp(3) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\s\desktop\p\sdlgl00\sdlgl00\game.cpp(3) : error C2371: 'gameRunning' : redefinition; different basic types
c:\documents and settings\s\desktop\p\sdlgl00\sdlgl00\game.h(9) : see declaration of 'gameRunning'