OSDN Git Service

==== chikyuu sprite sheet added ====
[proj16/16.git] / src / lib / scroll16.c
index 63a54d4..5f5aa9c 100755 (executable)
@@ -1,5 +1,5 @@
 /* Project 16 Source Code~
- * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
+ * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123
  *
  * This file is part of Project 16.
  *
 void walk(map_view_t *pip, player_t *player, word pn)
 {
        #define INC_PER_FRAME if(player[pn].q&1) player[pn].persist_aniframe++; if(player[pn].persist_aniframe>4) player[pn].persist_aniframe = 1;
+       //printf("player[%d].d=%d\n", pn, player[pn].d);
        switch(player[pn].d)
        {
+               //no direction
+               case 2:
+                       pip[0].video->startclk = (*clockw);
+               break;
                //right movement
                case 3:
-                       if(pip[0].tx >= 0 && pip[0].tx+20 < pip[0].map->width && player[pn].tx == pip[0].tx + 10 &&
+                       //printf("pip[0].page->tilesw=%d        ", pip[0].page->tilesw); printf("pip[0].page->tw=%d\n", pip[0].page->tw);
+                       if(pip[0].tx >= 0 && pip[0].tx+pip[0].page->tw < pip[0].map->width && player[pn].tx == pip[0].tx+pip[0].page->tilemidposscreenx &&
                        !(pip[0].map->data[(player[pn].tx)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx+1 == TRIGGX && player[pn].ty == TRIGGY))      //collision detection!
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        animatePlayer(pip, player, pn, 1);
-                                       mapScrollRight(pip, player, 0, pn);
-                                       mapScrollRight(pip, player, 1, pn);
-                                       modexShowPage(pip[1].page);
+                                       ScrollRight(pip, player, 3, pn);
+                                       ScrollRight(pip, player, 2, pn);
+                                       mapScrollRight(pip, player, !(pip[0].video->p), pn);
+                                       mapScrollRight(pip, player, (pip[0].video->p), pn);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
+                                       pip[0].video->clk = ((*clockw)-pip[0].video->startclk)/18.2;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].tx++; }
                        }
                        else if(player[pn].tx < pip[0].map->width && !(pip[0].map->data[(player[pn].tx)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx+1 == TRIGGX && player[pn].ty == TRIGGY))
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        player[pn].x+=(player[pn].speed);
                                        animatePlayer(pip, player, pn, 0);
-                                       modexShowPage(pip[1].page);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].tx++; }
                        }
                        else
                        {
-                               modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
+                               if(!pageflipflop) modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
 #ifdef SPRITE
-                               modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 32, 24, 32, &player[pn].data);
+                               PBUFSFUN(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 32, 24, 32, PLAYERBMPDATA);
 #else
                                modexClearRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 32, 14);
 #endif
-                               modexShowPage(pip[1].page);
+                               if(!pageflipflop) modexShowPage(pip[1].page);
                                player[pn].d = 2;
                        }
                        player[pn].triggerx = player[pn].tx+1;
@@ -72,39 +81,42 @@ void walk(map_view_t *pip, player_t *player, word pn)
 
                //left movement
                case 1:
-                       if(pip[0].tx > 0 && pip[0].tx+20 <= pip[0].map->width && player[pn].tx == pip[0].tx + 10 &&
+                       if(pip[0].tx > 0 && pip[0].tx+pip[0].page->tw <= pip[0].map->width && player[pn].tx == pip[0].tx+pip[0].page->tilemidposscreenx &&
                        !(pip[0].map->data[(player[pn].tx-2)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx-1 == TRIGGX && player[pn].ty == TRIGGY))    //collision detection!
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        animatePlayer(pip, player, pn, 1);
-                                       mapScrollLeft(pip, player, 0, pn);
-                                       mapScrollLeft(pip, player, 1, pn);
-                                       modexShowPage(pip[1].page);
+                                       ScrollLeft(pip, player, 3, pn);
+                                       ScrollLeft(pip, player, 2, pn);
+                                       mapScrollLeft(pip, player, !(pip[0].video->p), pn);
+                                       mapScrollLeft(pip, player, (pip[0].video->p), pn);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
+                                       pip[0].video->clk = ((*clockw)-pip[0].video->startclk)/18.2;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].tx--; }
                        }
                        else if(player[pn].tx > 1 && !(pip[0].map->data[(player[pn].tx-2)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx-1 == TRIGGX && player[pn].ty == TRIGGY))
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        player[pn].x-=(player[pn].speed);
                                        animatePlayer(pip, player, pn, 0);
-                                       modexShowPage(pip[1].page);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].tx--; }
                        }
                        else
                        {
-                               modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
+                               if(!pageflipflop) modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
 #ifdef SPRITE
-                               modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 96, 24, 32, &player[pn].data);
+                               PBUFSFUN(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 96, 24, 32, PLAYERBMPDATA);
 #else
                                modexClearRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 32, 10);
 #endif
-                               modexShowPage(pip[1].page);
+                               if(!pageflipflop) modexShowPage(pip[1].page);
                                player[pn].d = 2;
                        }
                        player[pn].triggerx = player[pn].tx-1;
@@ -113,39 +125,42 @@ void walk(map_view_t *pip, player_t *player, word pn)
 
                //down movement
                case 4:
-                       if(pip[0].ty >= 0 && pip[0].ty+15 < pip[0].map->height && player[pn].ty == pip[0].ty + 8 &&
+                       if(pip[0].ty >= 0 && pip[0].ty+pip[0].page->th < pip[0].map->height && player[pn].ty == pip[0].ty+pip[0].page->tilemidposscreeny &&
                        !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty))] == 0))//!(player[pn].tx == TRIGGX && player[pn].ty+1 == TRIGGY))      //collision detection!
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        animatePlayer(pip, player, pn, 1);
-                                       mapScrollDown(pip, player, 0, pn);
-                                       mapScrollDown(pip, player, 1, pn);
-                                       modexShowPage(pip[1].page);
+                                       ScrollDown(pip, player, 3, pn);
+                                       ScrollDown(pip, player, 2, pn);
+                                       mapScrollDown(pip, player, !(pip[0].video->p), pn);
+                                       mapScrollDown(pip, player, (pip[0].video->p), pn);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
+                                       pip[0].video->clk = ((*clockw)-pip[0].video->startclk)/18.2;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].ty++; }
                        }
                        else if(player[pn].ty < pip[0].map->height && !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty))] == 0))//!(player[pn].tx == TRIGGX && player[pn].ty+1 == TRIGGY))
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        player[pn].y+=(player[pn].speed);
                                        animatePlayer(pip, player, pn, 0);
-                                       modexShowPage(pip[1].page);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].ty++; }
                        }
                        else
                        {
-                               modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
+                               if(!pageflipflop) modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
 #ifdef SPRITE
-                               modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 64, 24, 32, &player[pn].data);
+                               PBUFSFUN(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 64, 24, 32, PLAYERBMPDATA);
 #else
                                modexClearRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 32, 9);
 #endif
-                               modexShowPage(pip[1].page);
+                               if(!pageflipflop) modexShowPage(pip[1].page);
                                player[pn].d = 2;
                        }
                        player[pn].triggerx = player[pn].tx;
@@ -154,39 +169,42 @@ void walk(map_view_t *pip, player_t *player, word pn)
 
                //up movement
                case 0:
-                       if(pip[0].ty > 0 && pip[0].ty+15 <= pip[0].map->height && player[pn].ty == pip[0].ty + 8 &&
+                       if(pip[0].ty > 0 && pip[0].ty+pip[0].page->th <= pip[0].map->height && player[pn].ty == pip[0].ty+pip[0].page->tilemidposscreeny &&
                        !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty-2))] == 0))//!(player[pn].tx == TRIGGX && player[pn].ty-1 == TRIGGY))    //collision detection!
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        animatePlayer(pip, player, pn, 1);
-                                       mapScrollUp(pip, player, 0, pn);
-                                       mapScrollUp(pip, player, 1, pn);
-                                       modexShowPage(pip[1].page);
+                                       ScrollUp(pip, player, 3, pn);
+                                       ScrollUp(pip, player, 2, pn);
+                                       mapScrollUp(pip, player, !(pip[0].video->p), pn);
+                                       mapScrollUp(pip, player, (pip[0].video->p), pn);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
+                                       pip[0].video->clk = ((*clockw)-pip[0].video->startclk)/18.2;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].ty--; }
                        }
                        else if(player[pn].ty > 1 && !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty-2))] == 0))//!(player[pn].tx == TRIGGX &&  player[pn].ty-1 == TRIGGY))
                        {
-                               if(player[pn].q<=(TILEWH/(player[pn].speed)))
+                               if(player[pn].q<=player[pn].spt)
                                {
                                        INC_PER_FRAME;
                                        player[pn].y-=(player[pn].speed);
                                        animatePlayer(pip, player, 0, pn);
-                                       modexShowPage(pip[1].page);
+                                       if(!pageflipflop) modexShowPage(pip[1].page);
                                        player[pn].q++;
                                } else { player[pn].q = 1; player[pn].d = 2; player[pn].ty--; }
                        }
                        else
                        {
-                               modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
+                               if(!pageflipflop) modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
 #ifdef SPRITE
-                               modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 0, 24, 32, &player[pn].data);
+                               PBUFSFUN(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 0, 24, 32, PLAYERBMPDATA);
 #else
                                modexClearRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 32, 12);
 #endif
-                               modexShowPage(pip[1].page);
+                               if(!pageflipflop) modexShowPage(pip[1].page);
                                player[pn].d = 2;
                        }
                        player[pn].triggerx = player[pn].tx;
@@ -195,6 +213,66 @@ void walk(map_view_t *pip, player_t *player, word pn)
        }
 }
 
+//panning page
+void panpagemanual(map_view_t *pip, player_t *player, word pn)
+{
+       switch(player[pn].d)
+       {
+               //right movement
+               case 3:
+                       if(pip[pip[0].pan->pn].tx >= 0 && pip[pip[0].pan->pn].tx+pip[pip[0].pan->pn].page->tw < pip[pip[0].pan->pn].page->tilesw)
+                       {
+                               if(player[pn].q<=player[pn].spt)
+                               {
+                                       pip[pip[0].pan->pn].page->dx+=4;
+                                       modexShowPage(pip[pip[0].pan->pn].page);
+                                       player[pn].q++;
+                               } else { player[pn].q = 1; player[pn].d = 2; pip[pip[0].pan->pn].tx++; }
+                       }
+               break;
+
+               //left movement
+               case 1:
+                       if(pip[pip[0].pan->pn].tx > 0 && pip[pip[0].pan->pn].tx+pip[pip[0].pan->pn].page->tw <= pip[pip[0].pan->pn].page->tilesw)
+                       {
+                               if(player[pn].q<=player[pn].spt)
+                               {
+                                       pip[pip[0].pan->pn].page->dx-=4;
+                                       modexShowPage(pip[pip[0].pan->pn].page);
+                                       player[pn].q++;
+                               } else { player[pn].q = 1; player[pn].d = 2; pip[pip[0].pan->pn].tx--; }
+                       }
+               break;
+
+               //down movement
+               case 4:
+                       if(pip[pip[0].pan->pn].ty >= 0 && pip[pip[0].pan->pn].ty+pip[pip[0].pan->pn].page->th < pip[pip[0].pan->pn].page->tilesh)
+                       {
+                               if(player[pn].q<=player[pn].spt)
+                               {
+                                       pip[pip[0].pan->pn].page->dy+=4;
+                                       modexShowPage(pip[pip[0].pan->pn].page);
+                                       player[pn].q++;
+                               } else { player[pn].q = 1; player[pn].d = 2; pip[pip[0].pan->pn].ty++; }
+                       }
+               break;
+
+               //up movement
+               case 0:
+                       if(pip[pip[0].pan->pn].ty > 0 && pip[pip[0].pan->pn].ty+pip[pip[0].pan->pn].page->th <= pip[pip[0].pan->pn].page->tilesh)
+                       {
+                               if(player[pn].q<=player[pn].spt)
+                               {
+                                       pip[pip[0].pan->pn].page->dy-=4;
+                                       modexShowPage(pip[pip[0].pan->pn].page);
+                                       player[pn].q++;
+                               } else { player[pn].q = 1; player[pn].d = 2; pip[pip[0].pan->pn].ty--; }
+                       }
+                       break;
+       }
+       //if (player[pn].d!=2) printf("player[%u].d=%u player[%u].q=%u\n", pn, player[pn].d, pn, player[pn].q);
+}
+
 /*map_t
 allocMap(int w, int h) {
        map_t result;
@@ -300,13 +378,14 @@ void near mapScrollRight(map_view_t *mv, player_t *player, word id, word plid)
        }
 
        /* draw the next column */
