-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
306 lines (263 loc) · 7.18 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
#include <stdio.h>
#include <stdint.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <ctype.h>
#include <time.h>
#include "flexptc.h"
#include "palerp.h"
#include "vec.h"
#include "fast_obj.h"
#include "polydraw.h"
#include "linedraw.h"
#include "matrix.h"
#include "3d.h"
#include "mesh.h"
#include "3dclip.h"
#include "3dsort.h"
#include "bmpload.h"
#include "esfmout.h"
#include "irq0.h"
#include "opmplay.h"
#include "player.h"
#include "main.h"
#include "atmta.h"
#include "menu.h"
#include "introrc.h"
main_props_t mainprops;
PTC_MODEHANDLE gfx_mode;
void close_all() {
#ifndef WIN32
// free IRQ0 timer
irq0_freeTimer();
// set ESFM back to OPL3 mode
esfm_reset();
esfm_disable();
// free opmplay
opmplay_free(&player_ctx.opm);
#endif
}
#ifndef WIN32
#include <i86.h>
#include <dos.h>
#include <direct.h>
static void (__interrupt __far *irq2F_oldhandler)(); // internal procedure - old INT70h handler
void _interrupt _far int2F_handler (union INTPACK r) {
if (r.w.ax == 0x1687) {
// mark DPMI as not present
return;
} else _chain_intr(irq2F_oldhandler);
}
// mad stuff!
void dos_shell() {
ptc_donemode();
_asm {
mov eax, 3
int 0x10
}
char *cwd = getcwd(NULL, NULL);
uint32_t drive, dummy; _dos_getdrive(&drive);
//printf("cwd = %s\n", cwd);
// jump to DOS
printf("type \"exit\" to return back!\n");
//system("command.com");
system("%COMSPEC%");
// return back
chdir(cwd);
free(cwd);
_dos_setdrive(drive, &dummy);
// reinit gfx mode
int rtn;
if ((rtn = ptc_setmode(gfx_mode)) != ptc_errOK) {
printf("can't open video mode - error %d\n", rtn);
close_all();
}
}
#else
void dos_shell() {
}
#endif
// ----------------------
// init gfxmode
bool init_gfx() {
#ifdef WIN32
mainprops.automode = true;
#endif
int ptcflags =
(mainprops.automode ? 0 : PTC_FORCEMANUAL) |
(mainprops.use_irq0 ? 0 : PTC_TIMERHANDLER);
int modeflags = PTC_MODE_MULTIPLEBUFFER | PTC_MODE_DOUBLED | PTC_MODE_WINDOWED;
int rtn = 0;
if ((rtn = ptc_init(ptcflags)) != ptc_errOK) {
printf("can't init flexptc - error %d\n", rtn);
return true;
}
gfx_mode = ptc_addmode(X_RES, Y_RES, 8, 60, modeflags);
if (gfx_mode < 0) {
if (gfx_mode != ptc_mode_USEREXIT) printf("can't find 320x200 8bpp video mode - error %d\n", gfx_mode);
return true;
}
return false;
}
bool set_gfx() {
int rtn;
if ((rtn = ptc_setmode(gfx_mode)) != ptc_errOK) {
if (rtn != ptc_mode_USEREXIT) printf("can't open video mode - error %d\n", rtn);
return true;
}
return false;
}
// done gfxmode
bool done_gfx() {
ptc_done();
return false;
}
// ------------------------
// check ESFM presence, prompt user for more actions
uint32_t try_esfm_detect() {
#ifndef WIN32
uint32_t iobase = esfm_detect();
if (iobase) {
printf("ESFM detected at base 0x%03X\n", iobase);
} else {
printf("ESFM not detected! Continue? <Y/N>\n");
char ch = getch();
if (ch == 'Y' || ch == 'y') iobase = 0x388; // try adlib default
}
return iobase;
#else
return 1; // fake
#endif
}
int main(int argc, char* argv[]) {
int esfmbase;
mainprops.automode = false;
mainprops.lq_mode = false;
mainprops.use_irq0 = true;
mainprops.fast_esfm_out = false;
mainprops.load_from_disk = false;
mainprops.skip_intro = false;
printf("koolnESS - an ESFM music disk - the furnace posse - 2o24\n");
printf("-----------------------------------\n");
// TODO: command line parsing
// parse argv
for (int p = 1; p < argc; p++) {
if (strstr(argv[p], "?")) {
printf("auto - autosetup\n");
printf("rtc - use RTC for timing instead of IRQ0\n");
printf("potato - lower display quality\n");
printf("nointro - skip intro (for 4MB machines)\n");
//printf("lowmem - load songs directly from disk instead of preloading at start\n");
printf("fast - remove delays from ESFM register out (can sound glitchy!)\n");
return 0;
}
#if 0
if (toupper(argv[p][0]) == 'W') {
mainprops.aspect = true;
}
#endif
if (toupper(argv[p][0]) == 'A') {
mainprops.automode = true;
}
if (toupper(argv[p][0]) == 'R') {
mainprops.use_irq0 = false;
}
if (toupper(argv[p][0]) == 'P') {
mainprops.lq_mode = true;
}
if (toupper(argv[p][0]) == 'N') {
mainprops.skip_intro = true;
}
#if 0
if (toupper(argv[p][0]) == 'L') {
mainprops.load_from_disk = true;
}
#endif
if (toupper(argv[p][0]) == 'F') {
mainprops.fast_esfm_out = true;
}
}
#ifndef WIN32
// try ESFM detect
if ((esfmbase = try_esfm_detect()) == 0) return 1;
// install IRQ0 timer
irq0_initTimer();
#endif
// initialize RNG
srand(time(NULL));
// init flexptc
if (init_gfx() != false) goto deinit;
// init player context
if (player_init(!mainprops.load_from_disk) != false) goto deinit;
printf("."); fflush(stdout);
// init parts
if (common_resources::init()) goto deinit;
printf("."); fflush(stdout);
if (menu::init()) goto deinit;
printf("."); fflush(stdout);
if (!mainprops.skip_intro) {
if (intro_resources::init()) goto deinit;
printf("."); fflush(stdout);
if (atmta::init()) goto deinit;
}
printf("."); fflush(stdout);
printf("-----------------------\n");
if (set_gfx() != false) goto deinit;
ptc_wait(-1);
ptc_wait(-1);
// we are in graphics mode now
// get refresh rate
{
// dummy
ptc_wait(-1);
float t = ptc_getfloat();
ptc_wait(-1);
t = ptc_getfloat() - t;
}
mainprops.frame_time = 0;
for (int i = 0; i < 4; i++) {
ptc_wait(-1);
float t = ptc_getfloat();
ptc_wait(-1);
mainprops.frame_time += ptc_getfloat() - t;
}
mainprops.frame_time *= (1.0 / 4.0);
// frame limit for scroller
if (mainprops.frame_time < (1.0 / 77.0)) mainprops.frame_time = (1.0 / 60.);
#ifndef WIN32
// init ESFM
esfm_set_baseport(esfmbase);
esfm_enable();
esfm_reset();
#endif
// wait for 1.5 seconds to wait for capture device
// TODO
ptc_resettimer();
while ((ptc_getfloat() < 1.5) && (!kbhit()));
ptc_resettimer();
// load first module
player_load_module_idx(&player_ctx, 0);
player_ctx.nextsong.loop_count = modulelist[0].loop_count; // HACK HACK!!!
//player_load_module(&player_ctx, modulelist[0].file);
//player_load_module_packed(&player_ctx, modulelist[0].data, modulelist[0].raw_size);
if (!mainprops.skip_intro) {
// run atmta
atmta::run();
// DEINIT wirebars and atmta to free memory
//wirebars::done();
atmta::done();
intro_resources::done();
}
// run menu
menu::run();
// deinit menu
menu::done();
// shut down flexptc
done_gfx();
deinit:
// close all stuff
close_all();
return 0;
}