Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rubberduckycooly
GitHub Repository: rubberduckycooly/Sonic-1-2-2013-Decompilation
Path: blob/main/RSDKv4/RetroEngine.hpp
817 views
1
#ifndef RETROENGINE_H
2
#define RETROENGINE_H
3
4
// Disables POSIX use c++ name blah blah stuff
5
#pragma warning(disable : 4996)
6
7
// Setting this to true removes (almost) ALL changes from the original code, the trade off is that a playable game cannot be built, it is advised to
8
// be set to true only for preservation purposes
9
#ifndef RETRO_USE_ORIGINAL_CODE
10
#define RETRO_USE_ORIGINAL_CODE (0)
11
#endif
12
13
#ifndef RETRO_USE_MOD_LOADER
14
#define RETRO_USE_MOD_LOADER (!RETRO_USE_ORIGINAL_CODE && 1)
15
#endif
16
17
#ifndef RETRO_USE_NETWORKING
18
#define RETRO_USE_NETWORKING (!RETRO_USE_ORIGINAL_CODE && 1)
19
#endif
20
21
// Forces all DLC flags to be disabled, this should be enabled in any public releases
22
#ifndef RSDK_AUTOBUILD
23
#define RSDK_AUTOBUILD (0)
24
#endif
25
26
// ================
27
// STANDARD LIBS
28
// ================
29
#include <stdio.h>
30
#include <string.h>
31
#include <cmath>
32
#if RETRO_USE_MOD_LOADER
33
#include <regex>
34
#endif
35
36
// ================
37
// STANDARD TYPES
38
// ================
39
typedef unsigned char byte;
40
typedef signed char sbyte;
41
typedef unsigned short ushort;
42
typedef unsigned int uint;
43
// typedef unsigned long long ulong;
44
45
// Platforms (RSDKv4 only defines these 7 (I assume), but feel free to add your own custom platform define for easier platform code changes)
46
#define RETRO_WIN (0)
47
#define RETRO_OSX (1)
48
#define RETRO_XBOX_360 (2)
49
#define RETRO_PS3 (3)
50
#define RETRO_iOS (4)
51
#define RETRO_ANDROID (5)
52
#define RETRO_WP7 (6)
53
// Custom Platforms start here
54
#define RETRO_UWP (7)
55
#define RETRO_LINUX (8)
56
57
// Platform types (Game manages platform-specific code such as HUD position using this rather than the above)
58
#define RETRO_STANDARD (0)
59
#define RETRO_MOBILE (1)
60
61
#if defined _WIN32
62
63
#if defined WINAPI_FAMILY
64
#if WINAPI_FAMILY != WINAPI_FAMILY_APP
65
#define RETRO_PLATFORM (RETRO_WIN)
66
#define RETRO_DEVICETYPE (RETRO_STANDARD)
67
#else
68
#include <WInRTIncludes.hpp>
69
70
#define RETRO_PLATFORM (RETRO_UWP)
71
#define RETRO_DEVICETYPE (UAP_GetRetroGamePlatform())
72
#endif
73
#else
74
#define RETRO_PLATFORM (RETRO_WIN)
75
#define RETRO_DEVICETYPE (RETRO_STANDARD)
76
#endif
77
78
#elif defined __APPLE__
79
#if __IPHONEOS__
80
#define RETRO_PLATFORM (RETRO_iOS)
81
#define RETRO_DEVICETYPE (RETRO_MOBILE)
82
#else
83
#define RETRO_PLATFORM (RETRO_OSX)
84
#define RETRO_DEVICETYPE (RETRO_STANDARD)
85
#endif
86
#elif defined __ANDROID__
87
#define RETRO_PLATFORM (RETRO_ANDROID)
88
#define RETRO_DEVICETYPE (RETRO_MOBILE)
89
#include <jni.h>
90
#elif defined(__linux__)
91
#define RETRO_PLATFORM (RETRO_LINUX)
92
#define RETRO_DEVICETYPE (RETRO_STANDARD)
93
#else
94
//#error "No Platform was defined"
95
#define RETRO_PLATFORM (RETRO_LINUX)
96
#define RETRO_DEVICETYPE (RETRO_STANDARD)
97
#endif
98
99
#define DEFAULT_SCREEN_XSIZE 424
100
#define DEFAULT_FULLSCREEN false
101
#define RETRO_USING_MOUSE
102
#define RETRO_USING_TOUCH
103
104
#ifndef BASE_PATH
105
#define BASE_PATH ""
106
#endif
107
108
#if !defined(RETRO_USE_SDL2) && !defined(RETRO_USE_SDL1)
109
#define RETRO_USE_SDL2 (1)
110
#endif
111
112
#if RETRO_PLATFORM == RETRO_WIN || RETRO_PLATFORM == RETRO_OSX || RETRO_PLATFORM == RETRO_LINUX || RETRO_PLATFORM == RETRO_UWP \
113
|| RETRO_PLATFORM == RETRO_ANDROID
114
#ifdef RETRO_USE_SDL2
115
#define RETRO_USING_SDL1 (0)
116
#define RETRO_USING_SDL2 (1)
117
#elif defined(RETRO_USE_SDL1)
118
#define RETRO_USING_SDL1 (1)
119
#define RETRO_USING_SDL2 (0)
120
#endif
121
#else // Since its an else & not an elif these platforms probably aren't supported yet
122
#define RETRO_USING_SDL1 (0)
123
#define RETRO_USING_SDL2 (0)
124
#endif
125
126
#if RETRO_PLATFORM == RETRO_iOS || RETRO_PLATFORM == RETRO_ANDROID || RETRO_PLATFORM == RETRO_WP7
127
#define RETRO_GAMEPLATFORM (RETRO_MOBILE)
128
#elif RETRO_PLATFORM == RETRO_UWP
129
#define RETRO_GAMEPLATFORM (UAP_GetRetroGamePlatform())
130
#else
131
#define RETRO_GAMEPLATFORM (RETRO_STANDARD)
132
#endif
133
134
#define RETRO_SW_RENDER (0)
135
#define RETRO_HW_RENDER (1)
136
137
#ifdef USE_SW_REN
138
#define RETRO_RENDERTYPE (RETRO_SW_RENDER)
139
#elif defined(USE_HW_REN)
140
#define RETRO_RENDERTYPE (RETRO_HW_RENDER)
141
#elif !defined(RETRO_RENDERTYPE)
142
#define RETRO_RENDERTYPE (RETRO_SW_RENDER)
143
#endif
144
145
#ifndef RETRO_USING_OPENGL
146
#define RETRO_USING_OPENGL (1)
147
#endif
148
149
#define RETRO_SOFTWARE_RENDER (RETRO_RENDERTYPE == RETRO_SW_RENDER)
150
//#define RETRO_HARDWARE_RENDER (RETRO_RENDERTYPE == RETRO_HW_RENDER)
151
152
#if RETRO_USING_OPENGL
153
#if RETRO_PLATFORM == RETRO_ANDROID
154
#define GL_GLEXT_PROTOTYPES
155
156
#include <GLES/gl.h>
157
#include <GLES/glext.h>
158
159
#undef glGenFramebuffers
160
#undef glBindFramebuffer
161
#undef glFramebufferTexture2D
162
#undef glDeleteFramebuffers
163
164
#undef GL_FRAMEBUFFER
165
#undef GL_COLOR_ATTACHMENT0
166
#undef GL_FRAMEBUFFER_BINDING
167
168
#define glGenFramebuffers glGenFramebuffersOES
169
#define glBindFramebuffer glBindFramebufferOES
170
#define glFramebufferTexture2D glFramebufferTexture2DOES
171
#define glDeleteFramebuffers glDeleteFramebuffersOES
172
173
#define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
174
#define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
175
#define GL_FRAMEBUFFER_BINDING GL_FRAMEBUFFER_BINDING_OES
176
#elif RETRO_PLATFORM == RETRO_OSX
177
#define GL_GLEXT_PROTOTYPES
178
#define GL_SILENCE_DEPRECATION
179
180
#include <OpenGL/gl.h>
181
#include <OpenGL/glext.h>
182
183
#undef glGenFramebuffers
184
#undef glBindFramebuffer
185
#undef glFramebufferTexture2D
186
#undef glDeleteFramebuffers
187
188
#undef GL_FRAMEBUFFER
189
#undef GL_COLOR_ATTACHMENT0
190
#undef GL_FRAMEBUFFER_BINDING
191
192
#define glGenFramebuffers glGenFramebuffersEXT
193
#define glBindFramebuffer glBindFramebufferEXT
194
#define glFramebufferTexture2D glFramebufferTexture2DEXT
195
#define glDeleteFramebuffers glDeleteFramebuffersEXT
196
197
#define GL_FRAMEBUFFER GL_FRAMEBUFFER_EXT
198
#define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_EXT
199
#define GL_FRAMEBUFFER_BINDING GL_FRAMEBUFFER_BINDING_EXT
200
#else
201
#include <GL/glew.h>
202
#endif
203
#endif
204
205
#define RETRO_USE_HAPTICS (1)
206
207
// NOTE: This is only used for rev00 stuff, it was removed in rev01 and later builds
208
#if RETRO_PLATFORM <= RETRO_WP7
209
#define RETRO_GAMEPLATFORMID (RETRO_PLATFORM)
210
#else
211
212
// use *this* macro to determine what platform the game thinks its running on (since only the first 7 platforms are supported natively by scripts)
213
#if RETRO_PLATFORM == RETRO_LINUX
214
#define RETRO_GAMEPLATFORMID (RETRO_WIN)
215
#elif RETRO_PLATFORM == RETRO_UWP
216
#define RETRO_GAMEPLATFORMID (UAP_GetRetroGamePlatformId())
217
#else
218
#error Unspecified RETRO_GAMEPLATFORMID
219
#endif
220
221
#endif
222
223
// Determines which revision to use (see defines below for specifics). Datafiles from REV00 and REV01 builds will not work on later revisions and vice versa.
224
#ifndef RSDK_REVISION
225
#define RSDK_REVISION (3)
226
#endif
227
228
// Revision from early versions of Sonic 1
229
#define RETRO_REV00 (RSDK_REVISION == 0)
230
231
// Revision from early versions of Sonic 2
232
#define RETRO_REV01 (RSDK_REVISION >= 1)
233
234
// Revision from the S3&K POC, this is also used in the Sega Forever versions of S1 & S2
235
#define RETRO_REV02 (RSDK_REVISION >= 2)
236
237
// Revision included as part of RSDKv5U (Sonic Origins)
238
#define RETRO_REV03 (RSDK_REVISION >= 3)
239
240
enum RetroLanguages {
241
RETRO_EN = 0,
242
RETRO_FR = 1,
243
RETRO_IT = 2,
244
RETRO_DE = 3,
245
RETRO_ES = 4,
246
RETRO_JP = 5,
247
RETRO_PT = 6,
248
RETRO_RU = 7,
249
RETRO_KO = 8,
250
RETRO_ZH = 9,
251
RETRO_ZS = 10,
252
};
253
254
#if RETRO_REV00
255
enum RetroEngineMessages {
256
MESSAGE_NONE = 0,
257
MESSAGE_MESSAGE_1 = 1,
258
MESSAGE_LOSTFOCUS = 2,
259
MESSAGE_MESSAGE_3 = 3,
260
MESSAGE_MESSAGE_4 = 4,
261
};
262
#endif
263
264
enum RetroStates {
265
ENGINE_DEVMENU = 0,
266
ENGINE_MAINGAME = 1,
267
ENGINE_INITDEVMENU = 2,
268
ENGINE_WAIT = 3,
269
ENGINE_SCRIPTERROR = 4,
270
ENGINE_INITPAUSE = 5,
271
ENGINE_EXITPAUSE = 6,
272
ENGINE_ENDGAME = 7,
273
ENGINE_RESETGAME = 8,
274
275
#if !RETRO_USE_ORIGINAL_CODE && RETRO_USE_NETWORKING
276
// Custom GameModes (required to make some features work)
277
ENGINE_CONNECT2PVS = 0x80,
278
ENGINE_WAIT2PVS = 0x81,
279
#endif
280
#if RETRO_USE_MOD_LOADER
281
ENGINE_INITMODMENU = 0x82,
282
#endif
283
};
284
285
enum RetroGameType {
286
GAME_UNKNOWN = 0,
287
GAME_SONIC1 = 1,
288
GAME_SONIC2 = 2,
289
};
290
291
// General Defines
292
#define SCREEN_YSIZE (240)
293
#define SCREEN_CENTERY (SCREEN_YSIZE / 2)
294
295
#if RETRO_PLATFORM == RETRO_WIN || RETRO_PLATFORM == RETRO_UWP || RETRO_PLATFORM == RETRO_ANDROID || RETRO_PLATFORM == RETRO_LINUX
296
#if RETRO_USING_SDL2
297
#include <SDL.h>
298
#elif RETRO_USING_SDL1
299
#include <SDL.h>
300
#endif
301
#include <vorbis/vorbisfile.h>
302
#elif RETRO_PLATFORM == RETRO_OSX
303
#include <SDL2/SDL.h>
304
#include <Vorbis/vorbisfile.h>
305
306
#include "cocoaHelpers.hpp"
307
308
#elif RETRO_USING_SDL2
309
#include <SDL2/SDL.h>
310
#include <vorbis/vorbisfile.h>
311
#else
312
313
#endif
314
315
#if !RETRO_USE_ORIGINAL_CODE
316
extern bool usingCWD;
317
extern bool engineDebugMode;
318
#endif
319
320
// Utils
321
#if !RETRO_USE_ORIGINAL_CODE
322
#include "Ini.hpp"
323
#endif
324
325
#include "Math.hpp"
326
#include "Reader.hpp"
327
#include "String.hpp"
328
#include "Animation.hpp"
329
#include "Audio.hpp"
330
#include "Input.hpp"
331
#include "Object.hpp"
332
#include "Palette.hpp"
333
#include "Drawing.hpp"
334
#include "Scene3D.hpp"
335
#include "Collision.hpp"
336
#include "Scene.hpp"
337
#include "Script.hpp"
338
#include "Sprite.hpp"
339
#include "Text.hpp"
340
#include "Networking.hpp"
341
#include "Renderer.hpp"
342
#include "Userdata.hpp"
343
#include "Debug.hpp"
344
#include "ModAPI.hpp"
345
346
// Native Entities
347
#include "NativeObjects.hpp"
348
349
class RetroEngine
350
{
351
public:
352
RetroEngine()
353
{
354
if (RETRO_GAMEPLATFORM == RETRO_STANDARD) {
355
gamePlatform = "STANDARD";
356
gameDeviceType = RETRO_STANDARD;
357
}
358
else {
359
gamePlatform = "MOBILE";
360
gameDeviceType = RETRO_MOBILE;
361
}
362
}
363
364
#if !RETRO_USE_ORIGINAL_CODE
365
bool usingDataFile_Config = false;
366
#endif
367
bool usingDataFile = false;
368
bool usingBytecode = false;
369
#if RETRO_REV03 && !RETRO_USE_ORIGINAL_CODE
370
bool usingOrigins = false;
371
#endif
372
373
char dataFile[RETRO_PACKFILE_COUNT][0x80];
374
375
bool initialised = false;
376
bool running = false;
377
double deltaTime = 0;
378
379
int gameMode = ENGINE_MAINGAME;
380
int language = RETRO_EN;
381
#if RETRO_REV00
382
int message = 0;
383
#endif
384
int gameDeviceType = RETRO_STANDARD;
385
int globalBoxRegion = REGION_JP;
386
bool nativeMenuFadeIn = false;
387
388
bool trialMode = false;
389
bool onlineActive = true;
390
bool useHighResAssets = false;
391
#if RETRO_USE_HAPTICS
392
bool hapticsEnabled = true;
393
#endif
394
395
int frameSkipSetting = 0;
396
int frameSkipTimer = 0;
397
398
#if !RETRO_USE_ORIGINAL_CODE
399
// Ported from RSDKv5
400
int startList_Game = -1;
401
int startStage_Game = -1;
402
403
bool consoleEnabled = false;
404
bool devMenu = false;
405
int startList = -1;
406
int startStage = -1;
407
int startPlayer = -1;
408
int startSave = -1;
409
int gameSpeed = 1;
410
int fastForwardSpeed = 8;
411
bool masterPaused = false;
412
bool frameStep = false;
413
int dimTimer = 0;
414
int dimLimit = 0;
415
float dimPercent = 1.0;
416
float dimMax = 1.0;
417
418
char startSceneFolder[0x10];
419
char startSceneID[0x10];
420
421
bool showPaletteOverlay = false;
422
bool useHQModes = true;
423
424
bool hasFocus = true;
425
int focusState = 0;
426
#endif
427
428
void Init();
429
void Run();
430
431
bool LoadGameConfig(const char *filepath);
432
#if RETRO_USE_MOD_LOADER
433
void LoadXMLWindowText();
434
void LoadXMLVariables();
435
void LoadXMLPalettes();
436
void LoadXMLObjects();
437
void LoadXMLSoundFX();
438
void LoadXMLPlayers(TextMenu *menu);
439
void LoadXMLStages(TextMenu *menu, int listNo);
440
#endif
441
442
char gameWindowText[0x40];
443
char gameDescriptionText[0x100];
444
#ifdef DECOMP_VERSION
445
const char *gameVersion = DECOMP_VERSION;
446
#else
447
const char *gameVersion = "1.3.3";
448
#endif
449
const char *gamePlatform = nullptr;
450
451
int gameTypeID = 0;
452
const char *releaseType = "USE_STANDALONE";
453
454
#if RETRO_RENDERTYPE == RETRO_SW_RENDER
455
const char *gameRenderType = "SW_RENDERING";
456
#elif RETRO_RENDERTYPE == RETRO_HW_RENDER
457
const char *gameRenderType = "HW_RENDERING";
458
#endif
459
460
#if RETRO_USE_HAPTICS
461
const char *gameHapticSetting = "USE_F_FEEDBACK"; // None is default, but people with controllers exist
462
#else
463
const char *gameHapticSetting = "NO_F_FEEDBACK";
464
#endif
465
466
#if !RETRO_USE_ORIGINAL_CODE
467
byte gameType = GAME_UNKNOWN;
468
#if RETRO_USE_MOD_LOADER
469
bool modMenuCalled = false;
470
bool forceSonic1 = false;
471
#endif
472
#endif
473
474
#if RETRO_SOFTWARE_RENDER
475
ushort *frameBuffer = nullptr;
476
ushort *frameBuffer2x = nullptr;
477
#endif
478
uint *texBuffer = nullptr;
479
480
#if !RETRO_USE_ORIGINAL_CODE
481
bool isFullScreen = false;
482
483
bool startFullScreen = false; // if should start as fullscreen
484
bool borderless = false;
485
bool vsync = false;
486
int scalingMode = 0;
487
int windowScale = 2;
488
int refreshRate = 60; // user-picked screen update rate
489
int screenRefreshRate = 60; // hardware screen update rate
490
int targetRefreshRate = 60; // game logic update rate
491
492
int renderFrameIndex = 0;
493
int skipFrameIndex = 0;
494
495
int windowXSize; // width of window/screen in the previous frame
496
int windowYSize; // height of window/screen in the previous frame
497
#endif
498
499
#if !RETRO_USE_ORIGINAL_CODE
500
#if RETRO_USING_SDL2
501
SDL_Window *window = nullptr;
502
#if !RETRO_USING_OPENGL
503
SDL_Renderer *renderer = nullptr;
504
#if RETRO_SOFTWARE_RENDER
505
SDL_Texture *screenBuffer = nullptr;
506
SDL_Texture *screenBuffer2x = nullptr;
507
#endif // RETRO_SOFTWARE_RENDERER
508
#endif
509
510
SDL_Event sdlEvents;
511
512
#if RETRO_USING_OPENGL
513
SDL_GLContext glContext; // OpenGL context
514
#endif // RETRO_USING_OPENGL
515
#endif // RETRO_USING_SDL2
516
517
#if RETRO_USING_SDL1
518
SDL_Surface *windowSurface = nullptr;
519
520
SDL_Surface *screenBuffer = nullptr;
521
SDL_Surface *screenBuffer2x = nullptr;
522
523
SDL_Event sdlEvents;
524
#endif // RETRO_USING_SDL1
525
#endif //! RETRO_USE_ORIGINAL_CODE
526
};
527
528
extern RetroEngine Engine;
529
#endif // !RETROENGINE_H
530
531