summaryrefslogtreecommitdiff
path: root/src/lib.cpp
blob: 9b3035c882b70def1a831fe562f656aa67ebf6c8 (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
#include "controller.h"
#include "fifo_queue.h"
#include "font.h"
#include "free_cam.h"
#include "gz_flags.h"
#include "input_viewer.h"
#include "libtp_c/include/controller.h"
#include "libtp_c/include/system.h"
#include "libtp_c/include/tp.h"
#include "menu.h"
#include "menus/main_menu.h"
#include "menus/position_settings_menu.h"
#include "menus/settings_menu.h"
#include "menus/tools_menu.h"
#include "movelink.h"
#include "timer.h"
#include "utils/card.h"
#include "utils/draw.h"
#include "utils/hook.h"
#include "utils/link.h"
#include "utils/loading.h"
#include "utils/memory.h"
#include "utils/texture.h"

_FIFOQueue Queue;
bool card_load = true;
Texture gzIconTex;
bool last_frame_was_loading = false;

extern "C" {

#if (NTSCU)
#define main_tampoline ((void (*)(void))0x803737b4)
#endif
#if (PAL)
#define main_tampoline ((void (*)(void))0x803745e4)
#endif
#if (NTSCJ)
#define main_tampoline ((void (*)(void))0x80375c44)
#endif

void apply_lib_hooks() {
    Hook::apply_hooks();
    main_tampoline();
}

void init() {
    Font::load_font("tpgz/fonts/consola.fnt");
    PosSettingsMenu::initDefaults();
    Draw::init();
    fifo_visible = true;
}

void game_loop() {
    using namespace Controller::Pad;

    // Button combo to bypass the automatic loading of the save file
    // in case of crash cause by the load.
    if (tp_mPadStatus.sval == (L | R | B) && card_load) {
        card_load = false;
    }

    // check and load gz settings card if found
    Utilities::load_gz_card(card_load);

    if (tp_mPadStatus.sval == (L | R | DPAD_DOWN) && tp_fopScnRq.isLoading != 1 &&
        !move_link_active) {
        MenuRendering::set_menu(MN_MAIN_MENU_INDEX);
        fifo_visible = false;
    }
    if (tp_fopScnRq.isLoading == 1) {
        MenuRendering::close_active_menus();
        move_link_active = false;
        last_frame_was_loading = true;
    }

    // save temp flags and tears after every loading zone
    if (last_frame_was_loading && tp_fopScnRq.isLoading != 1) {
        tp_memcpy(g_area_reload.temp_flags, tp_gameInfo.temp_flags.flags,
                  sizeof(tp_gameInfo.temp_flags.flags));
        g_area_reload.tears = tp_gameInfo.inventory.tears;
        last_frame_was_loading = false;
    }

    GZFlags::apply_active_flags();
    FreeCam::handle_free_cam();
    MoveLink::move_link();

    if (ToolItems[Tools::TURBO_MODE_INDEX].active) {
        tp_cPadInfo.triggerInput = tp_cPadInfo.input;
    }
}

void draw() {
    setupRendering();
    // Consolas.setupRendering();
    if (MenuRendering::is_menu_open()) {
        Font::gz_renderChars("tpgz v0.1", sprite_offsets[MENU_INDEX].x + 35.0f, 25.0f, cursor_rgba,
                             g_drop_shadows);

        if (gzIconTex.loadCode == TexCode::TEX_UNLOADED) {
            load_texture("tpgz/tex/tpgz.tex", &gzIconTex);
            if (gzIconTex.loadCode != TexCode::TEX_OK) {
                tp_osReport("Could not load TPGZ's icon texture (Code: %d)", gzIconTex.loadCode);
            }
        }

        if (gzIconTex.loadCode == TexCode::TEX_OK) {
            Draw::draw_rect(0xFFFFFFFF, {sprite_offsets[MENU_INDEX].x, 5.0f}, {30, 30},
                            &gzIconTex._texObj);
        }
    }
    if (fifo_visible) {
        FIFOQueue::renderItems(Queue);
    }
    if (ToolItems[Tools::LINK_DEBUG_INDEX].active) {
        Utilities::show_link_debug_info();
    }
    if (ToolItems[Tools::INPUT_VIEWER_INDEX].active) {
        InputViewer::render();
    }
    if (ToolItems[Tools::TIMER_INDEX].active || ToolItems[Tools::LOAD_TIMER_INDEX].active ||
        ToolItems[Tools::IGT_TIMER_INDEX].active) {
        Timer::render();
    }
    if (move_link_active) {
        MoveLink::render_info_input();
    }
    MenuRendering::render_active_menus();
    Utilities::render_active_watches();
}
}