Skip to content

Commit

Permalink
Read stick state from events to cut logging noise
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Jun 18, 2024
1 parent e28dedc commit 2fdf451
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 27 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import java.util.concurrent.Callable
class Explorer implements Callable<Integer> {

static {
System.setProperty('logback.configurationFile', 'logback-explorer.xml')
System.setProperty('logback.configurationFile', 'logback-application.xml')
}

@Spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import java.util.concurrent.Callable
@Command(name = 'shooter')
class Shooter implements Callable<Integer> {

static {
System.setProperty('logback.configurationFile', 'logback-application.xml')
}

@Spec
CommandSpec commandSpec

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Application implements EventTarget {
inputEventStream.addInputSource(event.window)
}
graphicsSystem.on(SystemReadyEvent) { event ->
graphicsSystem.imGuiLayer.addOverlay(new DebugOverlay(config.debug).toggleWith(inputEventStream, GLFW_KEY_D))
graphicsSystem.imGuiLayer.addOverlay(new DebugOverlay(inputEventStream, config.debug).toggleWith(inputEventStream, GLFW_KEY_D))
graphicsSystem.imGuiLayer.addOverlay(new ControlsOverlay(inputEventStream).toggleWith(inputEventStream, GLFW_KEY_C))
uiElements.each { overlayRenderPass ->
graphicsSystem.imGuiLayer.addUiElement(overlayRenderPass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package nz.net.ultraq.redhorizon.engine.graphics.imgui

import nz.net.ultraq.redhorizon.engine.EngineStats
import nz.net.ultraq.redhorizon.engine.graphics.Framebuffer
import nz.net.ultraq.redhorizon.engine.input.GamepadAxisEvent
import nz.net.ultraq.redhorizon.engine.input.InputEventStream

import imgui.ImGui
import imgui.type.ImBoolean
import static imgui.flag.ImGuiWindowFlags.*
import static org.lwjgl.glfw.GLFW.*

/**
* An overlay rendering pass for displaying debug information about the game.
Expand All @@ -36,12 +39,17 @@ class DebugOverlay implements ImGuiElement<DebugOverlay> {
private int debugWindowSizeY = 200
private final EngineStats engineStats

private float leftX
private float leftY
private float rightX
private float rightY

/**
* Constructor, create a new blank overlay. This is made more useful by
* adding the renderers via the {@code add*} methods to get stats on their
* use.
*/
DebugOverlay(boolean enabled) {
DebugOverlay(InputEventStream inputEventStream, boolean enabled) {

ImGuiLoggingAppender.instance?.on(ImGuiLogEvent) { event ->
if (event.persistentKey) {
Expand All @@ -50,6 +58,15 @@ class DebugOverlay implements ImGuiElement<DebugOverlay> {
}
engineStats = EngineStats.instance

inputEventStream.on(GamepadAxisEvent) { event ->
switch (event.type) {
case GLFW_GAMEPAD_AXIS_LEFT_X -> leftX = event.value
case GLFW_GAMEPAD_AXIS_LEFT_Y -> leftY = event.value
case GLFW_GAMEPAD_AXIS_RIGHT_X -> rightX = event.value
case GLFW_GAMEPAD_AXIS_RIGHT_Y -> rightY = event.value
}
}

this.enabled = enabled
}

Expand All @@ -73,6 +90,9 @@ class DebugOverlay implements ImGuiElement<DebugOverlay> {
ImGui.text("Active uniform buffers: ${engineStats.activeUniformBuffers}")
ImGui.text("Active sources: ${engineStats.activeSources}")
ImGui.text("Active buffers: ${engineStats.activeBuffers}")
ImGui.text("Gamepad sticks: " +
"(${sprintf('%.2f', leftX)}, ${sprintf('%.2f', leftY)}) " +
"(${sprintf('%.2f', rightX)}, ${sprintf('%.2f', rightY)})")
engineStats.drawCalls.set(0)

ImGui.separator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ class ImGuiLoggingAppender<E> extends UnsynchronizedAppenderBase<E> implements E
persistentKey: eventObject.argumentArray[0]
))
}
else if (eventObject.loggerName.contains('GamepadStateProcessor')) {
trigger(new ImGuiLogEvent(
message: message,
persistentKey: eventObject.message
))
}
else {
trigger(new ImGuiLogEvent(
message: message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package nz.net.ultraq.redhorizon.engine.input
import nz.net.ultraq.redhorizon.engine.graphics.GraphicsSystem

import org.lwjgl.glfw.GLFWGamepadState
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import static org.lwjgl.glfw.GLFW.*

import groovy.transform.TupleConstructor
Expand All @@ -42,8 +40,6 @@ import java.nio.FloatBuffer
@TupleConstructor(defaults = false)
class GamepadStateProcessor {

private static final Logger logger = LoggerFactory.getLogger(GamepadStateProcessor)

final InputEventStream inputEventStream

@Lazy
Expand All @@ -57,31 +53,31 @@ class GamepadStateProcessor {

if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) {
glfwGetGamepadState(GLFW_JOYSTICK_1, gamepadState)

var axes = gamepadState.axes()
processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_X, 'Gamepad left stick X: {}')
processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_Y, 'Gamepad left stick Y: {}')
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_X, 'Gamepad right stick X: {}')
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_Y, 'Gamepad right stick Y: {}')
processAxes(gamepadState.axes())
}

// Gamepad mappings not working on macOS, have to use joystick 😢
else if (glfwJoystickPresent(GLFW_JOYSTICK_1)) {
var axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1)
processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_X, 'Gamepad left stick X: {}')
processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_Y, 'Gamepad left stick Y: {}')
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_X, 'Gamepad right stick X: {}')
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_Y, 'Gamepad right stick Y: {}')
processAxes(glfwGetJoystickAxes(GLFW_JOYSTICK_1))
}
}

/**
* Process a set of axes.
*/
private void processAxes(FloatBuffer axes) {

processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_X)
processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_Y)
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_X)
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_Y)
}

/**
* Process a single axis.
*/
private void processAxis(FloatBuffer axes, int type, String logMessage) {
private void processAxis(FloatBuffer axes, int type) {

var value = axes.get(type)
logger.debug(logMessage, sprintf('%.2f', value))
inputEventStream.trigger(new GamepadAxisEvent(type, value <= -0.2f || 0.2f <= value ? value : 0f))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package nz.net.ultraq.redhorizon.shooter

import nz.net.ultraq.redhorizon.classic.filetypes.IniFile
import nz.net.ultraq.redhorizon.classic.filetypes.MapFile
import nz.net.ultraq.redhorizon.classic.filetypes.PalFile
import nz.net.ultraq.redhorizon.classic.maps.Map
import nz.net.ultraq.redhorizon.classic.nodes.GlobalPalette
import nz.net.ultraq.redhorizon.engine.Application
import nz.net.ultraq.redhorizon.engine.geometry.Dimension
Expand Down Expand Up @@ -82,6 +85,9 @@ class Shooter {
globalPalette = new GlobalPalette(loadPalette())
scene << globalPalette

var map = new Map(resourceManager.loadFile('scm22ea.ini', IniFile) as MapFile, resourceManager)
scene << map

player = new Player(resourceManager)
scene << player
}
Expand Down

0 comments on commit 2fdf451

Please sign in to comment.