Browse code

Do not overdraw

neauoire authored on 01/10/2021 02:35:22
Showing 1 changed files
... ...
@@ -41,26 +41,31 @@ ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
41 41
 Uint8
42 42
 ppu_read(Ppu *p, Uint16 x, Uint16 y)
43 43
 {
44
-	Uint32 row = (x + y * p->width) / 0x2;
45
-	Uint8 shift = !(x & 0x1) << 2;
46
-	Uint8 pix = p->pixels[row] >> shift;
47
-	if(pix & 0x0c)
48
-		pix = pix >> 2;
49
-	return pix & 0x3;
44
+	if(x < p->width && y < p->height) {
45
+		Uint32 row = (x + y * p->width) / 0x2;
46
+		Uint8 shift = !(x & 0x1) << 2;
47
+		Uint8 pix = p->pixels[row] >> shift;
48
+		if(pix & 0x0c)
49
+			pix = pix >> 2;
50
+		return pix & 0x3;
51
+	}
52
+	return 0x0;
50 53
 }
51 54
 
52 55
 void
53 56
 ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
54 57
 {
55
-	Uint32 row = (x + y * p->width) / 0x2;
56
-	Uint8 shift = (!(x & 0x1) << 2) + (layer << 1);
57
-	Uint8 pix = p->pixels[row];
58
-	Uint8 mask = ~(0x3 << shift);
59
-	Uint8 pixnew = (pix & mask) + (color << shift);
60
-	if(x < p->width && y < p->height)
61
-		p->pixels[row] = pixnew;
62
-	if(pix != pixnew)
63
-		p->reqdraw = 1;
58
+	if(x < p->width && y < p->height) {
59
+		Uint32 row = (x + y * p->width) / 0x2;
60
+		Uint8 shift = (!(x & 0x1) << 2) + (layer << 1);
61
+		Uint8 pix = p->pixels[row];
62
+		Uint8 mask = ~(0x3 << shift);
63
+		Uint8 pixnew = (pix & mask) + (color << shift);
64
+		if(x < p->width && y < p->height)
65
+			p->pixels[row] = pixnew;
66
+		if(pix != pixnew)
67
+			p->reqdraw = 1;
68
+	}
64 69
 }
65 70
 
66 71
 void