SDL doesn't want to work...

Started by
4 comments, last by Peter Conn 17 years, 7 months ago
Hiya I copied the tutorial at http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/tut3 and found that for some reason when I try to run it all that happens is that the whole screen goes black (like when you press Alt + F5) and then comes out of it with the command prompt showing but no screen, also I tried to output some text to the command prompt via "std::cout" at the start of "int main()" and it wasn't displayed. Here is my code: Main.cpp

#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include "Sprite.h"
#include <iostream>


SDL_Surface *screen, *back;
CSpriteBase gaurdBase;
CSprite gaurd;

SDL_Surface * ImageLoad(char *file){
  SDL_Surface *temp1, *temp2;
  temp1 = SDL_LoadBMP(file);
  temp2 = SDL_DisplayFormat(temp1);
  SDL_FreeSurface(temp1);
  return temp2;
}
int InitImages(){
  back = ImageLoad("data/bg.bmp");
  return 0;
}
void DrawIMG(SDL_Surface *img, int x, int y){
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);
}
void DrawBG(){
  DrawIMG(back, 0, 0);
}
void DrawScene(){
  gaurd.clearBG();
  gaurd.updateBG();
  gaurd.draw();
  SDL_Flip(screen);
}

int main(int argc, char *argv[])
{
    std::cout << "Hello";
  Uint8* keys;

  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    printf("Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  atexit(SDL_Quit);

  screen=SDL_SetVideoMode(640,480,32,
                      SDL_SWSURFACE|SDL_FULLSCREEN|SDL_HWPALETTE);
  if ( screen == NULL )
  {
    printf("Unable to set 640x480 video: %s\n", SDL_GetError());
    exit(1);
  }


  gaurdBase.init("Images/Person");

  gaurd.init(&gaurdBase,screen);
  gaurd.set(350,300);
  gaurd.Speed() = 1.5;


  SDL_ShowCursor(0);

  InitImages();
  DrawBG();
  int done=0;

  while(done == 0)
  {
    SDL_Event event;

    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT )  {  done = 1;  }

      if ( event.type == SDL_KEYDOWN )
      {
        if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
        //if ( event.key.keysym.sym ==SDLK_SPACE){sun.toggleAnim();}
      }
    }
    keys = SDL_GetKeyState(NULL);
    if ( keys[SDLK_UP] ) { gaurd.Y() += -1; }
    if ( keys[SDLK_DOWN] ) { gaurd.Y() += 1; }
    if ( keys[SDLK_LEFT] ) { gaurd.X() += -1; }
    if ( keys[SDLK_RIGHT] ) { gaurd.X() +=1 ; }
    DrawScene();
  }
  return 0;
}
/*
int main ( int argc, char** argv )
{
    // initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    // make sure SDL cleans up before exit
    atexit(SDL_Quit);

    // create a new window
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    // load an image
    SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
    if (!bmp)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    // center the bitmap on screen
    SDL_Rect dstrect;
    dstrect.x = (screen->w - bmp->w) / 2;
    dstrect.y = (screen->h - bmp->h) / 2;

    // program main loop
    bool done = false;
    while (!done)
    {
        // message processing loop
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = true;
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = true;
                    break;
                }
            } // end switch
        } // end of message processing

        // DRAWING STARTS HERE

        // clear screen
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

        // draw bitmap
        SDL_BlitSurface(bmp, 0, screen, &dstrect);

        // DRAWING ENDS HERE

        // finally, update the screen :)
        SDL_Flip(screen);
    } // end main loop

    // free loaded bitmap
    SDL_FreeSurface(bmp);

    // all is well ;)
    printf("Exited cleanly\n");
    return 0;
}*/



Sprite.h

#ifndef SPRITE_H
#define SPRITE_H

struct CSpriteFrame
{
  SDL_Surface *image;
  int pause;
};

class CSpriteBase
{
  public:
  int init(char *dir);
  ~CSpriteBase();

  CSpriteFrame *mAnim;
  int mBuilt, mNumframes, mW, mH;
};

class CSprite
{
public:
  int init(CSpriteBase *base, SDL_Surface *screen);
  void draw();
  void clearBG();
  void updateBG();
  int &Frame() { return mFrame; }
  float &Speed() { return mSpeed; }
  void toggleAnim() { mAnimating = !mAnimating; }
  void startAnim() { mAnimating = 1; }
  void stopAnim() { mAnimating = 0; }
  void rewind() { mFrame = 0; }
  int &X() { return mX; }
  int &Y() { return mY; }
  void set(int xx, int yy) { mX=xx; mY=yy; }
private:
  int mFrame;
  int mX, mY, mOldX, mOldY;
  int mAnimating;
  int mDrawn;
  float mSpeed;
  long mLastupdate;
  CSpriteBase *mSpriteBase;
  SDL_Surface *mBackreplacement;
  SDL_Surface *mScreen;
};



#endif



Sprite.cpp

#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include <String>
#include "Sprite.h"

