So i read the lazyfoo's article on splitting source files.
I did everything right, but i have a problem with my functions.
When i compile my main.cpp file, i have tons of errors, all saying 2 things:
' warning: <myVarName> initialized and declared `extern'
Where myVarName is the name of the variable
It says that for all my vars
and:
main.cpp: undefined reference to <myFunctionName>
Where myFunctionName is the name of the function
It says that for all my functions
I must be doing something wrong, but what? This is my functions.h:
Code: Select all
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_ttf.h"
#include <string>
int play_sound(Mix_Chunk *var1,Mix_Music *var2);
void log(std::string error);
SDL_Surface *load_image( std::string filename);
void draw_image(int x,int y,SDL_Surface *source,SDL_Surface *destination);
void handleMenuInput();
void initButtons();
void clean_up();
bool initMusic();
bool init();
void showButtons();
#endif
Code: Select all
#include "functions.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h
#include "SDL/SDL_mixer.h"
#include <string>
#include "vars.h"
SDL_Surface *load_image( std::string filename)
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image using SDL_image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
//If the image was optimized just fine
if( optimizedImage != NULL )
{
if (filename != "graphics/space.png" && filename != "graphics/spaceship.png")
{
//Map the color key
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 255, 255 );
//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
}
}
//Return the optimized image
return optimizedImage;
}
void draw_image(int x,int y,SDL_Surface *source,SDL_Surface *destination)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,NULL,destination,&offset);
}
int play_sound(Mix_Chunk *var1,Mix_Music *var2)
{
//Play the scratch effect
if (var1 != NULL && var2 == NULL)
{
if( Mix_PlayChannel( -1, var1, 0 ) == -1 ) {
return 1;
}
}
if (var1 == NULL && var2 != NULL)
{
Mix_HaltMusic();
//Play the music
if( Mix_PlayMusic(var2, -1 ) == -1 ) {
return 1;
}
}
}
#undef showButtons
void showButtons()
{
play.show();
howto.show();
options.show();
credits.show();
ok.show();
exit1.show();
back.show();
}
void startGame()
{
mainMenu = false;
inGame = true;
player.ship();
}
void handleMenuInput()
{
//While there's an event to handle
while(SDL_PollEvent(&event))
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
if (currentButton>0 && currentButton<=7)
{
switch( event.key.keysym.sym )
{
case SDLK_UP:
currentButton--;
play_sound(peep.sfx,NULL);
break;
case SDLK_DOWN:
currentButton++;
play_sound(peep.sfx,NULL);
break;
case SDLK_RETURN:
if (currentButton == 5)
{
quit = true;
}
if (currentButton == 1)
{
startGame();
}
}
}
}
}
}
void initButtons()
{
log("init buttons");
play.id = 1;
howto.id = 2;
options.id = 3;
credits.id = 4;
exit1.id = 5;
ok.id = 6;
back.id = 7;
play.offset.x = 404;
play.offset.y = 100;
howto.offset.x = 404;
howto.offset.y = 200;
options.offset.x = 404;
options.offset.y = 300;
credits.offset.x = 404;
credits.offset.y = 400;
exit1.offset.x = 404;
exit1.offset.y = 500;
ok.offset.x = 404;
ok.offset.y = 600;
back.offset.x = 404;
back.offset.y = 700;
back.imageOver = load_image("graphics/back_mouseover.png");
if (back.imageOver == NULL)
{
log(IMG_GetError());
}
back.imageUp = load_image("graphics/back_up.png");
if (back.imageUp == NULL)
{
log(IMG_GetError());
}
credits.imageOver = load_image("graphics/credits_mouseover.png");
if (credits.imageOver == NULL)
{
log(IMG_GetError());
}
credits.imageUp = load_image("graphics/credits_up.png");
if (credits.imageUp == NULL)
{
log(IMG_GetError());
}
exit1.imageOver = load_image("graphics/exit_mouseover.png");
if (exit1.imageOver == NULL)
{
log(IMG_GetError());
}
exit1.imageUp = load_image("graphics/exit_up.png");
if (exit1.imageUp == NULL)
{
log(IMG_GetError());
}
howto.imageOver = load_image("graphics/howto_mouseover.png");
if (howto.imageOver == NULL)
{
log(IMG_GetError());
}
howto.imageUp = load_image("graphics/howto_up.png");
if (howto.imageUp == NULL)
{
log(IMG_GetError());
}
ok.imageOver = load_image("graphics/ok_button mouse_over.png");
if (ok.imageOver == NULL)
{
log(IMG_GetError());
}
ok.imageUp = load_image("graphics/ok_button_up.png");
if (ok.imageUp == NULL)
{
log(IMG_GetError());
}
play.imageOver = load_image("graphics/playgame_mouseover.png");
if (play.imageOver == NULL)
{
log(IMG_GetError());
}
play.imageUp = load_image("graphics/playgame_up.png");
if (play.imageUp == NULL)
{
log(IMG_GetError());
}
}
void clean_up()
{
//Free the sound effects
Mix_FreeChunk(peep.sfx);
//Quit SDL_mixer
Mix_CloseAudio();
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
bool initMusic()
{
menu.sound = Mix_LoadMUS("sounds/menu.wav");
if(menu.sound == NULL )
{
log(Mix_GetError());
return false;
}
game.sound = Mix_LoadMUS("sounds/game.wav");
if(game.sound == NULL )
{
log(Mix_GetError());
return false;
}
peep.sfx = Mix_LoadWAV("sounds/peep.wav");
return true;
}
bool init()
{
//Initialize all SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING)==-1) {
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
//Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
{
return false;
}
player.image = load_image("graphics/spaceship.png");
if (player.image == NULL)
{
log(IMG_GetError());
}
bg = load_image("graphics/space.png");
gameOverImage = load_image("graphics/gameover.png");
return true;
}
Thanks.