Hello everyone!
The title is actually most of my compiler output!
This is my entire compiler output:

multiple definition of `game' 
  first defined here 
  ld returned 1 exit status 
 C:\Documents and Settings\Benjamin Dahse\Skrivebord\SDL 2D Platform Game\Makefile.win [Build Error]  ["2D] Error 1

And now this is all of my classes (The final function is that you'll be able to move around a sprite)..
Defs.h:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 450

enum
{
	PLAYER_SPRITE,
	MAX_SPRITES
};

Draw.cpp:

#include "Draw.h"

extern void draw_player(void);
extern void draw_entities(void);

void draw()
{	
	SDL_FillRect(game.screen, NULL, 0);
	draw_player();
	SDL_Flip(game.screen);	
	SDL_Delay(1);
}

void delay(unsigned int frame_limit)
{
	unsigned int ticks = SDL_GetTicks();

	if(frame_limit < ticks)
	{
		return;
	}	
	if(frame_limit > ticks + 16)
	{
		SDL_Delay(16);
	}	
	else
	{
		SDL_Delay(frame_limit - ticks);
	}
}

Draw.h:

#include "Structs.h"

Game game;

Graphics.cpp:

#include "Graphics.h"

SDL_Surface * load_image(char * name)
{	
	SDL_Surface * temp = IMG_Load(name);
	SDL_Surface * image = NULL;
	
	if(temp == NULL)
	{
		printf("Failed to load image %s\n", name);	
		return NULL;
	}
	
	SDL_SetColorKey(temp, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(temp->format, 0, 0xFF, 0xFF));
	image = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	
	if(image == NULL)
	{
		printf("Failed to convert image %s to native format\n", name);	
		return NULL;
	}
	
	return image;
}

void draw_image(SDL_Surface * image, int x, int y)
{
	SDL_Rect dest;
	
	dest.x = x;
	dest.y = y;
	dest.w = image->w;
	dest.h = image->h;
	
	SDL_BlitSurface(image, NULL, game.screen, & dest);
}

void load_sprite(int index, char *name)
{	
	if(index >= MAX_SPRITES || index < 0)
	{
	    printf("Invalid index for sprite! Index: %d Maximum: %d\n", index, MAX_SPRITES);	
		exit(1);
	}
	
	sprite[index].image = load_image(name);
	
	if (sprite[index].image == NULL)
	{
		exit(1);
	}
}

SDL_Surface * get_sprite(int index)
{
	if(index >= MAX_SPRITES || index < 0)
	{
		printf("Invalid index for sprite! Index: %d Maximum: %d\n", index, MAX_SPRITES);		
		exit(1);
	}
	
	return sprite[index].image;
}

void free_sprites()
{
	int i;
	
	for(i = 0; i < MAX_SPRITES; i++)
	{
		if(sprite[i].image != NULL)
		{
		    SDL_FreeSurface(sprite[i].image);
		}
	}
}

void load_all_sprites()
{
	load_sprite(PLAYER_SPRITE, "GFX/firefly.png");
}

Graphics.h:

#include "Structs.h"

extern Sprites sprite[MAX_SPRITES];
extern Game game;

Init.cpp:

#include "Init.h"

extern void free_sprites();

void init(char * title)
{	
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("Could not initialize SDL: %s\n", SDL_GetError());		
		exit(1);
	}
	
	game.screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_HWPALETTE|SDL_DOUBLEBUF);
	
	if(game.screen == NULL)
	{
		printf("Couldn't set screen mode to %d x %d: %s\n", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_GetError());
		exit(1);
	}
	
	SDL_WM_SetCaption(title, NULL);
}

void clean_up()
{	
	free_sprites();	
	SDL_Quit();
}

Init.h:

#include "Structs.h"

extern Game game;

Input.cpp:

#include "Input.h"

void get_input()
{
	SDL_Event event;
	
	while(SDL_PollEvent(& event))
	{
		switch(event.type)
		{	
			case SDL_QUIT:
				exit(0);
			break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
				    case SDLK_UP:
						input.up = 1;
	                break;
					case SDLK_DOWN:
						input.down = 1;
					break;	
					case SDLK_LEFT:
						input.left = 1;
					break;	
					case SDLK_RIGHT:
						input.right = 1;
					break;
					case SDLK_ESCAPE:
						exit(0);
					break;
					default:
					break;
				}
			break;
			case SDL_KEYUP:
				switch(event.key.keysym.sym)
				{
				    case SDLK_UP:
						input.up = 0;
	                break;
					case SDLK_DOWN:
						input.down = 0;
					break;	
					case SDLK_LEFT:
						input.left = 0;
					break;	
					case SDLK_RIGHT:
						input.right = 0;
					break;
				    default:
					break;
				}
			break;
		}
	}
}

Input.h:

#include "Structs.h"

extern Control input;

Main.cpp:

#include "Main.h"

extern void init(char *);
extern void clean_up(void);
extern void get_input(void);
extern void draw(void);
extern void init_player(void);
extern void do_player(void);
extern void do_entities(void);
extern void load_all_sprites(void);
extern void delay(unsigned int);

int main(int argc, char *argv[])
{
	unsigned int frame_limit = SDL_GetTicks() + 16;
	int go;
	
	init("2D Game");	
	atexit(clean_up);	
	go = 1;	
	load_all_sprites();
	init_player();
	
	while(go == 1)
	{	
		get_input();
		do_player();
		draw();
		delay(frame_limit);		
		frame_limit = SDL_GetTicks() + 16;
	}
	
	exit(0);
}

Main.h:

#include "Structs.h"

Game game;
Control input;
Entity player;
Sprites sprite[MAX_SPRITES];

Player.cpp:

#include "Player.h"

extern int load_sprite(char *);
extern void draw_image(SDL_Surface *, int, int);
extern SDL_Surface * get_sprite(int);

void init_player()
{
	player.sprite = get_sprite(PLAYER_SPRITE);
	player.x = SCREEN_WIDTH / 2;
	player.y = SCREEN_HEIGHT / 2;
}

void do_player()
{
	if(input.up == 1)
	{
		player.y -= 3;
		
		if(player.y < 0)
		{
		    player.y = 0;
		}
	}
	
	if(input.down == 1)
	{
		player.y += 3;
		
		if(player.y + player.sprite->h >= SCREEN_HEIGHT)
		{
			player.y = SCREEN_HEIGHT - (player.sprite->h + 1);
		}
	}
	
	if(input.left == 1)
	{
		player.x -= 3;
		
		if(player.x < 0)
		{
			player.x = 0;
		}
	}
	
	if(input.right == 1)
	{
		player.x += 3;
		
		if(player.x + player.sprite->w >= SCREEN_WIDTH)
		{
			player.x = SCREEN_WIDTH - (player.sprite->w + 1);
		}
	}
}

void draw_player()
{	
	draw_image(player.sprite, player.x, player.y);
}

Player.h:

#include "Structs.h"

extern Entity player;
extern Control input;
extern Sprites sprites[MAX_SPRITES];

and finally
Structs.h:

#include "Defs.h"

typedef struct Game
{
    SDL_Surface * screen;
}Game;

typedef struct Sprites
{
	SDL_Surface * image;
}Sprites;

typedef struct Entity
{
    int x, y;
	SDL_Surface * sprite;
}Entity;

typedef struct Control
{
    int up, down, left, right;
}Control;

That's all of my code.. And I use Dev C++ 4.9.9.2 as a compiler.. :D
Please help me!

Recommended Answers

All 3 Replies

In draw.h, you should have
extern Game game;

In game.cpp, have
Game game;

Umm.. dude.. I don't have a game.cpp class? And if you meant Main.cpp, Game game; is not defined in there? So could you please name a class that I have? ;) But otherwise thanks for your help :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.