Moving a surface with scroll Moving a surface with scroll of the screen

Hello,

I started with SDL by creating a simple 2D game and wanted to move an enemy randomly,

I managed to move the enemy on a simple screen but I was unable to move it on a scrolling screen.

And here is the code that allows to move the enemy on a simple screen:

int main (int argc,char *args[])

{int xvel=30;
int yvel=10;
SDL_Surface *screen=NULL;
SDL_Surface *back=NULL;
SDL_Surface *ennemi=NULL;
SDL_Event event;
SDL_Rect offset,offsetennemi;
SDL_Init(SDL_INIT_VIDEO);
back = IMG_Load("back.jpg");
ennemi = IMG_Load("en.png");
screen = SDL_SetVideoMode(740,580,16,SDL_HWSURFACE | SDL_DOUBLEBUF);

SDL_WM_SetCaption("jeu",NULL);

offset.x=0;
offset.y=0;

offsetennemi.x=0;
offsetennemi.y=0;
SDL_BlitSurface(back,NULL,screen,&offset);
SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
  SDL_Flip( screen );
while(1)
{ 
SDL_BlitSurface(back,NULL,screen,&offset);
offsetennemi.x+=xvel;
SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
SDL_Flip( screen );
SDL_Delay(1000);

SDL_BlitSurface(back,NULL,screen,&offset);
offsetennemi.x+=xvel;
SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
SDL_Flip( screen );
SDL_Delay(1000);


SDL_BlitSurface(back,NULL,screen,&offset);
offsetennemi.x+=xvel;
SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
SDL_Flip( screen );
SDL_Delay(1000);


SDL_BlitSurface(back,NULL,screen,&offset);
offsetennemi.x-=xvel;
SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
SDL_Flip( screen );
SDL_Delay(1000);


SDL_BlitSurface(back,NULL,screen,&offset);
offsetennemi.x-=xvel;
SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
SDL_Flip( screen );
SDL_Delay(1000);


SDL_BlitSurface(back,NULL,screen,&offset);
offsetennemi.x-=xvel;
SDL_BlitSurface(ennemi,NULL,screen,&offsetennemi);
SDL_Flip( screen );
SDL_Delay(1000);

}

return 0;
}

First of all, I would like to suggest you to move over to SDL2, since it has an improved renderer, texture usage and other good stuff.

Secondly, why do you ‘flip’ the screen (with ‘SDL_Flip’) several times? One (1) time in the end of the loop is enough.

Third, you need to use code brackets when posting here, since it makes it much easier to read posted code.

As for your question: whenever you want to implement scrolling in a game, you usually have a camera which offset’s the object(s), the background(s) etc. So whenever the camera is moving to the right, the object(s), the background(s) etc is moving to the left, giving the illusion that the level scrolls to the right.
Whenever you want an object to follow along with the camera, for example if you have a car driving to the right, and having the camera follow along with it, and have an enemy that flies in the sky and follows the car, the car and the flying enemy have a world position, which is not being offset by the camera (which all other scrolling objects are).
I hope this is of any help to you and that it makes sense.
Let me know if you need a better explanation and I will try to rephrase and explain in a better way, or with a code example.

Thanks a lot Naith ,
in fact i want the enemy to be independent from the camera so if you have a car driving to the right, and having the camera follow along with it, and have an enemy that flies in the sky which does not follows the car so it’s just moving.
I will be so grateful if you help me with a code example.

What if you want to add zoom and rotation to your camera as well? Do you perform that transform on each render entity?

If you want to use only SDL2, you can use the built in rotation- and scaling feature in SDL_Texture to rotate each entity relative to the camera. To get the “correct” rotation, you need to use sin and cos and some linear algebra to position the entity relative to the camera, since some math has do be done to position the entity on the correct place if the camera has both been translated and rotated.
If you also want to do zooming, you’ll have to take the zoom factor in consideration aswell.

The other way of doing rotation and zooming is by using OpenGL and matrices.
If you’re not familiar with OpenGL and matrices, you can use the external library SDL_GPU, which uses OpenGL under the hood, and this library gives you the opportunity to rotate and scale (zoom) your entities.
You also have shader support, which opens up for some cool image processing effects, if wanted.

Thanks for the reply Naith! If I use OpenGL matrices, can I still use the SDL Renderer or would I have to roll my own at that point?

Note that only older OpenGL version(s) have built-in matrices. In modern OpenGL (which I highly recommend using over older versions), built-in matrices etc has been removed, so you’ll either have to roll your own matrix class or use an existing third-party library, like GLM for example.

And to answer your question: yes, you can use matrices with the SDL renderer. Note though that SDL uses integer values when positioning objects, so you would have to typecast the end position/rotation result when rendering with, for example, SLD_RenderCopy, as SDL_RenderCopy’s third and fourth argument are SDL_Rect’s, which holds integer values for the position and the size of the texture.
Since the SDL_Renderer is a 2D renderer, you would only need 3x3 matrices (and not 4x4 as in a 3D application), which saves a bit of math since you don’t have to think about the third dimension.