Skip to content

Commit

Permalink
Experimenting with color_code
Browse files Browse the repository at this point in the history
  • Loading branch information
Baekalfen committed Jan 22, 2024
1 parent 352ed90 commit 6c6390d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions pyboy/core/lcd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ cdef class Renderer:
)
cdef void scanline_sprites(self, LCD, int, uint32_t[:,:], bint) noexcept nogil
cdef void sort_sprites(self, int) noexcept nogil
cdef inline uint8_t color_code(self, uint8_t, uint8_t, uint8_t) noexcept nogil

cdef void clear_cache(self) noexcept nogil
cdef void clear_tilecache0(self) noexcept nogil
Expand Down
27 changes: 20 additions & 7 deletions pyboy/core/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,19 @@ def clear_spritecache1(self):
for i in range(TILES):
self._spritecache1_state[i] = 0

def color_code(self, byte1, byte2, offset):
"""Convert 2 bytes into color code at a given offset.
The colors are 2 bit and are found like this:
Color of the first pixel is 0b10
| Color of the second pixel is 0b01
v v
1 0 0 1 0 0 0 1 <- byte1
0 1 1 1 1 1 0 0 <- byte2
"""
return (((byte2 >> (offset)) & 0b1) << 1) + ((byte1 >> (offset)) & 0b1)

def update_tilecache0(self, lcd, t, bank):
if self._tilecache0_state[t]:
return
Expand All @@ -682,7 +695,7 @@ def update_tilecache0(self, lcd, t, bank):
y = (t*16 + k) // 2

for x in range(8):
colorcode = utils.color_code(byte1, byte2, 7 - x)
colorcode = self.color_code(byte1, byte2, 7 - x)
self._tilecache0[y, x] = colorcode

self._tilecache0_state[t] = 1
Expand All @@ -700,7 +713,7 @@ def update_spritecache0(self, lcd, t, bank):
y = (t*16 + k) // 2

for x in range(8):
colorcode = utils.color_code(byte1, byte2, 7 - x)
colorcode = self.color_code(byte1, byte2, 7 - x)
self._spritecache0[y, x] = colorcode

self._spritecache0_state[t] = 1
Expand All @@ -715,7 +728,7 @@ def update_spritecache1(self, lcd, t, bank):
y = (t*16 + k) // 2

for x in range(8):
colorcode = utils.color_code(byte1, byte2, 7 - x)
colorcode = self.color_code(byte1, byte2, 7 - x)
self._spritecache1[y, x] = colorcode

self._spritecache1_state[t] = 1
Expand Down Expand Up @@ -817,7 +830,7 @@ def update_tilecache0(self, lcd, t, bank):
y = (t*16 + k) // 2

for x in range(8):
self._tilecache0[y, x] = utils.color_code(byte1, byte2, 7 - x)
self._tilecache0[y, x] = self.color_code(byte1, byte2, 7 - x)

self._tilecache0_state[t] = 1

Expand All @@ -835,7 +848,7 @@ def update_tilecache1(self, lcd, t, bank):
y = (t*16 + k) // 2

for x in range(8):
self._tilecache1[y, x] = utils.color_code(byte1, byte2, 7 - x)
self._tilecache1[y, x] = self.color_code(byte1, byte2, 7 - x)

self._tilecache1_state[t] = 1

Expand All @@ -853,7 +866,7 @@ def update_spritecache0(self, lcd, t, bank):
y = (t*16 + k) // 2

for x in range(8):
self._spritecache0[y, x] = utils.color_code(byte1, byte2, 7 - x)
self._spritecache0[y, x] = self.color_code(byte1, byte2, 7 - x)

self._spritecache0_state[t] = 1

Expand All @@ -871,7 +884,7 @@ def update_spritecache1(self, lcd, t, bank):
y = (t*16 + k) // 2

for x in range(8):
self._spritecache1[y, x] = utils.color_code(byte1, byte2, 7 - x)
self._spritecache1[y, x] = self.color_code(byte1, byte2, 7 - x)

self._spritecache1_state[t] = 1

Expand Down

0 comments on commit 6c6390d

Please sign in to comment.