Jump to content
  • Advertisement
Sign in to follow this  
petelta

problem with SDL tutorials

This topic is 4594 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 just started trying to learn some SDL. I am using the lazyfooproductions.com tutorials at the moment. THe first 4 or 5 went along greatly and was easy to pick up. when I got to detecting if a key was pressed i had a problem. I type the code in there and when i compile and run, it just flashes on hte screen for a second or less. I already have a X out event being used. and since thats not working I tried to put a 5 second delay but thats not working either. Im using dev C++ if that helps. here is an example of one of the lessons that isn't working to well: /*This source code copyrighted by Lazy Foo' Productions (2006) and may not be redestributed without written permission.*/ //The headers #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include <string> //Screen attributes const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //The frame rate const int FRAMES_PER_SECOND = 20; //The surfaces SDL_Surface *dot = NULL; SDL_Surface *background = NULL; SDL_Surface *screen = NULL; //The event structure SDL_Event event; //The timer class Timer { private: //The clock time when the timer started int startTicks; //The ticks stored when the timer was paused int pausedTicks; //The timer status bool paused; bool started; public: //Initializes variables Timer(); //The various clock actions void start(); void stop(); void pause(); void unpause(); //Get the number of ticks since the timer started //or gets the number of ticks when the timer was paused int get_ticks(); //Checks the status of the timer bool is_started(); bool is_paused(); }; SDL_Surface *load_image( std::string filename ) { //The image that's loaded SDL_Surface* loadedImage = NULL; //The optimized surface that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = SDL_LoadBMP( filename.c_str() ); //If the image loaded if( loadedImage != NULL ) { //Create an optimized surface optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old surface SDL_FreeSurface( loadedImage ); //If the surface was optimized if( optimizedImage != NULL ) { //Color key surface SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) ); } } //Return the optimized surface return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) { //Holds offsets SDL_Rect offset; //Get offsets offset.x = x; offset.y = y; //Blit SDL_BlitSurface( source, clip, destination, &offset ); } bool init() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } //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 false; } //Set the window caption SDL_WM_SetCaption( "Background Test", NULL ); //If everything initialized fine return true; } bool load_files() { //Load the dot image dot = load_image( "dot.bmp" ); //Load the background background = load_image( "background.bmp" ); //If there was a problem in loading the dot if( dot == NULL ) { return false; } //If there was a problem in loading the background if( background == NULL ) { return false; } //If everything loaded fine return true; } void clean_up() { //Free the surfaces SDL_FreeSurface( dot ); SDL_FreeSurface( background ); //Quit SDL SDL_Quit(); } Timer::Timer() { //Initialize the variables startTicks = 0; pausedTicks = 0; paused = false; started = false; } void Timer::start() { //Start the timer started = true; //Unpause the timer paused = false; //Get the current clock time startTicks = SDL_GetTicks(); } void Timer::stop() { //Stop the timer started = false; //Unpause the timer paused = false; } void Timer::pause() { //If the timer is running and isn't already paused if( ( started == true ) && ( paused == false ) ) { //Pause the timer paused = true; //Calculate the paused ticks pausedTicks = SDL_GetTicks() - startTicks; } } void Timer::unpause() { //If the timer is paused if( paused == true ) { //Unpause the timer paused = false; //Reset the starting ticks startTicks = SDL_GetTicks() - pausedTicks; //Reset the paused ticks pausedTicks = 0; } } int Timer::get_ticks() { //If the timer is running if( started == true ) { //If the timer is paused if( paused == true ) { //Return the number of ticks when the the timer was paused return pausedTicks; } else { //Return the current time minus the start time return SDL_GetTicks() - startTicks; } } //If the timer isn't running return 0; } bool Timer::is_started() { return started; } bool Timer::is_paused() { return paused; } int main( int argc, char* args[] ) { //Quit flag bool quit = false; //The offsets of the background int bgX = 0, bgY = 0; //The frame rate regulator Timer fps; //Initialize if( init() == false ) { return 1; } //Load the files if( load_files() == false ) { return 1; } //While the user hasn't quit while( quit == false ) { //Start the frame timer fps.start(); //While there's events to handle while( SDL_PollEvent( &event ) ) { //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } //Scroll background bgX -= 2; //If the background has gone too far if( bgX <= -background->w ) { //Reset the offset bgX = 0; } //Show the background apply_surface( bgX, bgY, background, screen ); apply_surface( bgX + background->w, bgY, background, screen ); //Show the dot apply_surface( 310, 230, dot, screen ); //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } //Cap the frame rate while( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { //wait } } //Clean up clean_up(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Quote:
//Load the background
background = load_image( "background.bmp" );


Try changing that to
Quote:
background = load_image( "bg.png" );

or if you renamed the file to background
Quote:
background = load_image( "background.png" );


Not sure if it will work or not, but it's worth a try.

Share this post


Link to post
Share on other sites
If you're going to change it to png, you most first replace the LoadBMP with this line:
loadedImage = IMG_Load(filename.c_str());

Share this post


Link to post
Share on other sites
i had the IMG_load command in at first and changed it to SDL_LoadBMP not knowing that it would do what it does. now when i changed it back it says that I cant run the program because the tiff.dll wasn't found? which ive never seen that before and can't find tiff.dll anywhere.

Share this post


Link to post
Share on other sites
If you are using devC++ then make sure you have downloaded and installed the SDL_image dev-pak (been a while, don't remember if the DLL's come with that but I imagine they do). You can also find all of the necessary DLL's from the SDL_image web page. They are in either of the Win32 Binary downloads. You can copy them into your Windows/System32 directory or (more useful since some people don't have them) keep a copy of them where your application can see them so that when you bundle your game for others you can give them the DLLs they will need to play your game.

[edit] Upon further inspection, the tiff.dll does not come in those zip archives. Fire up your favorite search engine and download a copy of tiff.dll from somewhere.

Share this post


Link to post
Share on other sites
ok, i downloaded the tiff.dll file and got that in order and changed the SDL_LoadBMP back to IMG_load and it still only quickly flashes up on the screen. i can't get it to stay up to see if it works.

Share this post


Link to post
Share on other sites
If you are using Visual C++ 6.0, go to Project -> Settings, then the "C/C++" tab, for "Category" pick "Code Generation", under "Use run-time library" choose "Multithreaded DLL"

Share this post


Link to post
Share on other sites
If you are using Visual C++ 6.0, go to Project -> Settings, then the "C/C++" tab, for "Category" pick "Code Generation", under "Use run-time library" choose "Multithreaded DLL"

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!