summaryrefslogtreecommitdiff
path: root/composeApp/src/commonMain/kotlin/coffee/liz/abstractionengine/game/Game.kt
blob: 0c3a007960dc71951ea11476a4ecbd24d5ee269e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package coffee.liz.abstractionengine.game

import androidx.compose.foundation.Canvas
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import coffee.liz.ecs.World
import kotlinx.coroutines.isActive
import kotlin.time.Duration.Companion.nanoseconds

/**
 * Manages and paints the [World].
 */
@Composable
fun Game(
    world: World,
    content: @Composable () -> Unit
) {
    var frameCount by remember { mutableStateOf(0) }

    LaunchedEffect(world) {
        var lastUpdate: Long? = null

        while (isActive) {
            withFrameNanos { frameTimeNanos ->
                frameCount++
            }
        }
    }

    content()
}

@Composable
fun GameCanvas(
    world: World,
    renderSystem: Renderer,
    backgroundColor: Color = Color.Transparent,
    modifier: Modifier = Modifier
) {
    Canvas(modifier = modifier) {
        // Clear background
        drawRect(backgroundColor)

        // Render all sprites
        renderSystem.render(world, this)
    }
}