more small cleanupses (#1722)

* reduce max supersampling options in progress menu

* cleanup knuth rng file

* save & load cheats

* allow keyboard controls when cpad is connected

* ignore key presses when imgui is being used

* save settings when quitting game
This commit is contained in:
ManDude
2022-08-04 02:51:13 +01:00
committed by GitHub
parent 3e538dabe6
commit 5148523917
7 changed files with 77 additions and 59 deletions
+13 -6
View File
@@ -8,10 +8,12 @@
#include "common/log/log.h"
#include "common/util/Assert.h"
#include <common/util/FileUtil.h>
#include "common/util/FileUtil.h"
#include "game/graphics/pipelines/opengl.h" // for GLFW macros
#include "third-party/imgui/imgui.h"
namespace Pad {
/*
@@ -57,6 +59,10 @@ void ClearKeys() {
}
void OnKeyPress(int key) {
if (ImGui::IsAnyItemActive()) {
return;
}
if (input_mode == InputModeStatus::Enabled) {
if (key == GLFW_KEY_ESCAPE) {
ExitInputMode(true);
@@ -118,14 +124,13 @@ int IsPressed(MappingInfo& mapping, Button button, int pad = 0) {
// returns the value of the analog axis (in the future, likely pressure sensitive if we support it?)
// if invalid or otherwise -- returns 127 (analog stick neutral position)
int AnalogValue(MappingInfo& /*mapping*/, Analog analog, int pad = 0) {
float input = 0.0f;
if (CheckPadIdx(pad) == -1) {
// Pad out of range, return a stable value
return 127;
}
if (pad == 0 && g_gamepads.gamepad_idx[0] == -1) { // Gamepad not present - use keyboard
float input = 0.0f;
if (pad == 0) {
// Movement controls mapped to WASD keys
if (g_buffered_key_status[GLFW_KEY_W] && analog == Analog::Left_Y)
input += -1.0f;
@@ -145,7 +150,7 @@ int AnalogValue(MappingInfo& /*mapping*/, Analog analog, int pad = 0) {
input += -1.0f;
if (g_buffered_key_status[GLFW_KEY_L] && analog == Analog::Right_X)
input += 1.0f;
} else if (pad == 1 && g_gamepads.gamepad_idx[1] == -1) {
} else if (pad == 1) {
// these bindings are not sane
if (g_buffered_key_status[GLFW_KEY_KP_5] && analog == Analog::Left_Y)
input += -1.0f;
@@ -165,7 +170,9 @@ int AnalogValue(MappingInfo& /*mapping*/, Analog analog, int pad = 0) {
input += -1.0f;
if (g_buffered_key_status[GLFW_KEY_KP_9] && analog == Analog::Right_X)
input += 1.0f;
} else { // Gamepad present
}
if (input == 0) {
input = g_gamepad_analogs[pad][(int)analog];
}
-17
View File
@@ -234,20 +234,3 @@
(define-extern *setting-control* setting-control)
(defun scf-time-to-uint64 ()
(let ((date (new 'stack 'scf-time)))
(let ((temp (the uint64 0)))
(scf-get-time date)
(logior! temp (-> date stat))
(logior! temp (shl (-> date second) 8))
(logior! temp (shl (-> date minute) 16))
(logior! temp (shl (-> date hour) 24))
(logior! temp (shl (-> date week) 32))
(logior! temp (shl (-> date day) 40))
(logior! temp (shl (-> date month) 48))
(logior! temp (shl (-> date year) 56))
temp
)
)
)
+12 -2
View File
@@ -1708,7 +1708,17 @@
"engine/connect.gc"
"ui/text-h.gc"
"game/settings-h.gc"
"math/knuth-rand.gc" ;; added
)
(goal-src "pc/util/knuth-rand.gc" "settings-h")
(goal-src-sequence
;; prefix
"engine/"
:deps
("$OUT/obj/settings-h.o")
"util/capture.gc"
"debug/memory-usage-h.gc"
"gfx/texture/texture.gc"
@@ -2021,7 +2031,7 @@
(goal-src "pc/pckernel.gc" "settings" "video")
(goal-src "pc/subtitle.gc" "text" "pckernel" "hint-control" "loader-h" "gsound" "ambient")
(goal-src "pc/progress-pc.gc" "progress" "pckernel")
(goal-src "pc/anim-tester-x.gc" "pckernel" "gstring" "joint" "process-drawable" "art-h" "effect-control")
(goal-src "pc/util/anim-tester-x.gc" "pckernel" "gstring" "joint" "process-drawable" "art-h" "effect-control")
(goal-src "pc/hud-classes-pc.gc" "pckernel" "hud" "battlecontroller" "generic-obs")
;; the debug menu is modified to include PC specific options:
+5
View File
@@ -859,6 +859,10 @@
(("hard-fish-hiscore") (set! (-> obj secrets hard-fish-hiscore) (file-stream-read-int file)))
(("hard-rats-hiscore") (set! (-> obj secrets hard-rats-hiscore) (file-stream-read-int file)))
(("hard-rats-hiwave") (set! (-> obj secrets hard-rats-hiwave) (file-stream-read-int file)))
(("cheats")
(set! (-> obj cheats-known) (the pc-cheats (file-stream-read-int file)))
(set! (-> obj cheats) (logand (-> obj cheats-known) (file-stream-read-int file)))
)
(("music")
(dotimes (i PC_MUSIC_LOG_LENGTH)
(when (!= #x29 (file-stream-get-next-char-ret file))
@@ -986,6 +990,7 @@
(format file " (hard-rats-hiscore ~D)~%" (-> obj secrets hard-rats-hiscore))
(format file " (hard-rats-hiwave ~D)~%" (-> obj secrets hard-rats-hiwave))
|#
(format file " (cheats #x~x #x~x)~%" (-> obj cheats-known) (-> obj cheats))
(format file " (music~%")
(dotimes (i PC_MUSIC_LOG_LENGTH)
+4 -7
View File
@@ -447,9 +447,9 @@
;; supersampling in fullscreen and stuff. this results in way too many resolution options lol.
(when (!= display-mode 'windowed)
(*! sx 4)
(*! sy 4)
(*! mult 4)
(*! sx 2)
(*! sy 2)
(*! mult 2)
)
;; game aspect ratio
@@ -1190,8 +1190,6 @@
(set-size! *pc-settings* newx newy))
(cpad-clear! 0 x)
(cpad-clear! 0 circle)
(cpad-clear! 0 square)
(cpad-clear! 0 triangle)
(sound-play "cursor-options")
(set! (-> obj next-display-state) (progress-screen invalid))
(commit-to-file *pc-settings*)
@@ -1234,8 +1232,6 @@
(set-aspect! *pc-settings* newx newy))))
(cpad-clear! 0 x)
(cpad-clear! 0 circle)
(cpad-clear! 0 square)
(cpad-clear! 0 triangle)
(sound-play "cursor-options")
(commit-to-file *pc-settings*)
(set! (-> obj next-display-state) (progress-screen invalid))
@@ -1631,6 +1627,7 @@
(cond
((-> *progress-state* yes-no-choice)
(sound-play "start-options")
(commit-to-file *pc-settings*)
(sound-volume-off)
(kernel-shutdown)
)
@@ -6,66 +6,82 @@
;;;; This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer
;;;; and described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.2.1.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftype knuth-rand-state (structure)
((seed uint64))
(defun scf-time-to-int64 ()
"pack system time into 64-bits for randomization purposes."
(let ((date (new 'stack-no-clear 'scf-time)))
(scf-get-time date)
(logior (the int (-> date stat))
(shl (-> date second) 8)
(shl (-> date minute) 16)
(shl (-> date hour) 24)
(shl (-> date week) 32)
(shl (-> date day) 40)
(shl (-> date month) 48)
(shl (-> date year) 56))
)
)
(define *knuth-rand-state* (new 'global 'knuth-rand-state))
(defun knuth-rand-init ((newSeed uint))
(set! (-> *knuth-rand-state* seed) newSeed)
)
(knuth-rand-init (scf-time-to-uint64))
(deftype knuth-rand-state (structure)
((seed int64))
)
(define *knuth-rand-state* (new 'static 'knuth-rand-state))
(defun knuth-rand-init ((seed int))
(set! (-> *knuth-rand-state* seed) seed)
)
(defun knuth-rand-advance-seed ()
;; seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)
(set! (-> *knuth-rand-state* seed) (the uint64 (logand (+ (imul64 (-> *knuth-rand-state* seed) 25214903917) 11) (- (shl 1 48) 1))))
)
(set! (-> *knuth-rand-state* seed) (logand (+ (imul64 (-> *knuth-rand-state* seed) #x5deece66d) #xb) (- (shl 1 48) 1)))
)
(defun knuth-rand-next ((bits int))
(knuth-rand-advance-seed)
;; return (int)(seed >>> (48 - bits))
(shr (-> *knuth-rand-state* seed) (- 48 bits))
)
)
(defun knuth-rand-nextInt ()
(defun knuth-rand-next-int ()
(knuth-rand-advance-seed)
(-> *knuth-rand-state* seed)
)
)
;; returns a pseudorandom int in the range [min, max)
(defun knuth-rand-nextIntRange ((min int) (max int))
(defun knuth-rand-int-range ((min int) (max int))
(let* ((bound (- max min))
;; int bits, val;
(bits (the uint64 0))
(val (the uint64 0)))
;; int bits, val;
(bits 0)
(val 0))
;; if ((bound & -bound) == bound) // i.e., bound is a power of 2
(if (= (logand bound (* -1 bound)) bound)
;; return (int)((bound * (long)next(31)) >> 31);
(return (sar (* bound (knuth-rand-next 31)) 31))
)
)
;; do {
;; bits = next(31);
;; val = bits % bound;
;; } while (bits - val + (bound-1) < 0);
;; return val;
(while #t
(until (not (< (+ bits (- val) bound -1) 0))
(set! bits (knuth-rand-next 31))
(set! val (mod bits bound))
(if (>= (+ bits (* -1 val) bound -1) 0)
(return (+ min val))
)
)
(+ min val))
)
-1
)
;; returns a pseudorandom float in the range [0, 1)
(defun knuth-rand-nextFloat ()
(defun knuth-rand-next-float ()
;; return next(24) / ((float)(1 << 24));
(/ (the float (knuth-rand-next 24)) (shl 1 24))
)
)
(knuth-rand-init (scf-time-to-int64))