Advertisement
Guest User

Road Scrolling demo(SGDK)

a guest
Feb 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <genesis.h>
  2.  
  3. #include "State_vscroll.h"
  4. #include "../res/gfx.h"
  5. #include "Globals.h"
  6.  
  7.  
  8. typedef struct
  9. {
  10.     s8 color;
  11.     s16 position;
  12.  
  13. } Segment;
  14.  
  15.  
  16. // road segments
  17. #define SEG_COUNT 64
  18. Segment s[SEG_COUNT];
  19.  
  20.  
  21. static int m_Size = 16;
  22.  
  23. // lookup table to index into the road
  24. static int m_Index[112];
  25.  
  26. // v-scroll offset
  27. static int m_Lines[112];
  28.  
  29.  
  30. static int m_Position = 0;
  31. static int m_Offset = 0;
  32. static int m_CurrentSegment = 0;
  33.  
  34.  
  35.  
  36. void State_VScroll_Start()
  37. {
  38.     fix32 size = FIX32(1.0f);
  39.     fix32 amount = FIX32(0.055f);
  40.  
  41.     int i = 111;
  42.     int place = -1;
  43.     do
  44.     {
  45.         m_Index[i] = place + fix32ToInt(size);
  46.         place++;
  47.  
  48.         if (place > 20)
  49.         {
  50.             size = fix32Add(size, fix32Mul(size, amount));
  51.         }
  52.         KLog_S2("i ", i, " index ", m_Index[i]);
  53.         i--;
  54.     } while (i >= 0);
  55.  
  56.  
  57.  
  58.     int co = 16;
  59.     int pos = 0;
  60.     for(i=0;i<SEG_COUNT;i++)
  61.     {
  62.         s[i].color = co;
  63.         s[i].position = pos;
  64.        
  65.         co = co == 16? -112 : 16;
  66.        
  67.         pos += m_Size;
  68.     }
  69.  
  70.  
  71.     SYS_disableInts();
  72.  
  73.     // Set palette to black
  74.     VDP_setPaletteColors(0, (u16*)palette_black, 63);
  75.  
  76.     int ind = TILE_USERINDEX;
  77.  
  78.     VDP_drawImageEx(PLAN_B, &gfx_outtest, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 0, 0, FALSE, DMA);
  79.  
  80.     VDP_setPalette(PAL0, gfx_outtest.palette->data);
  81.  
  82.  
  83.     internalHIntCB = &State_HScroll_HInt;
  84.     VDP_setHInterrupt(TRUE);   
  85.     VDP_setHIntCounter(0);
  86.  
  87.     SYS_enableInts();
  88. }
  89.  
  90.  
  91. fix16 inc = FIX16(0.33);
  92.  
  93. void State_VScroll_Update()
  94. {
  95.    
  96.     if (Pad1.Down == PAD_HELD)
  97.     {
  98.         m_Position++;
  99.         m_Offset++;
  100.     }  
  101.  
  102.  
  103.  
  104.     if (m_Offset == m_Size)
  105.     {
  106.         m_CurrentSegment++;
  107.         if (m_CurrentSegment >= SEG_COUNT)
  108.         {
  109.             m_CurrentSegment = 0;
  110.             m_Position = 0;
  111.         }
  112.     }
  113.     int indexSegment = m_CurrentSegment;
  114.     for (int i = 0; i < 112; ++i)
  115.     {
  116.         // % SEG_COUNT
  117.         int index = ((m_Index[i] + m_Position) >> 3) & 0x3F;
  118.         m_Lines[i] = s[index].color;
  119.     }
  120.  
  121.     KLog_S1("fps ", getFPS());
  122. }
  123.  
  124. void State_VScroll_End()
  125. {
  126.  
  127. }
  128.  
  129.  
  130. void State_VScroll_VInt()
  131. {
  132.     VDP_setVerticalScroll(PLAN_B, 256);
  133. }
  134.  
  135.  
  136. void State_HScroll_HInt()
  137. {      
  138.     if (GET_VCOUNTER > 111 && GET_VCOUNTER < 224)
  139.     {
  140.         VDP_setVerticalScroll(PLAN_B, m_Lines[GET_VCOUNTER - 112]);
  141.     }
  142. }
  143.  
  144. SimpleState VScroll_State =
  145. {
  146.     State_VScroll_Start,
  147.     State_VScroll_Update,
  148.     State_VScroll_End,
  149.     State_VScroll_VInt
  150. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement