Advertisement
MoonWatcherMD

MD/G Scroll - scroll.c

Dec 22nd, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include "../include/main.h"
  2.  
  3. #define _RETURN(x)         if ( !(x) ) return;
  4. #define _RETURN_N(x,ret)   if ( !(x) ) return (ret);
  5. #define _AB_PLAN(x)        u8 _plan = ( x == APLAN ) ? 0 : 1;
  6.  
  7. #define SCR  _scroll [ _plan ]
  8. #define RES  _scroll [ _plan ].res
  9.  
  10. #define COLS VDP_getScreenWidth()  / 8
  11. #define ROWS VDP_getScreenHeight() / 8
  12.  
  13.  
  14.  
  15. static SCROLL _scroll[2];
  16.  
  17.  
  18.  
  19. u16 scroll_init ( u16 plan, MAPPY_RES *res, u16 pal )
  20. {
  21.     _AB_PLAN(plan);
  22.  
  23.     SCR.x       = 0;
  24.     SCR.y       = 0;
  25.  
  26.     SCR.res     = res;
  27.     SCR.pal     = pal;
  28.     SCR.vram    = vram_new ( RES->nbTiles );
  29.     SCR.plan    = plan;
  30.     SCR.level   = 0;
  31.  
  32.     SCR.scrollH = 0;
  33.     SCR.scrollV = 0;
  34.  
  35.     SCR.plan_x = 0;
  36.     SCR.plan_y = 0;
  37.  
  38.     //VDP_setPalette ( SCR.pal, (u16*) RES->ptrPal );
  39.     VDP_setPalette ( SCR.pal, palette_black );
  40.     VDP_loadTileData ( (u32*) RES->ptrTiles, SCR.vram, RES->nbTiles, TRUE );
  41.  
  42.     return SCR.vram;
  43. }
  44.  
  45.  
  46.  
  47. void scroll_show ( u16 plan )
  48. {
  49.     _AB_PLAN(plan);
  50.  
  51.  
  52.     s16 i;
  53.  
  54.     for ( i=-1; i<ROWS+1; i++ )
  55.     {
  56.         _row ( SCR, i );
  57.     }
  58.  
  59.     VDP_setHorizontalScroll ( SCR.plan, SCR.x );
  60.     VDP_setVerticalScroll   ( SCR.plan, SCR.y );
  61.  
  62.     VDP_setPalette ( SCR.pal, (u16*) RES->ptrPal );
  63. }
  64.  
  65.  
  66.  
  67. s8 scroll_move_x ( u16 plan, s8 value )
  68. {
  69.     _RETURN_N(value,0);
  70.     _AB_PLAN(plan);
  71.  
  72.     return SCR.scrollH = value ;
  73. }
  74.  
  75.  
  76.  
  77. s8 scroll_move_y ( u16 plan, s8 value )
  78. {
  79.     _RETURN_N(value,0);
  80.     _AB_PLAN(plan);
  81.  
  82.     return SCR.scrollV = value ;
  83. }
  84.  
  85.  
  86.  
  87. void scroll_update ( u16 plan )
  88. {
  89.     _AB_PLAN ( plan );
  90.     _RETURN ( ( SCR.scrollH + SCR.scrollV ) );
  91.  
  92.  
  93.     VDP_setHorizontalScroll ( plan, SCR.x+=SCR.scrollH );
  94.     VDP_setVerticalScroll   ( plan, SCR.y+=SCR.scrollV );
  95.  
  96.  
  97.     s8 inc_x = 0;
  98.     s8 inc_y = 0;
  99.  
  100.  
  101.     if ( SCR.plan_x != -SCR.x/8 ) SCR.plan_x += ( inc_x = ( SCR.scrollH > 0 ) ? -1 : 1 );
  102.     if ( SCR.plan_y != +SCR.y/8 ) SCR.plan_y -= ( inc_y = ( SCR.scrollV > 0 ) ? -1 : 1 );
  103.  
  104.  
  105.     if      ( inc_x < 0 ) _col ( SCR, SCR.plan_x - 1    ) ;
  106.     else if ( inc_x > 0 ) _col ( SCR, SCR.plan_x + COLS ) ;
  107.  
  108.     if      ( inc_y > 0 ) _row ( SCR, SCR.plan_y - 1    ) ;
  109.     else if ( inc_y < 0 ) _row ( SCR, SCR.plan_y + ROWS ) ;
  110.  
  111.  
  112.     SCR.scrollH = 0;
  113.     SCR.scrollV = 0;
  114. }
  115.  
  116.  
  117.  
  118. static void _col ( SCROLL scr, s32 x )
  119. {
  120.     s16  i;
  121.     s32  v1 = _trunc ( x, scr.res->width );
  122.     s32  v2 = _trunc ( x, VDP_getPlanWidth() );
  123.     u16 *mp = (u16*) scr.res->maps [ scr.level ];
  124.     u16  t;
  125.  
  126.     for ( i=-1 ; i<ROWS+1; i++ )
  127.     {
  128.         s32 aux = i + scr.plan_y;
  129.         s32 aux2 = _trunc ( aux, VDP_getPlanHeight() );
  130.  
  131.         t = aux * scr.res->width + v1;
  132.         t = TILE_ATTR_FULL ( scr.pal, 0, 0, 0, mp[t] ) + scr.vram ;
  133.  
  134.         VDP_setTileMap ( scr.plan, t, v2, aux2 ) ;
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. static void _row ( SCROLL scr, s32 y )
  141. {
  142.     s16  i;
  143.     s32  v1 = _trunc ( y, scr.res->height );
  144.     s32  v2 = _trunc ( y, VDP_getPlanHeight() );
  145.     u16 *mp = (u16*) scr.res->maps [ scr.level ];
  146.     u16  t;
  147.  
  148.     for ( i=-1 ; i<COLS+1; i++ )
  149.     {
  150.         s32 aux = i + scr.plan_x;
  151.         s32 aux2 = _trunc ( aux, VDP_getPlanWidth() );
  152.  
  153.         t = v1 * scr.res->width + aux;
  154.         t = TILE_ATTR_FULL ( scr.pal, 0, 0, 0, mp[t] ) + scr.vram ;
  155.  
  156.         VDP_setTileMap ( scr.plan, t, aux2, v2 ) ;
  157.     }
  158. }
  159.  
  160.  
  161.  
  162. static s32 _trunc ( s32 value, s32 max )
  163. {
  164.     volatile register s32 v = value % max;
  165.  
  166.     if ( v < 0 )
  167.         return v + max;
  168.  
  169.     return v;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement