Skip to content

Commit 6cab199

Browse files
authored
Move graphics inside of Chip8 (#84)
The implementation is pretty stable now, since the rendering was moved outside. This keeps everything a lot simpler.
1 parent d84b37f commit 6cab199

File tree

5 files changed

+17
-44
lines changed

5 files changed

+17
-44
lines changed

app/src/main/java/com/emerjbl/ultra8/chip8/graphics/Chip8Graphics.kt

-25
This file was deleted.

app/src/main/java/com/emerjbl/ultra8/chip8/graphics/SimpleGraphics.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.emerjbl.ultra8.util.LockGuarded
44
import java.util.concurrent.locks.ReentrantLock
55

66

7-
class SimpleGraphics : Chip8Graphics, Chip8Render<SimpleGraphics.Frame> {
7+
class SimpleGraphics {
88
class Frame private constructor(
99
val width: Int,
1010
val height: Int,
@@ -17,7 +17,7 @@ class SimpleGraphics : Chip8Graphics, Chip8Render<SimpleGraphics.Frame> {
1717

1818
private var frame: LockGuarded<Frame> = LockGuarded(ReentrantLock(), lowRes())
1919

20-
override var hires: Boolean = false
20+
var hires: Boolean = false
2121
set(value) {
2222
field = value
2323
frame.update(if (value) hiRes() else lowRes())
@@ -26,11 +26,11 @@ class SimpleGraphics : Chip8Graphics, Chip8Render<SimpleGraphics.Frame> {
2626
private fun lowRes() = Frame(64, 32)
2727
private fun hiRes() = Frame(128, 64)
2828

29-
override fun clear() {
29+
fun clear() {
3030
frame.withLock { it.data.fill(0) }
3131
}
3232

33-
override fun scrollRight() = frame.withLock { frame ->
33+
fun scrollRight() = frame.withLock { frame ->
3434
for (row in 0 until frame.width) {
3535
val rowStart = row * frame.width
3636
val startIndex = rowStart
@@ -41,7 +41,7 @@ class SimpleGraphics : Chip8Graphics, Chip8Render<SimpleGraphics.Frame> {
4141
}
4242
}
4343

44-
override fun scrollLeft() = frame.withLock { frame ->
44+
fun scrollLeft() = frame.withLock { frame ->
4545
for (row in 0 until frame.height) {
4646
val rowStart = row * frame.width
4747
val startIndex = rowStart + 4
@@ -52,23 +52,23 @@ class SimpleGraphics : Chip8Graphics, Chip8Render<SimpleGraphics.Frame> {
5252
}
5353
}
5454

55-
override fun scrollDown(n: Int) = frame.withLock { frame ->
55+
fun scrollDown(n: Int) = frame.withLock { frame ->
5656
val destinationOffset = n * frame.width
5757
val startIndex = 0
5858
val endIndex = (frame.height - n) * frame.width
5959
frame.data.copyInto(frame.data, destinationOffset, startIndex, endIndex)
6060
frame.data.fill(0, 0, n * frame.width)
6161
}
6262

63-
override fun scrollUp(n: Int) = frame.withLock { frame ->
63+
fun scrollUp(n: Int) = frame.withLock { frame ->
6464
val destinationOffset = 0
6565
val startIndex = n * frame.width
6666
val endIndex = frame.width * frame.height
6767
frame.data.copyInto(frame.data, destinationOffset, startIndex, endIndex)
6868
frame.data.fill(0, 0, n * frame.width)
6969
}
7070

71-
override fun putSprite(
71+
fun putSprite(
7272
xBase: Int,
7373
yBase: Int,
7474
data: ByteArray,
@@ -119,7 +119,7 @@ class SimpleGraphics : Chip8Graphics, Chip8Render<SimpleGraphics.Frame> {
119119
return unset
120120
}
121121

122-
override fun nextFrame(lastFrame: Frame?): Frame = frame.withLock { frame ->
122+
fun nextFrame(lastFrame: Frame?): Frame = frame.withLock { frame ->
123123
if (lastFrame?.height == frame.height && lastFrame.width == frame.width) {
124124
frame.data.copyInto(lastFrame.data)
125125
lastFrame

app/src/main/java/com/emerjbl/ultra8/chip8/machine/Chip8.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.emerjbl.ultra8.chip8.machine
22

33
import android.util.Log
44
import com.emerjbl.ultra8.chip8.graphics.Chip8Font
5-
import com.emerjbl.ultra8.chip8.graphics.Chip8Graphics
5+
import com.emerjbl.ultra8.chip8.graphics.SimpleGraphics
66
import com.emerjbl.ultra8.chip8.input.Chip8Keys
77
import com.emerjbl.ultra8.chip8.sound.Chip8Sound
88
import com.emerjbl.ultra8.chip8.sound.Pattern
@@ -61,7 +61,6 @@ sealed class Halt(val pc: Int) {
6161
/** All state of a running Chip8 Machine. */
6262
class Chip8(
6363
private val keys: Chip8Keys,
64-
private val gfx: Chip8Graphics,
6564
private val sound: Chip8Sound,
6665
private val font: Chip8Font,
6766
timeSource: TimeSource,
@@ -140,6 +139,11 @@ class Chip8(
140139

141140
}
142141

142+
private val gfx: SimpleGraphics = SimpleGraphics()
143+
144+
fun nextFrame(frame: SimpleGraphics.Frame?): SimpleGraphics.Frame = gfx.nextFrame(frame)
145+
146+
143147
private val state = State().apply {
144148
font.lo.copyInto(mem, FONT_START)
145149
font.hi.copyInto(mem, HIRES_FONT_START)

app/src/main/java/com/emerjbl/ultra8/ui/viewmodel/Chip8ViewModel.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ class Chip8ViewModel(
4040
private val savedStateHandle: SavedStateHandle
4141
) : ViewModel() {
4242
private val keys = Chip8Keys()
43-
private val gfx = SimpleGraphics()
4443
private fun newMachine(program: ByteArray): Chip8 {
4544
val sound = AudioTrackSynthSound(viewModelScope, 48000)
46-
gfx.hires = false
47-
return Chip8(keys, gfx, sound, StandardChip8Font, TimeSource.Monotonic, program)
45+
return Chip8(keys, sound, StandardChip8Font, TimeSource.Monotonic, program)
4846
}
4947

5048
private var machine = newMachine(byteArrayOf())
@@ -157,7 +155,7 @@ class Chip8ViewModel(
157155
}
158156

159157
fun nextFrame(lastFrame: SimpleGraphics.Frame?): SimpleGraphics.Frame =
160-
gfx.nextFrame(lastFrame)
158+
machine.nextFrame(lastFrame)
161159

162160

163161
companion object {

app/src/test/java/com/emerjbl/ultra8/ScreenThreadTest.kt

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.emerjbl.ultra8
22

3-
import com.emerjbl.ultra8.chip8.graphics.SimpleGraphics
43
import com.emerjbl.ultra8.chip8.graphics.StandardChip8Font
54
import com.emerjbl.ultra8.chip8.input.Chip8Keys
65
import com.emerjbl.ultra8.chip8.machine.Chip8
@@ -25,16 +24,13 @@ class FakeSound : Chip8Sound {
2524

2625
@RunWith(RobolectricTestRunner::class)
2726
class ScreenThreadTest {
28-
private val gfx = SimpleGraphics()
29-
3027
private val runner = Chip8ThreadRunner().apply {
3128
cyclesPerTick = 2000
3229
}
3330

3431
val newMachine = { program: ByteArray ->
3532
Chip8(
3633
Chip8Keys(),
37-
gfx,
3834
FakeSound(),
3935
StandardChip8Font,
4036
TimeSource.Monotonic,

0 commit comments

Comments
 (0)