-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cpp
148 lines (130 loc) · 3.41 KB
/
Utils.cpp
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
#include "pch.h"
#include "Utils.h"
static const unsigned int sc_colorBufferCellsPerRow = 16;
unsigned int GetFileOffsetFromROMAddress(unsigned int romAddress)
{
if (romAddress >= 0x028000 && romAddress <= 0x02FFFF)
{
return 0x10200 + (romAddress - 0x028000);
}
else if (romAddress >= 0xA08000 && romAddress <= 0xA0FFFF)
{
return 0x100200 + (romAddress - 0xA08000);
}
else if (romAddress >= 0xA18000 && romAddress <= 0xA1FFFF)
{
return 0x108200 + (romAddress - 0xA18000);
}
else if (romAddress >= 0xA28000 && romAddress <= 0xA2FFFF)
{
return 0x110200 + (romAddress - 0xA28000);
}
else if (romAddress >= 0xA38000 && romAddress <= 0xA3FFFF)
{
return 0x118200 + (romAddress - 0xA38000);
}
else if (romAddress >= 0xA48000 && romAddress <= 0xA4FFFF)
{
return 0x120200 + (romAddress - 0xA48000);
}
else
{
assert(false); // Unexpected value
return 0xFFFFFFFF;
}
}
long GetFileSize(FILE* file)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
return fileSize;
}
unsigned int GetByteIndexForRow(
unsigned int cellNumber,
unsigned int indexWithinCell,
unsigned char* xWithinCell = nullptr)
{
assert(indexWithinCell < 64);
const unsigned int bytesPerCell = 16;
const unsigned int cellX = indexWithinCell % 8;
const unsigned int cellY = indexWithinCell / 8;
const unsigned int srcIndBase = (cellNumber * bytesPerCell) + (cellY * 2);
if (xWithinCell)
{
*xWithinCell = cellX;
}
return srcIndBase;
}
void SetCellRowData(
std::vector<unsigned char>& buffer,
unsigned int cellNumber,
unsigned int indexWithinCell,
unsigned char r0,
unsigned char r1)
{
const unsigned int srcIndBase = GetByteIndexForRow(cellNumber, indexWithinCell);
assert(srcIndBase + 1 < buffer.size());
buffer[srcIndBase + 0] = r0;
buffer[srcIndBase + 1] = r1;
}
void GetCellRowData(
std::vector<unsigned char> const& buffer,
unsigned int cellNumber,
unsigned int indexWithinCell,
unsigned char* r0,
unsigned char* r1,
unsigned char* xWithinCell)
{
const unsigned int srcIndBase = GetByteIndexForRow(cellNumber, indexWithinCell, xWithinCell);
*r0 = buffer[srcIndBase + 0];
*r1 = buffer[srcIndBase + 1];
}
int SnesB5G5R5ToR8B8G8(unsigned short snesFmt)
{
unsigned short r5 = snesFmt & 0x1F;
snesFmt >>= 5;
unsigned short g5 = snesFmt & 0x1F;
snesFmt >>= 5;
unsigned short b5 = snesFmt & 0x1F;
int r8 = r5 * 8;
int g8 = g5 * 8;
int b8 = b5 * 8;
int rgb = r8;
rgb <<= 8;
rgb |= g8;
rgb <<= 8;
rgb |= b8;
return rgb;
}
unsigned short R8B8G8ToSnesB5G5R5(int rgbFmt)
{
unsigned short b8 = rgbFmt & 0xFF;
rgbFmt >>= 8;
unsigned short g8 = rgbFmt & 0xFF;
rgbFmt >>= 8;
unsigned short r8 = rgbFmt & 0xFF;
int r5 = r8 / 8;
int g5 = g8 / 8;
int b5 = b8 / 8;
unsigned short snes = b5;
snes <<= 5;
snes |= g5;
snes <<= 5;
snes |= r5;
return snes;
}
std::wstring ManagedToWideString(System::String^ s)
{
array<wchar_t>^ arr = s->ToCharArray();
std::wstring result;
for (int i = 0; i < arr->Length; ++i)
{
result.push_back(arr[i]);
}
return result;
}
System::String^ NarrowASCIIStringToManaged(std::string const& s)
{
return gcnew System::String(s.c_str());
}