-       x= SCREEN_WIDTH + mv[id].map->tiles->tileWidth;
+       x= mv[0].page->sw + mv[id].map->tiles->tileWidth;
        if(player[plid].q%4)
+               if(!pageflipflop){
                if(id==0)
-                       mapDrawCol(&mv[0], mv[0].tx + 20 , mv[0].ty-1, x, player, mv->page->dx);
+                       mapDrawCol(&mv[0], mv[0].tx + mv[0].page->tw, mv[0].ty-1, x, player, mv->page->dx);
                else
-                       modexCopyPageRegion(mv[id].page, mv[0].page, x, 0, x, 0, mv[id].map->tiles->tileWidth, mv[id].map->tiles->tileHeight*17);
-       //}
+                       modexCopyPageRegion(mv[id].page, mv[0].page, x, 0, x, 0, mv[id].map->tiles->tileWidth, mv[id].map->tiles->tileHeight*(mv[0].page->th+2));
+               }else mapDrawCol(&mv[(!mv[0].video->p)], mv[0].tx + mv[0].page->tw, mv[0].ty-1, x, player, mv->page->dx);
 }
 
 
@@ -314,7 +393,7 @@ void near mapScrollLeft(map_view_t *mv, player_t *player, word id, word plid)
 {
        word x, y;  /* coordinate for drawing */
 
-       /* increment the pixel position and update the page */
+       /* decrement the pixel position and update the page */
        mv[id].page->dx -= player[plid].speed;
 
        /* check to see if this changes the tile */
@@ -331,11 +410,12 @@ void near mapScrollLeft(map_view_t *mv, player_t *player, word id, word plid)
        /* draw the next column */
        x= 0;
        if(player[plid].q%4)
+               if(!pageflipflop){
                if(id==0)
                        mapDrawCol(&mv[0], mv[0].tx - 1, mv[0].ty-1, x, player, mv->page->dx);
                else
-                       modexCopyPageRegion(mv[id].page, mv[0].page, x, 0, x, 0, mv[id].map->tiles->tileWidth, mv[id].map->tiles->tileHeight*17);
-       //}
+                       modexCopyPageRegion(mv[id].page, mv[0].page, x, 0, x, 0, mv[id].map->tiles->tileWidth, mv[id].map->tiles->tileHeight*(mv[0].page->th+2));
+       }else mapDrawCol(&mv[(!mv[0].video->p)], mv[0].tx - 1, mv[0].ty-1, x, player, mv->page->dx);
 }
 
 
