• Advertisement
Sign in to follow this  

Still a black window...

This topic is 4823 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 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!!! :)

Share this post


Link to post
Share on other sites
Advertisement
You get the source box by wrapping it in [source][/source]. For other tags, see the FAQ.

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement