Sign in to follow this  

What's wrong with this SDL?

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

Hi guys, I'm new here and a novice in Game programming. But I have this code from Cone3D that just won't work. I compiles allright but when I run it, it crashes. I think the problem is that my images isn't being fetched allright. The images are in the same folder as the source and binary
#include <stdio.h>
#include <stdlib.h>

#include <SDL/SDL.h>

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

int xpos=0,ypos=0;

int InitImages()
{
    back = SDL_LoadBMP("bg.bmp");
    image = SDL_LoadBMP("image.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 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;
  dest2.w = w;
  dest2.h = h;
  SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
     DrawIMG(back, 0, 0);
}
void DrawScene()
{
     DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
     DrawIMG(image, xpos, ypos);
     
     SDL_Flip(screen);
}

int main(int argc, char *argv[])
{
    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_HWSURFACE|SDL_DOUBLEBUF);
  if ( screen == NULL )
  {
    printf("Unable to set 640x480 video: %s\n", SDL_GetError());
    exit(1);
  }
  InitImages();
  DrawBG();
  
  int done=0;
  while(done == 0)
  {
             SDL_Event 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; }
DrawScene();

  return 0;
}

Share this post


Link to post
Share on other sites
Not sure but maybe this part:

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}


There is no width and height given to the dest rectangle?

Edit: I maybe wrong though, as I remember reading something about passing parametr 2 as NULL to blit the entire surface... dunno if that applies to the dest rectangle as well.

Share this post


Link to post
Share on other sites
Just checking, you do have bg.bmp and image.bmp in the same folder as your .exe (or if you're running from your IDE, the same folder as your project file)?

Edit: Why oh why don't I read? However, if you are running it from your IDE, the parentheses apply, still.

Share this post


Link to post
Share on other sites
Your code didn't work for me - your event loop is bork bork bork. You need to call either SDL_PollEvent or ... SDL_PeekEvent (I think that's the one) to fill the SDL_Event structure with data before you use it.

There were a couple other changes I made, can't remember them all; but this worked for me -

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

#include <SDL/SDL.h>

#pragma comment ( lib, "sdl.lib" )
#pragma comment ( lib, "sdlmain.lib" )

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

int xpos=0,ypos=0;

int InitImages()
{
back = SDL_LoadBMP("forest.bmp");
image = SDL_LoadBMP("forest.bmp");
return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = img->w;
dest.h = img->h;
SDL_BlitSurface(img, NULL, screen, &dest);
}
void 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;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
DrawIMG(back, 0, 0);
}
void DrawScene()
{
DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
DrawIMG(image, xpos, ypos);

SDL_UpdateRect( screen, 0, 0, 0 ,0 );
SDL_Flip(screen);
}

int main(int argc, char *argv[])
{
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_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
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;
}

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; }
DrawScene();
}

return 0;
}


Share this post


Link to post
Share on other sites
I'm sorry to say that it didn't work either.
In debugging I get: "An access violation (Segmentation Fault) raised in your program"

I don't understand where I get my buffer overflow.

EDIT: Nevermind, I got it to work :D thanks guys!

Share this post


Link to post
Share on other sites

This topic is 4104 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this