@@ -343,7 +423,7 @@ void near mapScrollUp(map_view_t *mv, player_t *player, word id, word plid)
 {
        word x, y;  /* coordinate for drawing */
 
-       /* increment the pixel position and update the page */
+       /* decrement the pixel position and update the page */
        mv[id].page->dy -= player[plid].speed;
 
        /* check to see if this changes the tile */
@@ -359,11 +439,12 @@ void near mapScrollUp(map_view_t *mv, player_t *player, word id, word plid)
        /* draw the next row */
        y= 0;
        if(player[plid].q%3)
+               if(!pageflipflop){
                if(id==0)
                        mapDrawRow(&mv[0], mv[0].tx - 1, mv[0].ty-1, y, player, mv->page->dy);
                else
-                       modexCopyPageRegion(mv[id].page, mv[0].page, 0, y, 0, y, mv[id].map->tiles->tileWidth*22, mv[id].map->tiles->tileHeight);
-       //}
+                       modexCopyPageRegion(mv[id].page, mv[0].page, 0, y, 0, y, mv[id].map->tiles->tileWidth*(mv[0].page->tw+2), mv[id].map->tiles->tileHeight);
+       }else mapDrawRow(&mv[(!mv[0].video->p)], mv[0].tx - 1, mv[0].ty-1, y, player, mv->page->dy);
 }
 
 void near mapScrollDown(map_view_t *mv, player_t *player, word id, word plid)
