Skip to main content
gamedev.net
🔒 Locked

sdl help

Started by dan1088352 Oct 28, 2004 at 9:49 AM 15 replies 3.9k views
Original Post
dan1088352
dan1088352

#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?
Muppi
Muppi
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.
dan1088352
dan1088352
the init went fine, it still flashes on, displays nothing then goes. the images did load, I put a check on that.
dan1088352
dan1088352
stderr says

Fatal signal: Segmentation Fault (SDL Parachute Deployed)
Gaiiden
Gaiiden
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
Boku San
Boku San
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.
Boku San
Boku San
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.
Gaiiden
Gaiiden
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
cplus programmer
cplus programmer
#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(){    back = SDL_LoadBMP("back.bmp");    if(back == NULL)    {        printf("cant load back");    }        image = SDL_LoadBMP("ball.bmp");    if(image == NULL)    {        printf("cant load the image");    }        atexit(SDL_Quit);}void drawimg(SDL_Surface *screen, int x, int y,int w, int h, SDL_Surface *blitto){      SDL_Rect dest;  dest.x = x;  dest.y = y;  dest.h=h;  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(){    xpos=xpos+1;    drawimg(image, 50, 50,xpos,xpos,back);    drawimg(back, 0, 0,800,600,screen);    SDL_Flip(screen);    SDL_Flip(back);}int main(int argc, char *argv[]){    if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0){    printf("unable to init", SDL_GetError());}    screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);    if(screen == NULL)    {        printf("cant set video mode");    }        Uint8 *keys;  init();  drawimg(back, 0, 0,800,600,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 image will drag and I have to go to the task manager to quit it, know why?
there are 10 types of people in this world, those who can do bianary and those who cant.
Gaiiden
Gaiiden
You should only be calling SDL_Flip for the main surface. The program's not quitting because you're setting done back to 0 every game loop (look above the drawit() call)
Quote:
Original post by PnP Bios
You would know right away if SDL supported hardware surface stretch blits...

Whazzat?
Drew Sikora
Executive Producer
GameDev.net
PnP Bios
PnP Bios
well, if the w and h were garbage, and you were using them as real parameters, you would get some funky looking blits, and you would be able to visibly see that your parameters were messed up.

I think...
cplus programmer
cplus programmer
I did, there wher some changes.
there are 10 types of people in this world, those who can do bianary and those who cant.
cplus programmer
cplus programmer
this is the code right now

#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>SDL_Surface *screen;SDL_Surface *back;SDL_Surface *image;int xpos,ypos;void init(){    back = SDL_LoadBMP("back1.bmp");    if(back == NULL)    {        printf("cant load back");    }        image = SDL_LoadBMP("ball.bmp");    if(image == NULL)    {        printf("cant load the image");    }        atexit(SDL_Quit);}void drawimg(SDL_Surface *screen, int x, int y,int w, int h, SDL_Surface *blitto){   SDL_Rect dest;  dest.x = x;  dest.y = y;  dest.h=h;  dest.y=y;  SDL_BlitSurface(screen, NULL, blitto, &dest);}int hitwall(int xpos ,int ypos ,int wall1 , int wall2){    if(xpos>wall1)    {        xpos=0;    }    if(xpos<0)    {        xpos=800;    }    if(ypos>wall2)    {        ypos = 0;    }    if(ypos<0)    {        ypos = 600;    }}                    int drawIMG(SDL_Surface *img, int x, int y,int w, int h,int x2,int y2){    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(){    hitwall(xpos,ypos,800,600);    drawIMG(back, xpos-2, ypos-2, 52, 52, xpos-2, ypos-2);    drawimg(image, xpos, ypos,500,50,screen);    SDL_Flip(screen);}int main(int argc, char *argv[]){    if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0){    printf("unable to init", SDL_GetError());}    screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);    if(screen == NULL)    {        printf("cant set video mode");    }        Uint8 *keys;  init();  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 -= 10; }    if ( keys[SDLK_DOWN] ) { ypos += 10; }    if ( keys[SDLK_LEFT] ) { xpos -= 10; }    if ( keys[SDLK_RIGHT] ) { xpos += 10; }    drawit();  }  return 0;}  


it just drags now
there are 10 types of people in this world, those who can do bianary and those who cant.
Gaiiden
Gaiiden
use this to clear the screen before you blit and flip
SDL_FillRect(screen, 0, 0);
Drew Sikora
Executive Producer
GameDev.net
cplus programmer
cplus programmer
thank you very much
there are 10 types of people in this world, those who can do bianary and those who cant.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.