]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/games/nes/ppu.c
demote libemu to common code
[plan9front.git] / sys / src / games / nes / ppu.c
index 09b875851071e5587c556dfef41b2531badb1a5e..0f7010198b6112111337105a8509c5be09b8e465 100644 (file)
@@ -3,18 +3,17 @@
 #include <thread.h>
 #include <draw.h>
 #include <mouse.h>
+#include "../eui.h"
 #include "dat.h"
 #include "fns.h"
 
 int ppuy, ppux, odd;
-uchar pic[256*240*4*9];
 
 static void
 pixel(int x, int y, int val, int back)
 {
-       int Y;
        union { u8int c[4]; u32int l; } u;
-       u32int *p, l;
+       u32int *p;
        static u8int palred[64] = {
                0x7C, 0x00, 0x00, 0x44, 0x94, 0xA8, 0xA8, 0x88, 
                0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
@@ -50,32 +49,31 @@ pixel(int x, int y, int val, int back)
        u.c[1] = palgreen[val];
        u.c[2] = palred[val];
        u.c[3] = back ? 0 : 0xFF;
-       l = u.l;
-       if(scale == 3){
-               p = ((u32int*)pic) + y * 3 * 3 * 256 + 3 * x;
-               for(Y = 0; Y < 3; Y++){
-                       *p++ = l;
-                       *p++ = l;
-                       *p = l;
-                       p += 3 * 256 - 2;
-               }
-       }else if(scale == 2){
-               p = ((u32int*)pic) + y * 2 * 2 * 256 + 2 * x;
-               *p++ = l;
-               *p = l;
-               p += 2 * 256 - 1;
-               *p++ = l;
-               *p = l;
-       }else{
-               p = ((u32int*)pic) + y * 256 + x;
-               *p = l;
+       p = (u32int *)pic + y * 256 * scale + x * scale;
+       switch(scale){
+       case 16: *p++ = u.l;
+       case 15: *p++ = u.l;
+       case 14: *p++ = u.l;
+       case 13: *p++ = u.l;
+       case 12: *p++ = u.l;
+       case 11: *p++ = u.l;
+       case 10: *p++ = u.l;
+       case 9: *p++ = u.l;
+       case 8: *p++ = u.l;
+       case 7: *p++ = u.l;
+       case 6: *p++ = u.l;
+       case 5: *p++ = u.l;
+       case 4: *p++ = u.l;
+       case 3: *p++ = u.l;
+       case 2: *p++ = u.l;
+       default: *p = u.l;
        }
 }
 
 static int
 iscolor(int x, int y)
 {
-       return pic[y * scale * scale * 256 * 4 + x * scale * 4 + 3] != 0;
+       return pic[(scale * 4) * (y * 256 + x) + 3] != 0;
 }
 
 static int
@@ -165,7 +163,7 @@ drawbg(void)
 }
 
 static void
-drawsprites(void)
+drawsprites(int show)
 {
        uchar *p;
        int big, dx, dy, i, x, cc, pri;
@@ -209,7 +207,7 @@ drawsprites(void)
                x = ppux - 2;
                dx = x - t[0].x;
                if(t0 && dx >= 0 && dx < 8 && ppux != 257){
-                       if((nz & 1) != 0 && iscolor(x, ppuy))
+                       if((nz & 1) != 0 && iscolor(x, ppuy) && show)
                                mem[PPUSTATUS] |= SPRITE0HIT;
                        nz >>= 1;
                }
@@ -227,7 +225,7 @@ drawsprites(void)
                        t[i].r1 >>= 1;
                        t[i].r2 >>= 1;
                }
-               if(cc != -1 && (pri || !iscolor(x, ppuy)))
+               if(cc != -1 && show && (pri || !iscolor(x, ppuy)))
                        pixel(x, ppuy, cc, 0);
        }
        if(ppux == 257){
@@ -252,29 +250,9 @@ drawsprites(void)
 static void
 flush(void)
 {
-       extern Rectangle picr;
-       extern Image *tmp, *bg;
-       extern Mousectl *mc;
-       Mouse m;
-       Point p;
-
-       while(nbrecv(mc->c, &m) > 0)
-               ;
-       if(nbrecvul(mc->resizec) > 0){
-               if(getwindow(display, Refnone) < 0)
-                       sysfatal("resize failed: %r");
-               p = divpt(addpt(screen->r.min, screen->r.max), 2);
-               picr = (Rectangle){subpt(p, Pt(scale * 128, scale * 120)), addpt(p, Pt(scale * 128, scale * 120))};
-               bg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0xCCCCCCFF);
-               draw(screen, screen->r, bg, nil, ZP);
-       }
-       if(tmp){
-               loadimage(tmp, tmp->r, pic, 256*240*4*scale*scale);
-               draw(screen, picr, tmp, nil, ZP);
-       }else
-               loadimage(screen, picr, pic, 256*240*4*scale*scale);
-       flushimage(display, 1);
-       memset(pic, sizeof pic, 0);
+       flushmouse(1);
+       flushscreen();
+       flushaudio(audioout);
 }
 
 void
@@ -287,10 +265,10 @@ ppustep(void)
                mask = mem[PPUMASK];
                if((mask & BGDISP) != 0)
                        drawbg();
-               if(((mask & BGDISP) == 0 && ppux <= 257 || ppux <= 10 && (mask & BG8DISP) == 0) && ppux >= 2)
+               if((((mask & BGDISP) == 0 && ppux <= 257 || ppux < 10 && (mask & BG8DISP) == 0) && ppux >= 2) && ppuy != 261)
                        pixel(ppux - 2, ppuy, ppuread(0x3F00), 1);
-               if((mask & SPRITEDISP) != 0 && ppuy != 261 && (ppux > 10 || (mask & SPRITE8DISP) != 0))
-                       drawsprites();
+               if((mask & SPRITEDISP) != 0 && ppuy != 261)
+                       drawsprites(ppux >= 10 || (mask & SPRITE8DISP) != 0);
                if(ppux == 240 && (mask & SPRITEDISP) != 0)
                        mapper[map](SCAN, 0);
                if(ppuy == 261){
@@ -303,7 +281,7 @@ ppustep(void)
                if(ppux == 1){
                        mem[PPUSTATUS] |= PPUVBLANK;
                        if((mem[PPUCTRL] & PPUNMI) != 0)
-                               nmi = 1;
+                               nmi = 2;
                        flush();
                }
        }