| 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 |
#include <assert.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <stdlib.h> |
| 52 |
#include <string.h> |
| 53 |
|
| 54 |
#define SDL_MAIN_HANDLED |
| 55 |
#include <SDL.h> |
| 56 |
|
| 57 |
#include <cdogs/SDL_JoystickButtonNames/SDL_joystickbuttonnames.h> |
| 58 |
#include <cdogs/ammo.h> |
| 59 |
#include <cdogs/campaigns.h> |
| 60 |
#include <cdogs/character_class.h> |
| 61 |
#include <cdogs/collision/collision.h> |
| 62 |
#include <cdogs/config_io.h> |
| 63 |
#include <cdogs/draw/char_sprites.h> |
| 64 |
#include <cdogs/draw/draw.h> |
| 65 |
#include <cdogs/files.h> |
| 66 |
#include <cdogs/font_utils.h> |
| 67 |
#include <cdogs/grafx.h> |
| 68 |
#include <cdogs/handle_game_events.h> |
| 69 |
#include <cdogs/joystick.h> |
| 70 |
#include <cdogs/keyboard.h> |
| 71 |
#include <cdogs/log.h> |
| 72 |
#include <cdogs/mission.h> |
| 73 |
#include <cdogs/music.h> |
| 74 |
#include <cdogs/net_client.h> |
| 75 |
#include <cdogs/net_server.h> |
| 76 |
#include <cdogs/objs.h> |
| 77 |
#include <cdogs/palette.h> |
| 78 |
#include <cdogs/particle.h> |
| 79 |
#include <cdogs/pic_manager.h> |
| 80 |
#include <cdogs/pickup.h> |
| 81 |
#include <cdogs/pics.h> |
| 82 |
#include <cdogs/player_template.h> |
| 83 |
#include <cdogs/sounds.h> |
| 84 |
#include <cdogs/triggers.h> |
| 85 |
#include <cdogs/utils.h> |
| 86 |
|
| 87 |
#include "autosave.h" |
| 88 |
#include "briefing_screens.h" |
| 89 |
#include "command_line.h" |
| 90 |
#include "credits.h" |
| 91 |
#include "loading_screens.h" |
| 92 |
#include "mainmenu.h" |
| 93 |
#include "prep.h" |
| 94 |
|
| 95 |
#ifdef __EMSCRIPTEN__ |
| 96 |
#include <emscripten.h> |
| 97 |
#endif |
| 98 |
|
| 99 |
int main(int argc, char *argv[]) |
| 100 |
{ |
| 101 |
#if defined(_MSC_VER) && !defined(NDEBUG) |
| 102 |
FreeConsole(); |
| 103 |
#endif |
| 104 |
int err = 0; |
| 105 |
const char *loadCampaign = NULL; |
| 106 |
ENetAddress connectAddr; |
| 107 |
memset(&connectAddr, 0, sizeof connectAddr); |
| 108 |
|
| 109 |
srand((unsigned int)time(NULL)); |
| 110 |
LogInit(); |
| 111 |
|
| 112 |
PrintTitle(); |
| 113 |
|
| 114 |
#ifdef __EMSCRIPTEN__ |
| 115 |
|
| 116 |
EM_ASM(FS.mkdir('/persistent_data'); |
| 117 |
FS.mount(IDBFS, {}, '/persistent_data'); |
| 118 |
|
| 119 |
Module.print("start file sync.."); Module.syncdone = 0; |
| 120 |
|
| 121 |
FS.syncfs( |
| 122 |
true, function(err) { |
| 123 |
assert(!err); |
| 124 |
Module.print("end file sync.."); |
| 125 |
Module.syncdone = 1; |
| 126 |
});); |
| 127 |
|
| 128 |
SetupConfigDir(); |
| 129 |
gConfig = ConfigDefault(); |
| 130 |
#else |
| 131 |
SetupConfigDir(); |
| 132 |
gConfig = ConfigLoad(GetConfigFilePath(CONFIG_FILE)); |
| 133 |
#endif |
| 134 |
|
| 135 |
ConfigGet(&gConfig, "Graphics.ShowHUD")->u.Bool.Value = true; |
| 136 |
ConfigGet(&gConfig, "Graphics.ShakeMultiplier")->u.Int.Value = 1; |
| 137 |
|
| 138 |
|
| 139 |
char buf[CDOGS_PATH_MAX]; |
| 140 |
ProcessCommandLine(buf, argc, argv); |
| 141 |
LOG(LM_MAIN, LL_INFO, "Command line (%d args):%s", argc, buf); |
| 142 |
int demoQuitTimer = 0; |
| 143 |
if (!ParseArgs(argc, argv, &connectAddr, &loadCampaign, &demoQuitTimer)) |
| 144 |
{ |
| 145 |
goto bail; |
| 146 |
} |
| 147 |
|
| 148 |
#ifndef __EMSCRIPTEN__ |
| 149 |
const int sdlFlags = SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | |
| 150 |
SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER; |
| 151 |
#else |
| 152 |
const int sdlFlags = SDL_INIT_AUDIO | SDL_INIT_VIDEO; |
| 153 |
#endif |
| 154 |
if (SDL_Init(sdlFlags) != 0) |
| 155 |
{ |
| 156 |
LOG(LM_MAIN, LL_ERROR, "Could not initialise SDL: %s", SDL_GetError()); |
| 157 |
err = EXIT_FAILURE; |
| 158 |
goto bail; |
| 159 |
} |
| 160 |
if (SDLJBN_Init() != 0) |
| 161 |
{ |
| 162 |
LOG(LM_MAIN, LL_ERROR, "Could not initialise SDLJBN: %s", |
| 163 |
SDLJBN_GetError()); |
| 164 |
err = EXIT_FAILURE; |
| 165 |
goto bail; |
| 166 |
} |
| 167 |
SDL_EventState(SDL_DROPFILE, SDL_DISABLE); |
| 168 |
|
| 169 |
PicManagerInit(&gPicManager); |
| 170 |
GraphicsInit(&gGraphicsDevice, &gConfig); |
| 171 |
GraphicsInitialize(&gGraphicsDevice); |
| 172 |
if (!gGraphicsDevice.IsInitialized) |
| 173 |
{ |
| 174 |
LOG(LM_MAIN, LL_WARN, |
| 175 |
"Cannot initialise video; trying default config"); |
| 176 |
ConfigResetDefault(ConfigGet(&gConfig, "Graphics")); |
| 177 |
GraphicsInit(&gGraphicsDevice, &gConfig); |
| 178 |
GraphicsInitialize(&gGraphicsDevice); |
| 179 |
} |
| 180 |
if (!gGraphicsDevice.IsInitialized) |
| 181 |
{ |
| 182 |
LOG(LM_MAIN, LL_ERROR, "Video didn't init!"); |
| 183 |
err = EXIT_FAILURE; |
| 184 |
goto bail; |
| 185 |
} |
| 186 |
FontLoadFromJSON(&gFont, "graphics/font.png", "graphics/font.json"); |
| 187 |
LoadingScreenInit(&gLoadingScreen, &gGraphicsDevice); |
| 188 |
LoadingScreenDraw(&gLoadingScreen, "Loading graphics...", 0.0f); |
| 189 |
PicManagerLoad(&gPicManager); |
| 190 |
|
| 191 |
GetDataFilePath(buf, ""); |
| 192 |
LOG(LM_MAIN, LL_INFO, "data dir(%s)", buf); |
| 193 |
LOG(LM_MAIN, LL_INFO, "config dir(%s)", GetConfigFilePath("")); |
| 194 |
|
| 195 |
LoadingScreenDraw(&gLoadingScreen, "Loading autosaves...", 0.09f); |
| 196 |
AutosaveInit(&gAutosave); |
| 197 |
#ifndef __EMSCRIPTEN__ |
| 198 |
AutosaveLoad(&gAutosave, GetConfigFilePath(AUTOSAVE_FILE)); |
| 199 |
#endif |
| 200 |
|
| 201 |
LoadingScreenDraw(&gLoadingScreen, "Initializing network client...", 0.18f); |
| 202 |
#ifndef __EMSCRIPTEN__ |
| 203 |
if (enet_initialize() != 0) |
| 204 |
{ |
| 205 |
LOG(LM_MAIN, LL_ERROR, "An error occurred while initializing ENet."); |
| 206 |
err = EXIT_FAILURE; |
| 207 |
goto bail; |
| 208 |
} |
| 209 |
NetClientInit(&gNetClient, (uint16_t)ConfigGetInt(&gConfig, "ListenPort")); |
| 210 |
#endif |
| 211 |
|
| 212 |
LoadingScreenDraw(&gLoadingScreen, "Initializing sound device...", 0.25f); |
| 213 |
SoundInitialize(&gSoundDevice, "sounds"); |
| 214 |
if (!gSoundDevice.isInitialised) |
| 215 |
{ |
| 216 |
LOG(LM_MAIN, LL_ERROR, "Sound initialization failed!"); |
| 217 |
} |
| 218 |
|
| 219 |
EventInit(&gEventHandlers); |
| 220 |
gEventHandlers.DemoQuitTimer = demoQuitTimer; |
| 221 |
NetServerInit(&gNetServer); |
| 222 |
LoadingScreenDraw(&gLoadingScreen, "Loading character sprites...", 0.34f); |
| 223 |
CharSpriteClassesInit(&gCharSpriteClasses); |
| 224 |
|
| 225 |
LoadingScreenDraw(&gLoadingScreen, "Loading particles...", 0.42f); |
| 226 |
ParticleClassesInit(&gParticleClasses, "data/particles.json"); |
| 227 |
LoadingScreenDraw(&gLoadingScreen, "Loading ammo...", 0.5f); |
| 228 |
AmmoInitialize(&gAmmo, "data/ammo.json"); |
| 229 |
LoadingScreenDraw(&gLoadingScreen, "Loading bullets and weapons...", 0.58f); |
| 230 |
BulletAndWeaponInitialize( |
| 231 |
&gBulletClasses, &gWeaponClasses, "data/bullets.json", |
| 232 |
"data/guns.json"); |
| 233 |
LoadingScreenDraw(&gLoadingScreen, "Loading character classes...", 0.66f); |
| 234 |
CharacterClassesInitialize( |
| 235 |
&gCharacterClasses, "data/character_classes.json"); |
| 236 |
#ifndef __EMSCRIPTEN__ |
| 237 |
LoadingScreenDraw(&gLoadingScreen, "Loading player templates...", 0.75f); |
| 238 |
PlayerTemplatesLoad(&gPlayerTemplates, &gCharacterClasses); |
| 239 |
#endif |
| 240 |
LoadingScreenDraw(&gLoadingScreen, "Loading pickups...", 0.86f); |
| 241 |
PickupClassesInit( |
| 242 |
&gPickupClasses, "data/pickups.json", &gAmmo, &gWeaponClasses); |
| 243 |
LoadingScreenDraw(&gLoadingScreen, "Loading map objects...", 0.92f); |
| 244 |
MapObjectsInit( |
| 245 |
&gMapObjects, "data/map_objects.json", &gAmmo, &gWeaponClasses); |
| 246 |
CollisionSystemInit(&gCollisionSystem); |
| 247 |
CampaignInit(&gCampaign); |
| 248 |
PlayerDataInit(&gPlayerDatas); |
| 249 |
|
| 250 |
LoadingScreenDraw(&gLoadingScreen, "Loading main menu...", 1.0f); |
| 251 |
LoopRunner l = LoopRunnerNew(); |
| 252 |
LoopRunnerPush(&l, MainMenu(&gGraphicsDevice, &l)); |
| 253 |
LoopRunnerPush(&l, ScreenLoading("Loading main menu...", false, NULL, false)); |
| 254 |
if (connectAddr.host != 0) |
| 255 |
{ |
| 256 |
if (NetClientTryScanAndConnect(&gNetClient, connectAddr.host)) |
| 257 |
{ |
| 258 |
LoopRunnerPush(&l, ScreenWaitForCampaignDef()); |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
printf("Failed to connect\n"); |
| 263 |
} |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
|
| 268 |
if (loadCampaign != NULL) |
| 269 |
{ |
| 270 |
LOG(LM_MAIN, LL_INFO, "Loading campaign %s...", loadCampaign); |
| 271 |
gCampaign.Entry.Mode = |
| 272 |
strstr(loadCampaign, "/" CDOGS_DOGFIGHT_DIR "/") != NULL |
| 273 |
? GAME_MODE_DOGFIGHT |
| 274 |
: GAME_MODE_NORMAL; |
| 275 |
CampaignEntry entry; |
| 276 |
if (!CampaignEntryTryLoad( |
| 277 |
&entry, loadCampaign, GAME_MODE_NORMAL) || |
| 278 |
!CampaignLoad(&gCampaign, &entry)) |
| 279 |
{ |
| 280 |
LOG(LM_MAIN, LL_ERROR, "Failed to load campaign %s", |
| 281 |
loadCampaign); |
| 282 |
} |
| 283 |
} |
| 284 |
else if (gEventHandlers.DemoQuitTimer > 0) |
| 285 |
{ |
| 286 |
LOG(LM_MAIN, LL_INFO, "Loading demo..."); |
| 287 |
gCampaign.Entry.Mode = GAME_MODE_QUICK_PLAY; |
| 288 |
if (!CampaignLoad(&gCampaign, &gCampaign.Entry)) |
| 289 |
{ |
| 290 |
LOG(LM_MAIN, LL_ERROR, "Failed to load demo campaign"); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
LOG(LM_MAIN, LL_INFO, "Starting game"); |
| 295 |
LoopRunnerRun(&l); |
| 296 |
LoopRunnerTerminate(&l); |
| 297 |
|
| 298 |
bail: |
| 299 |
NetServerTerminate(&gNetServer); |
| 300 |
PlayerDataTerminate(&gPlayerDatas); |
| 301 |
MapObjectsTerminate(&gMapObjects); |
| 302 |
PickupClassesTerminate(&gPickupClasses); |
| 303 |
ParticleClassesTerminate(&gParticleClasses); |
| 304 |
AmmoTerminate(&gAmmo); |
| 305 |
WeaponClassesTerminate(&gWeaponClasses); |
| 306 |
BulletTerminate(&gBulletClasses); |
| 307 |
CharacterClassesTerminate(&gCharacterClasses); |
| 308 |
MissionOptionsTerminate(&gMission); |
| 309 |
MapTerminate(&gMap); |
| 310 |
NetClientTerminate(&gNetClient); |
| 311 |
atexit(enet_deinitialize); |
| 312 |
EventTerminate(&gEventHandlers); |
| 313 |
CampaignTerminate(&gCampaign); |
| 314 |
CollisionSystemTerminate(&gCollisionSystem); |
| 315 |
|
| 316 |
CharSpriteClassesTerminate(&gCharSpriteClasses); |
| 317 |
PicManagerTerminate(&gPicManager); |
| 318 |
FontTerminate(&gFont); |
| 319 |
GraphicsTerminate(&gGraphicsDevice); |
| 320 |
AutosaveSave(&gAutosave, GetConfigFilePath(AUTOSAVE_FILE)); |
| 321 |
AutosaveTerminate(&gAutosave); |
| 322 |
PlayerTemplatesTerminate(&gPlayerTemplates); |
| 323 |
SoundTerminate(&gSoundDevice, true); |
| 324 |
ConfigDestroy(&gConfig); |
| 325 |
LogTerminate(); |
| 326 |
LoadingScreenTerminate(&gLoadingScreen); |
| 327 |
|
| 328 |
SDLJBN_Quit(); |
| 329 |
SDL_Quit(); |
| 330 |
|
| 331 |
return err; |
| 332 |
} |