Skip to content

Commit

Permalink
Fixed fullscreen issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Feb 3, 2024
1 parent 7d4b797 commit e937ea5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
37 changes: 25 additions & 12 deletions game/src/main/scala/hexacraft/game/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ class GameScene(
worldRenderer.onProjMatrixChanged(camera)
}

private def pauseGame(): Unit = {
import PauseMenu.Event.*

pauseMenu = PauseMenu:
case Unpause =>
overlays -= pauseMenu
pauseMenu.unload()
pauseMenu = null
setPaused(false)
case QuitGame =>
eventHandler.notify(GameScene.Event.GameQuit)

overlays += pauseMenu
setPaused(true)
}

private def handleKeyPress(key: KeyboardKey): Unit = key match {
case KeyboardKey.Letter('B') =>
val newCoords = camera.blockCoords.offset(0, -4, 0)
Expand All @@ -133,18 +149,7 @@ class GameScene(
world.setBlock(newCoords, new BlockState(player.blockInHand))
}
case KeyboardKey.Escape =>
import PauseMenu.Event.*

pauseMenu = PauseMenu:
case Unpause =>
overlays -= pauseMenu
pauseMenu.unload()
pauseMenu = null
setPaused(false)
case QuitGame => eventHandler.notify(GameScene.Event.GameQuit)

overlays += pauseMenu
setPaused(true)
pauseGame()
case KeyboardKey.Letter('E') =>
import InventoryBox.Event.*

Expand Down Expand Up @@ -255,6 +260,14 @@ class GameScene(
eventHandler.notify(if invisible then CursorCaptured else CursorReleased)
}

override def windowFocusChanged(focused: Boolean): Unit = {
if !focused then {
if !isPaused then {
pauseGame()
}
}
}

override def windowResized(width: Int, height: Int): Unit = {
val aspectRatio = width.toFloat / height
camera.proj.aspect = aspectRatio
Expand Down
1 change: 1 addition & 0 deletions game/src/main/scala/hexacraft/gui/Scene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hexacraft.gui
import hexacraft.gui.comp.Component

abstract class Scene extends Component {
def windowFocusChanged(focused: Boolean): Unit = ()
def windowResized(w: Int, h: Int): Unit = ()
def frameBufferResized(w: Int, h: Int): Unit = ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ enum CallbackEvent {
case MouseClicked(window: Window, button: MouseButton, action: MouseAction, mods: KeyMods)
case MouseScrolled(window: Window, xOffset: Double, yOffset: Double)
case WindowResized(window: Window, w: Int, h: Int)
case WindowFocusChanged(window: Window, focused: Boolean)
case FrameBufferResized(window: Window, w: Int, h: Int)
}
7 changes: 7 additions & 0 deletions game/src/main/scala/hexacraft/infra/window/Window.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class Window(val id: Window.Id, glfw: GlfwWrapper) {
)
}

def setWindowFocusCallback(callback: CallbackEvent.WindowFocusChanged => Unit): Unit = {
glfw.glfwSetWindowFocusCallback(
id.toLong,
(_, focused) => callback(CallbackEvent.WindowFocusChanged(this, focused))
)
}

def setFrameBufferSizeCallback(callback: CallbackEvent.FrameBufferResized => Unit): Unit = {
glfw.glfwSetFramebufferSizeCallback(
id.toLong,
Expand Down
9 changes: 7 additions & 2 deletions game/src/main/scala/hexacraft/infra/window/WindowSystem.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package hexacraft.infra.window

import hexacraft.util.PointerWrapper

import org.lwjgl.PointerBuffer
import org.lwjgl.glfw.*
import org.lwjgl.system.MemoryUtil
Expand Down Expand Up @@ -66,6 +64,7 @@ class WindowSystem(glfw: GlfwWrapper) {
glfw.glfwDefaultWindowHints()
glfw.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE)
glfw.glfwWindowHint(GLFW.GLFW_RESIZABLE, glfwResizable)
glfw.glfwWindowHint(GLFW.GLFW_AUTO_ICONIFY, GLFW.GLFW_FALSE)
glfw.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, settings.opengl.majorVersion)
glfw.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, settings.opengl.minorVersion)
glfw.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE)
Expand Down Expand Up @@ -109,6 +108,7 @@ trait GlfwWrapper {
def glfwSetCharCallback(window: Long, callback: GLFWCharCallbackI): Unit
def glfwSetMouseButtonCallback(window: Long, callback: GLFWMouseButtonCallbackI): Unit
def glfwSetWindowSizeCallback(window: Long, callback: GLFWWindowSizeCallbackI): Unit
def glfwSetWindowFocusCallback(window: Long, callback: GLFWWindowFocusCallbackI): Unit
def glfwSetFramebufferSizeCallback(window: Long, callback: GLFWFramebufferSizeCallbackI): Unit
def glfwSetScrollCallback(window: Long, callback: GLFWScrollCallbackI): Unit
def glfwGetKey(window: Long, key: Int): Int
Expand Down Expand Up @@ -194,6 +194,10 @@ object RealGlfw extends GlfwWrapper {
GLFW.glfwSetWindowSizeCallback(window, callback)
}

def glfwSetWindowFocusCallback(window: Long, callback: GLFWWindowFocusCallbackI): Unit = {
GLFW.glfwSetWindowFocusCallback(window, callback)
}

def glfwSetFramebufferSizeCallback(window: Long, callback: GLFWFramebufferSizeCallbackI): Unit = {
GLFW.glfwSetFramebufferSizeCallback(window, callback)
}
Expand Down Expand Up @@ -307,6 +311,7 @@ class NullGlfw extends GlfwWrapper {
def glfwSetCharCallback(window: Long, callback: GLFWCharCallbackI): Unit = ()
def glfwSetMouseButtonCallback(window: Long, callback: GLFWMouseButtonCallbackI): Unit = ()
def glfwSetWindowSizeCallback(window: Long, callback: GLFWWindowSizeCallbackI): Unit = ()
def glfwSetWindowFocusCallback(window: Long, callback: GLFWWindowFocusCallbackI): Unit = ()
def glfwSetFramebufferSizeCallback(window: Long, callback: GLFWFramebufferSizeCallbackI): Unit = ()
def glfwSetScrollCallback(window: Long, callback: GLFWScrollCallbackI): Unit = ()
def glfwGetKey(window: Long, key: Int): Int = GLFW.GLFW_RELEASE
Expand Down
4 changes: 4 additions & 0 deletions game/src/main/scala/hexacraft/main/MainWindow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ class MainWindow(isDebug: Boolean, saveFolder: File) extends GameWindow {
resetMousePos()
}

case CallbackEvent.WindowFocusChanged(_, focused) =>
scene.windowFocusChanged(focused)

case CallbackEvent.FrameBufferResized(_, w, h) =>
if w > 0 && h > 0
then {
Expand Down Expand Up @@ -252,6 +255,7 @@ class MainWindow(isDebug: Boolean, saveFolder: File) extends GameWindow {
window.setCharCallback(callbackQueue.enqueue)
window.setMouseButtonCallback(callbackQueue.enqueue)
window.setWindowSizeCallback(callbackQueue.enqueue)
window.setWindowFocusCallback(callbackQueue.enqueue)
window.setScrollCallback(callbackQueue.enqueue)
window.setFrameBufferSizeCallback(callbackQueue.enqueue)

Expand Down

0 comments on commit e937ea5

Please sign in to comment.