Black Window?

Started by
5 comments, last by Rowen 19 years, 4 months ago
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
Advertisement
Why do you have a prototype for SDL_BlitSurface in your code? Get rid of that and try again.
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.
How does SDL_BlitSurface treat SDL_rects with only two fields set (ie, with the other two fields as arbitrary garbage), in your opinion?
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.
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

Drew Sikora
Executive Producer
GameDev.net

Thanks!!! :)

I'll try to make it work...(hopefully)

This topic is closed to new replies.

Advertisement