Hello there

After I finally got my Mario-Clone to work fine with the mechanics and tiles,
I want to do it side-scrolling.
Right now it works, but in a very bad way.
I draw ALL the tiles(500*20) to a SDL_Surface of the length of 500*15.
When moving to the right, I go through every tile and set x-=1.
While you can walk 30Pixel a second(game is limited to 30fps), that makes 120000 calculations per second,
just for the redrawing.
I want it more efficent(even though modern Computers should be able to do this).
My Idea is the following:
Having one SDL_Surface(background) where all tiles a drawn to.
Having a second SDL_Surface(view), having the size of 20*20 tiles.
Then there is a rect of size of view, painted from the background, to view.
After every step the player does, the copied rectangles x start value will change.
Doing it this way, I will save 119970 calculations / second.
Problem: I don't get it done?!
Tried to abstract the needed things to test it. Here is the code:
Code:
#include "SDL.h"
const int WINDOW_WIDTH = 100;
const int WINDOW_HEIGHT = 100;
const char* WINDOW_TITLE = "";
int main(int argc, char **argv)
{
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
SDL_Surface* bg = SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,0,SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_Surface* background_img = SDL_LoadBMP("test.bmp"); //500*100 image
if(background_img == NULL)return -5;
SDL_Rect src;
src.x =0; // the part, that shall be blittet
src.y =0;
src.w =100;
src.h =100;
SDL_Rect ziel;
ziel.x =0;
ziel.y =0;
ziel.w =100;
ziel.h =100;
SDL_BlitSurface(background_img , NULL, screen, NULL); //painting the image to the surface(screen)
SDL_Event event;
bool gameRunning;
while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
if (event.type == SDL_KEYDOWN)
{
SDLKey keyPressed = event.key.keysym.sym;
switch(keyPressed)
{
case SDLK_ESCAPE:
break;
case SDLK_LEFT:
src.x -=10;
break;
case SDLK_RIGHT:
src.x +=10;
break;
default: break;
}
}
}
if(SDL_BlitSurface(screen,&src, bg,&ziel)==-2)return -4; //painting the part of screen given by rect(src)
SDL_Flip(bg);
}//end game running
SDL_FreeSurface(background_img);
SDL_Quit();
return 0;
}
I really don't know where my mistake is.
Probably I am using the surface in a wrong way?
Thanks for your help:)
Aestond