-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
308 lines (266 loc) · 6.64 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
/**
* Mac Demo
* Copyright (C) 2009 by Sami Kyostila <[email protected]>
*/
#include "Config.h"
#include "Demo.h"
#include "Font.h"
#include "Video.h"
#include "Audio.h"
#include "Mixer.h"
#include "ModPlayer.h"
#include "monoxide.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#if defined(_MSC_VER)
# include <sdl_main.h>
#endif
void flipScreen();
uint32_t swapEndian(uint32_t n);
MXSurface* loadImage(FILE* packFile);
/*
* Configuration
*/
#define PACKFILE "images.dat"
#define SONGFILE "macmod.mod"
#define SONG_SPEED 6
#define SCREEN_WIDTH 512
#define SCREEN_HEIGHT 342
#define MUSIC_LENGTH (2 * 60 + 15)
//#define MUSIC_STATS
//#define DEMO_STATS
//#define DEMO_LOOP
#if !defined(CODEWARRIOR)
# define REALTIME_MUSIC
#endif
#define RAWMUSICFILE "music.raw"
/*
* Global state
*/
static MXSurface* screen = 0;
static Audio* audio = 0;
static Video* video = 0;
static MXSurface* physicalScreen = 0;
int sawtooth(int t)
{
t &= 0xff;
if (t > 0x7f)
{
t = 0x7f + (0x7f - t);
}
return t;
}
inline int min(int a, int b)
{
return (a < b) ? a : b;
}
inline int max(int a, int b)
{
return (a > b) ? a : b;
}
inline int absi(int v)
{
const int mask = v >> sizeof(int) * 8 - 1;
return (v + mask) ^ mask;
}
inline int pow2(int i)
{
return i * i;
}
class MusicRenderer: public AudioRenderer
{
public:
MusicRenderer(int _mixFreq)
{
mixFreq = _mixFreq;
readBytes = playBytes = 0;
audioBuffer = new SampleChunk(&audio->format(), 0x120000, mixFreq);
assert(audioBuffer);
assert(audioBuffer->data);
audioFile = fopen(RAWMUSICFILE, "rb");
assert(audioFile);
preload(audioBuffer->bytes >> 1);
}
~MusicRenderer()
{
delete audioBuffer;
fclose(audioFile);
remove(RAWMUSICFILE);
}
void preload(int bytesToLoad = 0x9000)
{
while (bytesToLoad > 0)
{
int readPos = readBytes % (audioBuffer->bytes);
int readSize = min(bytesToLoad, audioBuffer->bytes - readPos);
#ifdef MUSIC_STATS
printf("LOAD %d bytes at %d (%d total)\n", readSize, readPos, readBytes);
#endif
int n = fread(audioBuffer->data + readPos, 1, readSize, audioFile);
if (n <= 0)
{
break;
}
bytesToLoad -= n;
readBytes += n;
}
}
void render(SampleChunk* buffer)
{
int bytesToPlay = buffer->bytes;
while (playBytes + buffer->bytes > readBytes && !feof(audioFile))
{
#ifdef MUSIC_STATS
printf("UNDERRUN (%d read, %d played)\n", readBytes, playBytes);
#endif
//preload();
return;
}
while (bytesToPlay > 0)
{
int playPos = playBytes % (audioBuffer->bytes);
int playSize = min(bytesToPlay, audioBuffer->bytes - playPos);
#ifdef MUSIC_STATS
printf("PLAY %d bytes (%d total, %d read)\n", playSize, playBytes, readBytes);
#endif
memcpy(buffer->data, audioBuffer->data + playPos, playSize);
bytesToPlay -= playSize;
playBytes += playSize;
}
}
private:
FILE* audioFile;
SampleChunk* audioBuffer;
int readBytes, playBytes, mixFreq;
};
static MusicRenderer* musicRenderer = 0;
#include "Effects.h"
void setup(bool fullscreen)
{
video = new Video(SCREEN_WIDTH, SCREEN_HEIGHT, fullscreen);
physicalScreen = mxCreateUserMemorySurface(video->screenWidth(), video->screenHeight(),
MX_PIXELFORMAT_I1, video->screenStride(),
0, video->screenPixels());
screen = mxCreateSurface(video->screenWidth(), video->screenHeight(), MX_PIXELFORMAT_I1, 0);
#if defined(CODEWARRIOR)
audio = new Audio(8, 11127, false, 0x1000);
#else
audio = new Audio(8, 44100, false, 4096);
#endif
//assert(player.load(SONGFILE));
//player.play();
//audio->start(&mixer);
setupEffects();
}
void teardown()
{
teardownEffects();
delete audio;
mxDestroySurface(screen);
mxDestroySurface(physicalScreen);
delete video;
}
void flipScreen()
{
video->waitRefresh();
mxBlit(physicalScreen, screen, NULL, 0, 0, NULL, 0);
video->swapBuffers();
}
uint32_t swapEndian(uint32_t x)
{
return (x >> 24) |
((x << 8) & 0x00ff0000) |
((x >> 8) & 0x0000ff00) |
(x << 24);
}
MXSurface* loadImage(FILE* packFile)
{
MXSurface header;
MXSurface* surf;
int headerLen = fread(&header, sizeof(int) * 7, 1, packFile);
assert(headerLen == 1);
#ifdef BIG_ENDIAN
header.w = swapEndian(header.w);
header.h = swapEndian(header.h);
header.pixelFormat = (MXPixelFormat)swapEndian(header.pixelFormat);
header.flags = swapEndian(header.flags);
header.planeSize = swapEndian(header.planeSize);
#endif
surf = mxCreateSurface(header.w, header.h, header.pixelFormat, header.flags);
if (!surf)
{
return surf;
}
int readLen = fread(surf->pixels, header.planeSize, 1, packFile);
assert(readLen == 1);
mxFlushSurface(surf);
return surf;
}
void demo()
{
int time, startTime;
int frameStartTime, frames = 0;
char status[64];
Timeline timeline(effects);
status[0] = 0;
frameStartTime = startTime = video->ticks();
while (1)
{
time = video->ticks();
if (!timeline.run(time - startTime))
{
#if DEMO_LOOP
timeline.reset();
startTime = time;
#else
break;
#endif
}
#ifdef DEMO_STATS
drawDebugText(screen, 512 - 20 * 8, 0, status);
#endif
flipScreen();
if (!video->processInput())
{
break;
}
#ifdef DEMO_STATS
if (++frames == 60 && time > frameStartTime)
{
int fps = 1000 * ((frames << 16) / ((time - frameStartTime)));
frameStartTime = time;
frames = 0;
sprintf(status, "%d fps, %d ms", fps >> 16, time);
}
#endif
}
}
void usage()
{
printf("Usage: three_and_a_half_inches_is_enough [OPTIONS]\n");
printf("Options:\n");
printf("-h This text\n");
printf("-w Windowed mode\n");
}
int main(int argc, char** argv)
{
bool fullscreen = true;
int i;
for (i = 0; i < argc; i++)
{
if (!strcmp(argv[i], "-h"))
{
usage();
return 0;
}
else if (!strcmp(argv[i], "-w"))
{
fullscreen = false;
}
}
setup(fullscreen);
demo();
teardown();
return 0;
}