SDL scrolling of the background

I wrote a simple program that allows to move a character. And when I add the functions allowing the background scrolling, at runtime, the program displays a black screen and not the background scrolling.
And here is the program code:

#include <stdio.h>

#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include "fonctions.h"
int main (int argc,char *args[])
{int game=1;
int collision=0,i=0;
int x=0,y=0;
SDL_Surface *screen=NULL;
SDL_Surface *back=NULL;
SDL_Surface *ennemi=NULL;
SDL_Event event;
SDL_Rect offset,offsetennemi;
back = IMG_Load(“background.bmp”);
ennemi = IMG_Load(“voiture0.bmp”);
screen = SDL_SetVideoMode(740,580,16,SDL_HWSURFACE | SDL_DOUBLEBUF);
int direction;
SDL_Rect clip[3];
SDL_Surface *faces=NULL;

> clip[1].x=212;
> clip[1].y=19;
> clip[1].w=83;
> clip[1].h=133;

> clip[2].x=364;
> clip[2].y=19;
> clip[2].w=83;
> clip[2].h=133;

> offsetennemi.x=0;
> offsetennemi.y=0;
>  SDL_Rect posback;
>   posback.x=50;
>   posback.y=50;
> SDL_BlitSurface(back,&posback,screen,&offset);
> //SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
> SDL_Flip(screen);
>  SDL_Delay(3000);
> while(game)
>   { 

>  SDL_Flip(screen);
>   	while(SDL_PollEvent(&event)) {
>     switch(event.type) {
>      case SDL_QUIT:
>       game=0;
>       break;
>      case SDL_KEYDOWN:
>       {
>          if (event.key.keysym.sym == SDLK_RIGHT)
>            direction = 1;
>           if (event.key.keysym.sym == SDLK_LEFT)
>            direction= 2;
>        }
>       break;
>       case SDL_KEYUP:
>           direction=0;
>       break;
>             }
>         }
> if(direction==1)
>     {  
>     	if(i==3)
>     		{i=0;}
>     	printf("%d\n",i );
>     offsetennemi.x+=20;
>     SDL_BlitSurface(back,&posback,screen,&offset);
>     SDL_BlitSurface(faces,&clip[i],screen,&offsetennemi);
>     SDL_Flip(screen);
>     SDL_Delay(200);
>     i++;

>     }

>    else if(direction==2)
>     {
>     	if(i==3)
>     		{i=0;}
>     	printf("%d\n",i );
>     	offsetennemi.x-=20;
> 	SDL_BlitSurface(back,&posback,screen,&offset);
>    SDL_BlitSurface(faces,&clip[i],screen,&offsetennemi);
>    SDL_Flip(screen);
>    i++;
>     SDL_Delay(200);
>     } 
>    if(offsetennemi.x==100)
>     	{   offsetennemi.x-=20;
>     		SDL_BlitSurface(faces,&clip[i],screen,&offsetennemi);
>     	}SDL_Flip(screen);
>     SDL_Delay(200);
>     posback.x+=70;
> }
> }

Sorry to ask this but do you know what you’re doing in your code? The reason I’m asking is because I get the feeling that you’re not entirely sure what you’re doing and by the look of your code, it looks like you’ve just inserted functions and stuff randomly and hope it will work.

I have no idea what’s going on in your while loop. Before the while loop starts, you render a surface 1 frame, then start the while loop and the while loop is then filled with SDL_BlitSurface calls and SDL_Flip calls.

Btw, sorry for not replying in your other post regarding (kind of) the same topic. Haven’t had the time to write a better answer and make a code example for you.

Sorry for replying too late ,but i haven’t had the time to answer.
In fact ,i do knowwhat i’m doing in my code.
I want to move the character to the left or to the right using the keyboard(we must use the clips so it will looks like the character is moving his arms or his legs )(the variable i refers to the number of the current clip).so this is the objective of the code.
Concerning the function,i’ve used the needed functions to blit the current clip of the surface of the character and of the background (scrolling of the background).

-i didn’t understand why you thought that functions and stuff are randomly inserted.

-the call of SDL flip many times is because i don’t know when i have to call it and when it’s not necessary.

-the problems of the last post are solved but thanks for your​ replying.

Sorry about the rude assumption. I re-read your code and it makes a bit more sense now.

Have the issues you have/had with your code in this thread been solved?