Problem with code...help

Okay as far as I can tell this code should work, but
for some reason it doesnt. I can’t find anything wrong
with it but I can get it to work if I take it in
pieces. What am I doing that is wrong? Thank you for
helping.

Jared

– BEGIN SOURCE CODE –

#include <stdio.h>
#include <sdl.h>

SDL_Surface
*image,*surface,*pic,*background,*screen;
SDL_Event event;
Uint8 *buffer,*keys;
int x,y;
char *filename;
int width,height;
int fps,i,r,g,b;
SDL_Color *colors;
SDL_Rect dest;
int speed,quit;
int posy,posx;
void startUp ()
{
printf(“StartUp Begin\n”);

speed = 5;
fps = 50;

pic = loadPic(“spout.bmp”);
background = loadPic(“background.bmp”);

if (SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,“Problem:
%s\n”,SDL_GetError());
}
screen = SDL_SetVideoMode(640, 480, 0,
SDL_HWSURFACE|SDL_FULLSCREEN);
if ( screen == NULL ) {
fprintf(stderr, “Couldn’t set 640x480x8 video mode:
%s\n”,SDL_GetError());
exit(1);
}
if ( pic->format->palette != NULL) {
SDL_SetColors(screen, pic->format->palette->colors,
0,
pic->format->palette->ncolors);
}
pic = SDL_DisplayFormat(pic);
if (pic == NULL) {
fprintf(stderr,“Problem: %s\n”,SDL_GetError());
}

if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 )
fprintf(stderr,“Problem:
%s\n”,SDL_GetError());
}
buffer=(Uint8 )screen->pixels;
for ( i=0; ih; ++i ) {
memset(buffer,(i
255)/screen->h,
screen->w*screen->format->BytesPerPixel);
buffer += screen->pitch;
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}

for ( i=0; iw; i += background->w ) {
dest.x = i;
dest.y = 0;
dest.w = background->w;
dest.h = background->h;
SDL_BlitSurface(background, NULL, screen,
&dest);
}
SDL_UpdateRect(screen, 0, 0, 0, 0);
x = (screen->w - pic->w)/2;
y = (screen->h - pic->h)/2;
setX(x,y);
printf(“Startup finished\n”);
}

SDL_Surface *loadPic(char *newName)
{
printf(“SetBMP Begin\n”);
filename = newName;
image = SDL_LoadBMP(filename);
if (image == NULL) {
fprintf(stderr,“Couldn’t load bitmap %s:
%s\n”,newName,SDL_GetError());
return NULL;
}
surface = SDL_DisplayFormat(image);
SDL_FreeSurface(image);
printf(“setBMP finished\n”);
return(surface);
}

void setX(int x2, int y2)
{
printf(“SetX:%d Y:%d Begin\n”,x2, y2);
x = x2;
y = y2;
dest.x = (x - pic->w);
dest.y = (y - pic->h);
dest.w = pic->w;
dest.h = pic->h;
if (SDL_BlitSurface(pic, NULL, screen, &dest) < 0 )
{
SDL_FreeSurface(pic);
fprintf(stderr,“Problem: %s\n”,
SDL_GetError());
}
SDL_UpdateRects(screen, 1, &dest);
printf(“setX:Y: finished\n”);
}
void shutDown ()
{
SDL_FreeSurface(pic);
SDL_FreeSurface(background);
SDL_UpdateRects(screen, 1, &dest);
SDL_Delay(3000);
exit(0);
}

main (int argc, char *argv[])
{
startUp();
transPic();
shutDown();
}

– END SOURCE CODE --__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.

Jared wrote:

Okay as far as I can tell this code should work, but
for some reason it doesnt. I can’t find anything wrong
with it but I can get it to work if I take it in
pieces. What am I doing that is wrong? Thank you for
helping.

Jared

Two questions, should transPic() be included as part of
the code you sent? What is it that this code doesn’t do,
that it should?–
“Though you die, La Resistance lives on” - SP:BL&U

Jared wrote:

Transpic is not a necessary function, heck it never
even gets as far as it I know I’ve tested the code
with and without it. Still the same error. :frowning: What the
code should do is it should load the “pic.bmp” and
"background.bmp" and then blit the background.bmp all
over the screen as a background, then it should blit
"pic.bmp" to the screen at the x,y coordinates
specified at the end of the startup function where the
setX function is called. I have tried everything I can
think of and nothing seems to cure it. I know it must
be something I’m doing wrong.

Three problems:

  1. Move the “loadPic” calls to AFTER the SetVideoMode() call.
  2. Don’t reuse the variable between SDL_LoadBMP() and
    DisplayFormat().
  3. In setX:
    dest.x = x2; // was dest.x = (x - pic->w);
    dest.y = y2; // was dest.y = (y - pic->h);

I’ll email you the modified code.

Thanks for taking the
time to even look at my feeble code.

Jared

No problem. You helped me with my compile problem. The
least I could do. :)–
“Though you die, La Resistance lives on” - SP:BL&U

Two questions, should transPic() be included as part
of the code you sent? What is it that this code
doesn’t do,that it should?

“Though you die, La Resistance lives on” - SP:BL&U

Transpic is not a necessary function, heck it never
even gets as far as it I know I’ve tested the code
with and without it. Still the same error. :frowning: What the
code should do is it should load the “pic.bmp” and
“background.bmp” and then blit the background.bmp all
over the screen as a background, then it should blit
“pic.bmp” to the screen at the x,y coordinates
specified at the end of the startup function where the
setX function is called. I have tried everything I can
think of and nothing seems to cure it. I know it must
be something I’m doing wrong. Thanks for taking the
time to even look at my feeble code.

Jared__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.

Thank you so much, I knew it had to be some stupid
little error on my part. It works fine now. Now to
just fix it so it doesn’t leave that nasty trail
behind it. One step at a time, you’ve got to love the
learning process :slight_smile:

Jared_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com