Advertisement

sdl help

Started by October 28, 2004 09:49 AM
15 comments, last by cplus programmer 19 years, 10 months ago

#include <stdio.h>
#include <stdlib.h>

#include <SDL/SDL.h>

SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *image;

int xpos=100,ypos=300;

void init(SDL_Surface *screen)
{
    SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO);
    screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
    back = SDL_LoadBMP("bg.bmp");
    image = SDL_LoadBMP("img.bmp");
    atexit(SDL_Quit);
}

void drawimg(SDL_Surface *screen, int x, int y, SDL_Surface *blitto)
{
    Uint8* keys;
    keys = SDL_GetKeyState(NULL);
    if ( keys[SDLK_UP] ) { y -= 1; }
    if ( keys[SDLK_DOWN] ) { y += 1; }
    if ( keys[SDLK_LEFT] ) { x -= 1; }
    if ( keys[SDLK_RIGHT] ) { x += 1; }


    
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(screen, NULL, blitto, &dest);
}


int drawIMG(SDL_Surface *img, int x, int y, int x2, int y2, int w, int h)
{
    SDL_Rect dest;
    dest.x=x;
    dest.y=y;
    SDL_Rect dest2;
    dest2.x=x2;
    dest2.y=y2;
    dest.w=w;
    dest2.h=h;
    SDL_BlitSurface(img,&dest,screen,&dest2);
}    

int drawit()
{ 
    drawimg(image,0,0,screen);
    drawIMG(back,xpos,ypos,xpos-2,ypos-2,132,132);
    SDL_Flip(screen);
}

int main(int argc, char *argv[])
{
    Uint8 *keys;
  init(screen);
  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; }
      }
    }
    
        keys = SDL_GetKeyState(NULL);
    if ( keys[SDLK_UP] ) { ypos -= 1; }
    if ( keys[SDLK_DOWN] ) { ypos += 1; }
    if ( keys[SDLK_LEFT] ) { xpos -= 1; }
    if ( keys[SDLK_RIGHT] ) { xpos += 1; }
    done = 0;
    drawit();
  }
  return 0;
}    

the screen closes instantly, anyone know why?
Try checking the return of your inits and such.

if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0){    cout << SDL_GetError();}
Things i felt yesterday, don''t matter any more.
Advertisement
the init went fine, it still flashes on, displays nothing then goes. the images did load, I put a check on that.
stderr says

Fatal signal: Segmentation Fault (SDL Parachute Deployed)
run the debugger or place some output points in the code to find out where the last line of execution is before the crash.

Drew Sikora
Executive Producer
GameDev.net

Ahhh...the joys of debugging. Anyway, I'm going to step through this and try and help out, I had a problem like this a little while ago, and I probably commented on it in one of my sources.

Hope I can help.
Things change.
Advertisement
It looks like something is messing up right around your drawit() function...commenting out SDL_Flip(screen); has a very interesting effect.

For the lazy, the effect is that it doesn't crash, but instead draws a blank screen. I'd say something's wrong with your drawing code...what, are you doublebuffering?
Things change.
the code is a clone of cone3d's tutorial, it says to so SDL_Flip(screen); so that is what I did
I've pointed this out before, and no one's contradicted me - yet. I also haven't tested it myself, but why does everyone always leave the w and h members of SDL_Rect uninitialized?
SDL_Rect dest;dest.x = x;dest.y = y;SDL_BlitSurface(screen, NULL, blitto, &dest);

I assume the assumption is that dest.w and dest.h will equal the width and height of the surface, but in reality they're prob initialized to some garbage that just happens to be bigger than the width and height of the surface. Thus the blit works - but at what cost? Drakkcon is doing the same thing and is also complaining about crashing.

Drew Sikora
Executive Producer
GameDev.net

You would know right away if SDL supported hardware surface stretch blits...
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One

This topic is closed to new replies.

Advertisement