-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathspinv.hpp
260 lines (223 loc) · 6.59 KB
/
spinv.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
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
#pragma once
//=====================================================================//
/*! @file
@brief Space Invaders Emulator (side)
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2018 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include "side/arcade.h"
#include "common/file_io.hpp"
#include "sound/snd_mgr.hpp"
extern "C" {
uint8_t get_fami_pad();
};
namespace emu {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief スペース・インベーダー・エミュレーション・クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class spinv {
public:
typedef sound::snd_mgr<9, 6, 184> SND_MGR;
private:
InvadersMachine im_;
uint16_t scan_lines_[InvadersMachine::ScreenHeight];
SND_MGR snd_mgr_;
uint32_t se_hnd_[9];
unsigned req_lvl_;
uint32_t ufo_hnd_;
bool load_sounds_(const char* root)
{
if(root == nullptr) {
return false;
}
static const char* sdf[] = {
"BaseHit.wav", "InvHit.Wav", "Shot.wav", "Ufo.wav",
"UfoHit.wav", "Walk1.wav", "Walk2.wav", "Walk3.wav",
"Walk4.wav"
};
uint32_t allsize = 0;
for(uint32_t i = 0; i < 9; ++i) {
char tmp[256];
strcpy(tmp, root);
strcat(tmp, "/");
strcat(tmp, sdf[i]);
se_hnd_[i] = snd_mgr_.set_sound(tmp);
utils::format("Load SE: '%s' at %d\n") % sdf[i] % se_hnd_[i];
}
return true;
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクタ
*/
//-----------------------------------------------------------------//
spinv() noexcept : im_(), se_hnd_{ 9 }, req_lvl_(0), ufo_hnd_(6) { }
//-----------------------------------------------------------------//
/*!
@brief 開始
@param[in] rom_path ROM ファイル・ルート・パス
@param[in] wav_path WAV ファイル・ルート・パス
@return 成功なら「tue」
*/
//-----------------------------------------------------------------//
bool start(const char* rom_path, const char* wav_path = nullptr)
{
static const char* rom_files[] = {
"invaders.h", "invaders.g", "invaders.f", "invaders.e"
};
{
char rom[0x2000];
for(uint32_t i = 0; i < 4; ++i) {
char tmp[256];
if(rom_path != nullptr) {
strcpy(tmp, rom_path);
} else {
strcpy(tmp, "/");
}
strcat(tmp, "/");
strcat(tmp, rom_files[i]);
utils::file_io fi;
if(!fi.open(tmp, "rb")) {
utils::format("Can't open: '%s'\n") % tmp;
return false;
}
if(fi.read(&rom[i * 0x800], 0x800) != 0x800) {
utils::format("Can't read data: '%s'\n") % tmp;
fi.close();
return false;
}
utils::format("Read ROM: '%s'\n") % tmp;
fi.close();
}
uint32_t sum = 0;
for(uint32_t i = 0; i < 0x2000; ++i) {
sum += (uint8_t)rom[i];
}
utils::format("ROM SUM: %08X\n") % sum;
im_.setROM(rom);
}
if(!load_sounds_(wav_path)) {
utils::format("Sound files not found. no sound\n");
}
im_.reset();
// auto fr = im_.getFrameRate();
// utils::format("FrameRate: %d\n") % fr;
for(int i = 0; i < InvadersMachine::ScreenHeight; ++i) {
uint16_t c = 0b11111'111111'11111;
if( i >= 32 && i < 64 ) c = 0b11111'000000'00000; // Red
if( i >= 184 && i < 240 ) c = 0b00000'111111'00000; // Green
scan_lines_[i] = c;
}
return true;
}
//-----------------------------------------------------------------//
/*!
@brief サービス
@param[in] org フレームバッファアドレス
@param[in] w 横幅
@param[in] h 高さ
*/
//-----------------------------------------------------------------//
void service(uint32_t org, int w, int h)
{
uint8_t pad = get_fami_pad();
const uint8_t* video = im_.getVideo();
if(video != nullptr) {
uint16_t* fb = reinterpret_cast<uint16_t*>(org);
uint32_t yo = (h - InvadersMachine::ScreenHeight) / 2;
uint32_t xo = (w - InvadersMachine::ScreenWidth) / 2;
for(uint32_t y = 0; y < InvadersMachine::ScreenHeight; ++y) {
uint32_t c = scan_lines_[y];
for(uint32_t x = 0; x < InvadersMachine::ScreenWidth; ++x) {
if( *video ) {
fb[(y + yo) * w + x + xo] = c;
} else {
fb[(y + yo) * w + x + xo] = 0x0000;
}
++video;
}
}
}
// 1P
if(chip::on(pad, chip::FAMIPAD_ST::START)) {
im_.fireEvent(InvadersMachine::KeyOnePlayerDown);
} else {
im_.fireEvent(InvadersMachine::KeyOnePlayerUp);
}
// 2P
// if(dev.get_positive(gl::device::key::_2)) {
// im_.fireEvent(InvadersMachine::KeyTwoPlayersDown);
// }
// if(dev.get_negative(gl::device::key::_2)) {
// im_.fireEvent(InvadersMachine::KeyTwoPlayersUp);
// }
// coin
if(chip::on(pad, chip::FAMIPAD_ST::SELECT)) {
im_.fireEvent(InvadersMachine::CoinInserted);
}
// Left
if(chip::on(pad, chip::FAMIPAD_ST::LEFT)) {
im_.fireEvent( InvadersMachine::KeyLeftDown);
} else {
im_.fireEvent( InvadersMachine::KeyLeftUp);
}
// Right
if(chip::on(pad, chip::FAMIPAD_ST::RIGHT)) {
im_.fireEvent( InvadersMachine::KeyRightDown);
} else {
im_.fireEvent( InvadersMachine::KeyRightUp);
}
// Fire
if(chip::on(pad, chip::FAMIPAD_ST::A)) {
im_.fireEvent( InvadersMachine::KeyFireDown);
} else {
im_.fireEvent( InvadersMachine::KeyFireUp);
}
im_.step();
static const unsigned se_mask[9] = {
InvadersMachine::SoundBaseHit,
InvadersMachine::SoundInvaderHit,
InvadersMachine::SoundShot,
InvadersMachine::SoundUfo,
InvadersMachine::SoundUfoHit,
InvadersMachine::SoundWalk1,
InvadersMachine::SoundWalk2,
InvadersMachine::SoundWalk3,
InvadersMachine::SoundWalk4
};
unsigned lvl = im_.getSounds();
unsigned pos = ~req_lvl_ & lvl;
unsigned neg = req_lvl_ & ~lvl;
for(int i = 0; i < 9; ++i) {
if(i == 3) {
if(pos & se_mask[i]) {
ufo_hnd_ = snd_mgr_.request(se_hnd_[i], true);
}
if(neg & se_mask[i]) {
snd_mgr_.stop(ufo_hnd_);
ufo_hnd_ = snd_mgr_.sound_max();
}
} else {
if(pos & se_mask[i]) {
snd_mgr_.request(se_hnd_[i]);
}
}
}
req_lvl_ = lvl;
snd_mgr_.update();
}
//-----------------------------------------------------------------//
/*!
@brief サウンド・マネージャーの参照
@return サウンド・マネージャー
*/
//-----------------------------------------------------------------//
SND_MGR& at_sound() noexcept { return snd_mgr_; }
};
}