You are not logged in.
Pages: 1
Hi,
I have a header/source file I include in my SDL programs. I recently added the global "SDL_Surface *screen" to the header file as shown below:
#ifdef ENGINE_H
#define ENGINE_H
extern SDL_Surface *screen;
extern SDL_Surface *load_image( const char *file );
extern void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination );
extern void INIT_subs();
extern void INIT_win( int width, int height, int bpp, const char *caption, int time );
#endif
...and then defined it in my main program (example.c):
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "Engine.h"
SDL_Surface* screen;
SDL_Surface* background = NULL;
int main( int argc, char** argv ) {
INIT_subs();
INIT_win( 640, 480, 32, "Example Window", 2 );
background = load_image( "example.png" );
apply_surface( 0, 0, background, screen );
if( SDL_Flip(screen) == -1 ) {
return 0;
}
SDL_FreeSurface( background );
SDL_Quit();
return 0;
}
But when I try to use it in my source file (Engine.c), I get errors( 'screen undeclared (first use in this function )' ):
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdio.h>
#include "Engine.h"
SDL_Surface* load_image( const char *file ) {
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( file );
if( loadedImage != NULL ) {
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination) {
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );
}
void INIT_subs() {
if( SDL_Init( SDL_INIT_EVERYTHING < 0 ) ) {
printf( "Error initializing subsystems: %s.\n", SDL_GetError() );
}
}
void INIT_win( int width, int height, int bpp, const char *caption, int time ) {
screen = SDL_SetVideoMode( width, height, bpp, SDL_SWSURFACE );
SDL_WM_SetCaption( caption, NULL );
if( screen == NULL ) {
printf( "Screen not initialized: %s.\n", SDL_GetError() );
}
time *= 1000;
SDL_Delay( time );
}
I thought you could only define it in one source file or you'll get errors ( information from here http://www.velocityreviews.com/forums/t … files.html in the third post )...or is it something else entirely?
Thanks .
-Josh
Calm down, it is only ones and zeroes.
-flyingfsck ( on /. )
Offline
when linking the object files, try putting the file that defines the variable before the part that defines it.
gcc -c main.c
gcc -c Engine.c
gcc -o foo main.o Engine.o
Offline
The "main.o" object file compiles fine, but when I try 'gcc -o Engine.c I get the same errors as before.
Calm down, it is only ones and zeroes.
-flyingfsck ( on /. )
Offline
#ifdef ENGINE_H
Shouldn't that be:
#ifndef ENGINE_H
Offline
If memory serves, you have to declare file-scope variables inside your functions before assigning to them:
void INIT_win( int width, int height, int bpp, const char *caption, int time ) {
extern SDL_Surface *screen;
screen = SDL_SetVideoMode( width, height, bpp, SDL_SWSURFACE );
I only gave this a cursory glance, so I may be a mile off.
Offline
mikkop92 is right, it should read in your header file
#ifndef ENGINE_H
#define ENGINE_H
/* rest of code here */
#endif
then you can compile using
gcc -o example Engine.c example.c `sdl-config --cflags --libs` -lSDL_image
at a glance everything else looks ok, although the externs before the functions in the header file aren't necessary as those are just function prototypes. anyway you're using externs properly here, which is you declare the global variables once in a source file and then have any files that need access to them include the header file with the extern declarations.
Offline
Pages: 1