SDL: Why does this not work?

Started by
14 comments, last by The Rug 19 years, 2 months ago
Hello, I'm creating a simple tile engine in SDL, and right now, I just want to create a map, put a sprite on it and be able to move it. I got this code, and it compiles fine, but it just shows the map and not the sprite. Can someone help me figure this out?

#include <iostream>
#include <SDL/SDL.h>
using namespace std; //yes, I know this is evil.
SDL_Surface *screen;
SDL_Surface *grass;
SDL_Surface *water;
SDL_Surface *wall;
SDL_Surface *tree;
SDL_Surface *mySprite;
int sxpos = 0;
int sypos = 0;
int map[10][10] = {{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
                   {4, 1, 1, 1, 1, 1, 1, 1, 1, 4},
                   {4, 1, 2, 2, 1, 1, 2, 2, 1, 4},
                   {4, 1, 3, 3, 3, 3, 3, 3, 1, 4},
                   {4, 1, 2, 2, 1, 1, 2, 2, 1, 4},
                   {4, 1, 1, 1, 1, 1, 1, 1, 1, 4},
                   {4, 1, 3, 3, 3, 3, 3, 3, 1, 4},
                   {4, 1, 1, 1, 4, 4, 1, 1, 1, 4},
                   {4, 1, 1, 1, 1, 1, 1, 1, 1, 4},
                   {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}};
int xpos = 0;
int ypos = 0;
int InitImages() {
  grass = SDL_LoadBMP("grass.bmp");
  water = SDL_LoadBMP("water.bmp");
  wall = SDL_LoadBMP("wall.bmp");
  tree = SDL_LoadBMP("tree.bmp");
  mySprite = SDL_LoadBMP("mysprite.bmp");
  return 0;
}
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *destrect);
void drawIMG(SDL_Surface *img, int x, int y) {
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);
}
int drawMap() {
  xpos = 0;
  ypos = 0;
  for(int y=0; y<=9; ++y) {
    xpos = 0;
    for(int x=0; x<=9; ++x) {
      if(map[y][x] == 1) {
        drawIMG(grass, xpos, ypos);
        SDL_Flip(screen);
      }
      if(map[y][x] == 2) {
        drawIMG(water, xpos, ypos);
        SDL_Flip(screen);
      }
      if(map[y][x] == 3) {
        drawIMG(wall, xpos, ypos);
        SDL_Flip(screen);
      }
      if(map[y][x] == 4) {
        drawIMG(tree, xpos, ypos);
        SDL_Flip(screen);
      }
      xpos += 32;
      if(x == 9) {
        ypos += 32;
      }
    }
  }
  return 1;
}
void sprite() {
  drawIMG(mySprite, sxpos, sypos);
}
void drawScene() {
  drawMap();
  sprite();
  SDL_Flip(screen);
}
int main(int argc, char *argv[]) {
  Uint8* keys;
  if(SDL_Init(SDL_INIT_VIDEO) < 0) {
    cout<<"Unable to init SDL."<<endl;
    SDL_GetError();
  }
  atexit(SDL_Quit);
  screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  if(screen == NULL) {
    cout<<"Unable to set video mode."<<endl;
    SDL_GetError();
  }
  InitImages();
  drawMap();
  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]) {
      for(int sxu=0; sxu<=32; sxu++) {
        for(int syu=0; syu<=32; syu++) {
          sypos -= 1;
        }
      }
    }
    if(keys[SDLK_DOWN]) {
      for(int sxd=0; sxd<=32; sxd++) {
        for(int syd=0; syd<=32; syd++) {
          sypos +=1;
        }
      }
    }
    if(keys[SDLK_LEFT]) {
      for(int sxl=0; sxl<=32; sxl++) {
        for(int syl=0; syl<=32; syl++) {
          sxpos -= 1;
        }
      }
    }
    if(keys[SDLK_RIGHT]) {
      for(int sxr=0; sxr<=32; sxr++) {
        for(int syr=0; syr<=32; syr++) {
          sxpos += 1;
        }
      }
    }
    drawScene();
  }
  return 0;
}

Thanks.
Meta AdamOne of a million noob C++ programmers.
Advertisement
One thing that I see right away is that you do not need to call:
SDL_Flip(screen);


in the drawMap() at all. That should fix it.

- Drew
Made the sprite come up, but then I pressed right and it disappeared.
Meta AdamOne of a million noob C++ programmers.
if(keys[SDLK_UP]) {      for(int sxu=0; sxu<=32; sxu++) {        for(int syu=0; syu<=32; syu++) {          sypos -= 1;        }      }    }

Not sure what this is trying to accomplish. You want to decrease sypos by 1089?
sxu<=32 means 33 iterations, 33*33. And even then, why loop it, just decrease it by the final calculation.

As for why its not showing, my initial impression is that you don't have a src SDL_Rect in the drawing thing for the sprite, but I'm not sure.
william bubel
Quote:Original post by Meta Adam
Made the sprite come up, but then I pressed right and it disappeared.


Heed Inmate2993's advice on movement [wink].

[source lang = "cpp"]if(keys[SDLK_UP])     {          sypos -= 32;    }    if(keys[SDLK_DOWN]) {          sypos +=32;    }    if(keys[SDLK_LEFT])     {         sxpos -= 32;     }        if(keys[SDLK_RIGHT]) {          sxpos += 32;    }


- Drew
There, it works now, but the loops were for the sprite, if say down was pressed, it move 1 square, rather than 1 pixel, 1 square is 32 pixels wide and high, so I figured hey add it up in a for loop. Also, I want it to slide there with 1 press, not just appear there.
Meta AdamOne of a million noob C++ programmers.
Quote:Original post by Meta Adam
There, it works now, but the loops were for the sprite, if say down was pressed, it move 1 square, rather than 1 pixel, 1 square is 32 pixels wide and high, so I figured hey add it up in a for loop. Also, I want it to slide there with 1 press, not just appear there.


Ahh yes - that's a better idea than what I posted. Glad it works!
Well, it works but it doesnt slide, it appears, and it goes about 8 squares at a time lol.
Meta AdamOne of a million noob C++ programmers.
Ahh, I just changed it back to 1, said heck with it, lol, but the next question is, collision detection?
Meta AdamOne of a million noob C++ programmers.
Quote:Original post by Meta Adam
Ahh, I just changed it back to 1, said heck with it, lol, but the next question is, collision detection?


This tutorial from Cone3D should be what you need. Basically you compare "bounding boxes" of the object with each other and handle the even if they overlap. Go ahead and take a look at that - I know of a few more tutorials on collisions, but I can't find them at the moment.

- Drew

This topic is closed to new replies.

Advertisement