-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
246 lines (201 loc) · 6.56 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
#include <iostream>
#include "RESFile.h"
#include "SCRFile.h"
#include "PALFile.h"
#include "BMPFile.h"
#include "Robinson.h"
#include "CommandlineParser.h"
#include "RIFFPlayer.h"
#ifdef WIN32
#include <SDL.h>
#include <SDL_ttf.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#endif
#define ticksPerFrame 1000/30
using namespace std;
SDL_Window *g_Mainwindow = NULL;
SDL_Renderer *g_Renderer = NULL;
TTF_Font *g_Font = NULL;
std::string g_path = "";
bool g_readUnpackedRes = false;
void cleanup() {
if (g_Renderer != NULL) {
SDL_DestroyRenderer(g_Renderer);
}
if (g_Mainwindow != NULL) {
SDL_DestroyWindow(g_Mainwindow);
}
TTF_CloseFont(g_Font);
Mix_Quit();
TTF_Quit();
SDL_Quit();
}
bool init() {
//Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL could not initialize VIDEO! SDL_Error: " << SDL_GetError() << std::endl;
return false;
}
if (TTF_Init() < 0) {
std::cerr << "SDL could not initialize TTF! SDL_Error: " << SDL_GetError() << std::endl;
return false;
}
g_Font = TTF_OpenFont("font.ttf", 14);
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
std::cerr << "SDL could not initialize AUDIO! SDL_Error: " << SDL_GetError() << std::endl;
return false;
}
if (Mix_OpenAudio(11025, AUDIO_U8, 1, 2048) < 0) {
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
return false;
}
//Create window
g_Mainwindow = SDL_CreateWindow(
"Johnny's World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL_WINDOW_SHOWN
);
if (g_Mainwindow == NULL) {
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
cleanup();
return false;
}
g_Renderer = SDL_CreateRenderer(g_Mainwindow, -1, 0);
if (g_Renderer == NULL) {
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
cleanup();
return false;
}
// Set size of renderer to the same as window
SDL_RenderSetLogicalSize(g_Renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
return true;
}
bool handleCommandline(char **begin, char **end) {
CommandlineParser commandlineParser(begin, end);
std::string path = "";
std::string outputPath = "";
std::string prepackedPath = "";
if (commandlineParser.cmdOptionExists("--resourcePath")) {
path = commandlineParser.getCmdOption("--resourcePath");
if (path.substr(path.size() - 1) != "/") {
path += "/";
}
g_path = path;
}
if (commandlineParser.cmdOptionExists("--outputPath")) {
outputPath = commandlineParser.getCmdOption("--outputPath");
if (outputPath.substr(outputPath.size() - 1) != "/") {
outputPath += "/";
}
}
if (commandlineParser.cmdOptionExists("--prepackedPath")) {
prepackedPath = commandlineParser.getCmdOption("--prepackedPath");
if (prepackedPath.substr(prepackedPath.size() - 1) != "/") {
prepackedPath += "/";
}
}
if (commandlineParser.cmdOptionExists("--unpack")) {
bool onlyFiles = commandlineParser.cmdOptionExists("--onlyFiles");
SCRANTIC::RESFile res(path + "RESOURCE.MAP", false);
res.unpackResources(outputPath, onlyFiles);
SCRANTIC::RIFFPlayer::extractRIFFFiles(path + "SCRANTIC.SCR", outputPath + "RIFF/", true);
return true;
}
if (commandlineParser.cmdOptionExists("--repack")) {
SCRANTIC::RESFile res(path + "RESOURCE.MAP", true);
res.repackResources(outputPath, prepackedPath);
return true;
}
if (commandlineParser.cmdOptionExists("--unpackedResources")) {
std::cout << "Unpacked resources will be read from " << path << std::endl;
g_readUnpackedRes = true;
}
return false;
}
int main(int argc, char **argv) {
if (handleCommandline(argv, argv + argc)) {
return 0;
}
if (!init()) {
std::cerr << "Error in init()!" << std::endl;
return 1;
}
cout << "Hello Johnny's World!" << endl;
SCRANTIC::Robinson *crusoe = new SCRANTIC::Robinson(g_path, g_readUnpackedRes);
crusoe->initRenderer(g_Renderer, g_Font);
crusoe->loadMovie("VISITOR.ADS", 3);
crusoe->startMovie();
bool quit = false;
SDL_Event e;
u16 delay;
Uint32 ticks, newTicks, frameTicks, waitTicks;
ticks = SDL_GetTicks();
bool waiting = false;
bool pause = false;
while (!quit) {
newTicks = SDL_GetTicks();
frameTicks = newTicks - ticks;
if (!waiting) {
crusoe->advanceScripts();
if (!crusoe->isMovieRunning()) {
crusoe->displayMenu(true);
}
delay = crusoe->getCurrentDelay();
if (delay == 0) {
delay = 100;
}
}
if (!pause) {
if (delay > frameTicks) {
delay -= frameTicks;
} else {
delay = 0;
}
}
if (frameTicks < ticksPerFrame) {
waitTicks = ticksPerFrame - frameTicks;
} else {
waitTicks = 0;
}
ticks = newTicks;
waiting = (delay > waitTicks);
SDL_Delay(waitTicks);
crusoe->render();
SDL_RenderPresent(g_Renderer);
while(SDL_PollEvent(&e) != 0) {
switch(e.type) {
case SDL_KEYDOWN:
//case SDL_KEYUP:
if (e.key.keysym.sym == SDLK_SPACE && !e.key.repeat) {
pause = !pause;
} else if (e.key.keysym.sym == SDLK_ESCAPE && !e.key.repeat) {
if (crusoe->isMenuOpen()) {
crusoe->displayMenu(false);
pause = false;
} else {
quit = true;
}
} else if (!e.key.repeat && crusoe->isMenuOpen()) {
if (crusoe->navigateMenu(e.key.keysym.sym)) {
pause = false;
}
} else if (e.key.keysym.sym == SDLK_RETURN && !e.key.repeat) {
pause = true;
crusoe->displayMenu(true);
}
break;
case SDL_QUIT:
quit = true;
break;
}
}
}
delete crusoe;
cleanup();
return 0;
}