   Yet another scrolling thread :P
  Hey guys!I've been a bit of a lurker here thus far but I am loving the site, it's motivated me to finally have a crack at making my own top down tilebased "RPG" thing.My problem is this:I have an extremely large 2D array which stores a number which relates to a "type" of tile, so 1 is grass, 2 is stone etc.I can't work out a logical way of displaying a different portion of the map when the user leaves the bounds of the screen.I have experimented with using a camera, and incrementing it by 800 pixels when the user moves to the far right of the screen. This didn't end well, I had constant scrolling to the right hand side and the game needed to be restarted. Any ideas would be welcome guys, I'll post some of my source below:(still in alpha stage atm)#include <iostream>#include <SDL/SDL.h>#include <SDL/SDL_image.h>#include <sstream>using namespace std;//width and height if the screenconst int width = 800;const int height = 600const int bpp = 32;//constants used for blitting the tilesconst int black = 0;const int grass = 1;const int rock = 2;int map[21][61] = {} //truncated for obvious reasons!//holds the persons co-ordinatesSDL_Rect personRect;//was intended to be a cameraSDL_Rect camera;//all of the surfacesSDL_Surface *screen = NULL;SDL_Surface *grassTile = NULL;SDL_Surface *rockTile = NULL;SDL_Surface *blackTile = NULL;SDL_Surface *person = NULL;//a large canvas to hold each of the tilesSDL_Surface *canvas = NULL;//condition for game loopbool done;SDL_Surface *loadImages(string filename){SDL_Surface *temp = IMG_Load(filename.c_str());SDL_Surface *optimo = SDL_DisplayFormat(temp);return optimo;}//function to blit the tiles, correct me if i'm doing this the long way or something :Pvoid reset(){;for(int y = 0; y < 21;y++){for(int x = 0; x < 62; x++){if(map[y][x] == grass){SDL_Rect rect;rect.x = x * 20;rect.y = y * 20;SDL_BlitSurface(grassTile,NULL,canvas,&rect);}else if(map[y][x] == rock){SDL_Rect rect;rect.x = x * 20;rect.y = y * 20;SDL_BlitSurface(rockTile,NULL,canvas,&rect);}else{SDL_Rect rect;rect.x = x * 20;rect.y = y * 20;SDL_BlitSurface(blackTile,NULL,canvas,&rect);}}}}//function to move the player/check for collisionsvoid movePlayer(int dir){//upif(dir == 1){personRect.y -= 2;if(map[(personRect.y) / 20][(personRect.x / 20)] == rock){personRect.y += 2;return;}}//rightif (dir == 2){personRect.x += 10; //potential method of increasing the cameras x position, didn't work too wellif(personRect.x >= 800){camera.x = 800 - (61*20);}if(map[personRect.y / 20][(personRect.x + 20) / 20] == rock){personRect.x -= 2;return;}}//downif (dir == 3){personRect.y += 2;if(map[(personRect.y+ 18) / 20][(personRect.x / 20)] == rock){personRect.y -= 2;return;}}//leftif (dir == 4){personRect.x -= 10;if(personRect.x <= 800){camera.x = 0;}if(map[personRect.y / 20][(personRect.x)/ 20] == rock){personRect.x += 2;return;}}}int main (int argc, char *argv[]){    /* Initialize SDL */    if (SDL_Init (SDL_INIT_VIDEO) < 0)    {   exit (1);    }    atexit (SDL_Quit);    screen = SDL_SetVideoMode (width, height, bpp, SDL_SWSURFACE | SDL_DOUBLEBUF);    if (screen == NULL)    {   exit (2);    }    SDL_WM_SetCaption ("Yet another RoflRPG, lolliver style.", NULL);    SDL_EnableKeyRepeat(20,20);    grassTile = loadImages("grass.bmp");    rockTile = loadImages("rock.bmp");    blackTile = loadImages("black.bmp");    person = loadImages("person.jpg");    //init canvas    canvas = SDL_CreateRGBSurface(SDL_SWSURFACE,61 * 20, 21 * 20, 32,0,0,0,0);    personRect.x = 50;    personRect.y = 50;    camera.x = 0;    camera.y = 0;    camera.w = 800;    camera.h = 600;    done = false;   while (!done)    {   SDL_Event event;   /* Check for events */   while (SDL_PollEvent (&event))   {  switch (event.type)  {  case SDL_KEYDOWN: switch(event.key.keysym.sym) {case(SDLK_LEFT):movePlayer(4);break;case(SDLK_RIGHT):movePlayer(2);break;case(SDLK_UP):movePlayer(1);break;case(SDLK_DOWN):movePlayer(3);break;default:break;}break;  case SDL_QUIT: done = 1; break;  default: break;  }   }   /* Draw to screen */reset();   SDL_BlitSurface(person,NULL,canvas,&personRect);   SDL_BlitSurface(canvas,&camera,screen,&screen -> clip_rect);   SDL_UpdateRect(screen, camera.x,camera.y,camera.w,camera.h);   SDL_Flip(screen);    }    return 0;}I know it's sloppy atm, but please try to be constructive with the criticism.   
  You could eliminate a lot of code by storing your surfaces in an array or, better yet, stack all of your tiles in a 1 tile wide image and just store rects from that image in an array.  That way if you need to convert your surface to a new format depending on the screenmode, you only have to do it once.Also, your tile map is hard-coded into the program.  You might want to be able to load and save the map into RAM later on.  If you do that, then consider using the SDL_endian.h header so that it will be easy to port your code to a PowerPC or big-endian ARM or MIPS-based computer in the future, and the UInt16 type for your tile map rather than int.  (Int can vary in size from one processor type to the next but the SDL typedefs will correct for this.)If you want to see a really cool demo of a smooth-scrolling screen, look at the smoothscroll-1.1.tar.gz file containing the source code on http://olofson.net/examples.html.  (If you don't know how to unpack a .tar.gz file, use 7zip from http://www.7-zip.org/.)   ;  I appreciate your fast reply and the links were quite helpful, but I think you misunderstood what I was after here slightly (I didn't explain very well)when the player reaches one of the far extremes of the screen, I'd like the camera to shift position by however many pixels wide the screen is.so for example if the user gets to the right hand side of the screen, I'd like the camera to shift forward by 800 pixels (for example)Although every time I try and do this the screen seems to just refresh infinitely.I have found a temporary solution:if(personRect.x >= 800){  camera.x = 800;}this fails for 2 reasons though:1. the camera wont be able to shift forward again (if this is necessary)2. when using this method, it redraws the canvas properly on the left hand side of the screen (the new part), but the right hand side of the canvas still retains the tiles from the first 800 pixels.Any ideas as to how I'd fix this? I realise that my code is sloppy atm and that there's virtually no error checking or optimisation or anything. We all need to start somewhere though :PI appreciate the advice so far!Cheers   ;  Aha! never mind guys, Lazyfoo tutorials were my saviour again :Pcheers for the help though.I can post my solution if anyone wants it heh.   ;  I would like it :) Please post :)   ;  Yey, I feel important now!Anyway, I have a SDL_Rect for the persons co-ordinates and another for the "camera", here's the code to keep the camera centred over the player:camera.x = (personRect.x + (personWidth / 2)) - (screenWidth / 2);camera.y = (personRect.y + (personHeight / 2) - (screenHeight / 2);then do the normal error checking for it:if(camera.x < 0){camera.x = 0;}and so on.this was all taken from Lazy Foo's awesome tutorial series, I did not come up with this code.If anyone fancies helping though I'm stuck again :PI have some code currently which makes the camera stop moving on the X axis when it hits the far right hand side of the canvas, I'll demonstrate this below:if(camera.x >= (xTiles*tileWidth) - scrWidth){camera.x = (xTiles*tileWidth) - scrWidth;}This works fine. However when I perform the same test on the Y axis I get a segmentation fault for some reason. code's below: if(camera.y >= ((yTiles*tileHeight) - scrHeight){  camera.y = (yTiles*tileHeight) - scrHeight;}this makes it segfault, any ideas guys?heh.   ;ollyb342 said:I have some code currently which makes the camera stop moving on the X axis when it hits the far right hand side of the canvas, I'll demonstrate this below:if(camera.x >= (xTiles*tileWidth) - scrWidth){camera.x = (xTiles*tileWidth) - scrWidth;}This works fine. However when I perform the same test on the Y axis I get a segmentation fault for some reason. code's below:if(camera.y >= ((yTiles*tileHeight) - scrHeight){camera.y = (yTiles*tileHeight) - scrHeight;}this makes it segfault, any ideas guys?Are you checking against the boundaries of your array?   ;Quote:Original post by samuraicrowAre you checking against the boundaries of your array?*FACEPALMS REPEATEDLY*That was the problem, thankyou very much :PI just need some ideas as to where I should take this thing next! Might pop some enemies in there if I can work out how, maybe give the hero a sword to swing :PCheers for the help samuraicrow etc.heh.   