int CSpriteBase::init(char *dir)
{
  char buffer[255];
  char filename[255];
  char name[255];
  int pause=0, r=0, g=0, b=0;
  FILE *fp;
  sprintf(filename, "%s/info", dir);

  if((fp=fopen(filename, "r")) == NULL)
  {
    printf("ERROR opening file %s\n\n", filename);
    return -1;
  }
  fgets(buffer, 255, fp);
  sscanf(buffer, "FILES: %d", &mNumframes);
  mAnim = new CSpriteFrame[mNumframes];
  mBuilt = 1;
  int count = 0;
  while(!feof(fp) && count<mNumframes)
  {
    fgets(buffer, 255, fp);
    if(buffer[0] != '#' && buffer[0] != '\r' && buffer[0] != '\0'
                     && buffer[0] != '\n' && strlen(buffer) != 0)
    {
        sscanf(buffer, "%s %d %d %d %d", name, &pause, &r, &g, &b);
        sprintf(filename, "%s/%s", dir, name);
        SDL_Surface *temp;
        if((temp = SDL_LoadBMP(filename)) == NULL) return -1;
        if(r >= 0) SDL_SetColorKey(temp, SDL_SRCCOLORKEY,
                    SDL_MapRGB(temp->format, r, g, b));
        mAnim[count].image = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
        mAnim[count].pause = pause;
      if(!mW) mW = mAnim[count].image->w;
      if(!mH) mH = mAnim[count].image->h;

      count++;
    }
  }
  fclose(fp);
  return 0;
}

CSpriteBase::~CSpriteBase()
{
    delete []mAnim;
}

int CSprite::init(CSpriteBase *base, SDL_Surface *screen)
{
  mSpriteBase = base;
  if(mSpriteBase->mBuilt)
  {
    if(mSpriteBase->mNumframes>1) mAnimating=1;
    mBackreplacement =
               SDL_DisplayFormat(mSpriteBase->mAnim[0].image);
  }
  mScreen = screen;
}

void CSprite::clearBG()
{
  if(mDrawn==1)
  {
    SDL_Rect dest;
    dest.x = mOldX;
    dest.y = mOldY;
    dest.w = mSpriteBase->mW;
    dest.h = mSpriteBase->mH;
    SDL_BlitSurface(mBackreplacement, NULL, mScreen, &dest);
  }
}

void CSprite::updateBG()
{
  SDL_Rect srcrect;
  srcrect.w = mSpriteBase->mW;
  srcrect.h = mSpriteBase->mH;
  srcrect.x = mX;
  srcrect.y = mY;
  mOldX=mX;mOldY=mY;
  SDL_BlitSurface(mScreen, &srcrect, mBackreplacement, NULL);
}

void CSprite::draw()
{
  if(mAnimating == 1)
  {
    if(mLastupdate+mSpriteBase->mAnim[mFrame].pause*mSpeed<
                                                SDL_GetTicks())
    {
      mFrame++;
      if(mFrame>mSpriteBase->mNumframes-1) mFrame=0;
      mLastupdate = SDL_GetTicks();
    }
  }
  if(mDrawn==0) mDrawn=1;

  SDL_Rect dest;
  dest.x = mX; dest.y = mY;
  SDL_BlitSurface(mSpriteBase->mAnim[mFrame].image, NULL, mScreen,
                                                           &dest);
}



Any help would be much appreciated. On a different note I found a useful trick on www.functionx.com - returning a reference, therefore you don't need get and set functions but still can (if required) to make sure the variable isn't set outside a range. I use this for my Sprite class for Speed, Frame, X, Y, are there any disadvantages to this approach? [Edited by - Peter Conn on September 15, 2006 3:06:35 PM]
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Advertisement
Try this code to see if it works; it's about the most basic SDL application you can make that uses the video subsystem.

#include <SDL/sdl.h>bool quit = false;int main(int argc, char * argv[]){    if (SDL_Init(SDL_INIT_VIDEO) == -1) //Init video subsystem    {        //An error occurred.  Log it and abort.        fprintf(stderr, "Error initializing SDL video subsystem: %s", SDL_GetError());        return 1;    }    if (SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF) == NULL)    {        //Error.  Log and abort        fprintf(stderr, "Error setting up video buffer: %s", SDL_GetError());        return 1;    }    while (!quit) //Loop until keypress    {        SDL_Event event;        while (SDL_PollEvent(&event))        {            if (event.type == SDL_KEYDOWN || event.type == SDL_QUIT) quit = true;        }    }    return 0;}


Let us know what this program does. If it crashes or quits without showing a window, post any contents in the stderr.txt file.
my siteGenius is 1% inspiration and 99% perspiration
Well I'm using CodeBlocks and have run the program that you get when you make a new SDL application with CodeBlocks.
I've also made a program which moves a picture around the background so I think its something to do with the sprites.

The stderr.txt contains this one line:
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

Does that help?
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Oh hell! I just realised - I had misspelt the background file name!
I'm sorry for that, is there thing in the furture I can do so that next time an image file goes missing the image just isn't displayed, without taking down the whole application.?
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Maybe make a replacer image that would be displayed in it's place if it's not find. Sort of how a web-browser works by displaying a red X in place of the image that doesn't work (just do a lot of error checking to display the red x, if the image can't be found.
"I'd rather know one thing, no matter how ordinary, than discourse endlessly on great issues." -- Galileo
Ok thanks
Whether you think you can or think you can’t, you’re probably right – Henry Ford

This topic is closed to new replies.

Advertisement