summaryrefslogtreecommitdiff
path: root/src/code/game.c
blob: a9a37edce5daef872ce626ea69a7ecdb69908984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "game.h"
#include "gfx.h"
#include "m_controller.h"
#include "m_debug.h"
#include "67E840.h"
#include "speed_meter.h"
#include "graph.h"
#include "libu64/debug.h"
#include "libc64/malloc.h"
#include "6EDD10.h"
#include "padmgr.h"
#include "69E2C0.h"
#include "gfxalloc.h"
#include "6E0F50.h"
#include "code_variables.h"
#include "overlays/gamestates/ovl_play/m_play.h"
#include "overlays/gamestates/ovl_famicom_emu/famicom_emu.h"
#include "macros.h"

Game* gamePT = NULL;

struct_80145020_jp B_80145020_jp;
s16 B_80145040_jp;
Game* game_class_p;
u8 game_GameFrame;
f32 game_GameFrameF;
f32 game_GameFrame_2F;
f32 game_GameFrame__1F;

void func_800D2E00_jp(Game* gameState) {
    if (zurumode_flag >= 2) {
        Debug_mode_input(CONTROLLER2(gameState));
    }

    if (SREG(0) != 0) {
        func_800D88E0_jp(&B_80145020_jp);
    }
}

const u16 RO_80117CE0_jp[0x10] = {
    GPACK_RGBA5551(255, 255, 0, 1),   // R_CBUTTONS
    GPACK_RGBA5551(255, 255, 0, 1),   // L_CBUTTONS
    GPACK_RGBA5551(255, 255, 0, 1),   // D_CBUTTONS
    GPACK_RGBA5551(255, 255, 0, 1),   // U_CBUTTONS
    GPACK_RGBA5551(127, 127, 127, 1), // R_TRIG
    GPACK_RGBA5551(127, 127, 127, 1), // L_TRIG
    GPACK_RGBA5551(0, 255, 255, 1),   //
    GPACK_RGBA5551(255, 0, 255, 1),   //
    GPACK_RGBA5551(127, 127, 127, 1), // R_JPAD
    GPACK_RGBA5551(127, 127, 127, 1), // L_JPAD
    GPACK_RGBA5551(127, 127, 127, 1), // D_JPAD
    GPACK_RGBA5551(127, 127, 127, 1), // U_JPAD
    GPACK_RGBA5551(255, 0, 0, 1),     // START_BUTTON
    GPACK_RGBA5551(127, 127, 127, 1), // Z_TRIG
    GPACK_RGBA5551(0, 255, 0, 1),     // B_BUTTON
    GPACK_RGBA5551(0, 0, 255, 1),     // A_BUTTON
};

void func_800D2E58_jp(u16 button, Gfx** gfxP) {
    Gfx* gfx;
    u32 i;
    u32 j;

    gfx = *gfxP;

    gDPPipeSync(gfx++);
    gDPSetOtherMode(gfx++,
                    G_AD_PATTERN | G_CD_MAGICSQ | G_CK_NONE | G_TC_CONV | G_TF_POINT | G_TT_NONE | G_TL_TILE |
                        G_TD_CLAMP | G_TP_NONE | G_CYC_FILL | G_PM_NPRIMITIVE,
                    G_AC_NONE | G_ZS_PIXEL | G_RM_NOOP | G_RM_NOOP2);

    for (i = 0; i < ARRAY_COUNT(RO_80117CE0_jp); i++) {
        if (button & (1 << i)) {
            j = i + 1;
            gDPSetFillColor(gfx++, (RO_80117CE0_jp[i] << 0x10) | RO_80117CE0_jp[i]);
            gfx = func_800BE12C_jp(gfx, (i * 4) + 226, 220, (j * 4) + 226, 224);

            gDPPipeSync(gfx++);
        }
    }

    *gfxP = gfx;
}

