aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/gui/WindowContainer.kt
blob: 8dcd37b0662ea11af56b17ada9059a8dba5cd2d1 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gui

import cpu.Cpu
import glm_.vec4.Vec4
import gln.checkError
import imgui.Context
import imgui.ImGui
import imgui.destroy
import imgui.impl.LwjglGL3
import org.lwjgl.opengl.GL11
import uno.glfw.GlfwWindow
import uno.glfw.glfw
import java.io.File

class WindowContainer {

  var cpu: Cpu

  init {
    glfw.init("3.2")
    cpu = Cpu()
    val file = File("src/main/resources/roms/boot-rom.gb")
    val rom = file.readBytes()
    cpu.loadRom(rom)
    //cpu.run()
  }

  val window = GlfwWindow(1280, 720, "KGB - KotlinGameBoy").apply {
    init()
  }


  fun run() {

    // Enable vsync
    glfw.swapInterval = 1

    val ctx = Context()
    LwjglGL3.init(window)
    ImGui.styleColorsDark()

    window.loop(::mainLoop)

    LwjglGL3.shutdown()
    ctx.destroy()

    window.destroy()
    glfw.terminate()

  }

  private fun mainLoop() {

    LwjglGL3.newFrame()

    with(ImGui) {
      paintDebugWindow()
      paintEmulationOutputWindow()
      paintCpuRegisterWindow(cpu.registers)
      paintRunControlWindow(cpu)
      paintRamDumpWindow(cpu.ram)
    }

    gln.glViewport(window.framebufferSize)
    gln.glClearColor(Vec4(0.45f, 0.55f, 0.6f, 1f))
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT)

    ImGui.render()
    LwjglGL3.renderDrawData(ImGui.drawData!!)

    checkError("mainLoop")
  }
}