Browse code

Cleaned up ppu_read

neauoire authored on 30/09/2021 16:34:50
Showing 2 changed files
... ...
@@ -13,8 +13,9 @@ WITH REGARD TO THIS SOFTWARE.
13 13
 */
14 14
 
15 15
 /*
16
-      fgbg fgbg
17
-byte [0000 0000]
16
+pixel 0001 0002
17
+layer fgbg fgbg
18
+byte  1010 1010
18 19
 */
19 20
 
20 21
 static Uint8 blending[5][16] = {
... ...
@@ -27,42 +28,25 @@ static Uint8 blending[5][16] = {
27 28
 static void
28 29
 ppu_clear(Ppu *p)
29 30
 {
30
-	int x, y;
31
-	for(y = 0; y < p->height; ++y) {
32
-		for(x = 0; x < p->width; ++x) {
33
-			ppu_write(p, 0, x, y, 0);
34
-			ppu_write(p, 1, x, y, 0);
35
-		}
36
-	}
31
+	int row;
32
+	for(row = 0; row < p->height * p->width / 2; ++row)
33
+		p->pixels[row] = 0;
37 34
 }
38 35
 
39 36
 Uint8
40 37
 ppu_read(Ppu *p, Uint16 x, Uint16 y)
41 38
 {
42 39
 	int row = (x + y * p->width) / 0x2;
43
-
44
-	if(x % 2) {
45
-		if(p->pixels[row] & 0x0c) {
46
-			return (p->pixels[row] >> 0x2) & 0x3;
47
-		} else {
48
-			return (p->pixels[row] >> 0x0) & 0x3;
49
-		}
50
-	} else {
51
-		if(p->pixels[row] & 0xc0) {
52
-			return (p->pixels[row] >> 0x6) & 0x3;
53
-		} else {
54
-			return (p->pixels[row] >> 0x4) & 0x3;
55
-		}
56
-	}
57
-
58
-	return 0;
40
+	Uint8 seg = !(x & 0x1) << 2;
41
+	Uint8 byte = p->pixels[row] >> seg;
42
+	return (byte & 0x0c ? (byte >> 2) : byte) & 0x3;
59 43
 }
60 44
 
61 45
 void
62 46
 ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
63 47
 {
64 48
 	int row = (x + y * p->width) / 0x2;
65
-	int original = p->pixels[row];
49
+	Uint8 original = p->pixels[row];
66 50
 	Uint8 next = 0x0;
67 51
 	if(x % 2) {
68 52
 		if(layer) {
... ...
@@ -82,7 +66,8 @@ ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
82 66
 		}
83 67
 	}
84 68
 	p->pixels[row] = next;
85
-	p->reqdraw = 1;
69
+	if(original != next)
70
+		p->reqdraw = 1;
86 71
 }
87 72
 
88 73
 void
... ...
@@ -127,9 +112,7 @@ ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
127 112
 	ppu_clear(p);
128 113
 	p->width = width;
129 114
 	p->height = height;
130
-	p->pixels = realloc(p->bg, p->width * p->height * sizeof(Uint8) * 2);
131
-	p->bg = p->pixels;
132
-	p->fg = p->pixels + (p->width * p->height * sizeof(Uint8));
115
+	p->pixels = realloc(p->pixels, p->width * p->height * sizeof(Uint8) / 2);
133 116
 	ppu_clear(p);
134
-	return p->bg && p->fg;
117
+	return !!p->pixels;
135 118
 }
... ...
@@ -22,7 +22,7 @@ typedef unsigned short Uint16;
22 22
 typedef unsigned int Uint32;
23 23
 
24 24
 typedef struct Ppu {
25
-	Uint8 *bg, *fg, *pixels, reqdraw;
25
+	Uint8 *pixels, reqdraw;
26 26
 	Uint16 width, height;
27 27
 } Ppu;
28 28