void game_debug_draw_last(Game* gameState, GraphicsContext* gfxCtx) {
    Gfx* gfx;
    Gfx* gfxHead;

    if (zurumode_flag != 0) {
        OPEN_DISPS(gfxCtx);

        {
            UNUSED s32 requiredScopeTemp;

            gfxHead = POLY_OPA_DISP;
            gfx = gfxopen(gfxHead);

            gSPDisplayList(OVERLAY_DISP++, gfx);
        }

        B_80145040_jp = CONTROLLER1(gameState)->press.button | CONTROLLER1(gameState)->cur.button;

        func_800D2E58_jp(B_80145040_jp, &gfx);

        gSPEndDisplayList(gfx++);

        gfxclose(gfxHead, gfx);
        POLY_OPA_DISP = gfx;

        do {
        } while (0);

        CLOSE_DISPS(gfxCtx);
    }

    if (zurumode_flag != 0) {
        Debug_mode_output(gfxCtx);
    }

    if (SREG(0) != 0) {
        func_800D8A54_jp(&B_80145020_jp, gfxCtx);
        func_800D9018_jp(&B_80145020_jp, gfxCtx, gameState);
    }
}

void game_draw_first(GraphicsContext* gfxCtx) {
    void* temp_v0 = gfxCtx->unk_008;

    OPEN_DISPS(gfxCtx);

    gSPSegment(POLY_OPA_DISP++, 0x00, NULL);
    gSPSegment(POLY_OPA_DISP++, 0x0F, gfxCtx->unk_2E4);
    gSPSegment(POLY_OPA_DISP++, 0x0E, temp_v0);

    gSPSegment(POLY_XLU_DISP++, 0x00, NULL);
    gSPSegment(POLY_XLU_DISP++, 0x0F, gfxCtx->unk_2E4);
    gSPSegment(POLY_XLU_DISP++, 0x0E, temp_v0);

    gSPSegment(OVERLAY_DISP++, 0x00, NULL);
    gSPSegment(OVERLAY_DISP++, 0x0F, gfxCtx->unk_2E4);
    gSPSegment(OVERLAY_DISP++, 0x0E, temp_v0);

    gSPSegment(UNK_2B0_DISP++, 0x00, NULL);
    gSPSegment(UNK_2B0_DISP++, 0x0F, gfxCtx->unk_2E4);
    gSPSegment(UNK_2B0_DISP++, 0x0E, temp_v0);

    gSPSegment(UNK_2C0_DISP++, 0x00, NULL);
    gSPSegment(UNK_2C0_DISP++, 0x0F, gfxCtx->unk_2E4);
    gSPSegment(UNK_2C0_DISP++, 0x0E, temp_v0);

    gSPSegment(LIGHT_DISP++, 0x00, NULL);
    gSPSegment(LIGHT_DISP++, 0x0F, gfxCtx->unk_2E4);
    gSPSegment(LIGHT_DISP++, 0x0E, temp_v0);

    CLOSE_DISPS(gfxCtx);
}

void game_draw_last(GraphicsContext* gfxCtx) {
    Gfx* gfx;
    Gfx* gfxHead;

    OPEN_DISPS(gfxCtx);

    gfx = gfxopen(gfxHead = POLY_OPA_DISP);

    gSPDisplayList(OVERLAY_DISP++, gfx);
    gSPEndDisplayList(gfx++);

    gfxclose(gfxHead, gfx);
    POLY_OPA_DISP = gfx;

    CLOSE_DISPS(gfxCtx);
}

void game_get_controller(Game* gameState) {
    if (gameState->unk_74 == 1) {
        padmgr_RequestPadData(gameState->input, 1);
    } else {
        padmgr_ClearPadData(gameState->input);
    }
}

void SetGameFrame(s32 divisor) {
    if (divisor != game_GameFrame) {
        game_GameFrame = divisor;
        game_GameFrameF = (divisor & 0xFF);
        game_GameFrame_2F = game_GameFrameF / 2.0f;
        game_GameFrame__1F = 1.0f / game_GameFrameF;
        SREG(30) = divisor;
    }
}

