Welcome to Dream.In.Code
Getting Help is Easy!

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!



SDL : Blitting image

 
Reply to this topicStart new topic

SDL : Blitting image

red_4900
post 27 Jul, 2008 - 10:27 PM
Post #1


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 521


My Contributions


here's the full code :

cpp
//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;
}


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

This post has been edited by red_4900: 27 Jul, 2008 - 10:27 PM
User is online!Profile CardPM

Go to the top of the page


BetaWar
post 27 Jul, 2008 - 10:46 PM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,120



Thanked 38 times

Dream Kudos: 725
My Contributions


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...
User is offlineProfile CardPM

Go to the top of the page

red_4900
post 28 Jul, 2008 - 07:19 AM
Post #3


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 521


My Contributions


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.
User is online!Profile CardPM

Go to the top of the page

stayscrisp
post 28 Jul, 2008 - 04:55 PM
Post #4


D.I.C Head

**
Joined: 14 Feb, 2008
Posts: 193



Thanked 4 times
My Contributions


load_image is just a function which you do call in your main function

here:

CODE

//Load the images  
message = load_image( "hello_world.bmp" );  
background = load_image( "background.bmp" );  


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;  
}  


now when you call this function here:
CODE

//Load the images  
message = load_image( "hello_world.bmp" );  
background = load_image( "background.bmp" );  


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

This post has been edited by stayscrisp: 28 Jul, 2008 - 04:57 PM
User is offlineProfile CardPM

Go to the top of the page

red_4900
post 1 Aug, 2008 - 01:38 AM
Post #5


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 521


My Contributions


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 :

cpp
#include"SDL/SDL.h"
#include<string>
using namespace std;
//function prototype
SDL_Surface* load_image(string filename);
void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface* destination);

int main(int argc,char* args[]){
SDL_INIT_EVERYTHING(); //initialize SDL subsystem

SDL_Surface* back=NULL;
SDL_Surface* front=NULL;
SDL_Surface* screen=NULL;

//set windows
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
SDL_WM_SetCaption("Testing",NULL);

//load image
front = load_image("hello_world.bmp");
back = load_image("background.bmp");

//apply image to windows's screen
apply_surface(0,0,back,screen);
apply_surface(150,100,front,screen);

//flip the screen from inside memory to the screen
SDL_Flip(screen);

//free memory
SDL_FreeSurface(front);
SDL_FreeSurface(back);

SDL_Delay(5000);

//quit SDL
SDL_Quit();
return 0;
}

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
}

void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface* destination){
//create a rectangular offset
SDL_Rect offset;

//offset coordinate
offset.x=x;
offset.y=y;

//blit to surface
SDL_BlitSurface(source,NULL,destination,&offset);
}


here's the error :
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Programming\SDL\Blitting image 2\Makefile.win"
Executing make...
mingw32-make -f "C:\Dev-Cpp\Programming\SDL\Blitting image 2\Makefile.win" all
gcc.exe -c Untitled1.cpp -o Untitled1.o -I"C:/Dev-Cpp/include"

gcc.exe Untitled1.o -o "Project1.exe" -L"C:/Dev-Cpp/lib" -mwindows -lmingw32 -lSDLmain -lSDL

