Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Scrolling and Sorting

Scrolling and Sorting

Daerax
Journal · · 3 min read
1,322 0
I was able to add a couple of things to the game tonight. Which was basically to enable defining a world and per pixel scrolling through it. This was done by essentially three things, using a Red Black Tree to hold the objects in the world. Adding the notion of layer to objects and adding a camera. One boon from the way I implemented things is that the world is in a sense infinite sized (technically int.Max sized) . You can place objects practically anywhere with negative or positive values and never be out of bounds. I could easily make it truly infinite but dont really have time to waste. As well you can keep on scolling the camera and also never be out of bounds. Youd just see endless stretches of the default tile.

Originally I was using a simple list to hold objects. Upon further thought I realized that I would like to be able to drop items, pick them up, kill npcs and summon cows. This meant using a data structure that allows quick insertion and deletion.

The red black tree (similar to C++ Multimap but does not allow multiple keys of same value) is a balanced and ordered tree. Objects implement the ICompare interface and are sorted upon insertion by first checking if the one of objects being compared is in a higher layer, if it is then it will automatically sink so as to be displayed on top, if not just compare Y values. For example, all objects which have a layer of 2 will always be above objects with a layer value of 0. This for example allows me to put an apple on a table or hang a picture on a wall. The default layer is zero. Layers can be negative, have decimal and are bound by float.Max. The Renderer asks the World for all visible objects, this is done by simply filtering out objects that are not in view. On a key down event the camera attaches a scrolling method to the tick events list and removes it on a key up.

Next will be to add NPCs which walk around and fight. Projectile and summoning spells. I will also be integrating the old Conversation scripting language I made some time ago.

--------------------
Snippets: full source here .

To display:

strip (z : option[Surface]) :  Surface            match(z)                | Some(s) => s                | None => base_gfx                            public Display() : void            seq [ _ = video.Blit(base_gfx,Drawing.Point(x,y)),                                    x in [0,base_gfx.Width..video.Width ], y in [0,base_gfx.Height..video.Height]];                        seq[ _ = video.Blit (surface , Drawing.Point(p.X - camera.Location.X, p.Y - camera.Location.Y)),                                           o in TheWorld.BuildVisibleList(), strip(surfacePool.Find ( o.Key)) -> surface,                                           o.Position -> p ];               video.Update ();   


Representing the World:

  public AddObject(o  : ObjectType ) : void {                 objects = Tree.Insert(objects,o,false);    }        public BuildVisibleList () : list[ObjectType] {        def CheckFits( o, c, s, s2){            if ( (o + s2 - c > 0) && (o - c  < s))                  true            else                 false }                objects.Filter(  fun(o){                            if ( CheckFits(o.Position.X, camera.Location.X,Nsdl.ScreenRes.Width, o.Size.Width  ) &&                                   CheckFits(o.Position.Y, camera.Location.Y,Nsdl.ScreenRes.Height, o.Size.Height  ))                                          true                             else                                         false                                                          });   }

    Main() : void         def objs = [ObjectType(@".\Art\014.bmp", Drawing.Point(480,250), Drawing.Color.FromArgb (255,255,255)),                          ObjectType(@".\Art\stand2.png", Drawing.Point(920,300),Drawing.Color.FromArgb (200,140,208)),                      ObjectType(@".\Art\tavernx.bmp", Drawing.Point(420,260),Drawing.Color.FromArgb (200,140,208), 2.0f)] ;                def o1 = ObjectType(@".\Art\017.bmp",Drawing.Color.FromArgb (255,255,255));                            def w = World(@".\Art\012.bmp");                seq [ w.AddObject (o) , o in objs];                w.AddObjectManyTimes(o1, World.RandomDistribution(95, 500));                _ = Narrator(w);        


Objects all in same layer.

Moving the house to a higher layer.

Discussion

Loading comments...