First SDL Try a Failure

Started by
3 comments, last by Rob Loach 17 years, 12 months ago

#include "SDL/SDL.h"
#include <string>
#include "SDL/SDL_image.h"

const int SCREEN_WIDTH = 700;
const int SCREEN_HEIGHT = 500;
const int SCREEN_BPP = 32;

SDL_Surface *guy = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image(std::string filename)
{   
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    
    loadedImage = IMG_Load(filename.c_str());
    
    if(loadedImage != NULL)
    {
        optimizedImage = SDL_Display Format(loadedImage);
        SDL_FreeSurface(loadedImage);
    }
    if(optimizedImage != NULL)
    {
        Uint32 colorkey = SDL_MapRGB(optimizdImage->format, 0, 0xFF, 0xFF);
        SDL_SetColorkey(optimizedImage, SDL_RLEACCEL | SDL_SRCOLORKEY, colorkey);
    }
    
    return optimizedImage;
}

    void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
    {
        SDL_Rect offset;
        offset.x = x;
        offset.y = y;
        SDL_BlitSurface(source, NULL, destination, &offset);
    }
    
int main(int argc, char** args)
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 1;
    }
    
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SCREEN_SWSURFACE);
    
    SDL_WM_SetCaption("Look At Me", NULL);
    
    guy = load_image("SuperChar2.png");
    background = load_image("background.png");
    
    apply_surface(0,0,background,screen);
    apply_surface(100,120,guy,screen);
    
    if(SDL_Flip(screen)==-1)
    {
        return 1;
    }
    
    SDL_Delay(5000);
    SDL_FreeSurface(guy);
    SDL_FreeSurface(background);
    
    SDL_Quit();
}

That was the code to my first ever try at an SDL Program. I followed Lazy Foo's tutuorial and put in all the files in the way that the site showed you should. I get these errors- Compiler: Default compiler Building Makefile: "C:\Documents and Settings\dhill\Desktop\Cody Linville Productions\All Homebrew Programs and Source\Cody's SDL\Makefile.win" Executing make... make.exe -f "C:\Documents and Settings\dhill\Desktop\Cody Linville Productions\All Homebrew Programs and Source\Cody's SDL\Makefile.win" all g++.exe -c Untitled1.cpp -o Untitled1.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" Untitled1.cpp: In function `SDL_Surface* load_image(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': Untitled1.cpp:22: `SDL_Display' undeclared (first use this function) Untitled1.cpp:22: (Each undeclared identifier is reported only once for each function it appears in.) Untitled1.cpp:22: parse error before `(' token Untitled1.cpp:27: `optimizdImage' undeclared (first use this function) Untitled1.cpp:28: `SDL_SRCOLORKEY' undeclared (first use this function) Untitled1.cpp:28: `SDL_SetColorkey' undeclared (first use this function) Untitled1.cpp: In function `int SDL_main(int, char**)': Untitled1.cpp:49: `SCREEN_SWSURFACE' undeclared (first use this function) make.exe: *** [Untitled1.o] Error 1 Execution terminated Can Anyone help me to figure out what is wrong with my program? Any thoughts will be greatly appreciated.
Advertisement
optimizedImage = SDL_Display Format(loadedImage);

should be
optimizedImage = SDL_DisplayFormat(loadedImage);


You've also got case issues with your SDL_SetColorKey calls and some other typos. Be sure to type the function names and other items exactly as they appear in the tutorial.
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SCREEN_SWSURFACE);


I fixed most of the typos, but there was still one in this line. I don't know what it is can you help?
SCREEN_SWSURFACE should be SDL_SWSURFACE.
Both in game development and teaching yourself, you're bound to run into many "failures". This, however, is the best way to learn. Don't give up and keep on trying to find out what's wrong.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement