Jump to content
  • Advertisement
Sign in to follow this  
red_4900

SDL basic image blitting

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

//The headers
#include "SDL/SDL.h"
#include <string.h>
//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 think some of you might be very familiar with this code. It was taken directly from Lazy Foo website. I got this compile error: Compiler: Default compiler Building Makefile: "C:\Dev-Cpp\Programming\SDL\Blitting Image\Makefile.win" Executing make... mingw32-make -f "C:\Dev-Cpp\Programming\SDL\Blitting Image\Makefile.win" all gcc.exe -c main.c -o main.o -I"C:/Dev-Cpp/include" -Dmain=SDL_main main.c:17: error: syntax error before ':' token main.c: In function `load_image': main.c:26: error: `filename' undeclared (first use in this function) main.c:26: error: (Each undeclared identifier is reported only once main.c:26: error: for each function it appears in.) mingw32-make: *** [main.o] Error 1 Execution terminated Can someone tell me what did I do wrong? I know the variable "filename" is undeclared, but have little idea how to correct it. And I have no idea why do we need this SDL_Surface *load_image() function in the code. Any help would be greatly appreciated :) edit : how do I put the code in code box? [Edited by - red_4900 on August 2, 2008 2:02:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
I think your problem is you need to take off the .h in #include <string.h>

string.h is a deprecated header and std strings are I believe are only in string (with out the h).


So, change this
#include <string.h>
to this
#include <string>

Share this post


Link to post
Share on other sites
std::string is a 'C++' variable type, yet you use appear to be coding in 'C'.

Solution: If using 'C++', change #include <string.h> to #include <string>. The 'string.h' is the 'C' version, I believe, while 'string' is the 'C++' edition of the standard library.

If you are using 'C', you can't use std::string. Instead, change std::string to char*, and change this line...
loadedImage = SDL_LoadBMP( filename.c_str() );
to this line:
loadedImage = SDL_LoadBMP( filename );

Furthermore, make sure you link properly the the SDL library. If you need help doing this, just ask, but first figure out whether you are coding in 'C' or 'C++' and remedy your code accordingly. (SDL works in both languages)

Share this post


Link to post
Share on other sites
okey.that works.now for a question that went a bit out of topic actually.I can compile and run the program just fine from my IDE(Dev-Cpp).But I can open the program outside the IDE,it will gives this error : "This application has failed to start because SDL.dll was not found.Re-installing the application may fix the problem.".how do I fix this?

Share this post


Link to post
Share on other sites
I tried to re-write the same code.little has been changed from the last time.

#include"SDL/SDL.h"
#include&lt;string&gt;
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);
}


and I get this 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

can someone tell me what have I done wrong?

[Edited by - red_4900 on August 2, 2008 1:08:44 PM]

Share this post


Link to post
Share on other sites
The very first line of function 'main', you have this:
SDL_INIT_EVERYTHING();

SDL_INIT_EVERYTHING is not a function. It's a const, enum, or #define. (You'll learn about these later, but they aren't functions)

What you want, is to replace it with: SDL_Init(SDL_INIT_EVERYTHING);

Other than that, it compiles fine on my computer. (Make sure to link to SDL!)

Also, you can post code in [ source ] and [ /source ] boxes, to have it show up neater.

Share this post


Link to post
Share on other sites
#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(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);

SDL_Delay(5000);

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

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



I'm still getting this 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+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

I don't think the problem lies with SDL.what the heck is all the error about "std::".

Share this post


Link to post
Share on other sites
I don't know what's going wrong, but when I googled your error, other posts with the same problem came up. (Example)

The solution seems to be to add this: -lstdc++ to your linker. (Read the linked thread to see why) Hopefully that solves it? If not, you'll need someone who actually understands these things. [smile]

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!