|
| 1 | +#include "MirrorMode.h" |
| 2 | + |
| 3 | +#include <libultraship.h> |
| 4 | +#include "../../ui/cvar_prefixes.h" |
| 5 | +#include "sm64.h" |
| 6 | +#include "game/game_init.h" |
| 7 | + |
| 8 | +#define CVAR_MIRROR_MODE CVAR_ENHANCEMENT("Modes.MirroredWorld.Mode") |
| 9 | + |
| 10 | +static int sMirrorEnabled = 0; |
| 11 | +static int sMirrorActive = 0; |
| 12 | +static int sCounterMirror = 0; |
| 13 | +static int sExcludeForFrame = 0; |
| 14 | + |
| 15 | +int mirror_mode_is_enabled(void) { |
| 16 | + return CVarGetInteger(CVAR_MIRROR_MODE, 0); |
| 17 | +} |
| 18 | + |
| 19 | +int mirror_mode_is_active(void) { |
| 20 | + return sMirrorActive; |
| 21 | +} |
| 22 | + |
| 23 | +void mirror_mode_apply_projection(void) { |
| 24 | + if (!mirror_mode_is_enabled()) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // Check if we should exclude mirroring for this frame |
| 29 | + if (sExcludeForFrame) { |
| 30 | + sExcludeForFrame = 0; |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + Mtx* mtx = (Mtx*)alloc_display_list(sizeof(Mtx)); |
| 35 | + if (mtx == NULL) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Apply horizontal scale flip |
| 40 | + guScale(mtx, -1.0f, 1.0f, 1.0f); |
| 41 | + gSPMatrix(gDisplayListHead++, VIRTUAL_TO_PHYSICAL(mtx), G_MTX_PROJECTION | G_MTX_MUL | G_MTX_NOPUSH); |
| 42 | + |
| 43 | + // Set invert culling flag for proper face sorting |
| 44 | + gSPSetExtraGeometryMode(gDisplayListHead++, G_EX_INVERT_CULLING); |
| 45 | + |
| 46 | + sMirrorActive = 1; |
| 47 | +} |
| 48 | + |
| 49 | +void mirror_mode_undo_projection(void) { |
| 50 | + if (!sMirrorActive) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Clear invert culling flag before HUD renders |
| 55 | + gSPClearExtraGeometryMode(gDisplayListHead++, G_EX_INVERT_CULLING); |
| 56 | + |
| 57 | + sMirrorActive = 0; |
| 58 | +} |
| 59 | + |
| 60 | +void mirror_mode_register(void) { |
| 61 | + // Events will be registered in mirror_mode_init via PortEnhancements |
| 62 | +} |
| 63 | + |
| 64 | +void mirror_mode_init(void) { |
| 65 | + // Update enabled state from CVAR |
| 66 | + sMirrorEnabled = mirror_mode_is_enabled(); |
| 67 | +} |
| 68 | + |
| 69 | +void mirror_mode_set_counter_mirror(void) { |
| 70 | + sCounterMirror = 1; |
| 71 | +} |
| 72 | + |
| 73 | +void mirror_mode_clear_counter_mirror(void) { |
| 74 | + sCounterMirror = 0; |
| 75 | +} |
| 76 | + |
| 77 | +int mirror_mode_should_counter_mirror(void) { |
| 78 | + return sCounterMirror && sMirrorActive; |
| 79 | +} |
| 80 | + |
| 81 | +// Exclude mirroring for the current frame (for file select, etc.) |
| 82 | +void mirror_mode_exclude_for_frame(void) { |
| 83 | + sExcludeForFrame = 1; |
| 84 | +} |
| 85 | + |
| 86 | +// Apply counter-mirror scale to cancel projection mirror for text/numbers |
| 87 | +void mirror_mode_apply_counter_scale(void) { |
| 88 | + if (!mirror_mode_should_counter_mirror()) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + Mtx* mtx = (Mtx*)alloc_display_list(sizeof(Mtx)); |
| 93 | + if (mtx == NULL) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // Apply counter-scale to cancel the projection mirror |
| 98 | + guScale(mtx, -1.0f, 1.0f, 1.0f); |
| 99 | + gSPMatrix(gDisplayListHead++, VIRTUAL_TO_PHYSICAL(mtx), G_MTX_MODELVIEW | G_MTX_MUL | G_MTX_NOPUSH); |
| 100 | +} |
| 101 | + |
| 102 | +// Patch display lists to add counter-mirror for number sprites |
| 103 | +void mirror_mode_patch_number_dl(void) { |
| 104 | + // This function would patch the MODEL_NUMBER display list |
| 105 | + // to include a counter-mirror transform at the beginning |
| 106 | + // However, in the PC port, display lists are loaded dynamically |
| 107 | + // and the patching would need to happen at runtime |
| 108 | + // |
| 109 | + // The approach would be: |
| 110 | + // 1. Load the number_geo display list |
| 111 | + // 2. Inject a counter-mirror matrix at the beginning |
| 112 | + // 3. This would make all number sprites appear unmirrored |
| 113 | + // |
| 114 | + // This is currently a placeholder for future implementation |
| 115 | + // as it requires deeper integration with the display list system |
| 116 | +} |
| 117 | + |
| 118 | +void mirror_mode_invert_input(void) { |
| 119 | + if (!mirror_mode_is_enabled()) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + struct Controller* ctrl = gPlayer1Controller; |
| 124 | + if (ctrl == NULL) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + // Invert BOTH rawStickX and processed stickX |
| 129 | + // rawStickX is used for some purposes, but stickX is what mario.c uses for movement |
| 130 | + ctrl->rawStickX = -ctrl->rawStickX; |
| 131 | + ctrl->stickX = -ctrl->stickX; |
| 132 | + |
| 133 | + // Also need to update magnitude |
| 134 | + ctrl->stickMag = sqrtf(ctrl->stickX * ctrl->stickX + ctrl->stickY * ctrl->stickY); |
| 135 | +} |
0 commit comments