// File: donfreiday[P]gb/main.cpp
// Function: imguiLCD()
void imguiLCD() {
  // Update texture
  // bind the texture again when you want to update it.
  glBindTexture(GL_TEXTURE_2D, g_lcdTexture);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, g_gpu.width, g_gpu.height, 0, GL_RGB,
               GL_UNSIGNED_BYTE, g_gpu.screenData);

  // Set up window flags
  ImGuiWindowFlags windowFlags = 0;
  windowFlags |= ImGuiWindowFlags_NoTitleBar;
  windowFlags |= ImGuiWindowFlags_NoScrollbar;
  windowFlags |= ImGuiWindowFlags_NoCollapse;
  windowFlags |= ImGuiWindowFlags_NoResize;
  windowFlags |= ImGuiWindowFlags_NoMove;

  // Get window dimensions
  float winWidth = ImGui::GetIO().DisplaySize.x;
  float winHeight = ImGui::GetIO().DisplaySize.y;

  // Fullscreen
  if (g_fullscreenLcd) {
    // Scale while maintaining approximate aspect ratio
    // Todo: calculate proper scale factor
    float width = g_gpu.width;
    float height = g_gpu.height;
    while (width * 1.1f < winWidth && height * 1.1f < winHeight) {
      width *= 1.1;
      height *= 1.1;
    }
    ImGui::SetNextWindowSize(ImVec2(width, height));

    // Position centered
    ImGui::SetNextWindowPosCenter();
  }

  // Windowed
  else {
    // Set the window size to Gameboy LCD dimensions
    ImGui::SetNextWindowSize(ImVec2(g_gpu.width * 2, g_gpu.height * 2));
    // Position top right
    ImGui::SetNextWindowPos(ImVec2(winWidth - g_gpu.width * 2, 0.0f));
  }

  // No padding
  ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));

  // Render window
  ImGui::Begin("lcd", nullptr, windowFlags);
  ImGui::Image((void*)g_lcdTexture, ImGui::GetWindowSize());

  // Done, clean up
  ImGui::End();
  ImGui::PopStyleVar();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl 0x3f6cf(%rip), %esi     # 0x96fa0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x78f0
movl 0x2e813(%rip), %ecx     # 0x860f4
movl 0x2e810(%rip), %r8d     # 0x860f8
movl $0xde1, %edi            # imm = 0xDE1
xorl %r9d, %r9d
movl $0x1907, %edx           # imm = 0x1907
leaq 0x2e7e4(%rip), %rax     # 0x860e0
addq $0x1d, %rax
movl %r9d, %esi
movl $0x1907, (%rsp)         # imm = 0x1907
movl $0x1401, 0x8(%rsp)      # imm = 0x1401
movq %rax, 0x10(%rsp)
callq 0x7840
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x4(%rbp)
callq 0xca90
movss (%rax), %xmm0
movss %xmm0, -0x8(%rbp)
callq 0xca90
movss 0x4(%rax), %xmm0
movss %xmm0, -0xc(%rbp)
testb $0x1, 0x3f61e(%rip)     # 0x96f92
je 0x57a39
cvtsi2ssl 0x2e772(%rip), %xmm0 # 0x860f4
movss %xmm0, -0x10(%rbp)
cvtsi2ssl 0x2e769(%rip), %xmm0 # 0x860f8
movss %xmm0, -0x14(%rbp)
movss 0x10060(%rip), %xmm1    # 0x679fc
mulss -0x10(%rbp), %xmm1
movss -0x8(%rbp), %xmm0
xorl %eax, %eax
ucomiss %xmm1, %xmm0
movb %al, -0x6d(%rbp)
jbe 0x579cb
movss 0x10044(%rip), %xmm1    # 0x679fc
mulss -0x14(%rbp), %xmm1
movss -0xc(%rbp), %xmm0
ucomiss %xmm1, %xmm0
seta %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x579d4
jmp 0x57a12
movss -0x10(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1001b(%rip), %xmm1    # 0x67a00
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x10(%rbp)
movss -0x14(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xfffd(%rip), %xmm1     # 0x67a00
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x14(%rbp)
jmp 0x57994
movss -0x10(%rbp), %xmm0
movss -0x14(%rbp), %xmm1
leaq -0x1c(%rbp), %rdi
callq 0x2d280
leaq -0x1c(%rbp), %rdi
xorl %esi, %esi
callq 0xe790
xorl %edi, %edi
callq 0x194a0
jmp 0x57a91
movl 0x2e6b5(%rip), %eax     # 0x860f4
shll %eax
cvtsi2ss %eax, %xmm0
movl 0x2e6ad(%rip), %eax     # 0x860f8
shll %eax
cvtsi2ss %eax, %xmm1
leaq -0x24(%rbp), %rdi
callq 0x2d280
leaq -0x24(%rbp), %rdi
xorl %esi, %esi
callq 0xe790
movss -0x8(%rbp), %xmm0
movl 0x2e684(%rip), %eax     # 0x860f4
shll %eax
cvtsi2ss %eax, %xmm1
subss %xmm1, %xmm0
leaq -0x2c(%rbp), %rdi
xorps %xmm1, %xmm1
callq 0x2d280
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
callq 0x19450
leaq -0x34(%rbp), %rdi
movq %rdi, -0x80(%rbp)
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x2d280
movq -0x80(%rbp), %rsi
movl $0x1, %edi
callq 0x18ae0
movl -0x4(%rbp), %edx
leaq 0x10028(%rip), %rdi     # 0x67ae4
xorl %eax, %eax
movl %eax, %esi
callq 0xe7e0
movl 0x3f4d5(%rip), %eax     # 0x96fa0
movq %rax, -0x78(%rbp)
callq 0x15fa0
movlpd %xmm0, -0x3c(%rbp)
leaq -0x44(%rbp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x2d280
leaq -0x4c(%rbp), %rdi
movss 0x3524(%rip), %xmm1     # 0x5b018
movaps %xmm1, %xmm0
callq 0x2d280
leaq -0x5c(%rbp), %rdi
movss 0x3510(%rip), %xmm3     # 0x5b018
movaps %xmm3, %xmm0
movaps %xmm3, %xmm1
movaps %xmm3, %xmm2
callq 0x2d2b0
leaq -0x6c(%rbp), %rdi
xorps %xmm3, %xmm3
movaps %xmm3, %xmm0
movaps %xmm3, %xmm1
movaps %xmm3, %xmm2
callq 0x2d2b0
movq -0x78(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
leaq -0x44(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
leaq -0x5c(%rbp), %r8
leaq -0x6c(%rbp), %r9
callq 0x1aec0
callq 0xee30
movl $0x1, %edi
callq 0x159f0
addq $0xa0, %rsp
popq %rbp
retq