Jump to content
  • Advertisement
Sign in to follow this  
monp

SDL Basic help

This topic is 4001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am following the tutorials of lazyfoo.net, and i cant get this to compile.

//The headers
#include "SDL/SDL.h"
#include <string>

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image( std::string filename ) 
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;
    
    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    
    //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );
    
    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //Free the old image
        SDL_FreeSurface( loadedImage );
    }
    
    //Return the optimized image
    return optimizedImage;
}

void apply_surface( 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;
    
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

int main( int argc, char* args[] )
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;    
    }
    
    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return 1;    
    }
    
    //Set the window caption
    SDL_WM_SetCaption( "Hello World", NULL );
    
    //Load the images
    message = load_image( "hello_world.bmp" );
    background = load_image( "background.bmp" );
    
    //Apply the background to the screen
    apply_surface( 0, 0, background, screen );
    
    //Apply the message to the screen
    apply_surface( 180, 140, message, screen );
    
    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;    
    }
    
    //Wait 2 seconds
    SDL_Delay( 2000 );
    
    //Free the surfaces
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );
    
    //Quit SDL
    SDL_Quit();
    
    return 0;    
}

im getting these errors. C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x2b): undefined reference to `SDL_RWFromFile' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x3b): undefined reference to `SDL_LoadBMP_RW' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x4f): undefined reference to `SDL_DisplayFormat' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x5d): undefined reference to `SDL_FreeSurface' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x97): undefined reference to `SDL_UpperBlit' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0xe5): undefined reference to `SDL_Init' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x121): undefined reference to `SDL_SetVideoMode' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x156): undefined reference to `SDL_WM_SetCaption' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x34b): undefined reference to `SDL_Flip' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x36c): undefined reference to `SDL_Delay' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x379): undefined reference to `SDL_FreeSurface' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x386): undefined reference to `SDL_FreeSurface' C:\WINDOWS\Desktop\New Folder (2)\lesson02\lesson02.o:lesson02.cpp:(.text+0x38b): undefined reference to `SDL_Quit' C:\PROGRAM FILES\CODEBLOCKS\lib/libmingw32.a(main.o):main.c:(.text+0x106): undefined reference to `WinMain@16'

Share this post


Link to post
Share on other sites
Advertisement
I linked them with my compiler but i get this error now.

libSDL.la: file not recognized: File format not recognized

and if i remove the link to libSDL.la i get this error again

C:\PROGRAM FILES\CODEBLOCKS\lib/libmingw32.a(main.o):main.c:(.text+0x106): undefined reference to `WinMain@16'

Share this post


Link to post
Share on other sites
Linking SDL with MinGW should be:

-lmingw32 -lSDLmain -lSDL

If you are using any other SDL extension libraries, link them after the above.

Share this post


Link to post
Share on other sites
-lmingw32 -lSDLmain -lSDL

i put that under other linker options in code::blocks but i still get the file format not recognized for libSDL.la

Share this post


Link to post
Share on other sites
With Code::Blocks, you only need to specify -lmingw32 on "Other linker options". You then must point to these files on "Link libraries": libSDLMain.a, libSDL.dll.a, SDL_image.lib, SDL_ttf.lib, and whatever else you are using.

Share this post


Link to post
Share on other sites
Yes, this is definitly a linker problem -lmingw32 -lSDLmain -lSDL is the correct command line. Maybe also -mwindows if you want to get rid of the DOS box.
And also note that the official way to include SDL related headers is #include "SDL.h".

Share this post


Link to post
Share on other sites
Ok i added the link files but now i am getting these errors.

undefined reference to SDL_strlcpy, SDL_GetError, SDL_SetModuleHandle, SDL_strlcat.

all these errors accur in the SDL_win32_main.c

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!