-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
336 lines (279 loc) · 9.02 KB
/
main.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
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
334
335
336
#include <turbojpeg.h>
#include <inipp.h>
#include "gfx/Gfx.hpp"
#include "gfx/JPEG.h"
#include "gfx/font/Text.hpp"
#include "input/Input.hpp"
#include "menu/Menu.hpp"
#include "common.h"
#include "util.hpp"
#include "Audio.h"
#include "config/Config.hpp"
#include "menu/StatusOverlay.hpp"
#ifdef __WIIU__
#include <nn/swkbd.h>
#include <coreinit/filesystem.h>
#include <proc_ui/procui.h>
#include <sysapp/launch.h>
#include <whb/log.h>
#include <whb/log_udp.h>
#include <whb/log_cafe.h>
#include <whb/proc.h>
#include <sys/iosupport.h>
static ssize_t wiiu_log_write(struct _reent* r, void* fd, const char* ptr, size_t len) {
(void)r; (void)fd;
WHBLogPrintf("%*.*s", len, len, ptr);
return len;
}
static devoptab_t dotab_stdout = {
.name = "udp_out",
.write_r = &wiiu_log_write,
};
#endif
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <thread>
#include <fstream>
#include "Network.hpp"
void update3DSTextures(tjhandle tj_handle, Gfx::Texture& topTexture, Gfx::Texture& btmTexture);
int main(int argc, char** argv) {
int ret;
bool bret;
(void)argc, (void)argv;
#ifdef __WIIU__
WHBLogUdpInit();
WHBLogCafeInit();
devoptab_list[STD_OUT] = &dotab_stdout;
devoptab_list[STD_ERR] = &dotab_stdout;
#warning "Building for Wii U"
#ifndef GFX_SDL
WHBProcInit();
#endif
OnLeavingScope prc_c([&] {
printf("bye!\n");
WHBLogUdpDeinit();
WHBLogCafeDeinit();
#ifndef GFX_SDL
WHBProcShutdown();
#endif
});
#endif
#ifdef __WIIU__
KPADInit();
WPADEnableURCC(true);
WPADEnableWiiRemote(true);
OnLeavingScope kpd_c([&] {
//KPADShutdown();
});
#endif
printf("hi\n");
bret = Gfx::Init();
OnLeavingScope gfx_c([&] { Gfx::Quit(); });
if (!bret) {
printf("Graphics init error!\n");
return 3;
}
Gfx::Resolution curRes = Gfx::GetResolution();
bret = Text::Init();
OnLeavingScope txt_c([&] { Text::Quit(); });
if (!bret) {
printf("Text init error!\n");
return 3;
}
FSClient* swkbdFSClient = (FSClient*)malloc(sizeof(FSClient));
FSAddClient(swkbdFSClient, (FSErrorFlag)0);
OnLeavingScope sfs_c([&] {
if (swkbdFSClient) {
FSDelClient(swkbdFSClient, (FSErrorFlag)0);
free(swkbdFSClient);
}
});
nn::swkbd::CreateArg swkbdCreateArg = {
.workMemory = malloc(nn::swkbd::GetWorkMemorySize(0)),
.regionType = nn::swkbd::RegionType::Europe,
.fsClient = swkbdFSClient,
};
OnLeavingScope swm_c([&] {
if (swkbdCreateArg.workMemory) free(swkbdCreateArg.workMemory);
});
if (!nn::swkbd::Create(swkbdCreateArg)) {
printf("swkbd init error!\n");
return 3;
}
OnLeavingScope swk_c([&] { nn::swkbd::Destroy(); });
tjhandle tj_handle = tjInitDecompress();
Config config;
printf("did init\n");
/* Show loading text */
Text::Text loading_text(u"Now Loading");
Gfx::PrepRender();
Gfx::PrepRenderBtm();
Gfx::Clear(config.background);
loading_text.Render(30, 480 - 30, {});
Gfx::DoneRenderBtm();
Gfx::Present();
/* Allocate textures for the received frames */
Gfx::Texture topTexture(240, 400);
if (!topTexture.valid()) {
printf("Couldn't make texture: %s\n", Gfx::GetError());
return 3;
}
Gfx::Texture btmTexture(240, 320);
if (!btmTexture.valid()) {
printf("Couldn't make texture: %s\n", Gfx::GetError());
return 3;
}
{
/* Read config file */
std::ifstream config_file(NTRVIEW_DIR "/ntrview.ini");
config.LoadINI(config_file);
} //config_file goes out of scope here
Menu menus(config, tj_handle);
/* Start off networking thread */
std::thread networkThread(Network::mainLoop, &config.networkconfig);
/* Set up audio */
Audio::Init();
OnLeavingScope _aud_c([] {
Audio::Shutdown();
});
printf("gonna start rendering\n");
bool menu = false;
bool menu_input_buffering = false;
Input::Priority last_input_priority = Input::Priority::VPAD;
#ifdef __WIIU__
while (WHBProcIsRunning()) {
#else
while (1) {
#endif
Network::State networkState = Network::GetNetworkState();
const auto& profile = config.profiles[config.profile];
menus.overlay.NetworkState(networkState);
//inputs
auto input = Input::Get(profile.layout_drc[1]);
if (input) {
if (menu) {
//swkbd stuff
nn::swkbd::Calc((nn::swkbd::ControllerInfo) {
.vpad = &input->native.vpad,
.kpad = { &input->native.kpad[0], &input->native.kpad[1],
&input->native.kpad[2], &input->native.kpad[3] }
});
if (nn::swkbd::IsNeedCalcSubThreadFont()) {
nn::swkbd::CalcSubThreadFont();
}
if (nn::swkbd::IsNeedCalcSubThreadPredict()) {
nn::swkbd::CalcSubThreadPredict();
}
menu = menus.Update(config, menu, *input);
if (!menu) menu_input_buffering = true;
} else {
menu = menus.Update(config, menu, *input);
if (networkState == Network::CONNECTED_STREAMING && !menu_input_buffering) {
Network::Input(input->ds);
} else if (menu_input_buffering) {
if (input->native.vpad.hold == 0) menu_input_buffering = false;
}
}
if (input->priority != last_input_priority) {
menus.overlay.InputPriorityMessage(input->priority, input->ext);
last_input_priority = input->priority;
}
}
//logic and pre-render work
if (networkState == Network::CONNECTED_STREAMING) {
update3DSTextures(tj_handle, topTexture, btmTexture);
}
//render
Gfx::PrepRender();
Gfx::PrepRenderTop();
Gfx::Clear(config.background);
if (networkState == Network::CONNECTED_STREAMING) {
if (profile.layout_tv[curRes][0].d.w) {
topTexture.Render(profile.layout_tv[curRes][0]);
}
if (profile.layout_tv[curRes][1].d.w) {
btmTexture.Render(profile.layout_tv[curRes][1]);
}
}
menus.overlay.Render();
if (menu) menus.Render();
nn::swkbd::DrawTV();
Gfx::DoneRenderTop();
Gfx::PrepRenderBtm();
Gfx::Clear(config.background);
if (networkState == Network::CONNECTED_STREAMING) {
if (profile.layout_drc[0].d.w) {
topTexture.Render(profile.layout_drc[0]);
}
if (profile.layout_drc[1].d.w) {
btmTexture.Render(profile.layout_drc[1]);
}
}
menus.overlay.Render();
if (menu) menus.Render();
nn::swkbd::DrawDRC();
Gfx::DoneRenderBtm();
Gfx::Present();
}
printf("Quitting...\n");
Network::Quit();
/* While we wait, write config file */
{
std::ofstream config_file(NTRVIEW_DIR "/ntrview.ini", std::ios::binary);
config.SaveINI(config_file);
} //config_file goes out of scope here
printf("waiting for network to quit\n");
networkThread.join();
printf("network quit!\n");
//ProcUI hack lol
//SDL_DestroyRenderer(renderer);
//SDL_DestroyWindow(window);
printf("done!\n");
return 0;
}
void update3DSTextures(tjhandle tj_handle, Gfx::Texture& topTexture, Gfx::Texture& btmTexture) {
static int lastTopJPEG = 0, lastBtmJPEG = 0;
int ret;
int cTopJPEG = Network::GetTopJPEGID();
if (lastTopJPEG != cTopJPEG) {
auto jpeg = Network::GetTopJPEG(cTopJPEG);
auto pixels = topTexture.Lock();
if (topTexture.locked && !pixels.empty()) {
ret = tjDecompress2(tj_handle,
jpeg.data(), jpeg.size(), pixels.data(),
topTexture.d.w, topTexture.pitch, 0,
TJPF_RGBA, 0
);
topTexture.Unlock(pixels);
if (ret) {
printf("[Decoder] %s\n", tjGetErrorStr());
}
lastTopJPEG = cTopJPEG;
} else {
printf("[Decoder] Error: %s\n", Gfx::GetError());
}
}
int cBtmJPEG = Network::GetBtmJPEGID();
if (lastBtmJPEG != cBtmJPEG) {
auto jpeg = Network::GetBtmJPEG(cBtmJPEG);
auto pixels = btmTexture.Lock();
if (btmTexture.locked && !pixels.empty()) {
ret = tjDecompress2(tj_handle,
jpeg.data(), jpeg.size(), pixels.data(),
btmTexture.d.w, btmTexture.pitch, 0,
TJPF_RGBA, 0
);
btmTexture.Unlock(pixels);
if (ret) {
printf("[Decoder] %s\n", tjGetErrorStr());
}
lastBtmJPEG = cBtmJPEG;
} else {
printf("[Decoder] Error: %s\n", Gfx::GetError());
}
}
}