Join 107,666 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,060 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
//Free the surfaces SDL_FreeSurface( message ); SDL_FreeSurface( background );
//Quit SDL SDL_Quit();
return 0; }
I just need explanation on this part :
cpp
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; }
there's explanation in Lazy Foo's website what this code does, but I don't understand what do this part has to do with other part of the code. is it a function? if it is, I don't see the main function call it.
can someone explain it to me? any help greatly appreciated
This post has been edited by red_4900: 27 Jul, 2008 - 10:27 PM
Yes it is a function basically what the SDL_Surface* portion fo the code does is say what is returned by the function (a pointer to the image/surface that SDL can use).
The function is called SDL_Surface *load_image but the * should be on the other side of the space to make it easier to read (as a pointer).
Basically (as far as I can tell) what the function does it loads an image and then optimizes it for SDL_Surface and returns the optomized version of the image so you can use it easily.
REALIZE - I may be wrong with this as I am in the process of teaching myself C++, but I think I have it right...
thanks for the explanation. the only thing I do not understand is why do we have that function? I don't see the main function calling the SDL_surface function. I need explanation on relation between the function and the other parts of the code.
with this piece of code you are setting how to load images in your code, so this load image function takes a string parameter, which will be your bitmap filename. You are optimizing SDL's bitmap loading function by first creating a place to load the bitmap and setting it to NULL (probably for error checking). Then creating a place to store the loaded image ready to be drawn to the screen
CODE
//Temporary storage for the image that's loaded SDL_Surface* loadedImage = NULL; //Image to be used SDL_Surface* optomizedImage = NULL;
you then use SDL's load bitmap function to load a bitmap into the storage for your temporary image, this takes the string "filename" that you pass into your load_image function
CODE
//Load the image loadedImage = SDL_LoadBMP( filename.c_str() );
then check if the bmp was loaded ok and if it was load it into the surface your going to draw to the screen (optomised image) then free the temporary surfaces memory allocation using SDL's free surface function and return the optomised image to be drawn
CODE
//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; }
it goes through all the steps that you wrote out in your function and returns an optomized image, then you go on to have your apply surface function which takes as parameters an x and y position and 2 pointers to SDLSurfaces one which is the image you want to display and one the place you want to display it, which is your screen. Think of surfaces as just layers for images to be drawn to.
CODE
//here you load the images using your load_image function, message = load_image( "hello_world.bmp" ); background = load_image( "background.bmp" );
//Apply the background to the screen , using your apply surface function which take an x and y position and a pointer to one of your optomized surfaces and your pointer to your screen SDL surface apply_surface( 0, 0, background, screen );
//Apply the message to the screen apply_surface( 180, 140, message, screen );
hope that helps
This post has been edited by stayscrisp: 28 Jul, 2008 - 04:57 PM
thanks a lot for the explanation. now that I know what happens behind the code, I could start writing the code. and here's what happens when I try to re-write the code :
SDL_Surface *load_image(string filename){ //create 2 surfaces, initialize both to NULL SDL_Surface *loadedImage = NULL; SDL_Surface *optImage = NULL;
//load image to surface loadedImage = SDL_LoadBMP(filename.c_str());
//convert image for it to be of the same format as the screen if(loadedImage!=NULL){ optImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface(loadedImage); } return optImage; //return optimized image }
this tutorial you are following is a very horrible C style way of programming, although i guess it doesnt matter since it is just a hello world program. and also just for checking really, why did you choose to go for declaring your functions and then defining them after your main loop when the code you had working clearly did not do this?
for a start i wouldn't declare your functions like this especially as it is just a small program. (some people may disagree) just place the full functions before your main loop.
I think the only problem is that your function declaration is not the same as your definition, which it always needs to be
see that your pointer is in a different place, i dont think it matters which side you have this but your declaration has to be the same as your definition.
The only problem I see is that you try to call SDL_INIT_EVERYTHING as a function. It's actually just used to pass to another function. Try something like this:
cpp
SDL_Init (SDL_INIT_EVERYTHING);
Here is the code that compiles perfectly on my machine:
SDL_Surface *load_image(string filename){ //create 2 surfaces, initialize both to NULL SDL_Surface *loadedImage = NULL; SDL_Surface *optImage = NULL;
//load image to surface loadedImage = SDL_LoadBMP(filename.c_str());
//convert image for it to be of the same format as the screen if(loadedImage!=NULL){ optImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface(loadedImage); } return optImage; //return optimized image }
Untitled1.o(.text+0xf):Untitled1.cpp: undefined reference to `__gxx_personality_sj0' Untitled1.o(.text+0xa2):Untitled1.cpp: undefined reference to `std::allocator<char>::allocator()' Untitled1.o(.text+0xc3):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)' Untitled1.o(.text+0xf8):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' Untitled1.o(.text+0x118):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' Untitled1.o(.text+0x131):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()' Untitled1.o(.text+0x15d):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()' Untitled1.o(.text+0x168):Untitled1.cpp: undefined reference to `std::allocator<char>::allocator()' Untitled1.o(.text+0x189):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
Untitled1.o(.text+0x1f6):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' Untitled1.o(.text+0x216):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' Untitled1.o(.text+0x22f):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()' Untitled1.o(.text+0x25b):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()' Untitled1.o(.text+0x317):Untitled1.cpp: undefined reference to `std::string::c_str() const' collect2: ld returned 1 exit status
mingw32-make: *** [Project1.exe] Error 1
Execution terminated
and I'm close to giving up.
edit : I deleted all files and tried making a new project, used the same code..and it works? what the heck. oh, thanks staycrisp and gabehabe
edit edit : I just found out that the actual problem lies with the makefile generated by the IDE. for anyone else having the same problem, you might find this link useful.
This post has been edited by red_4900: 2 Aug, 2008 - 11:32 PM
I might play around with SDL sometime myself if I can figure out how to make it a windowed mode sort of thing. If the above code does windowed mode by default and you specify full screen somewhere later than I know where to go from there.