Untitled1.o(.text+0xf):Untitled1.cpp: undefined reference to `__gxx_personality_sj0'
Untitled1.o(.text+0x96):Untitled1.cpp: undefined reference to `std::allocator<char>::allocator()'
Untitled1.o(.text+0xb7):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+0xec):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
Untitled1.o(.text+0x10c):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
Untitled1.o(.text+0x125):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()'
Untitled1.o(.text+0x151):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()'
Untitled1.o(.text+0x15c):Untitled1.cpp: undefined reference to `std::allocator<char>::allocator()'
Untitled1.o(.text+0x17d):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+0x1ea):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
Untitled1.o(.text+0x20a):Untitled1.cpp: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
Untitled1.o(.text+0x223):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()'
Untitled1.o(.text+0x24f):Untitled1.cpp: undefined reference to `std::allocator<char>::~allocator()'
Untitled1.o(.text+0x30b):Untitled1.cpp: undefined reference to `std::string::c_str() const'
collect2: ld returned 1 exit status

mingw32-make: *** [Project1.exe] Error 1

Execution terminated

I don't understand all that.....somebody help me~ T_T
User is online!Profile CardPM

Go to the top of the page

stayscrisp
post 1 Aug, 2008 - 06:06 PM
Post #6


D.I.C Head

**
Joined: 14 Feb, 2008
Posts: 193



Thanked 4 times
My Contributions



hey

first of all i would do this

CODE

SDL_Surface* back=NULL;  
SDL_Surface* front=NULL;  
SDL_Surface* screen=NULL;  


outside your main function, just better that way

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
CODE

SDL_Surface* load_image(string filename);


CODE

SDL_Surface *load_image(string filename) {
//blah blah
}


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.

hope that works smile.gif
User is offlineProfile CardPM

Go to the top of the page

red_4900
post 2 Aug, 2008 - 11:26 AM
Post #7


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 521


My Contributions


QUOTE(stayscrisp @ 1 Aug, 2008 - 06:06 PM) *

hope that works smile.gif

no, it didn't. still the same error.
User is online!Profile CardPM

Go to the top of the page

gabehabe
post 2 Aug, 2008 - 03:02 PM
Post #8


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,300



Thanked 44 times

Dream Kudos: 1450

Expert In: C, C++

My Contributions


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:
cpp
#include"SDL/SDL.h"
#include<string>
using namespace std;
//function prototype
SDL_Surface* load_image(string filename);
void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface* destination);

int main(int argc,char* argv[]){
SDL_Init (SDL_INIT_EVERYTHING); //initialize SDL subsystem

SDL_Surface* back=NULL;
SDL_Surface* front=NULL;
SDL_Surface* screen=NULL;

//set windows
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
SDL_WM_SetCaption("Testing",NULL);

//load image
front = load_image("hello_world.bmp");
back = load_image("background.bmp");

//apply image to windows's screen
apply_surface(0,0,back,screen);
apply_surface(150,100,front,screen);

//flip the screen from inside memory to the screen
SDL_Flip(screen);

//free memory
SDL_FreeSurface(front);
SDL_FreeSurface(back);

SDL_Delay(5000);

//quit SDL
SDL_Quit();
return 0;
}

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
}

void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface* destination){
//create a rectangular offset
SDL_Rect offset;

//offset coordinate
offset.x=x;
offset.y=y;

//blit to surface
SDL_BlitSurface(source,NULL,destination,&offset);
}


Hope this helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

red_4900
post 2 Aug, 2008 - 11:18 PM
Post #9


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 521


My Contributions


thanks for the help. but I'm still stuck at this error (despite copy n paste from gabehabe's code):

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Programming\SDL\Blitting image 2\Makefile.win"
Executing make...
mingw32-make -f "C:\Dev-Cpp\Programming\SDL\Blitting image 2\Makefile.win" all
gcc.exe -c Untitled1.cpp -o Untitled1.o -I"C:/Dev-Cpp/include"

gcc.exe Untitled1.o -o "Project1.exe" -L"C:/Dev-Cpp/lib" -mwindows -lmingw32 -lSDLmain -lSDL

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. sad.gif

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

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. wink2.gif

This post has been edited by red_4900: 2 Aug, 2008 - 11:32 PM
User is online!Profile CardPM

Go to the top of the page

WolfCoder
post 3 Aug, 2008 - 09:48 PM
Post #10


Gyuu~

Group Icon
Joined: 5 May, 2005
Posts: 2,953



Thanked 1 times

Dream Kudos: 1300
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 17 Aug, 2008 - 07:28 AM
Post #11


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,300



Thanked 44 times

Dream Kudos: 1450

Expert In: C, C++

My Contributions


It's being made windowed by default, by using SDL_SWSURFACE

If you want it full screen, then it's SDL_FULLSCREEN

smile.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/29/08 10:49PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month