@@ -384,13 +465,93 @@ void near mapScrollDown(map_view_t *mv, player_t *player, word id, word plid)
        }
 
        /* draw the next row */
-       y= SCREEN_HEIGHT + mv[id].map->tiles->tileHeight;
+       y= mv[0].page->sh + mv[id].map->tiles->tileHeight;
        if(player[plid].q%3)
+               if(!pageflipflop){
                if(id==0)
-                       mapDrawRow(&mv[0], mv[0].tx - 1, mv[0].ty+15, y, player, mv->page->dy);
+                       mapDrawRow(&mv[0], mv[0].tx - 1, mv[0].ty+mv[0].page->th, y, player, mv->page->dy);
                else
-                       modexCopyPageRegion(mv[id].page, mv[0].page, 0, y, 0, y, mv[id].map->tiles->tileWidth*22, mv[id].map->tiles->tileHeight);
-       //}
+                       modexCopyPageRegion(mv[id].page, mv[0].page, 0, y, 0, y, mv[id].map->tiles->tileWidth*(mv[0].page->tw+2), mv[id].map->tiles->tileHeight);
+       }else mapDrawRow(&mv[(!mv[0].video->p)], mv[0].tx - 1, mv[0].ty+mv[0].page->th, y, player, mv->page->dy);
+}
+
+
+//TODO finish this wwww
+void near ScrollRight(map_view_t *mv, player_t *player, word id, word plid)
+{
+       /* increment the pixel position and update the page */
+       mv[0].video->page[id].dx += player[plid].speed;
+
+       /* check to see if this changes the tile */
+       if(mv[0].video->page[id].dx >= mv[0].dxThresh )
+       {
+               /* block copy pattern to where we will draw the sprite */
+               vga_setup_wm1_block_copy();
+               _fmemmove(mv[0].video->page[id].data+4, mv[0].video->page[id].data, mv[0].video->page[id].pagesize);
+               /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
+               vga_restore_rm0wm0();
+               /* Snap the origin forward */
+               mv[0].video->page[id].data += 4;
+               mv[0].video->page[id].dx = mv[0].map->tiles->tileWidth;
+       }
+}
+
+
+void near ScrollLeft(map_view_t *mv, player_t *player, word id, word plid)
+{
+       /* decrement the pixel position and update the page */
+       mv[0].video->page[id].dx -= player[plid].speed;
+
+       /* check to see if this changes the tile */
+       if(mv[0].video->page[id].dx == 0)
+       {
+               /* block copy pattern to where we will draw the sprite */
+               vga_setup_wm1_block_copy();
+               _fmemmove(mv[0].video->page[id].data-4, mv[0].video->page[id].data, mv[0].video->page[id].pagesize);
+               /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
+               vga_restore_rm0wm0();
+               /* Snap the origin backward */
+               mv[0].video->page[id].data -= 4;
+               mv[0].video->page[id].dx = mv[0].map->tiles->tileWidth;
+       }
+}
+
+void near ScrollUp(map_view_t *mv, player_t *player, word id, word plid)
+{
+       /* decrement the pixel position and update the page */
+       mv[0].video->page[id].dy -= player[plid].speed;
+
+       /* check to see if this changes the tile */
+       if(mv[0].video->page[id].dy == 0)
+       {
+               /* block copy pattern to where we will draw the sprite */
+               vga_setup_wm1_block_copy();
+               _fmemmove(mv[0].video->page[id].data-mv[0].video->page[id].pi, mv[0].video->page[id].data, mv[0].video->page[id].pagesize);
+               /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
+               vga_restore_rm0wm0();
+               /* Snap the origin backward */
+               mv[0].video->page[id].data -= mv[0].video->page[id].pi;
+               mv[0].video->page[id].dy = mv[0].map->tiles->tileWidth;
+       }
+}
+
+void near ScrollDown(map_view_t *mv, player_t *player, word id, word plid)
+{
+       /* increment the pixel position and update the page */
+       mv[0].video->page[id].dy += player[plid].speed;
+
+       /* check to see if this changes the tile */
+       if(mv[0].video->page[id].dy >= mv[0].dxThresh )
+       {
+               /* block copy pattern to where we will draw the sprite */
+               vga_setup_wm1_block_copy();
+               _fmemmove(mv[0].video->page[id].data+mv[0].video->page[id].pi, mv[0].video->page[id].data, mv[0].video->page[id].pagesize);
+               /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
+               vga_restore_rm0wm0();
+               /* Snap the origin forward */
+               mv[0].video->page[id].data += mv[0].video->page[id].pi;
+               mv[0].video->page[id].dy = mv[0].map->tiles->tileWidth;
+       }
 }
 
 sword chkmap(map_t *map, word q)
