more Pandora porting work
[picodrive.git] / Pico / Draw.c
index f20af59..acdd15d 100644 (file)
@@ -36,7 +36,6 @@ static int  HighCacheA[41+1];   // caches for high layers
 static int  HighCacheB[41+1];\r
 int  HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
 \r
-#define MAX_LINE_SPRITES 29\r
 #define SPRL_HAVE_HI     0x80 // have hi priority sprites\r
 #define SPRL_HAVE_LO     0x40 // *lo*\r
 #define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line\r
@@ -65,9 +64,9 @@ struct TileStrip
 #ifdef _ASM_DRAW_C\r
 void DrawWindow(int tstart, int tend, int prio, int sh);\r
 void BackFill(int reg7, int sh);\r
-void DrawAllSprites(int prio, int sh);\r
+void DrawAllSprites(unsigned char *sprited, int prio, int sh);\r
 void DrawTilesFromCache(int *hc, int sh, int rlim);\r
-void DrawSpritesSHi(unsigned short *sprited);\r
+void DrawSpritesSHi(unsigned char *sprited);\r
 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);\r
 void FinalizeLineBGR444(int sh);\r
 void FinalizeLineRGB555(int sh);\r
@@ -958,7 +957,7 @@ void PrepareSprites(int full)
   {\r
     int pack;\r
     // updates: tilecode, sx\r
-    for (u=0; u < max_lines && (pack = *pd); u++, pd+=2)\r
+    for (u=0; u < max_sprites && (pack = *pd); u++, pd+=2)\r
     {\r
       unsigned int *sprite;\r
       int code2, sx, sy, height;\r
@@ -1007,12 +1006,10 @@ found:;
   }\r
   else\r
   {\r
-    int old_prio = 0x8000, lo_above_hi = 0;\r
-\r
     for (u = 0; u < max_lines; u++)\r
       *((int *)&HighLnSpr[u][0]) = 0;\r
 \r
-    for (u = 0; u < max_lines; u++)\r
+    for (u = 0; u < max_sprites; u++)\r
     {\r
       unsigned int *sprite;\r
       int code, code2, sx, sy, hv, height, width;\r
@@ -1039,37 +1036,34 @@ found:;
         if (sh && (code2 & 0x6000) == 0x6000)\r
           maybe_op = SPRL_MAY_HAVE_OP;\r
 \r
-        if (onscr_x && !old_prio && (code2 & 0x8000))\r
-          lo_above_hi = SPRL_LO_ABOVE_HI;\r
-        old_prio = code2 & 0x8000;\r
-\r
         entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
         y = (sy >= DrawScanline) ? sy : DrawScanline;\r
         for (; y < sy + (height<<3) && y < max_lines; y++)\r
         {\r
-          int cnt = HighLnSpr[y][0];\r
+         unsigned char *p = &HighLnSpr[y][0];\r
+          int cnt = p[0];\r
           if (cnt >= max_line_sprites) continue;              // sprite limit?\r
 \r
-          if (HighLnSpr[y][2] >= max_line_sprites*2) {        // tile limit?\r
-            HighLnSpr[y][0] |= 0x80;\r
+          if (p[2] >= max_line_sprites*2) {        // tile limit?\r
+            p[0] |= 0x80;\r
             continue;\r
           }\r
-          HighLnSpr[y][2] += width;\r
+          p[2] += width;\r
 \r
           if (sx == -0x78) {\r
             if (cnt > 0)\r
-              HighLnSpr[y][0] |= 0x80; // masked, no more sprites for this line\r
+              p[0] |= 0x80; // masked, no more sprites for this line\r
             continue;\r
           }\r
           // must keep the first sprite even if it's offscreen, for masking\r
           if (cnt > 0 && !onscr_x) continue; // offscreen x\r
 \r
-          HighLnSpr[y][3+cnt] = entry;\r
-          HighLnSpr[y][0] = cnt + 1;\r
-          if (entry & 0x80)\r
-               HighLnSpr[y][1] |= SPRL_HAVE_HI;\r
-          else HighLnSpr[y][1] |= SPRL_HAVE_LO;\r
-          HighLnSpr[y][1] |= maybe_op|lo_above_hi; // there might be op sprites or priority mess on this line\r
+          p[3+cnt] = entry;\r
+          p[0] = cnt + 1;\r
+          p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO;\r
+          p[1] |= maybe_op; // there might be op sprites on this line\r
+          if (cnt > 0 && (code2 & 0x8000) && !(p[3+cnt-1]&0x80))\r
+            p[1] |= SPRL_LO_ABOVE_HI;\r
         }\r
       }\r
 \r
@@ -1144,6 +1138,37 @@ static void BackFill(int reg7, int sh)
 \r
 unsigned short HighPal[0x100];\r
 \r
+void PicoDoHighPal555(int sh)\r
+{\r
+  unsigned short *pal=HighPal;\r
+  int i, t;\r
+\r
+  Pico.m.dirtyPal = 0;\r
+\r
+  {\r
+    unsigned int *spal=(void *)Pico.cram;\r
+    unsigned int *dpal=(void *)HighPal;\r
+    for (i = 0x3f/2; i >= 0; i--)\r
+#ifdef USE_BGR555\r
+      dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);\r
+#else\r
+      dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);\r
+#endif\r
+  }\r
+\r
+  if (sh)\r
+  {\r
+    // shadowed pixels\r
+    for (i = 0x3f; i >= 0; i--)\r
+      pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);\r
+    // hilighted pixels\r
+    for (i = 0x3f; i >= 0; i--) {\r
+      t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;\r
+      pal[0x80|i]=(unsigned short)t;\r
+    }\r
+  }\r
+}\r
+\r
 #ifndef _ASM_DRAW_C\r
 static void FinalizeLineBGR444(int sh)\r
 {\r
@@ -1188,34 +1213,10 @@ static void FinalizeLineRGB555(int sh)
   unsigned short *pd=DrawLineDest;\r
   unsigned char  *ps=HighCol+8;\r
   unsigned short *pal=HighPal;\r
-  int len, i, t, dirtyPal = Pico.m.dirtyPal;\r
-\r
-  if (dirtyPal)\r
-  {\r
-    unsigned int *spal=(void *)Pico.cram;\r
-    unsigned int *dpal=(void *)HighPal;\r
-    for (i = 0x3f/2; i >= 0; i--)\r
-#ifdef USE_BGR555\r
-      dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);\r
-#else\r
-      dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);\r
-#endif\r
-    Pico.m.dirtyPal = 0;\r
-  }\r
+  int len;\r
 \r
-  if (sh)\r
-  {\r
-    if (dirtyPal) {\r
-      // shadowed pixels\r
-      for (i = 0x3f; i >= 0; i--)\r
-        pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);\r
-      // hilighted pixels\r
-      for (i = 0x3f; i >= 0; i--) {\r
-        t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;\r
-        pal[0x80|i]=(unsigned short)t;\r
-      }\r
-    }\r
-  }\r
+  if (Pico.m.dirtyPal)\r
+    PicoDoHighPal555(sh);\r
 \r
   if (Pico.video.reg[12]&1) {\r
     len = 320;\r
@@ -1226,7 +1227,7 @@ static void FinalizeLineRGB555(int sh)
 \r
   {\r
 #ifndef PSP\r
-    int mask=0xff;\r
+    int i, mask=0xff;\r
     if (!sh && (rendstatus & PDRAW_ACC_SPRITES))\r
       mask=0x3f; // accurate sprites, upper bits are priority stuff\r
 \r
@@ -1302,7 +1303,7 @@ static void DrawBlankedLine(void)
     PicoScanEnd(DrawScanline);\r
 }\r
 \r
-static int DrawDisplay(int sh, int as)\r
+static int DrawDisplay(int sh)\r
 {\r
   unsigned char *sprited = &HighLnSpr[DrawScanline][0];\r
   struct PicoVideo *pvid=&Pico.video;\r
@@ -1374,7 +1375,7 @@ static int DrawDisplay(int sh, int as)
   else if (rendstatus & PDRAW_INTERLACE)\r
     DrawAllSpritesInterlace(1, sh);\r
   // AS on and have both lo/hi sprites and lo before hi sprites?\r
-  else if (as && (sprited[1] & 0xd0) == 0xd0)\r
+  else if ((sprited[1] & 0xd0) == 0xd0 && (rendstatus & PDRAW_ACC_SPRITES))\r
     DrawSpritesHiAS(sprited, sh);\r
   else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))\r
     DrawSpritesSHi(sprited);\r
