Sign in to follow this  

This sucks...SDL

This topic is 4237 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

#include <sdl.h> #include <string> //The attributes of the window 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 rect to hold the offsets
    SDL_Rect Offset;
    
    //Give the offsets to the rect
    Offset.x = x;
    Offset.y = y;
    
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &Offset );
}

int main( int argc, char* args[] )
{
    //Initialize all SDL sub systems
    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 in error in setting up the screen
    if( screen == NULL )
    {
        return 1;    
    }
    
    //Set the window caption
    SDL_WM_SetCaption( "C:\Dev-CPP\lessson02\Hello World", NULL );
    
    //Load the images
    message = load_image( "C:\Dev-CPP\lessson02\hello_world.bmp" );
    background = load_image( "C:\Dev-CPP\lessson02\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 4 seconds
    SDL_Delay( 4000 );
    
    //Free the images
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );
    
    //Quit SDL
    SDL_Quit();
    
    return 0;    
}
I have all the linkers in there. I set it up just how lazyfooproductions tutorials said, but it always says "C:\Dev-Cpp\Makefile.win [Build Error] [2sdl.exe] Error 1" If someone will please help, it will be great.

Share this post


Link to post
Share on other sites
No, it didn't work. I am using DEV C++ unless that could be useful for anyones input. By the way, thanks for trying. Here are the linkers I used:
SDL-1.2.9/lib/libSDL.a
SDL-1.2.9/lib/libSDL.dll.a
SDL-1.2.9/lib/libSDLmain.a
"-lmingw32 -lSDLmain -lSDL"
SDL-1.2.9/include/begin_code.h
SDL-1.2.9/include/close_code.h
SDL-1.2.9/include/SDL.h
SDL-1.2.9/include/SDL_active.h
SDL-1.2.9/include/SDL_audio.h
SDL-1.2.9/include/SDL_byteorder.h
SDL-1.2.9/include/SDL_cdrom.h
SDL-1.2.9/include/SDL_copying.h
SDL-1.2.9/include/SDL_cpuinfo.h
SDL-1.2.9/include/SDL_endian.h
SDL-1.2.9/include/SDL_error.h
SDL-1.2.9/include/SDL_events.h
SDL-1.2.9/include/SDL_getenv.h
SDL-1.2.9/include/SDL_joystick.h
SDL-1.2.9/include/SDL_keyboard.h
SDL-1.2.9/include/SDL_keysym.h
SDL-1.2.9/include/SDL_loadso.h
SDL-1.2.9/include/SDL_main.h
SDL-1.2.9/include/SDL_mouse.h
SDL-1.2.9/include/SDL_mutex.h
SDL-1.2.9/include/SDL_name.h
SDL-1.2.9/include/SDL_opengl.h
SDL-1.2.9/include/SDL_quit.h
SDL-1.2.9/include/SDL_rwops.h
SDL-1.2.9/include/SDL_syswm.h
SDL-1.2.9/include/SDL_thread.h
SDL-1.2.9/include/SDL_timer.h
SDL-1.2.9/include/SDL_types.h
SDL-1.2.9/include/SDL_version.h
SDL-1.2.9/include/SDL_video.h

Thanks.

Share this post


Link to post
Share on other sites
I tried compiling it. It worked once I had changed the linker mentioned above and double back-slashed all your load_images, like below.

message = load_image( "C:\\Dev-CPP\\lessson02\\hello_world.bmp" );

See if it works on yours.

[Edit:]I used this linker: -lmingw32 -lSDLmain -lSDL

Share this post


Link to post
Share on other sites
As well as setting up SDL development, you need to have the SDL.dll file which is a seperate download on the same webpage as where you downloaded the development package. If you did download this then make sure the dll is actually in your debug folder or where you are developing the program.

Just mentioning this in case this is your first SDL program

Share this post


Link to post
Share on other sites
Have you tried using this in an SDL project? Its one of the options when you make a new project in Dev C++(assuming that you installed SDL via a DevPak).

Share this post


Link to post
Share on other sites
Try the following:

1) For the header, use: (reason: [url]http://www.libsdl.org/faq.php?action=listentries&category=2#19[/url] )
#include "SDL.h"



2) Make sure sure in dev-c++ when you make a new project, choose console.

3) Link with:
-lmingw32 -lSDLmain -lSDL -mwindows




4) Did you download the header and lib files for SDL? Make sure you place them in dev-c++'s include and lib directories, else you'll have to add include directories in options.

5) You can place the SDL.dll in the programs working directory, or c:/windows/system32 Even if you don't have the dll, I think it should compile, but give an error when you try to run it.

6) As mentioned, if you use a path either escape the slashes, or use foreward slashes.
background = load_image( "C:\Dev-CPP\lessson02\background.bmp" );
//becomes:
background = load_image( "C:\\Dev-CPP\\lessson02\\background.bmp" );
//or: (Pretty sure I've used foreward slashes in windows
background = load_image( "C:/Dev-CPP/lessson02/background.bmp" );



If dev-c++ is installed correctly, and you link with the above, this code should compile. (I just tested it, basically a sdl-hi world)
#include <iostream>
//#include <cstdlib> //if you need to pause
#include "SDL.h"

using namespace std;
int screen_w = 800;
int screen_h = 600;
int screen_bpp = 32; //32 works -- 16?

//push escape to quit.
//compiling: linked with: -lmingw32 -lSDLmain -lSDL -mwindows

bool bDone = false;

//proto's
void game_loop(void);

//surfaces
SDL_Surface *screen;

void game_loop(void) {
//render blank screen

//init before loop
SDL_Event event;
Uint8 *keys;

cout << "Starting render loop" << endl;

//loop
do {

//events
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
//nice quit
bDone = true;
}
}

//input
keys = SDL_GetKeyState(0);
if(keys[SDLK_ESCAPE] == SDL_PRESSED) {
bDone = true;
}

//render
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,0,128,128));
SDL_UpdateRect(screen,0,0,0,0);
}while(!bDone);

cout << "Exited render loop" << endl;
}

int main(int argc, char *argv[]) {

//init before loop

//init SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cerr << "Could not initialize SDL: " << SDL_GetError() << endl;
SDL_Quit();
return -1;
}

//set video mode
screen = SDL_SetVideoMode(screen_w, screen_h, screen_bpp, SDL_SWSURFACE);
if(screen == NULL) {
cerr << "Failed setting mode: " << screen_w << "x" << screen_h << "x"
<< screen_bpp << endl << SDL_GetError() << endl;
SDL_Quit();
return -1;
}

game_loop();

//end SDL

//free surfaces, but not screen

cout << "Quitting SDL..." << endl;
SDL_Quit();
cout << "Done." << endl;
return 0;
}


If that still doesn't work, post all errors.

Share this post


Link to post
Share on other sites
hey,
sorry your having such a difficult time with this.
i know it was a pain to get SDL up and running on my Dev C++ when i first started. I got this error a lot and it was always due to libraries.

i would check the order in which you have linked your libraries.
i read once that you need to have libSDLmain.lib first, foremost, and before all the others. if i wasnt at work, id check myself to see if this is valid, but unfortunatly all i can do is blindly poke at the problem.

good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by Feidias
The proper way to include is <SDL/SDL.h> not "SDL.h" or anything else. Just wanted to clear this one.


So do I [smile]

The correct way, according to the SDL webpage, is actaully "SDL.h".

Then, (for dev-cpp), go into tools->compiler options->directories->c (and c++) includes and add your SDL include directory to it, so on mine in both c and c++ includes it reads "C:\path\to\Dev-Cpp\include\SDL"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Feidias
The proper way to include is <SDL/SDL.h> not "SDL.h" or anything else. Just wanted to clear this one.


Incorrect. just wanted to make it clear :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Feidias
The proper way to include is <SDL/SDL.h> not "SDL.h" or anything else. Just wanted to clear this one.


No, it's <#SDL.//h-#>

Seriously, it completely depends on what your include directories are set as. That's why everyone's is different... there is no "correct" way.

Share this post


Link to post
Share on other sites
Your include directory should be set to "include" directory of the compiler. Then when you write for example include <stdio.h> it's found from the include directory. Using "myheader.h" suggests that the file is located in the project directory, where your source files are.

Share this post


Link to post
Share on other sites
Quote:
Original post by Feidias
Your include directory should be set to "include" directory of the compiler. Then when you write for example include <stdio.h> it's found from the include directory. Using "myheader.h" suggests that the file is located in the project directory, where your source files are.


Yes, but you can set multiple include directories, and some people (myself included) like to keep the external libraries like SDL seperate from the internal libraries of the compiler (especially if you use multiple compilers on your machine).

Anyway, this is off-topic, so I'll stop now :)

Share this post


Link to post
Share on other sites

This topic is 4237 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this