summaryrefslogtreecommitdiff
path: root/src/JSystem/JUtility/JUTXfb.cpp
blob: 77862e20bae8e8a77011cd2600f370626d34e9c6 (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
//
// Generated by dtk
// Translation Unit: JUTXfb.cpp
//

#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JUtility/JUTXfb.h"
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/JUtility/JUTAssert.h"
#include "dolphin/gx/GX.h"

JUTXfb* JUTXfb::sManager;

/* 802C81D8-802C81EC       .text clearIndex__6JUTXfbFv */
void JUTXfb::clearIndex() {
    mDrawingXfbIndex = -1;
    mDrawnXfbIndex = -1;
    mDisplayingXfbIndex = -1;
}

/* 802C81EC-802C8224       .text common_init__6JUTXfbFi */
void JUTXfb::common_init(int bufNum) {
    mBufferNum = bufNum;
    clearIndex();
    mSDrawingFlag = 99;
}

/* 802C8224-802C82CC       .text __ct__6JUTXfbFPC16_GXRenderModeObjP7JKRHeapQ26JUTXfb10EXfbNumber */
JUTXfb::JUTXfb(const GXRenderModeObj* pObj, JKRHeap* pHeap, JUTXfb::EXfbNumber xfbNum) {
    common_init(xfbNum);

    if (pObj) {
        initiate(pObj->fbWidth, pObj->xfbHeight, pHeap, xfbNum);
    } else {
#if VERSION <= VERSION_JPN
        GXRenderModeObj* obj = JUTVideo::getManager()->getRenderMode();
        initiate(obj->fbWidth, obj->xfbHeight, pHeap, xfbNum);
#else
        u16 fb_width = JUTVideo::getManager()->getRenderMode()->fbWidth;
        u16 efb_height = (u32)JUTVideo::getManager()->getRenderMode()->efbHeight;
        u16 xfb_height = JUTVideo::getManager()->getRenderMode()->xfbHeight;
        f32 scale_factor = GXGetYScaleFactor(efb_height, xfb_height);
        u16 xfb_lines = GXGetNumXfbLines(efb_height, scale_factor);

        initiate(fb_width, xfb_lines, pHeap, xfbNum);
#endif
    }
}

/* 802C82CC-802C833C       .text __dt__6JUTXfbFv */
JUTXfb::~JUTXfb() {
    for (int i = 0; i < 3; i++) {
        delXfb(i);
    }
    sManager = NULL;
}

/* 802C833C-802C837C       .text delXfb__6JUTXfbFi */
void JUTXfb::delXfb(int xfbIdx) {
    if (mXfbAllocated[xfbIdx] && mBuffer[xfbIdx]) {
        delete mBuffer[xfbIdx];
    }
}

/* 802C837C-802C8410       .text createManager__6JUTXfbFPC16_GXRenderModeObjP7JKRHeapQ26JUTXfb10EXfbNumber */
JUTXfb* JUTXfb::createManager(const GXRenderModeObj* pObj, JKRHeap* pHeap, JUTXfb::EXfbNumber xfbNum) {
    JUT_CONFIRM(VERSION_SELECT(198, 198, 203, 203), sManager == NULL);
    if (sManager == NULL) {
        sManager = new JUTXfb(pObj, pHeap, xfbNum);
    }
    return sManager;
}

/* 802C8410-802C8468       .text destroyManager__6JUTXfbFv */
void JUTXfb::destroyManager() {
    /* Compiler bug: Nondeterministically nonmatching */
    // sManager (a pointer) gets passed to a bool parameter in JUTAssertion::setConfirmMessage.
    // MWCC is supposed to implicitly convert the pointer to a boolean (0 or 1) value, but due to a
    // compiler bug (in RemoveRedundantMonadicOp), it sometimes removes the implicit conversion and
    // incorrectly passes the sManager pointer as-is to the bool. This can result in incorrect
    // behavior as setConfirmMessage actually checks if the bool is exactly equal to 1, not just
    // that it's nonzero.
    // Whether the compiler bug occurs or not depends on the exact memory layout of the compiler's
    // internal state, and so it's effectively random.
    // The kiosk demo version of TWW had the bug occur during compilation, while the three retail
    // versions of the game did not have the bug occur.
    // There's no consistent way to get it to pick one or the other, so this TU cannot be linked.
    JUT_CONFIRM(VERSION_SELECT(339, 339, 344, 344), sManager);
    delete sManager;
    sManager = NULL;
}

/* 802C8468-802C8544       .text initiate__6JUTXfbFUsUsP7JKRHeapQ26JUTXfb10EXfbNumber */
void JUTXfb::initiate(u16 width, u16 height, JKRHeap* pHeap, JUTXfb::EXfbNumber xfbNum) {
    if (pHeap == NULL) {
        pHeap = JKRHeap::getSystemHeap();
    }

    int size = ((u32)width + 0xf & 0xfff0) * (u32)height * 2;

    void* buf = ::operator new[](size, pHeap, 0x20);
    mBuffer[0] = static_cast<u8*>(buf);
    mXfbAllocated[0] = true;

    if (xfbNum >= 2) {
        buf = ::operator new[](size, pHeap, 0x20);
        mBuffer[1] = static_cast<u8*>(buf);
        mXfbAllocated[1] = true;
    } else {
        mBuffer[1] = NULL;
        mXfbAllocated[1] = false;
    }

    if (xfbNum >= 3) {
        buf = ::operator new[](size, pHeap, 0x20);
        mBuffer[2] = static_cast<u8*>(buf);
        mXfbAllocated[2] = true;
    } else {
        mBuffer[2] = NULL;
        mXfbAllocated[2] = false;
    }
}

static void dummy() {
    OSReport("JX:: disp = %d\n");
    OSReport("JX:: drawing %d -> ");
    OSReport("%d\n");
}