SDL problem

Started by
6 comments, last by Gaiiden 19 years, 5 months ago
I have a problem when using SDL. Whenever I load a bitmap onto the screen (other than in the left corner), the picture will disapear. For instance, when I load a bitmap into the left corner of the screen, it comes up fine, but when I load it up anywhere else on the screen, it disapears. HELP ME!
Advertisement
Hm...
Fraid you are going to have to post some code. Or atleast be a bit more specific.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
So when you display the image at (0,0) it shows up fine but otherwise it doesn't? Did you see if it shows up at (0,y) for any y, or (x,0) for any x?

Are you updating the screen differently that results in the background getting drawn over the image immediately?
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
Yep, sounds like a screen update problem. We'll have to see some code for a proper diagnosis.

Drew Sikora
Executive Producer
GameDev.net

SDL_UpdateRect doesn't work if the source image doesn't fit on the screen entirely. Setting the clipping rect might work.
Sorrry I couldn't put the code up earlier. I have dial up! Anyway, here is the source:
#include <stdio.h>#include <stdlib.h>#include <SDL.h>SDL_Surface *background;SDL_Surface *cardA;SDL_Surface *card2;SDL_Surface *card3;SDL_Surface *card4;SDL_Surface *card5;SDL_Surface *card6;SDL_Surface *card7;SDL_Surface *card8;SDL_Surface *card9;SDL_Surface *cardJ;SDL_Surface *cardQ;SDL_Surface *cardK;SDL_Surface *screen;int xpos=0,ypos=0;int cxpos=0, cypos=0;int InitImages(){  background = SDL_LoadBMP("background.bmp");  cardA = SDL_LoadBMP ("cardA.bmp");  card2 = SDL_LoadBMP ("card2.bmp");  card4 = SDL_LoadBMP ("card4.bmp");  card5 = SDL_LoadBMP ("card5.bmp");  card6 = SDL_LoadBMP ("card6.bmp");  card7 = SDL_LoadBMP ("card7.bmp");  card8 = SDL_LoadBMP ("card8.bmp");  card9 = SDL_LoadBMP ("card9.bmp");  cardJ = SDL_LoadBMP ("cardJ.bmp");  cardQ = SDL_LoadBMP ("cardQ.bmp");  cardK = SDL_LoadBMP ("cardK.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(background, 0, 0);}void DrawScene(){  DrawIMG(background, xpos-2, ypos-2, 800,600, xpos-2, ypos-2);  DrawIMG(card2,cxpos,cypos,30,40,cxpos,cypos);}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(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);  if ( screen == NULL )  {    printf("Unable to set 800x600 video: %s\n", SDL_GetError());    exit(1);  }InitImages();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; }      }    }		DrawBG();keys = SDL_GetKeyState(NULL);if (keys[SDLK_DOWN]){	cxpos++;}DrawScene();}  return 0;}


This source is supposed to display a card on the screen and when you hit the down key, the xpos will add one more. After you press down 6 times, half of the card disapears (it's not supposed to disappear). HELP!
it would probably be easier to have one surface / image for the cards. If the widths are the same for each card (usualy are) you can just change the x position when you blit onto the screen.

say if you put your card pictures next to each other like this in the bmp file 1,2,3,4,5,6,7,8,9,10,J,Q,K and you could just define what each card's position is, like #define ONE 0 , #define KING 12 and the card width eg #define CARDWIDTH 50. Then when you blit the king you would set the source.x to KING * CARDWIDTH.

Hope that makes sence (although it doesnt solve your original problem)
Couple of things.

  • Why did you define a protoype of SDL_BlitSurface?

  • You're not calling SDL_FLip anywhere to update the screen or flip the back buffer. Since you're in a window, you probably have a software surface rather than a hardware double-buffered surface, so that's ok since you're just drawing to the same surface, and I guess your background image is erasing what was drawn before. But still...

  • What's the purpose of your background image? Is it an actual image? Cause if not you'd be better off using SDL_FillRect(screen, 0, 0) to clear the screen

  • Why are you drawing the background twice? (once with DrawBG and again in DrawScene)

  • Since you have the message loop set up anyways, there's no reason to poll for keystrokes when you can just add it to the loop like you did the Esc key. Polling isn't as efficient as messaging.


Those are all my issues. Now, onto the problem at hand. I think it has to do with this:
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;

That's not a destination rect, that's a source rect. The destination rect is this:
  SDL_Rect dest2;  dest2.x = x2;  dest2.y = y2;  dest2.w = w;  dest2.h = h;

so that is correct. However you're changing the source x/y of the card image rect, which means you're blitting less and less of the card (vertically) with each keystroke, as well as moving the card downwards on the screen. So stop changing the source rect and you should be fine.

Drew Sikora
Executive Producer
GameDev.net

This topic is closed to new replies.

Advertisement