QuickBASIC/QBASIC newsletter

 
Tricks of the Trade
 
A real easy Pixel*tile*pixel*tile engine using Blast!
by 19day
 
19day@geocities.com
http://members.xoom.com/19day/maze.html
 
            *I am not liable if any information in this document causes you computer to crash, lose data,
            *slow down, stumble when running, vomit, sing or spontaneously explode.

                    Dash can do this, I know, but I had already created this little beauty and wanted to keep it,
            although it can go slow at the true pixel scrolling level, skipping over other pixel works well, too.
                    All right, first of all I'm assuming you already have a tile engine to adapt to the scrolling-
            engine, as this is how I made mine.  My tile engine was based on Tsugumo's RPG-CODE, a
            simple direct tile engine, that one can rip apart quite easily.  If you look at Tsu's code, he uses
            a pair of variables called spotx and spoty to define where each tile is to be placed.  In a for loop
            that goes around 3 tiles above and to the left of the player it starts, and a nested loop does the
            trick, here is the excerpt:

                    'shows the playing area 5 tiles left and right, and 4 tile up and down the player
                    xstart = -5
                    xend = 5
                    ystart = -4
                    yend = 4
                    'map is the array that holds the table of numbers which is the map
                    'pmx is the players center screen X co-ords relative to the screen.
                    'pmy is the players center screen Y co-ords relative to the screen.
                    'tiles is the pic array
                    'offset is the number to offset for each pic in the tiles array
                    FOR x = xstart TO xend
                        FOR y = ystart TO yend
                            tile = map(pmx + x, pmy + y)
                            spotx = ppx + x * 15
                            spoty = ppy + y * 15
                            PUT (spotx, spoty), tiles(tile * offset), PSET
                        NEXT y
                    NEXT x

                    If this is your method, converting to Blast! is easy.  I assume you know how to use
            Blast!, and to convert the above to a tile scrolling engine into Blast!, but here is where
            the tile engine comes in.  You'll want it to know which way it should scroll, so we add an
            argument, direct, if 1 up, if 2 down, if 3 left, 4 right.  Now, to scroll, you need to have the
            tiles move pixel-by-pixel to the next tile, so do it in the tile putting:

                    ...
                    FOR pixit = 1 TO 15 STEP 2        'scroll the tile putting anchor by 2
                    CALL BlastCLS(VARSEG(buffer1%(0)), VARPTR(buffer1%(0)), 0)  'clear the buffer
                    FOR x = xstart - 1 TO xend + 1     'see one more in
                        FOR y = ystart - 1 TO yend + 1     'every direction to scoll onto screen
                            tile = map(pmx + x, pmy + y)
                            spoty = ppy + y * 15
                            spotx = ppx + x * 15
                            IF direct = 1 THEN
                                spoty = ppy + y * 15 + pixit       'moves the tiles
                            ELSEIF direct = 2 THEN
                                spoty = ppy + y * 15 - pixit
                            ELSEIF direct = 3 THEN
                                spotx = ppx + x * 15 + pixit
                            ELSEIF direct = 4 THEN
                                spotx = ppx + x * 15 - pixit
                            END IF
                            CALL BlastPut(VARSEG(buffer1%(0)), VARPTR(buffer1%(0)), VARSEG(tiles(0)),_
                                                                            VARPTR(tiles(tile * offsets)), INT(spotx), INT(spoty), 0)
                        NEXT y
                    NEXT x
                    ...

                    New problem: Now, when you move, you have to erase the junk that you don't want to
            be seen, like the screen jumps bigger, try it, you'll see how it works.  Blast! has no built in
            Line Box Fill, which is what I would use, since there would be no flicker.  At first I used a
            blank tile, but then I made this:

                    SUB BlastBox (dsegment%, doffset%, x1%, y1%, x2%, y2%, colr%, fill%)
                        IF fill% = 1 THEN
                            FOR lines% = x1% TO x2%
                                CALL BlastLine(dsegment%, doffset%, lines%, y1%, lines%, y2%, colr%)
                            NEXT lines%
                        ELSE
                            CALL BlastLine(dsegment%, doffset%, x1%, y1%, x1%, y2%, colr%)
                            CALL BlastLine(dsegment%, doffset%, x1%, y1%, x2%, y1%, colr%)
                            CALL BlastLine(dsegment%, doffset%, x1%, y2%, x2%, y2%, colr%)
                            CALL BlastLine(dsegment%, doffset%, x2%, y1%, x2%, y2%, colr%)
                        END IF
                    END SUB

                    It does it well enough, too, so I erased everything around the visible playing area,
            and now, each tile scrolls onto the screen as it should.  Collision detection: A really big
            problem after the convertion.  Now you have to modify each one to see one tile to each
            side when you try to move into that side.  (As a normal collision detector will only do
            anything when you walk onto the water, and not when you try.)  It's easy when you get
            the idea of how the math works now.

                    Any questions on the subject should be directed to 19day@geocities.com.
 

Back