@@ -436,29 +597,42 @@ sword chkmap(map_t *map, word q)
        return 0;
 }
 
+//TODO: player position here
 void mapGoTo(map_view_t *mv, int tx, int ty)
 {
        int px, py;
        unsigned int i;
 
        /* set up the coordinates */
-       mv->tx = tx;
-       mv->ty = ty;
-       mv->page->dx = mv->map->tiles->tileWidth;
-       mv->page->dy = mv->map->tiles->tileHeight;
+       mv[0].tx = mv[1].tx = tx;
+       mv[0].ty = mv[1].ty = ty;
+       mv[0].page->dx = mv[1].page->dx = mv[2].page->dx = mv[3].page->dx = mv->map->tiles->tileWidth;
+       mv[0].page->dy = mv[1].page->dy = mv[2].page->dy = mv[3].page->dy = mv->map->tiles->tileHeight;
 
        /* set up the thresholds */
-       mv->dxThresh = mv->map->tiles->tileWidth * 2;
-       mv->dyThresh = mv->map->tiles->tileHeight * 2;
+       mv[0].dxThresh = mv[1].dxThresh = mv[2].dxThresh = mv[3].dxThresh = mv->map->tiles->tileWidth * 2;
+       mv[0].dyThresh = mv[1].dyThresh = mv[2].dxThresh = mv[3].dxThresh = mv->map->tiles->tileHeight * 2;
 
        /* draw the tiles */
-       modexClearRegion(mv->page, 0, 0, mv->page->width, mv->page->height, 0);
+       modexClearRegion(mv[0].page, 0, 0, mv[0].page->width, mv[0].page->height, 0);
        py=0;
-       i=mv->ty * mv->map->width + mv->tx;
-       for(ty=mv->ty-1; py < SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; ty++, py+=mv->map->tiles->tileHeight) {
-               mapDrawWRow(mv, tx-1, ty, py);
+       i=mv[0].ty * mv[0].map->width + mv[0].tx;
+       for(ty=mv[0].ty-1; py < mv[0].page->sh+mv->dyThresh && ty < mv[0].map->height; ty++, py+=mv[0].map->tiles->tileHeight) {
+               mapDrawWRow(&mv[0], tx-1, ty, py);
        i+=mv->map->width - tx;
        }
+       modexCopyPageRegion(mv[1].page, mv[0].page, 0, 0, 0, 0, mv[0].page->width, mv[0].page->height);
+       {
+               unsigned int k,j,o;
+               /* fill screen with a distinctive pattern */
+               for (k=0;k < vga_state.vga_width;k++) {
+                       o = k >> 2;
+                       vga_write_sequencer(0x02/*map mask*/,1 << (k&3));
+                               for (j=0;j < (mv[0].page->height)+(mv[1].page->height)+(mv[2].page->height)+(mv[3].page->height);j++,o += vga_state.vga_stride)
+                                       vga_state.vga_graphics_ram[o] = (k^j)&15; // VRL samples put all colors in first 15!
+               }
+       }
+       modexCopyPageRegion(mv[3].page, mv[!(mv->video->p)].page, 0/**/, 0/**/, 0, 128, 28, 36);
 }
 
 
@@ -483,15 +657,18 @@ mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y)
                switch(t->debug_text)
                {
                        case 0:
-#ifndef SPRITE
-                               modexClearRegion(page, x, y, t->tileWidth, t->tileHeight, ((t->debug_data[i])+1)*2);
-                               //cannot print number value du to it being slow as bakapee
+#ifndef TILERENDER
+                               modexClearRegion(page, x, y, t->tileWidth, t->tileHeight, ((t->debug_data[i])+1));
+                               //modexprint(page, x, y, 1, 15, 0, (char const *)(t->debug_data[i]));
 #else
-                               modexDrawBmpRegion(page, x, y, rx, ry, t->tileWidth, t->tileHeight, (t->data));
+                               PBUFBFUN                (page, x, y, rx, ry, t->tileWidth, t->tileHeight, (t->data));
+                               /* then the sprite. note modding ram ptr means we just draw to (x&3,0) */
+                               //draw_vrl1_vgax_modex(x-rx,y-ry,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
+                               //modexDrawBmpRegion    (page, x, y, rx, ry, t->tileWidth, t->tileHeight, (t->data));
 #endif
                        break;
                        case 1:
-                               modexClearRegion(page, x, y, t->tileWidth, t->tileHeight, (t->debug_data[i])+1);
+                               modexClearRegion(page, x, y, t->tileWidth, t->tileHeight, (t->debug_data[i]+1));
                                //modexprintbig(page, x, y, 1, 15, 0, (t->debug_data));
                                /*for(texty=0; texty<2; texty++)
                                {
@@ -513,7 +690,7 @@ void near mapDrawRow(map_view_t *mv, int tx, int ty, word y, player_t *p, word p
 //printf("y: %d\n", poopoffset);
        /* the position within the map array */
        i=ty * mv->map->width + tx;
-       for(x=poopoffset; x<(SCREEN_WIDTH+mv->dxThresh)/(poopoffset+1) && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {
+       for(x=poopoffset; x<(mv->page->sw+mv->dxThresh)/(poopoffset+1) && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {
        if(i>=0) {
                /* we are in the map, so copy! */
                mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
@@ -533,7 +710,7 @@ void near mapDrawCol(map_view_t *mv, int tx, int ty, word x, player_t *p, word p
 
        /* We'll copy all of the columns in the screen,
           i + 1 row above and one below */
-       for(y=poopoffset; y<(SCREEN_HEIGHT+mv->dyThresh)/(poopoffset+1) && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {
+       for(y=poopoffset; y<(mv->page->sh+mv->dyThresh)/(poopoffset+1) && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {
        if(i>=0) {
                /* we are in the map, so copy away! */
                mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
@@ -549,7 +726,7 @@ void mapDrawWRow(map_view_t *mv, int tx, int ty, word y)
 
        /* the position within the map array */
        i=ty * mv->map->width + tx;
-       for(x=0; x<SCREEN_WIDTH+mv->dxThresh && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {
+       for(x=0; x<mv->page->sw+mv->dxThresh && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {
        if(i>=0) {
                /* we are in the map, so copy! */
                mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
@@ -568,7 +745,7 @@ void mapDrawWCol(map_view_t *mv, int tx, int ty, word x)
 
        /* We'll copy all of the columns in the screen,
           i + 1 row above and one below */
-       for(y=0; y<SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {
+       for(y=0; y<mv->page->sh+mv->dyThresh && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {
        if(i>=0) {
                /* we are in the map, so copy away! */
                mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
@@ -579,110 +756,151 @@ void mapDrawWCol(map_view_t *mv, int tx, int ty, word x)
 
 /*void qclean()
 {
-       modexLeave();
        //setkb(0);
 }*/
 
+unsigned char shinku_fps_indicator_page = 2;
+boolean pageflipflop;
+//gv->video.p
+
 /*     sync    */
-void shinku(page_t *page, global_game_variables_t *gv)
+void shinku(global_game_variables_t *gv)
 {
-       //float t;
-       static byte *pee;
+       word x = (0) + gv->video.page[!(gv->video.p)].dx; // follow the screen
+       word y = (0) + gv->video.page[!(gv->video.p)].dy; // follow the screen
+       word w = 64;
+       word h = 8;
+       word col = 7;
+       word bgcol = 0;
+       word type = 1;
+       byte o,o2,i;
+       //modexClearRegion(&gv->video.page[2], 0, 0, gv->video.page[2].sw, gv->video.page[2].sh, 47);
+       //modexClearRegion(&gv->video.page[3], 0, 0, gv->video.page[3].sw, gv->video.page[3].sh, 45);
+       //modexCopyPageRegion(pip[1].page, pip[2].page, 16, 16, 16, 16, (14*8)+4, 8+4);
+       /* block copy to visible RAM from offscreen */
+//     vga_setup_wm1_block_copy();
+//     modexCopyPageRegion(&(gv->video.page[shinku_fps_indicator_page]), &(gv->video.page[!shinku_fps_indicator_page]), x, y, x+w, 0, w, h);
+//     o =     *(gv->video.page[2].data); // source offscreen
+//     o2 =    *(gv->video.page[shinku_fps_indicator_page].data)+(y * vga_state.vga_stride) + (x >> 2); // dest visible (original stride)
+//     for (i=0;i < h;i++,o += vga_state.vga_draw_stride,o2 += vga_state.vga_stride) vga_wm1_mem_block_copy(o2,o,w >> 2);
+       /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
+//     vga_restore_rm0wm0();
        if(elapsed_timer(gv) >= (1.0 / gv->kurokku.frames_per_second))
        {
-               word x = (16);
-               word y = (8+16);
-               word col = 15;
-               word bgcol = 0;
-               word type = 1;
-               pee = _fmalloc(sizeof(byte)*2);
-               //t=(((*(gv->clock))-gv->clock_start) /18.2);
-               sprintf(pee, "%f", (double)gv->kurokku.tiku/ticktock(gv));
-               modexprint(page, 16, 16, 1, 15, 0, pee);
-               //(gv->clock_start)=*(gv->clock);
+               sprintf(gv->pee, "%.0f fps", (double)gv->kurokku.tiku/ticktock(gv));
+               //modexClearRegion(&(gv->video.page[shinku_fps_indicator_page]), x, y, w, h, 45);
+               modexprint(&(gv->video.page[!(gv->video.p)]), x, y, type, col, bgcol, gv->pee);
                gv->kurokku.tiku=0;
-       }
-       //modexprint(page, 16, 16, 1, 15, 0, pee);
+               /* block copy to visible RAM from offscreen */
+//             vga_setup_wm1_block_copy();
+//             o =     *(gv->video.page[shinku_fps_indicator_page].data); // source offscreen
+//             o2 =    *(gv->video.page[2].data)+(y * vga_state.vga_stride) + (x >> 2); // dest visible (original stride)
+//             for (i=0;i < h;i++,o += vga_state.vga_draw_stride,o2 += vga_state.vga_stride) vga_wm1_mem_block_copy(o2,o,w >> 2);
+//             modexCopyPageRegion(&(gv->video.page[shinku_fps_indicator_page]), &(gv->video.page[!shinku_fps_indicator_page]), x, y, x, 0, w, h);
+               /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
+//             vga_restore_rm0wm0();
+       }else //copy dat sheet
        gv->kurokku.tiku++;
+
        switch(gv->kurokku.fpscap)
        {
                case 0:
-                       //modexprint(page, 16, 32, 1, 15, 0, "sanic!");
+                       modexprint(&(gv->video.page[shinku_fps_indicator_page]), x, y+8, type, col, bgcol, "sanic!");
                        gv->kurokku.frames_per_second=1;
                break;
                case 1:
                        //turn this off if XT
-                       modexWaitBorder();
+                       //modexWaitBorder();
+                       vga_wait_for_vsync();
                        gv->kurokku.frames_per_second=60;
                break;
        }
+       if(pageflipflop){
+       if(gv->video.r){
+               /* block copy pattern to where we will draw the sprite */
+               vga_setup_wm1_block_copy();
+               //_fmemmove((gv->video.page[(gv->video.p)]).data, (gv->video.page[(!gv->video.p)]).data, gv->video.page[(!gv->video.p)].pagesize);
+               modexCopyPageRegion(&(gv->video.page[(gv->video.p)]), &(gv->video.page[(!gv->video.p)]), 0, 0, 0, 0, gv->video.page[gv->video.p].width, gv->video.page[!gv->video.p].height);
+               /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
+               vga_restore_rm0wm0();
+               modexShowPage(&(gv->video.page[gv->video.p]));
+               gv->video.p=!gv->video.p;
+               gv->video.r=!gv->video.r;
+               gv->video.tickclk = ((*clockw)-gv->video.startclk)/18.2;
+       }
+       }
 }
 
-void near animatePlayer(map_view_t *pip, player_t *player, word playnum, sword scrollswitch)
+void near animatePlayer(map_view_t *pip, player_t *player, word pn, sword scrollswitch)
 {
-       sword x = player[playnum].x;
-       sword y = player[playnum].y;
+       sword x = player[pn].x;
+       sword y = player[pn].y;
 #ifdef SPRITE
        word dire=32; //direction
 #else
        word dire=8; //direction
 #endif
        sword qq; //scroll offset
-       word ls = player[playnum].persist_aniframe;
+       word ls = player[pn].persist_aniframe;
 
        if(scrollswitch==0) qq = 0;
-       else qq = ((player[playnum].q)*(player[playnum].speed));
+       else qq = ((player[pn].q)*(player[pn].speed));
        x-=4;
        y-=TILEWH;
-       switch (player[playnum].d)
+       switch (player[pn].d)
        {
                case 0:
                        //up
-                       dire*=player[playnum].d;
+                       dire*=player[pn].d;
                        y-=qq;
                break;
                case 3:
                        // right
-                       dire*=(player[playnum].d-2);
+                       dire*=(player[pn].d-2);
                        x+=qq;
                break;
                case 2:
                break;
                case 4:
                        //down
-                       dire*=(player[playnum].d-2);
+                       dire*=(player[pn].d-2);
                        y+=qq;
                break;
                case 1:
                        //left
-                       dire*=(player[playnum].d+2);
+                       dire*=(player[pn].d+2);
                        x-=qq;
                break;
        }
 
+       //modexCopyPageRegion(pip[!(pip->video->p)].page, pip[3].page, 0, 128, x-4, y-4, 28, 36);
 #ifdef SPRITE
-#ifdef BMPTYPE
-#define FRAME1 oldDrawBmp(VGA, x, y, &player[playnum].data, 1);
-#define FRAME2 oldDrawBmp(VGA, x, y, &player[playnum].data, 1);
-#define FRAME3 oldDrawBmp(VGA, x, y, &player[playnum].data, 1);
-#define FRAME4 oldDrawBmp(VGA, x, y, &player[playnum].data, 1);
+// #define FRAME1 PBUFSFUN(pip[1].page, x, y, 48, dire, 24, 32,        PLAYERBMPDATA);
+// #define FRAME2 PBUFSFUN(pip[1].page, x, y, 24, dire, 24, 32,        PLAYERBMPDATA);
+// #define FRAME3 PBUFSFUN(pip[1].page, x, y, 0, dire, 24, 32, PLAYERBMPDATA);
+// #define FRAME4 PBUFSFUN(pip[1].page, x, y, 24, dire, 24, 32,        PLAYERBMPDATA);
+#define FRAME1 PBUFSFUN(pip[!(pip->video->p)].page, x, y, 48, dire, 24, 32,    PLAYERBMPDATA);
+#define FRAME2 PBUFSFUN(pip[!(pip->video->p)].page, x, y, 24, dire, 24, 32,    PLAYERBMPDATA);
+#define FRAME3 PBUFSFUN(pip[!(pip->video->p)].page, x, y, 0, dire, 24, 32,     PLAYERBMPDATA);
+#define FRAME4 PBUFSFUN(pip[!(pip->video->p)].page, x, y, 24, dire, 24, 32,    PLAYERBMPDATA);
 #else
-#define FRAME1 modexDrawSpriteRegion(pip[1].page, x, y, 48, dire, 24, 32, &player[playnum].data);
-#define FRAME2 modexDrawSpriteRegion(pip[1].page, x, y, 24, dire, 24, 32, &player[playnum].data);
-#define FRAME3 modexDrawSpriteRegion(pip[1].page, x, y, 0, dire, 24, 32, &player[playnum].data);
-#define FRAME4 modexDrawSpriteRegion(pip[1].page, x, y, 24, dire, 24, 32, &player[playnum].data);
-#endif
-#else
-#define FRAME1 modexClearRegion(pip[1].page, x, y, 24, 32, 2+dire);
-#define FRAME2 modexClearRegion(pip[1].page, x, y, 24, 32, 1+dire);
-#define FRAME3 modexClearRegion(pip[1].page, x, y, 24, 32, dire);
-#define FRAME4 modexClearRegion(pip[1].page, x, y, 24, 32, 1+dire);
-#endif
-       modexCopyPageRegion(pip[1].page, pip[0].page, x-4, y-4, x-4, y-4, 28, 40);
+#define FRAME1 modexClearRegion(pip[!(pip->video->p)].page, x, y, 24, 32, 2+dire);
+#define FRAME2 modexClearRegion(pip[!(pip->video->p)].page, x, y, 24, 32, 1+dire);
+#define FRAME3 modexClearRegion(pip[!(pip->video->p)].page, x, y, 24, 32, dire);
+#define FRAME4 modexClearRegion(pip[!(pip->video->p)].page, x, y, 24, 32, 1+dire);
+       #endif
+       if(!pageflipflop)
+               modexCopyPageRegion(pip[1].page, pip[0].page, x-4, y-4, x-4, y-4, 28, 36);
+//modexCopyPageRegion(page_t *dest, page_t *src, word sx, word sy, word dx, word dy, word width, word height);
+       //modexCopyPageRegion(pip[3].page, pip[!(pip->video->p)].page, x-4, y-4, 0, 128, 28, 36);
+       /*modexCopyPageRegion(pip[pip->video->p].page,
+ pip[!(pip->video->p)].page, x-4, y-4, x-4, y-4, 28, 36);*/
+//     else modexCopyPageRegion(pip[1].page, pip[0].page, x-4, y-4, x-4, y-4, 28, 40);
        if(2>ls && ls>=1) { FRAME1 }else
        if(3>ls && ls>=2) { FRAME2 }else
        if(4>ls && ls>=3) { FRAME3 }else
        if(5>ls && ls>=4) { FRAME4 }
+       pip->video->r=1;
        //TODO: mask copy //modexCopyPageRegion(dest->page, src->page, x-4, y-4, x-4, y-4, 28, 40);
        //modexClearRegion(top->page, 66, 66, 2, 40, 0);
        //modexCopyPageRegion(dest->page, top->page, 66, 66, 66, 66, 2, 40);