-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_varvara.hpp
177 lines (159 loc) · 5.27 KB
/
circle_varvara.hpp
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
#pragma once
#include <circle/koptions.h>
#include <circle/devicenameservice.h>
#include <circle/2dgraphics.h>
#include <circle/serial.h>
#include <circle/exceptionhandler.h>
#include <circle/interrupt.h>
#include <circle/timer.h>
#include <circle/time.h>
#include <circle/logger.h>
#include <circle/usb/usbhcidevice.h>
#include <circle/usb/usbgamepad.h>
#include <circle/sound/soundbasedevice.h>
#include <circle/types.h>
#include <fatfs/ff.h>
#include "uxn-cpp/varvara.hpp"
#include "safe_shutdown.hpp"
namespace uxn {
class CircleConsole : public Console {
CLogger& logger;
char buf[1024];
u16 buf_sz;
public:
CircleConsole(Uxn& uxn, CLogger& logger) : Console(uxn), logger(logger), buf_sz(0) {}
void write_byte(u8 b) final;
void flush();
};
class CircleScreen : public PixelScreen<TScreenColor> {
C2DGraphics& gfx;
u16 offset_x, offset_y;
u8 zoom;
public:
CircleScreen(Uxn& uxn, C2DGraphics& gfx) :
PixelScreen<TScreenColor>(uxn, gfx.GetWidth(), gfx.GetHeight()),
gfx(gfx), offset_x(0), offset_y(0), zoom(1) {}
void try_resize(u16 width, u16 height) final {
auto max_w = gfx.GetWidth(), max_h = gfx.GetHeight();
PixelScreen::try_resize(width > max_w ? max_w : width, height > max_h ? max_h : height);
}
void on_resize() final {
auto max_w = gfx.GetWidth(), max_h = gfx.GetHeight();
for (zoom = 1; w * zoom <= max_w && h * zoom <= max_h; zoom += 1) {}
if (zoom > 1) zoom -= 1;
offset_x = (max_w - w * zoom) / 2, offset_y = (max_h - h * zoom) / 2;
}
protected:
TScreenColor color_from_12bit(u8 r, u8 g, u8 b, u8 index) const final {
return COLOR16(r*2, g*2, b*2);
}
void on_paint() final {
if (offset_x || offset_y) gfx.ClearScreen(palette[0]);
if (zoom <= 1) {
gfx.DrawImage(offset_x, offset_y, w, h, const_cast<TScreenColor*>(pixels));
} else {
auto max_w = gfx.GetWidth();
TScreenColor* buf = gfx.GetBuffer();
for (unsigned x = 0; x < w; x++) {
for (unsigned y = 0; y < h; y++) {
unsigned pixel_ix = y * w + x;
for (unsigned xx = offset_x + x * zoom; xx < offset_x + (x+1) * zoom; xx++) {
for (unsigned yy = offset_y + y * zoom; yy < offset_y + (y+1) * zoom; yy++) {
buf[yy * max_w + xx] = pixels[pixel_ix];
}
}
}
}
}
gfx.UpdateDisplay();
}
};
class CircleAudio : public Audio {
static constexpr size_t BUFSIZE = static_cast<size_t>(AUDIO_BUFSIZE);
static void need_data_callback(void* user_data) {
CircleAudio* self = reinterpret_cast<CircleAudio*>(user_data);
u8 buf[BUFSIZE];
self->write(buf, BUFSIZE);
self->device->Write(buf, BUFSIZE);
}
CSoundBaseDevice* device;
public:
CircleAudio(Uxn& uxn, CSoundBaseDevice* device) : Audio(uxn), device(device) {}
bool init() final {
// if we have no audio device, don't bother
if (!device) return true;
device->SetWriteFormat(TSoundFormat::SoundFormatSigned16, 2);
device->RegisterNeedDataCallback(need_data_callback, this);
// Allocate a queue of AUDIO_BUFSIZE 16-bit frames.
// AUDIO_BUFSIZE is actually in bytes, so this is twice the buffer size.
// This is intentional: according to Circle's documentation,
// RegisterNeedDataCallback's callback is called when at least half
// of the queue is empty, so we can always safely write the full buffer.
return device->AllocateQueueFrames(BUFSIZE);
}
void start(u8 instance) final {
if (!device) return;
Audio::start(instance);
if (!device->IsActive()) device->Start();
}
};
class CircleFilesystem : public Filesystem {
FATFS& fs;
CLogger& logger;
enum class OpenState : u8 { None, ReadFile, ReadDir, Write, Append };
union { FIL fil; DIR dir; } open_file;
OpenState open_state = OpenState::None;
FILINFO last_filinfo;
u8* loaded_rom = nullptr;
u32 loaded_rom_capacity = 0;
char absolute_buffer[UXN_PATH_MAX] = {0};
const char* absolute_filename(const char* relative);
public:
CircleFilesystem(Uxn& uxn, FATFS& fs, CLogger& logger) : Filesystem(uxn), fs(fs), logger(logger) {}
virtual ~CircleFilesystem() { close(); if (loaded_rom) delete[] loaded_rom; }
bool init() final { return true; }
const u8* load(const char* filename, size_t& out_size) final;
protected:
void close() final;
u16 read(MutableSlice dest) final;
Stat stat() final;
bool list_dir(Stat& out) final;
u16 write(Slice src, u8 append) final;
u16 remove() final;
};
class CircleDatetime : public Datetime {
CTimer& timer;
public:
CircleDatetime(CTimer& timer) : timer(timer) {}
u8 datetime_byte(u8 port) final;
};
class CircleVarvara : public Varvara {
CircleConsole console;
CircleScreen screen;
CircleAudio audio;
Input input;
CircleFilesystem file;
CircleDatetime datetime;
C2DGraphics& gfx;
CTimer& timer;
public:
CircleVarvara(
C2DGraphics& gfx,
CSoundBaseDevice* sound,
CTimer& t,
CLogger& logger,
FATFS& fs,
const char* rom_filename = "boot.rom"
) : console(*this, logger),
screen(*this, gfx),
audio(*this, sound),
input(*this),
file(*this, fs, logger),
datetime(t),
Varvara(&console, &screen, &audio, &input, &file, &datetime, rom_filename),
gfx(gfx),
timer(t) {}
void game_pad_input(const TGamePadState* state);
ShutdownMode run(SafeShutdown* safe_shutdown = nullptr);
};
}