void game_main(Game* gameState) {
    GraphicsContext* gfxCtx = gameState->gfxCtx;
    u8 gameframe = game_GameFrame;

    if (SREG(30) != gameframe) {
        SetGameFrame(SREG(30));
    }

    game_draw_first(gfxCtx);
    gfxCtx->unk_2F0 = 5;
    mTM_time();
    gfxCtx->unk_2F0 = 7;

    gameState->main(gameState);

    gfxCtx->unk_2F0 = 9;
    mBGM_main(gameState);
    gfxCtx->unk_2F0 = 0xA;
    func_800D2E00_jp(gameState);

    gameState->unk_A0++;
}

void game_init_hyral(Game* gameState, size_t size) {
    void* buf = gamealloc_malloc(&gameState->alloc, size);

    if (buf != NULL) {
        THA_ct(&gameState->heap, buf, size);
        return;
    }

    THA_ct(&gameState->heap, NULL, 0);
    _dbg_hungup("../game.c", 462);
}

void game_resize_hyral(Game* gameState, size_t size) {
    GameAlloc* alloc = &gameState->alloc;
    void* gameArena;
    size_t maxFree;
    size_t bytesFree;
    size_t bytesAllocated;
    void* heapStart = gameState->heap.start;

    THA_dt(&gameState->heap);

    gamealloc_free(alloc, heapStart);

    GetFreeArena(&maxFree, &bytesFree, &bytesAllocated);
    if (size == 0x7D0000) {
        size = maxFree - sizeof(GameAllocEntry);
    } else {
        size = (maxFree - sizeof(GameAllocEntry) < size) ? maxFree - sizeof(GameAllocEntry) : size;
    }

    gameArena = gamealloc_malloc(alloc, size);
    if (gameArena != NULL) {
        THA_ct(&gameState->heap, gameArena, size);
        return;
    }

    THA_ct(&gameState->heap, NULL, 0);
    _dbg_hungup("../game.c", 508);
}

void game_ct(Game* gameState, GameStateFunc init, GraphicsContext* gfxCtx) {
    gamePT = gameState;

    gfxCtx->unk_2F2 = 1;
    gfxCtx->unk_25C = &osViModeNtscLan1;
    gfxCtx->unk_2EC = 0x42;
    gfxCtx->unk_2FC = 1.0f;
    gfxCtx->unk_300 = 1.0f;

    mCon_ct(gameState);

    gameState->unk_A0 = 0;
    gameState->main = NULL;
    gameState->destroy = NULL;
    gameState->running = true;
    gameState->unk_74 = 1;

    {
        s32 requiredScopeTemp UNUSED;

        gameState->gfxCtx = gfxCtx;
        gameState->init = NULL;
        gameState->size = 0;
    }

    gamealloc_init(&gameState->alloc);

    game_init_hyral(gameState, 0x100000); // 1 MiB
    SetGameFrame(2);

    init(gameState);

    func_8005F020_jp();
    func_800D88B0_jp(&B_80145020_jp);
    func_800D3E14_jp(gameState->gfxCtx);
}

void game_dt(Game* gameState) {
    mCon_dt(gameState);
    func_800D3E40_jp(gameState->gfxCtx);
    mBGM_cleanup();

    if (gameState->destroy != NULL) {
        gameState->destroy(gameState);
    }

    func_800D88D4_jp(&B_80145020_jp);
    THA_dt(&gameState->heap);
    gamealloc_cleanup(&gameState->alloc);
    gamePT = NULL;
}

GameStateFunc game_get_next_game_init(Game* gameState) {
    return gameState->init;
}

size_t game_get_next_game_class_size(Game* gameState) {
    return gameState->size;
}

s32 game_is_doing(Game* gameState) {
    return gameState->running;
}

s32 game_getFreeBytes(Game* gameState) {
    return THA_getFreeBytes(&gameState->heap);
}

void game_goto_next_game_play(Game* gameState) {
    STOP_GAMESTATE(gameState);
    SET_NEXT_GAMESTATE(gameState, play_init, sizeof(Game_Play));
}

void game_goto_next_game_name_famicom_emu(Game* gameState) {
    STOP_GAMESTATE(gameState);
    SET_NEXT_GAMESTATE(gameState, famicom_emu_init, sizeof(Game_FamicomEmu));
}