Sign in to follow this  
Hop chewer

undefined reference to game_loop'

Recommended Posts

Hello, I am having trouble with my game_loop variable. It controls the game loop and determines when to end. I am not sure where to define and initialize it so I can use it from any file. Here is my code:
main.cpp
//SDL template
#include <iostream>
#include <SDL/SDL.h>
#include "display.h"
#include "event.h"
#include "layout.h"

using namespace std;



int main(int argc, char* args[])
{
//	static int game_loop;		//loop unti game_loop = true
	game_loop = 0;
	//initialize SDL
	SDL_init();
	//star game loop
	if(!game_loop)
	{
		//get keyboard/mouse event to quit
		event_update();
	}
	//free surfaces and exit SDL
	SDL_exit();
	
	return 0;
}
event.cpp
//handle events
#include "event.h"
#include <SDL/SDL.h>
#include "layout.h"

SDL_Event event;

void event_update()
{
	while(SDL_PollEvent(&event))
	{
		if(event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
		{
			game_loop = 1;
		}
	}
}
display.cpp
#include "display.h"
#include "layout.h"
#include <SDL/SDL.h>
#include <stdexcept>
using namespace std;

SDL_Surface *screen;

void SDL_init()		//initialize SDL
{
	if(SDL_Init(SDL_INIT_VIDEO) == -1)
	{
		throw runtime_error("Cannot init video.");
	}
	
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
	if(!screen)
	{
		throw runtime_error("Cannot set SDL video mode");
	}
	
	SDL_WM_SetCaption("SDL-Template", "SDL-Template");
}

void SDL_exit()		//free surfaces and quit SDL
{
	SDL_Quit();		//quit SDL
}
layout.h
#ifndef LAYOUT_H
#define LAYOUT_H
//set dimensions of screen and the bits per pixel
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

extern int game_loop;
#endif

I didn't post event.h or display.h since I didn't think it was relevent. Here are the errors: g++ -O2 -Wall -Werror -ansi -c -o main.o main.cpp g++ -O2 -Wall -Werror -ansi -c -o event.o event.cpp g++ -o SDL main.o display.o event.o -lSDL_image -lpthread main.o: In function `main': main.cpp:(.text+0x66): undefined reference to `game_loop' main.cpp:(.text+0x75): undefined reference to `game_loop' event.o: In function `event_update()': event.cpp:(.text+0x2b): undefined reference to `game_loop' collect2: ld returned 1 exit status make: *** [SDL] Error 1

Share this post


Link to post
Share on other sites
You put:


extern int game_loop;


in layout.h - you also need to put:


int game_loop;


in layout.cpp (or another .cpp file). The extern declaration just tells the compiler that game_loop is defined elsewhere and to leave it for the linker to find it once all the object files have been stitched together.

However, since the only places you seem to need this variable are in main() and in event_update(), why not avoid the use of a global altogether and have event_update() return a value:


bool event_update()
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
{
return false;
}
}

return true;
}

int main()
{
bool game_loop=true;

while(game_loop)
{
game_loop=event_update();
}
}

Share this post


Link to post
Share on other sites
Why not have a check in the loop itself to decide when to quit?
Simplified below:


#include <iostream>
#include <SDL/SDL.h>
#include "display.h"
#include "event.h"
#include "layout.h"

using namespace std;



int main(int argc, char* args[])
{

bool quit = false;
SDL_init();

// while quit is false - continue game loop
while(!quit){

Get_Input(); // take input from user
Update_Game_Logic(); // update the game world
Render(); // render whatever needs rendering

// if the escape key is pressed - end game loop
if(IsKeyDown(Key_Escape))
quit = true;
}
SDL_exit();

return 0;
}


Share this post


Link to post
Share on other sites
Quote:
However, since the only places you seem to need this variable are in main() and in event_update(), why not avoid the use of a global altogether and have event_update() return a value:
Okay, this worked. Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this