Sign in to follow this  
Rowen

Black Window?

Recommended Posts

Once again I have encountered a problem... The code below should output an oval background and a black ball. You should be able to move the ball around with the arrow keys. But instead it outputs a black window... (This is the 2nd lesson at cone3d)
#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("oval.bmp");
  image = SDL_LoadBMP("ballmask.bmp");
  return 0;
}

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect,SDL_Surface *dst, SDL_Rect *dstrect);

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;

    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;
}

Thanks again for any help!!! edit: added [source] tags -SiCrane

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Ekim_Gram
Why do you have a prototype for SDL_BlitSurface in your code? Get rid of that and try again.
You can have multiple prototypes. So long as there's just one matching definition, it's okay.


That's just it though, he never defined it. I don't see why he should have a prototype for a funtion that he's going to use but no actual definition.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
How does SDL_BlitSurface treat SDL_rects with only two fields set (ie, with the other two fields as arbitrary garbage), in your opinion?

I've pointed that out in other threads myself. I don't like the way these Cone3D tuts are coded at all.

Anyways I do see one problem at least:

DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);

Take a look in the function here:

SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;

x2 and y2 are the source x,y values. You should not be changing them like you do the destination x,y values. Therefore change your code to:

DrawIMG(back, xpos-2, ypos-2, 132, 132, 0, 0);

And change dest2 to src so that it's more readable

Share this post


Link to post
Share on other sites

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