X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=Pico%2FDraw.c;h=c07662495f1ba95a97fd46a4d5ac8a5c497e0d64;hb=c060a9ab9c428e1ed9c4159b56529a2a36031e44;hp=d993bea5659c829436f72388625a2d22bad1c7ac;hpb=97a7f774d376d0ff9f65643c7b780772cb8449b7;p=picodrive.git diff --git a/Pico/Draw.c b/Pico/Draw.c index d993bea..c076624 100644 --- a/Pico/Draw.c +++ b/Pico/Draw.c @@ -36,19 +36,21 @@ static int HighCacheA[41+1]; // caches for high layers static int HighCacheB[41+1]; int HighPreSpr[80*2+1]; // slightly preprocessed sprites -#define MAX_LINE_SPRITES 30 -unsigned char HighLnSpr[240][2 + MAX_LINE_SPRITES]; // sprite_count, tile_count, [spritep]... +#define MAX_LINE_SPRITES 29 +#define SPRL_HAVE_HI 0x80 // have hi priority sprites +#define SPRL_HAVE_LO 0x40 // *lo* +#define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line +#define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi +unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]... int rendstatus = 0; int DrawScanline = 0; +int PicoDrawMask = -1; static int skip_next_line=0; //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c }; -static void (*DrawAllSpritesLoPri)(int prio, int sh) = NULL; -static void (*DrawAllSpritesHiPri)(int prio, int sh) = NULL; - struct TileStrip { int nametab; // Position in VRAM of name table (for this tile line) @@ -63,9 +65,9 @@ struct TileStrip #ifdef _ASM_DRAW_C void DrawWindow(int tstart, int tend, int prio, int sh); void BackFill(int reg7, int sh); -void DrawAllSprites(int prio, int sh); +void DrawAllSprites(unsigned char *sprited, int prio, int sh); void DrawTilesFromCache(int *hc, int sh, int rlim); -void DrawSpritesSHi(int prio, int sh); +void DrawSpritesSHi(unsigned char *sprited); void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells); void FinalizeLineBGR444(int sh); void FinalizeLineRGB555(int sh); @@ -772,16 +774,16 @@ static void DrawAllSpritesInterlace(int pri, int sh) #ifndef _ASM_DRAW_C // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8 -static void DrawSpritesSHi(int prio, int sh) +static void DrawSpritesSHi(unsigned char *sprited) { int (*fTileFunc)(int sx,int addr,int pal); unsigned char *p; int cnt; - cnt = HighLnSpr[DrawScanline][0] & 0x7f; + cnt = sprited[0] & 0x7f; if (cnt == 0) return; - p = &HighLnSpr[DrawScanline][2]; + p = &sprited[3]; // Go through sprites backwards: for (cnt--; cnt >= 0; cnt--) @@ -794,7 +796,7 @@ static void DrawSpritesSHi(int prio, int sh) code = sprite[1]; pal = (code>>9)&0x30; - if (sh && pal == 0x30) + if (pal == 0x30) { if (code & 0x8000) // hi priority { @@ -840,16 +842,16 @@ static void DrawSpritesSHi(int prio, int sh) } #endif -static void DrawSpritesHiAS(int prio, int sh) +static void DrawSpritesHiAS(unsigned char *sprited, int sh) { int (*fTileFunc)(int sx,int addr,int pal); unsigned char *p; int entry, cnt, sh_cnt = 0; - cnt = HighLnSpr[DrawScanline][0] & 0x7f; + cnt = sprited[0] & 0x7f; if (cnt == 0) return; - p = &HighLnSpr[DrawScanline][2]; + p = &sprited[3]; // Go through sprites: for (entry = 0; entry < cnt; entry++) @@ -908,7 +910,7 @@ static void DrawSpritesHiAS(int prio, int sh) } } - if (!sh) return; + if (!sh || !(sprited[1]&SPRL_MAY_HAVE_OP)) return; /* nasty 1: remove 'sprite' flags */ { @@ -920,8 +922,8 @@ static void DrawSpritesHiAS(int prio, int sh) } /* nasty 2: sh operator pass */ - HighLnSpr[DrawScanline][0] = sh_cnt; - DrawSpritesSHi(1, 1); + sprited[0] = sh_cnt; + DrawSpritesSHi(sprited); } @@ -934,7 +936,7 @@ static void DrawSpritesHiAS(int prio, int sh) void PrepareSprites(int full) { struct PicoVideo *pvid=&Pico.video; - int u,link=0; + int u,link=0,sh; int table=0; int *pd = HighPreSpr; int max_lines = 224, max_sprites = 80, max_width = 328; @@ -946,6 +948,7 @@ void PrepareSprites(int full) max_line_sprites = MAX_LINE_SPRITES; if (pvid->reg[1]&8) max_lines = 240; + sh = Pico.video.reg[0xC]&8; // shadow/hilight? table=pvid->reg[5]&0x7f; if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode @@ -981,15 +984,15 @@ void PrepareSprites(int full) if (cnt >= max_line_sprites) continue; // sprite limit? for (i = 0; i < cnt; i++) - if (((HighLnSpr[y][2+i] ^ entry) & 0x7f) == 0) goto found; + if (((HighLnSpr[y][3+i] ^ entry) & 0x7f) == 0) goto found; // this sprite was previously missing - HighLnSpr[y][2+cnt] = entry; + HighLnSpr[y][3+cnt] = entry; HighLnSpr[y][0] = cnt + 1; found:; if (entry & 0x80) - rendstatus |= PDRAW_HAVE_HI_SPR; - else rendstatus |= PDRAW_HAVE_LO_SPR; + HighLnSpr[y][1] |= SPRL_HAVE_HI; + else HighLnSpr[y][1] |= SPRL_HAVE_LO; } } @@ -1011,7 +1014,6 @@ found:; { unsigned int *sprite; int code, code2, sx, sy, hv, height, width; - int sx_min; sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite @@ -1025,36 +1027,44 @@ found:; code2 = sprite[1]; sx = (code2>>16)&0x1ff; sx -= 0x78; // Get X coordinate + 8 - sx_min = 8-(width<<3); if (sy < max_lines && sy + (height<<3) > DrawScanline) // sprite onscreen (y)? { - int y = (sy >= DrawScanline) ? sy : DrawScanline; - int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80); + int entry, y, sx_min, onscr_x, maybe_op = 0; + + sx_min = 8-(width<<3); + onscr_x = sx_min < sx && sx < max_width; + if (sh && (code2 & 0x6000) == 0x6000) + maybe_op = SPRL_MAY_HAVE_OP; + + entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80); + y = (sy >= DrawScanline) ? sy : DrawScanline; for (; y < sy + (height<<3) && y < max_lines; y++) { - int cnt = HighLnSpr[y][0]; + unsigned char *p = &HighLnSpr[y][0]; + int cnt = p[0]; if (cnt >= max_line_sprites) continue; // sprite limit? - if (HighLnSpr[y][1] >= max_line_sprites*2) { // tile limit? - HighLnSpr[y][0] |= 0x80; + if (p[2] >= max_line_sprites*2) { // tile limit? + p[0] |= 0x80; continue; } - HighLnSpr[y][1] += width; + p[2] += width; if (sx == -0x78) { if (cnt > 0) - HighLnSpr[y][0] |= 0x80; // masked, no more sprites for this line + p[0] |= 0x80; // masked, no more sprites for this line continue; } // must keep the first sprite even if it's offscreen, for masking - if (cnt > 0 && (sx <= sx_min || sx >= max_width)) continue; // offscreen x - - HighLnSpr[y][2+cnt] = entry; - HighLnSpr[y][0] = cnt + 1; - if (entry & 0x80) - rendstatus |= PDRAW_HAVE_HI_SPR; - else rendstatus |= PDRAW_HAVE_LO_SPR; + if (cnt > 0 && !onscr_x) continue; // offscreen x + + p[3+cnt] = entry; + p[0] = cnt + 1; + p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO; + p[1] |= maybe_op; // there might be op sprites on this line + if (cnt > 0 && (code2 & 0x8000) && !(p[3+cnt-1]&0x80)) + p[1] |= SPRL_LO_ABOVE_HI; } } @@ -1071,9 +1081,9 @@ found:; for (u = 0; u < max_lines; u++) { int y; - printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][1]); + printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][2]); for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++) - printf(" %i", HighLnSpr[u][y+2]); + printf(" %i", HighLnSpr[u][y+3]); printf("\n"); } #endif @@ -1081,9 +1091,9 @@ found:; } #ifndef _ASM_DRAW_C -static void DrawAllSprites(int prio, int sh) +static void DrawAllSprites(unsigned char *sprited, int prio, int sh) { - int rs = rendstatus, scan = DrawScanline; + int rs = rendstatus; unsigned char *p; int cnt; @@ -1093,10 +1103,10 @@ static void DrawAllSprites(int prio, int sh) rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES); } - cnt = HighLnSpr[scan][0] & 0x7f; + cnt = sprited[0] & 0x7f; if (cnt == 0) return; - p = &HighLnSpr[scan][2]; + p = &sprited[3]; // Go through sprites backwards: for (cnt--; cnt >= 0; cnt--) @@ -1287,15 +1297,16 @@ static void DrawBlankedLine(void) PicoScanEnd(DrawScanline); } -static int DrawDisplay(int sh, int as) +static int DrawDisplay(int sh) { + unsigned char *sprited = &HighLnSpr[DrawScanline][0]; struct PicoVideo *pvid=&Pico.video; int win=0,edge=0,hvwind=0; - int maxw, maxcells; + int maxw,maxcells; rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO); - if(pvid->reg[12]&1) { + if (pvid->reg[12]&1) { maxw = 328; maxcells = 40; } else { maxw = 264; maxcells = 32; @@ -1308,7 +1319,8 @@ static int DrawDisplay(int sh, int as) if (win&0x80) { if (DrawScanline>=edge) hvwind=1; } else { if (DrawScanline< edge) hvwind=1; } - if (!hvwind) { // we might have a vertical window here + if (!hvwind) // we might have a vertical window here + { win=pvid->reg[0x11]; edge=win&0x1f; if (win&0x80) { @@ -1321,28 +1333,48 @@ static int DrawDisplay(int sh, int as) } } - DrawLayer(1|((sh|as)<<1), HighCacheB, 0, maxcells); - if (hvwind == 1) - DrawWindow(0, maxcells>>1, 0, sh|as); + /* - layer B low - */ + if (PicoDrawMask & PDRAW_LAYERB_ON) + DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells); + /* - layer A low - */ + if (!(PicoDrawMask & PDRAW_LAYERA_ON)); + else if (hvwind == 1) + DrawWindow(0, maxcells>>1, 0, sh); else if (hvwind == 2) { - // ahh, we have vertical window - DrawLayer(0|((sh|as)<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells); - DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh|as); + DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells); + DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh); } else - DrawLayer(0|((sh|as)<<1), HighCacheA, 0, maxcells); - if (rendstatus & PDRAW_HAVE_LO_SPR) - DrawAllSpritesLoPri(0, sh); - - if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, maxw); - if (hvwind == 1) + DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells); + /* - sprites low - */ + if (!(PicoDrawMask & PDRAW_SPRITES_LOW_ON)); + else if (rendstatus & PDRAW_INTERLACE) + DrawAllSpritesInterlace(0, sh); + else if (sprited[1] & SPRL_HAVE_LO) + DrawAllSprites(sprited, 0, sh); + + /* - layer B hi - */ + if ((PicoDrawMask & PDRAW_LAYERB_ON) && HighCacheB[0]) + DrawTilesFromCache(HighCacheB, sh, maxw); + /* - layer A hi - */ + if (!(PicoDrawMask & PDRAW_LAYERA_ON)); + else if (hvwind == 1) DrawWindow(0, maxcells>>1, 1, sh); else if (hvwind == 2) { - if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw); + if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw); DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh); } else if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, maxw); - if ((rendstatus & PDRAW_HAVE_HI_SPR) || sh) - DrawAllSpritesHiPri(1, sh); + /* - sprites hi - */ + if (!(PicoDrawMask & PDRAW_SPRITES_HI_ON)); + else if (rendstatus & PDRAW_INTERLACE) + DrawAllSpritesInterlace(1, sh); + // AS on and have both lo/hi sprites and lo before hi sprites? + else if ((sprited[1] & 0xd0) == 0xd0 && (rendstatus & PDRAW_ACC_SPRITES)) + DrawSpritesHiAS(sprited, sh); + else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP)) + DrawSpritesSHi(sprited); + else if (sprited[1] & SPRL_HAVE_HI) + DrawAllSprites(sprited, 1, 0); #if 0 { @@ -1360,28 +1392,11 @@ static int DrawDisplay(int sh, int as) PICO_INTERNAL void PicoFrameStart(void) { // prepare to do this frame - int sh = Pico.video.reg[0xC] & 8; // shadow/hilight? rendstatus = 0; if (PicoOpt & POPT_ACC_SPRITES) rendstatus |= PDRAW_ACC_SPRITES; - - if (sh) - { - DrawAllSpritesLoPri = DrawAllSprites; - DrawAllSpritesHiPri = DrawSpritesSHi; - } - else - { - DrawAllSpritesLoPri = - DrawAllSpritesHiPri = DrawAllSprites; - } - if (rendstatus & PDRAW_ACC_SPRITES) - DrawAllSpritesHiPri = DrawSpritesHiAS; - if ((Pico.video.reg[12]&6) == 6) { + if ((Pico.video.reg[12]&6) == 6) rendstatus |= PDRAW_INTERLACE; // interlace mode - DrawAllSpritesLoPri = - DrawAllSpritesHiPri = DrawAllSpritesInterlace; - } if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed @@ -1392,19 +1407,18 @@ PICO_INTERNAL void PicoFrameStart(void) static void PicoLine(void) { - int sh, as = 0; + int sh; if (skip_next_line>0) { skip_next_line--; return; } // skip rendering lines sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight? - if (rendstatus & PDRAW_ACC_SPRITES) as|=1; // accurate sprites if (PicoScanBegin != NULL) skip_next_line = PicoScanBegin(DrawScanline); // Draw screen: - BackFill(Pico.video.reg[7], sh|as); + BackFill(Pico.video.reg[7], sh); if (Pico.video.reg[1]&0x40) - DrawDisplay(sh, as); + DrawDisplay(sh); if (FinalizeLine != NULL) FinalizeLine(sh); @@ -1451,3 +1465,95 @@ void PicoDrawSetColorFormat(int which) #endif } +/* debug and fun */ +#define GREEN1 0x0700 +#ifdef USE_BGR555 + #define YELLOW1 0x071c + #define BLUE1 0xf000 + #define RED1 0x001e +#else + #define YELLOW1 0xe700 + #define BLUE1 0x001e + #define RED1 0xf000 +#endif + +static void set16(unsigned short *p, unsigned short d, int cnt) +{ + while (cnt-- > 0) + *p++ = d; +} + +void PicoDrawShowSpriteStats(unsigned short *screen, int stride) +{ + int lines, i, u, step; + unsigned short *dest; + unsigned char *p; + + step = (320-4*4-1) / MAX_LINE_SPRITES; + lines = 240; + if (!Pico.m.pal || !(Pico.video.reg[1]&8)) + lines = 224, screen += stride*8; + + for (i = 0; i < lines; i++) + { + dest = screen + stride*i; + p = &HighLnSpr[i][0]; + + // sprite graphs + for (u = 0; u < (p[0] & 0x7f); u++) { + set16(dest, (p[3+u] & 0x80) ? YELLOW1 : GREEN1, step); + dest += step; + } + + // flags + dest = screen + stride*i + 320-4*4; + if (p[1] & SPRL_HAVE_LO) set16(dest+4*0, GREEN1, 4); + if (p[1] & SPRL_HAVE_HI) set16(dest+4*1, YELLOW1, 4); + if (p[1] & SPRL_MAY_HAVE_OP) set16(dest+4*2, BLUE1, 4); + if (p[1] & SPRL_LO_ABOVE_HI) set16(dest+4*3, RED1, 4); + } + + // draw grid + for (i = step*5; i <= 320-4*4-1; i += step*5) { + for (u = 0; u < lines; u++) + screen[i + u*stride] = 0x182; + } +} + +void PicoDrawShowPalette(unsigned short *screen, int stride) +{ + unsigned int *spal=(void *)Pico.cram; + unsigned int *dpal=(void *)HighPal; + int x, y, i; + + for (i = 0x3f/2; i >= 0; i--) +#ifdef USE_BGR555 + dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4); +#else + dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7); +#endif + for (i = 0x3f; i >= 0; i--) + HighPal[0x40|i] = (unsigned short)((HighPal[i]>>1)&0x738e); + for (i = 0x3f; i >= 0; i--) { + int t=HighPal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c; + HighPal[0x80|i] = (unsigned short)t; + } + + screen += 16*stride+8; + for (y = 0; y < 8*4; y++) + for (x = 0; x < 8*16; x++) + screen[x + y*stride] = HighPal[x/8 + (y/8)*16]; + + screen += 160; + for (y = 0; y < 8*4; y++) + for (x = 0; x < 8*16; x++) + screen[x + y*stride] = HighPal[(x/8 + (y/8)*16) | 0x40]; + + screen += stride*48; + for (y = 0; y < 8*4; y++) + for (x = 0; x < 8*16; x++) + screen[x + y*stride] = HighPal[(x/8 + (y/8)*16) | 0x80]; + + Pico.m.dirtyPal = 1; +} +