Still a black window...

Started by
4 comments, last by Rowen 19 years, 4 months ago
Hi again... I tried some of the things u said, but it didn't work. I might have misunderstood something... But here's the changed code: - Deleted SDL_BlitSurface - Changed dest2 to src CODE:

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

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 src;
  src.x = x2;
  src.y = y2;
  src.w = w;
  src.h = h;
  SDL_BlitSurface(img, &src, screen, &dest);
}

void DrawBG()
{
  DrawIMG(back, 0, 0);
}

void DrawScene()
{
  DrawIMG(back, xpos-2, ypos-2, 132, 132, 0,0);
  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;
}

PS: How do create the code-box??? [Answer: Use the [source][/source] tags - Oluseyi.] Thanks!!! :)
Advertisement
You get the source box by wrapping it in [source][/source]. For other tags, see the FAQ.
"It doesn't work" doesn't give us much information. Do you mean that it compiles and executes, but you continue to see a black window?

Where are the bitmap files relative to the executable in the directory tree? If they are not in the same directory, then SDL_LoadBMP can not find them and is returning, IIRC, null. That would certainly result in black/blank screens.
Sorry for not giving you any concrete advice, but at the initial stages having a "black window" can be a very frustrating thing. It's particularily hard to find the problem because so many things can go wrong. Just keep working, trying different things, changing things around. Eventually you'll get there and it'll be very satisfactory. Don't give up!
I compiled it and ran it, and it runs fine you must not have the images in the directory. . .
Thank You very much!!!!!!!!!!!!

Putting the images into the directory made it work.

And I will in the future be a bit more precise... ;)

Thanks again and keep up the good work!

This topic is closed to new replies.

Advertisement