forked from bailli/Johnny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
197 lines (164 loc) · 4.61 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
#include <iostream>
#include "RESFile.h"
#include "SCRFile.h"
#include "PALFile.h"
#include "BMPFile.h"
#include "Robinson.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;
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,
640,
480,
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, 640, 480);
return true;
}
int main()
{
if (!init())
{
std::cerr << "Error in init()!" << std::endl;
return 1;
}
cout << "Hello Johnny's World!" << endl;
SCRANTIC::Robinson *crusoe = new SCRANTIC::Robinson("RESOURCE.MAP", "SCRANTIC.SCR");
crusoe->initRenderer(g_Renderer);
crusoe->initMenu(g_Font);
crusoe->loadMovie("VISITOR.ADS", 3);
crusoe->startMovie();
bool quit = false;
SDL_Event e;
u_int16_t 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;
}