@@ -1412,19 +1413,18 @@ PICO_INTERNAL void PicoFrameStart(void)
 \r
 static void PicoLine(void)\r
 {\r
-  int sh, as = 0;\r
+  int sh;\r
   if (skip_next_line>0) { skip_next_line--; return; } // skip rendering lines\r
 \r
   sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
-  if (rendstatus & PDRAW_ACC_SPRITES) as|=1; // accurate sprites\r
 \r
   if (PicoScanBegin != NULL)\r
     skip_next_line = PicoScanBegin(DrawScanline);\r
 \r
   // Draw screen:\r
-  BackFill(Pico.video.reg[7], sh|as);\r
+  BackFill(Pico.video.reg[7], sh);\r
   if (Pico.video.reg[1]&0x40)\r
-    DrawDisplay(sh, as);\r
+    DrawDisplay(sh);\r
 \r
   if (FinalizeLine != NULL)\r
     FinalizeLine(sh);\r
@@ -1471,87 +1471,3 @@ void PicoDrawSetColorFormat(int which)
 #endif\r
 }\r
 \r
-/* debug and fun */\r
-static void set16(unsigned short *p, unsigned short d, int cnt)\r
-{\r
-  while (cnt-- > 0)\r
-    *p++ = d;\r
-}\r
-\r
-void PicoDrawShowSpriteStats(unsigned short *screen)\r
-{\r
-  int lines, i, u, step;\r
-  unsigned short *dest;\r
-  unsigned char *p;\r
-\r
-  memset(screen, 0, 320*240*2);\r
-  step = (320-4*4-1) / MAX_LINE_SPRITES;\r
-  lines = 240;\r
-  if (!Pico.m.pal || !(Pico.video.reg[1]&8))\r
-    lines = 224, screen += 320*8;\r
-\r
-  for (i = 0; i < lines; i++)\r
-  {\r
-    dest = screen + 320*i;\r
-    p = &HighLnSpr[i][0];\r
-\r
-    // sprite graphs\r
-    for (u = 0; u < (p[0] & 0x7f); u++) {\r
-      set16(dest, (p[3+u] & 0x80) ? 0xe700 : 0x0700, step);\r
-      dest += step;\r
-    }\r
-\r
-    // flags\r
-    dest = screen + 320*i + 320-4*4;\r
-    if (p[1] & SPRL_HAVE_LO)     set16(dest+4*0, 0x0700, 4);\r
-    if (p[1] & SPRL_HAVE_HI)     set16(dest+4*1, 0xe700, 4);\r
-    if (p[1] & SPRL_MAY_HAVE_OP) set16(dest+4*2, 0x001e, 4);\r
-    if (p[1] & SPRL_LO_ABOVE_HI) set16(dest+4*3, 0xf000, 4);\r
-  }\r
-\r
-  // draw grid\r
-  for (i = step*5; i <= 320-4*4-1; i += step*5) {\r
-    for (u = 0; u < lines; u++)\r
-      screen[i + u*320] = 0x182;\r
-  }\r
-}\r
-\r
-void PicoDrawShowPalette(unsigned short *screen)\r
-{\r
-  unsigned int *spal=(void *)Pico.cram;\r
-  unsigned int *dpal=(void *)HighPal;\r
-  int x, y, i;\r
-\r
-  memset(screen, 0, 320*240*2);\r
-\r
-  for (i = 0x3f/2; i >= 0; i--)\r
-#ifdef USE_BGR555\r
-    dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);\r
-#else\r
-    dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);\r
-#endif\r
-  for (i = 0x3f; i >= 0; i--)\r
-    HighPal[0x40|i] = (unsigned short)((HighPal[i]>>1)&0x738e);\r
-  for (i = 0x3f; i >= 0; i--) {\r
-    int t=HighPal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;\r
-    HighPal[0x80|i]=(unsigned short)t;\r
-  }\r
-\r
-  screen += 16*320+8;\r
-  for (y = 0; y < 8*4; y++)\r
-    for (x = 0; x < 8*16; x++)\r
-      screen[x + y*320] = HighPal[x/8 + (y/8)*16];\r
-\r
-  screen += 160;\r
-  for (y = 0; y < 8*4; y++)\r
-    for (x = 0; x < 8*16; x++)\r
-      screen[x + y*320] = HighPal[(x/8 + (y/8)*16) | 0x40];\r
-\r
-  screen += 320*48;\r
-  for (y = 0; y < 8*4; y++)\r
-    for (x = 0; x < 8*16; x++)\r
-      screen[x + y*320] = HighPal[(x/8 + (y/8)*16) | 0x80];\r
-\r
-  Pico.m.dirtyPal = 1;\r
-}\r
-\r