You are not logged in.

#1 2009-07-30 13:21:33

luskjh
Member
Registered: 2009-03-24
Posts: 19

SDL Global Problem (C)

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 smile.

-Josh


Calm down, it is only ones and zeroes.
       -flyingfsck ( on /. )

Offline

#2 2009-07-30 13:53:43

krolden
Member
Registered: 2009-06-30
Posts: 12

Re: SDL Global Problem (C)

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

#3 2009-07-30 14:07:42

luskjh
Member
Registered: 2009-03-24
Posts: 19

Re: SDL Global Problem (C)

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

#4 2009-07-30 14:25:07

mikkop92
Member
Registered: 2009-06-18
Posts: 4

Re: SDL Global Problem (C)

#ifdef ENGINE_H

Shouldn't that be:

#ifndef ENGINE_H

Offline

#5 2009-07-30 19:37:47

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: SDL Global Problem (C)

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

#6 2009-07-31 02:19:14

e_tank
Member
Registered: 2006-12-21
Posts: 80

Re: SDL Global Problem (C)

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

Board footer

Powered by FluxBB