[SDL] correct way of map scrolling
L-28C
kixdemp at gmail.com
Thu Aug 9 07:35:50 PDT 2007
I'm in that same situation... I'm doing the same thing you're doing
(placing the map at an offset position), but I'm thinking, wouldn't it
be faster (process wise) to crop the map and blit just that part on the
screen? Or does SDL do that cropping automatically? :-P
Hasan Zorlu wrote:
> Hello !
>
> i'm writing a little game with c# sdlDotNet, and i made my own map
> scrolling. but i don't known its right way or not.
> i create large map (5000x5000), actually i don't create 5000px surface
> (you'll see below)
>
> here is the code:
>
> Game Class
> ----------------------------
> public const int ScreenW = 1024;
> //Screen Width
> public const int ScreenH = 768;
> //Screen Height
>
> public static int parseX = 0;
> //mapX in large map
> public static int parseY = 0;
> //mapY in large map
> public const short scrollSpeed = 10;
> //map scrolling speed
> public static char scrollWayX = 'N';
> //scrolling where x? N = nowhere
> public static char scrollWayY = 'N';
> //scrolling where y? N = nowhere
>
>
>
> Draw Method
> -----------------------------------
> Map.ParseMap(parseX, parseY);
> //draw map objects (see above)
> Video.Screen.Update();
> //update screen
>
>
> MouseMotion Method
> -----------------------------------------
> public void Motion ( MouseMotionEventArgs e ) {
>
> if (e.X > Game.ScreenW - 20) {
> //mouse scroll right
> Game.parseX += Game.scrollSpeed;
> //increase showmap X
> Game.scrollWayX = 'R';
> //set our way to R (Right)
> }
> else if (e.X < 10) {
> //mouse scroll left
> Game.parseX -= Game.scrollSpeed;
> //decrease showmap X
> Game.scrollWayX = 'L';
> //set our wat to L (Left)
> }
> else Game.scrollWayX =
> 'N'; //stop x scrolling
>
>
>
> if ( e.Y > Game.ScreenH - 20) {
> //mouse scroll down
> Game.parseY += Game.scrollSpeed;
> //increase showmap Y
> Game.scrollWayY = 'D';
> //set our way to D (Down)
> }
> else if (e.Y < 10) {
> //mouse scroll up
> Game.parseY -= Game.scrollSpeed;
> //decrease showmap Y
> Game.scrollWayY = 'U';
> //set our way to U (Up)
> }
> else Game.scrollWayY = 'N';
> //stop y scrolling
> }
>
>
>
> Map File
> -----------------------------------
> #split , each line
> #1. argument: C is "Content Block" not used
> #2. argument: Sprite file name (1.png, 2.png, 3.png, 4.png)
> #3. argument: Sprite's X position in our map
> #4. argument: Sprite's Y position in our map
>
> C,1,899,1200
> C,2,949,1205
> C,3,998,1190
> C,4,2350,4500
>
>
> Map Class - ParseMap Method
> ------------------------------------------------
> //contents (SpriteConnection) variable in this method, loaded map
> contents (see above)
>
> public void ParseMap(int x, int y) {
> //parse current view map sprites
>
> int stopX = x + Game.ScreenW;
> //check end sprite's x
> int stopY = y + Game.ScreenH;
> //check end sprite's y
>
> int startX = x - Game.ScreenW;
> //check start sprite's x
> int startY = y - Game.ScreenH;
> //check start sprite's y
>
>
> for (int i = 0; i < contents.Count; i++) {
> //check for all map contents ("C" prefix lines in
> map file)
> bool parse = false;
> //set parse or not option of each sprite
>
> if (contents[i].X >= 0 && contents[i].X <= stopX) {
> //if each sprite's x positions in our blocks
> if (contents[i].Y >= 0 && contents[i].Y <= stopY)
> { //if each sprite's y positions in our blocks
> parse = true;
> //sprite is in our blocks, we can draw it
> }
> }
>
> if (Game.scrollWayX == 'L') {
> //if mouse left scrolling
> contents[i].X += Game.scrollSpeed;
> //mouse going left, sprite must go right
> }
> else if (Game.scrollWayX == 'R') {
> //if mouse right scrolling
> contents[i].X -= Game.scrollSpeed;
> //mouse going right, sprite must go left
> }
>
> if (Game.scrollWayY == 'U') {
> //if mouse up scrolling
> contents[i].Y += Game.scrollSpeed;
> //mouse going up, sprite must go down
> }
> else if (Game.scrollWayY == 'D') {
> //if mouse down scrolling
> contents[i].Y -= Game.scrollSpeed;
> //mouse going down, sprite must go up
> }
>
> if (parse == true) {
> //if we can draw this sprite
> Video.Screen.Blit(contents[i],
> contents[i].Rectangle); //so draw it
> }
> }
> }
>
>
> basicly
> - i set x and y of sprites (in map file),
> - when mouse scrolling, check if any sprites in our 1024 x 768 block
> (parse=true) blit sprite, if not in our block not draw (parse = false)
>
> its working fine yet, but i scare about future if hundreds of sprite on
> our map
>
> Question is:
> am i go wrong way or what?
>
> What do you think about that?
>
> thank you.
>
> Hasan ZORLU
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
More information about the SDL
mailing list