summaryrefslogtreecommitdiff
path: root/src/blupi.cxx
blob: 8c294ada15ffd8646a6fbc0b7d625424083629b8 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
/*
 * This file is part of the planetblupi source code
 * Copyright (C) 1997, Daniel Roux & EPSITEC SA
 * Copyright (C) 2017, Mathieu Schroeter
 * http://epsitec.ch; http://www.blupi.org; http://github.com/blupi-games
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://gnu.org/licenses
 */

#include <atomic>
#include <fstream>
#include <iostream>
#include <iterator>
#include <regex>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <thread>
#include <vector>

#include <SDL2/SDL_image.h>
#include <argagg/argagg.hpp>
#ifdef USE_CURL
#include <curl/curl.h>
#endif /* USE_CURL */

#include "json/json.hpp"

#include "blupi.h"
#include "button.h"
#include "decor.h"
#include "def.h"
#include "event.h"
#include "menu.h"
#include "misc.h"
#include "movie.h"
#include "pixmap.h"
#include "progress.h"
#include "sound.h"

// Global variables

SDL_Window *   g_window;
SDL_Renderer * g_renderer;

CEvent *          g_pEvent       = nullptr;
CPixmap *         g_pPixmap      = nullptr; // pixmap principal
CSound *          g_pSound       = nullptr; // sound principal
CMovie *          g_pMovie       = nullptr; // movie principal
CDecor *          g_pDecor       = nullptr;
std::thread *     g_updateThread = nullptr;
std::atomic<bool> g_updateAbort (false);

bool        g_bFullScreen    = false; // false si mode de test
Uint8       g_zoom           = 1;
Sint32      g_speedRate      = 1;
Sint32      g_timerInterval  = 50; // inverval = 50ms
int         g_rendererType   = 0;
bool        g_enableRecorder = false;
std::string g_playRecord;
bool        g_restoreBugs = false; // restore original < v1.9 bugs
bool        g_restoreMidi = false; // restore music playback based on midi files
int         g_settingsOverload = 0;

bool        g_bTermInit = false; // initialisation en cours
Uint32      g_lastPhase = 999;
static bool g_pause;

#ifdef USE_CURL
struct url_data {
  CURLcode status;
  char *   buffer;
  size_t   size;
};
#endif

template <typename Out>
static void
split (const std::string & s, char delim, Out result)
{
  std::stringstream ss;
  ss.str (s);
  std::string item;
  while (std::getline (ss, item, delim))
    *(result++) = item;
}

static std::vector<std::string>
split (const std::string & s, char delim)
{
  std::vector<std::string> elems;
  split (s, delim, std::back_inserter (elems));
  return elems;
}

/**
 * \brief Read the config file.
 *
 * \returns true on success.
 */
static bool
ReadConfig ()
{
  const auto config = GetBaseDir () + "data/config.json";

  std::ifstream file (config, std::ifstream::in);
  if (!file)
    return false;

  nlohmann::json j;
  file >> j;

  if (
    !(g_settingsOverload & SETTING_SPEEDRATE) &&
    j.find ("speedrate") != j.end ())
  {
    g_speedRate = j["speedrate"].get<int> ();
    if (g_speedRate < 1)
      g_speedRate = 1;
    if (g_speedRate > 2)
      g_speedRate = 2;
  }

  if (
    !(g_settingsOverload & SETTING_TIMERINTERVAL) &&
    j.find ("timerinterval") != j.end ())
  {
    g_timerInterval = j["timerinterval"].get<int> ();
    if (g_timerInterval < 10)
      g_timerInterval = 10;
    if (g_timerInterval > 1000)
      g_timerInterval = 1000;
  }

  if (
    !(g_settingsOverload & SETTING_FULLSCREEN) &&
    j.find ("fullscreen") != j.end ())
    g_bFullScreen = j["fullscreen"].get<bool> ();

  if (!(g_settingsOverload & SETTING_ZOOM) && j.find ("zoom") != j.end ())
  {
    g_zoom = j["zoom"].get<Uint8> ();
    if (g_zoom != 1 && g_zoom != 2)
      g_zoom = 1;
  }

  if (
    !(g_settingsOverload & SETTING_RENDERER) && j.find ("renderer") != j.end ())
  {
    if (j["renderer"] == "software")
      g_rendererType = SDL_RENDERER_SOFTWARE;
    else if (j["renderer"] == "accelerated")
      g_rendererType = SDL_RENDERER_ACCELERATED;
  }

  if (
    !(g_settingsOverload & SETTING_DRIVER) && j.find ("driver") != j.end () &&
    (!g_rendererType || g_rendererType == SDL_RENDERER_ACCELERATED))
  {
    std::string input = j["driver"];
    if (std::regex_match (
          input, std::regex ("direct3d|direct3d11|opengl|opengles2|opengles")))
      SDL_SetHint (SDL_HINT_RENDER_DRIVER, input.c_str ());
  }

  if (
    !(g_settingsOverload & SETTING_MIDI) && j.find ("restoremidi") != j.end ())
    g_restoreMidi = j["restoremidi"].get<bool> ();

  return true;
}

/**
 * \brief Main frame update.
 */
static bool
UpdateFrame (void)
{
  Rect   clip, rcRect;
  Uint32 phase;
  Point  posMouse;
  Sint32 i, term, speed;
  bool   display = true;

  posMouse = g_pEvent->GetLastMousePos ();

  phase = g_pEvent->GetPhase ();

  SDL_RenderClear (g_renderer);

  rcRect.left   = 0;
  rcRect.top    = 0;
  rcRect.right  = LXIMAGE;
  rcRect.bottom = LYIMAGE;
  g_pPixmap->DrawImage (-1, CHBACK, rcRect); // draw the background

  if (phase == EV_PHASE_INTRO1)
    g_pEvent->IntroStep ();

  if (phase == EV_PHASE_PLAY)
  {
    clip.left   = POSDRAWX;
    clip.top    = POSDRAWY + g_pDecor->GetInfoHeight ();
    clip.right  = POSDRAWX + DIMDRAWX;
    clip.bottom = POSDRAWY + DIMDRAWY;

    if (g_pEvent->IsShift ()) // screen shifting
      g_pEvent->DecorAutoShift ();

    if (!g_pEvent->GetPause ())
    {
      speed = g_pEvent->GetSpeed () * g_speedRate;
      for (i = 0; i < speed; i++)
      {
        g_pDecor->BlupiStep (i == 0); // move all blupi
        g_pDecor->MoveStep (i == 0);  // move the environment
        g_pEvent->DemoStep ();        // forward the recording or demo playing
      }
    }

    g_pEvent->DecorAutoShift ();
    g_pDecor->Build (clip, posMouse); // build the environment
    g_pDecor->NextPhase (1);          // rebuild the map sometimes
  }

  if (phase == EV_PHASE_BUILD)
  {
    clip.left   = POSDRAWX;
    clip.top    = POSDRAWY;
    clip.right  = POSDRAWX + DIMDRAWX;
    clip.bottom = POSDRAWY + DIMDRAWY;
    g_pEvent->DecorAutoShift ();
    g_pDecor->Build (clip, posMouse); // build the environment
    g_pDecor->NextPhase (-1);         // rebuild the map sometimes
  }

  if (phase == EV_PHASE_INIT)
  {
    g_pEvent->DemoStep (); // start automatically (eventually) the demo
  }

  g_pEvent->DrawButtons ();

  g_lastPhase = phase;

  if (
    phase == EV_PHASE_H0MOVIE || phase == EV_PHASE_H1MOVIE ||
    phase == EV_PHASE_H2MOVIE || phase == EV_PHASE_PLAYMOVIE ||
    phase == EV_PHASE_WINMOVIE)
  {
    display = g_pEvent->MovieToStart (); // start a movie if necessary
  }

  if (phase == EV_PHASE_INSERT)
    g_pEvent->TryInsert ();

  if (phase == EV_PHASE_PLAY)
  {
    term = g_pDecor->IsTerminated ();
    if (term == 1)
      g_pEvent->ChangePhase (EV_PHASE_LOST); // lost
    if (term == 2)
      g_pEvent->ChangePhase (EV_PHASE_WINMOVIE); // win
  }

  return display;
}

/**
 * \brief Finished with all objects we use; release them.
 */
static void
FinishObjects (void)
{
  if (g_pMovie != nullptr)
  {
    delete g_pMovie;
    g_pMovie = nullptr;
  }

  if (g_pEvent != nullptr)
  {
    delete g_pEvent;
    g_pEvent = nullptr;
  }

  if (g_pDecor != nullptr)
  {
    delete g_pDecor;
    g_pDecor = nullptr;
  }

  if (g_pSound != nullptr)
  {
    g_pSound->StopMusic ();

    delete g_pSound;
    g_pSound = nullptr;
  }

  if (g_pPixmap != nullptr)
  {
    delete g_pPixmap;
    g_pPixmap = nullptr;
  }
}

static void
HandleEvent (const SDL_Event & event)
{
  if (!g_pause && g_pEvent != nullptr && g_pEvent->TreatEvent (event))
    return;

  switch (event.type)
  {
  case SDL_WINDOWEVENT:
  {
#ifndef DEBUG
    Point totalDim, iconDim;

    switch (event.window.event)
    {
    case SDL_WINDOWEVENT_FOCUS_GAINED:
      g_pause = false;

      if (g_bFullScreen)
        g_lastPhase = 999;

      if (!g_bFullScreen && g_bTermInit)
      {
        totalDim.x = 64;
        totalDim.y = 66;
        iconDim.x  = 64;
        iconDim.y  = 66 / 2;
        g_pPixmap->Cache (CHHILI, "image/hili.png", totalDim, iconDim);
      }
      SDL_SetWindowTitle (g_window, gettext ("Planet Blupi"));
      if (g_pSound != nullptr)
        g_pSound->RestartMusic ();
      if (g_pMovie)
        g_pMovie->Resume ();
      return;

    case SDL_WINDOWEVENT_FOCUS_LOST:
      g_pause = true;

      SDL_SetWindowTitle (g_window, gettext ("Planet Blupi -- stop"));
      if (g_pSound != nullptr)
        g_pSound->SuspendMusic ();
      if (g_pMovie)
        g_pMovie->Pause ();
      return;
    }
#endif /* !DEBUG */
    break;
  }

  case SDL_KEYDOWN:
    switch (event.key.keysym.sym)
    {
    case SDLK_F5:
      g_pEvent->SetSpeed (1);
      break;
    case SDLK_F6:
      g_pEvent->SetSpeed (2);
      break;
    case SDLK_F7:
      g_pEvent->SetSpeed (4);
      break;
    case SDLK_F8:
      g_pEvent->SetSpeed (8);
      break;
    }
    break;

  case SDL_RENDER_DEVICE_RESET:
  case SDL_RENDER_TARGETS_RESET:
    g_pPixmap->ReloadTargetTextures ();
    break;

  case SDL_USEREVENT:
  {
    switch (event.user.code)
    {
    case EV_UPDATE:
      if (!g_pEvent->IsMovie ()) // pas de film en cours ?
      {
        bool display = true;

        if (!g_pause)
          display = UpdateFrame ();

        if (!g_pEvent->IsMovie () && display)
          g_pPixmap->Display ();
      }
      break;

    case EV_WARPMOUSE:
    {
      const SDL_Point * coord = static_cast<SDL_Point *> (event.user.data1);
      SDL_WarpMouseInWindow (g_window, coord->x, coord->y);
      delete coord;
      break;
    }

    case EV_CHECKUPDATE:
    {
      std::string * data = static_cast<std::string *> (event.user.data1);

      using json    = nlohmann::json;
      json jsonData = json::parse (*data);

      /* Check if the remote version is newer */
      std::vector<std::string>  list = split (jsonData["version"], '.');
      std::vector<unsigned int> version (3);
      std::transform (
        list.begin (), list.end (), version.begin (),
        [](const std::string & s) -> unsigned int { return std::stoi (s); });

      if (
        (!!PB_VERSION_EXTRA[0] &&
         PB_VERSION_INT (version[0], version[1], version[2]) >=
           PLANETBLUPI_VERSION_INT) ||
        PB_VERSION_INT (version[0], version[1], version[2]) >
          PLANETBLUPI_VERSION_INT)
        g_pEvent->SetUpdateVersion (jsonData["version"]);

      delete data;
    }

    case EV_MUSIC_STOP:
      if (g_pSound->IsStoppedOnDemand ())
        break;

      g_pSound->RestartMusic ();
      break;

    case EV_MOVIE_PLAY:
      if (!g_pMovie->Render ())
        g_pEvent->StopMovie ();
      break;
    }
    break;
  }
  }
}

// Error with DoInit function.

static void
InitFail (const char * msg)
{
  char buffer[100];

  strcpy (buffer, "Error (");
  strcat (buffer, msg);
  strcat (buffer, ")");

  SDL_ShowSimpleMessageBox (
    SDL_MessageBoxFlags::SDL_MESSAGEBOX_ERROR, "Error", buffer, g_window);

  FinishObjects ();
}

#ifdef USE_CURL
static size_t
updateCallback (void * ptr, size_t size, size_t nmemb, void * data)
{
  size_t     realsize = size * nmemb;
  url_data * mem      = static_cast<url_data *> (data);

  mem->buffer =
    static_cast<char *> (realloc (mem->buffer, mem->size + realsize + 1));
  if (mem->buffer)
  {
    memcpy (&(mem->buffer[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->buffer[mem->size] = 0;
  }

  return realsize;
}

static int
progressCallback (
  void * userData, double dltotal, double dlnow, double ultotal, double ulnow)
{
  return g_updateAbort ? 1 : 0;
}
#endif /* USE_CURL */

static void
CheckForUpdates ()
{
#ifdef USE_CURL
  url_data chunk;

  chunk.buffer = nullptr; /* we expect realloc(NULL, size) to work */
  chunk.size   = 0;       /* no data at this point */
  chunk.status = CURLE_FAILED_INIT;

  CURL * curl = curl_easy_init ();
  if (!curl)
    return;

  curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
  curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
  curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
  curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 5);

  curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, updateCallback);

  curl_easy_setopt (curl, CURLOPT_URL, "http://blupi.org/update/planet.json");
  curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk);

  curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
  curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, nullptr);
  curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progressCallback);

  chunk.status = curl_easy_perform (curl);

  if (chunk.status)
  {
    const char * err = curl_easy_strerror (chunk.status);
    SDL_LogError (
      SDL_LOG_CATEGORY_APPLICATION, "Check for updates, error: %s", err);
  }
  else
  {
    std::string * res = new std::string (chunk.buffer, chunk.size);
    CEvent::PushUserEvent (EV_CHECKUPDATE, res);
  }

  if (chunk.buffer)
    free (chunk.buffer);

  curl_easy_cleanup (curl);
#endif /* USE_CURL */
}

static int
parseArgs (int argc, char * argv[], bool & exit)
{
  argagg::parser argparser{
    {{"help", {"-h", "--help"}, "print this help message and exit", 0},
     {"version", {"-V", "--version"}, "print version and exit", 0},
     {"speedrate",
      {"-s", "--speed-rate"},
      "change the speed rate [1;2] (default: 1)",
      1},
     {"timerinterval",
      {"-t", "--timer-interval"},
      "set the timer interval (refresh)",
      1},
     {"fullscreen",
      {"-f", "--fullscreen"},
      "load in fullscreen [on;off] (default: on)",
      1},
     {"zoom",
      {"-z", "--zoom"},
      "change the window scale (only if fullscreen is off) [1;2] (default: 1)",
      1},
     {"renderer",
      {"-r", "--renderer"},
      "set a renderer [auto;software;accelerated] (default: auto)",
      1},
     {"driver",
      {"-d", "--driver"},
      "set a driver [auto;direct3d;direct3d11;opengl;opengles2;opengles] "
      "(default: auto, ignored with "
      "software renderer)",
      1},
     {"enablerecorder",
      {"-c", "--enable-recorder"},
      "enable the recorder feature (F3/F4)",
      0},
     {"playrecord",
      {"-p", "--play-record"},
      "play a record generated by F3 (--enable-recorder)",
      1},
     {"restorebugs",
      {"-b", "--restore-bugs"},
      "restore funny original bugs of older versions < v1.9",
      0},
     {"restoremidi",
      {"-m", "--restore-midi"},
      "restore playback based on MIDI music instead of OGG",
      0}}};

  argagg::parser_results args;
  try
  {
    args = argparser.parse (argc, argv);
  }
  catch (const std::exception & e)
  {
    std::cerr << e.what () << std::endl;
    exit = true;
    return EXIT_FAILURE;
  }

  if (args["help"])
  {
    std::cerr << "Usage: planetblupi [options]" << std::endl << argparser;
    exit = true;
    return EXIT_SUCCESS;
  }

  if (args["version"])
  {
    std::cerr << PLANETBLUPI_VERSION_STR << std::endl;
    exit = true;
    return EXIT_SUCCESS;
  }

  if (args["speedrate"])
  {
    g_speedRate = args["speedrate"];
    g_settingsOverload |= SETTING_SPEEDRATE;
  }

  if (args["timerinterval"])
  {
    g_timerInterval = args["timerinterval"];
    g_settingsOverload |= SETTING_TIMERINTERVAL;
  }

  if (args["fullscreen"])
  {
    g_bFullScreen =
      args["fullscreen"].as<std::string> () != std::string ("off");
    g_settingsOverload |= SETTING_FULLSCREEN;
  }

  if (args["zoom"])
  {
    g_zoom = args["zoom"];
    if (g_zoom != 1 && g_zoom != 2)
      g_zoom = 1;
    g_settingsOverload |= SETTING_ZOOM;
  }

  if (args["renderer"])
  {
    if (args["renderer"].as<std::string> () == "auto")
      g_rendererType = 0;
    else if (args["renderer"].as<std::string> () == "software")
      g_rendererType = SDL_RENDERER_SOFTWARE;
    else if (args["renderer"].as<std::string> () == "accelerated")
      g_rendererType = SDL_RENDERER_ACCELERATED;
    g_settingsOverload |= SETTING_RENDERER;
  }

  if (
    args["driver"] &&
    (!g_rendererType || g_rendererType == SDL_RENDERER_ACCELERATED))
  {
    std::string input = args["driver"].as<std::string> ();
    if (std::regex_match (
          input, std::regex ("direct3d|direct3d11|opengl|opengles2|opengles")))
      SDL_SetHint (SDL_HINT_RENDER_DRIVER, input.c_str ());
    g_settingsOverload |= SETTING_DRIVER;
  }

  if (args["enablerecorder"])
    g_enableRecorder = true;

  if (args["playrecord"])
    g_playRecord = args["playrecord"].as<std::string> ();

  if (args["restorebugs"])
    g_restoreBugs = true;

  if (args["restoremidi"])
  {
    g_restoreMidi = true;
    g_settingsOverload |= SETTING_MIDI;
  }

  return EXIT_SUCCESS;
}

// Main initialization function.

static int
DoInit (int argc, char * argv[], bool & exit)
{
  int rc = parseArgs (argc, argv, exit);
  if (exit)
    return rc;

  Point totalDim, iconDim;
  Rect  rcRect;
  bool  bOK;

  bOK = ReadConfig (); // lit le fichier config.json

  if (!bOK) // Something wrong with config.json file?
  {
    InitFail ("Game not correctly installed");
    return EXIT_FAILURE;
  }

#ifdef _WIN32
  /* Fix laggy sounds on Windows by not using winmm driver. */
  SDL_setenv ("SDL_AUDIODRIVER", "directsound", true);
#endif /* _WIN32 */

  auto res = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
  if (res < 0)
  {
    SDL_Log ("Unable to initialize SDL: %s", SDL_GetError ());
    return EXIT_FAILURE;
  }

  // Create a window.
  g_window = SDL_CreateWindow (
    gettext ("Planet Blupi"), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
    LXIMAGE, LYIMAGE, 0);

  if (!g_window)
  {
    printf ("%s", SDL_GetError ());
    return EXIT_FAILURE;
  }

#if 0
  auto icon = IMG_Load ((GetShareDir () + "icons/hicolor/256x256/apps/blupi.png").c_str ());
  SDL_SetWindowIcon (g_window, icon);
  SDL_FreeSurface (icon);
#endif /* 0 */

  g_renderer = SDL_CreateRenderer (
    g_window, -1, g_rendererType | SDL_RENDERER_TARGETTEXTURE);
  if (!g_renderer)
  {
    printf ("%s", SDL_GetError ());
    SDL_DestroyWindow (g_window);
    return EXIT_FAILURE;
  }

  SDL_RenderSetLogicalSize (g_renderer, LXIMAGE, LYIMAGE);

  const auto renders = SDL_GetNumRenderDrivers ();
  for (int i = 0; i < renders; ++i)
  {
    SDL_RendererInfo info = {0};
    if (SDL_GetRenderDriverInfo (i, &info))
    {
      SDL_LogError (SDL_LOG_CATEGORY_APPLICATION, "renderer[%d]: failed\n", i);
      continue;
    }

    SDL_LogInfo (
      SDL_LOG_CATEGORY_APPLICATION, "renderer[%d]: name=%s", i, info.name);
    SDL_LogInfo (
      SDL_LOG_CATEGORY_APPLICATION, "renderer[%d]: flags=%u", i, info.flags);
    SDL_LogInfo (
      SDL_LOG_CATEGORY_APPLICATION, "renderer[%d]: num_texture_formats=%u", i,
      info.num_texture_formats);
    SDL_LogInfo (
      SDL_LOG_CATEGORY_APPLICATION, "renderer[%d]: max_texture_width=%u", i,
      info.max_texture_width);
    SDL_LogInfo (
      SDL_LOG_CATEGORY_APPLICATION, "renderer[%d]: max_texture_height=%u", i,
      info.max_texture_height);
  }

  // Create the main pixmap.
  g_pPixmap = new CPixmap;
  if (g_pPixmap == nullptr)
  {
    InitFail ("New pixmap");
    return EXIT_FAILURE;
  }

  totalDim.x = LXIMAGE;
  totalDim.y = LYIMAGE;
  if (!g_pPixmap->Create (totalDim))
  {
    InitFail ("Create pixmap");
    return EXIT_FAILURE;
  }

  OutputDebug ("Image: init\n");
  totalDim.x = LXIMAGE;
  totalDim.y = LYIMAGE;
  iconDim.x  = 0;
  iconDim.y  = 0;
#if _INTRO
  if (!g_pPixmap->Cache (CHBACK, "image/intro1.png", totalDim, iconDim))
#else
  if (!g_pPixmap->Cache (CHBACK, "image/init.png", totalDim, iconDim))
#endif
    return EXIT_FAILURE;

  OutputDebug ("Image: init\n");
  totalDim.x = LXIMAGE;
  totalDim.y = LYIMAGE;
  iconDim.x  = 0;
  iconDim.y  = 0;
  if (!g_pPixmap->Cache (CHGROUND, "image/init.png", totalDim, iconDim))
    return EXIT_FAILURE;

  rcRect.left   = 0;
  rcRect.top    = 0;
  rcRect.right  = LXIMAGE;
  rcRect.bottom = LYIMAGE;
  g_pPixmap->DrawImage (-1, CHBACK, rcRect); // dessine le fond
  g_pPixmap->Display ();

  totalDim.x = DIMCELX * 2 * 16;
  totalDim.y = DIMCELY * 2 * 6;
  iconDim.x  = DIMCELX * 2;
  iconDim.y  = DIMCELY * 2;
  if (!g_pPixmap->Cache (CHFLOOR, "image/floor000.png", totalDim, iconDim))
  {
    InitFail ("Cache floor000.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMOBJX * 16;
  totalDim.y = DIMOBJY * 8;
  iconDim.x  = DIMOBJX;
  iconDim.y  = DIMOBJY;
  if (!g_pPixmap->Cache (CHOBJECT, "image/obj000.png", totalDim, iconDim))
  {
    InitFail ("Cache obj000.png");
    return EXIT_FAILURE;
  }

  if (!g_pPixmap->Cache (CHOBJECTo, "image/obj-o000.png", totalDim, iconDim))
  {
    InitFail ("Cache obj-o000.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMBLUPIX * 16;
  totalDim.y = DIMBLUPIY * 23;
  iconDim.x  = DIMBLUPIX;
  iconDim.y  = DIMBLUPIY;
  if (!g_pPixmap->Cache (CHBLUPI, "image/blupi.png", totalDim, iconDim))
  {
    InitFail ("Cache blupi.png");
    return EXIT_FAILURE;
  }

  totalDim.x = 64;
  totalDim.y = 66;
  iconDim.x  = 64;
  iconDim.y  = 66 / 2;
  if (!g_pPixmap->Cache (CHHILI, "image/hili.png", totalDim, iconDim))
  {
    InitFail ("Cache hili.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMCELX * 2 * 3;
  totalDim.y = DIMCELY * 2 * 5;
  iconDim.x  = DIMCELX * 2;
  iconDim.y  = DIMCELY * 2;
  if (!g_pPixmap->Cache (CHFOG, "image/fog.png", totalDim, iconDim))
  {
    InitFail ("Cache fog.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMCELX * 2 * 16;
  totalDim.y = DIMCELY * 2 * 1;
  iconDim.x  = DIMCELX * 2;
  iconDim.y  = DIMCELY * 2;
  if (!g_pPixmap->Cache (CHMASK1, "image/mask1.png", totalDim, iconDim))
  {
    InitFail ("Cache mask1.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMCELX * 2 * 16;
  totalDim.y = DIMCELY * 2 * 1;
  iconDim.x  = DIMCELX * 2;
  iconDim.y  = DIMCELY * 2;
  if (!g_pPixmap->Cache (CHMASK2, "image/mask2.png", totalDim, iconDim))
  {
    InitFail ("Cache mask2.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMBUTTONX * 6;
  totalDim.y = DIMBUTTONY * 21;
  iconDim.x  = DIMBUTTONX;
  iconDim.y  = DIMBUTTONY;
  if (!g_pPixmap->Cache (CHBUTTON, "image/button00.png", totalDim, iconDim))
  {
    InitFail ("Cache button00.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMJAUGEX * 1;
  totalDim.y = DIMJAUGEY * 4;
  iconDim.x  = DIMJAUGEX;
  iconDim.y  = DIMJAUGEY;
  if (!g_pPixmap->Cache (CHJAUGE, "image/jauge.png", totalDim, iconDim))
  {
    InitFail ("Cache jauge.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMTEXTX * 16;
  totalDim.y = DIMTEXTY * 9 * 3;
  iconDim.x  = DIMTEXTX;
  iconDim.y  = DIMTEXTY;
  if (!g_pPixmap->Cache (CHTEXT, "image/text.png", totalDim, iconDim))
  {
    InitFail ("Cache text.png");
    return EXIT_FAILURE;
  }

  totalDim.x = DIMLITTLEX * 16;
  totalDim.y = DIMLITTLEY * 9;
  iconDim.x  = DIMLITTLEX;
  iconDim.y  = DIMLITTLEY;
  if (!g_pPixmap->Cache (CHLITTLE, "image/little.png", totalDim, iconDim))
  {
    InitFail ("Cache little.png");
    return EXIT_FAILURE;
  }

  totalDim.x = 426;
  totalDim.y = 52;
  iconDim.x  = 426;
  iconDim.y  = 52;
  if (!g_pPixmap->Cache (CHBIGNUM, "image/bignum.png", totalDim, iconDim))
  {
    InitFail ("Cache bignum.png");
    return EXIT_FAILURE;
  }

  // Create the sound manager.
  g_pSound = new CSound;
  if (g_pSound == nullptr)
  {
    InitFail ("New sound");
    return EXIT_FAILURE;
  }

  g_pSound->Create ();
  g_pSound->CacheAll ();
  g_pSound->SetState (true);

  // Create the movie manager.
  g_pMovie = new CMovie;
  if (g_pMovie == nullptr)
  {
    InitFail ("New movie");
    return EXIT_FAILURE;
  }

  g_pMovie->Create ();

  // Create the decor manager.
  g_pDecor = new CDecor;
  if (g_pDecor == nullptr)
  {
    InitFail ("New decor");
    return EXIT_FAILURE;
  }

  g_pDecor->Create (g_pSound, g_pPixmap);
  g_pDecor->MapInitColors ();

  // Create the event manager.
  g_pEvent = new CEvent;
  if (g_pEvent == nullptr)
  {
    InitFail ("New event");
    return EXIT_FAILURE;
  }

  const bool zoom = g_zoom;

  g_pEvent->Create (g_pPixmap, g_pDecor, g_pSound, g_pMovie);

  // Load all cursors
  g_pPixmap->LoadCursors (g_zoom);
  g_pPixmap->ChangeSprite (SPRITE_WAIT);

  g_updateThread = new std::thread (CheckForUpdates);
  if (g_bFullScreen)
    g_pEvent->SetFullScreen (true);
  if (!g_bFullScreen && zoom != g_zoom)
    g_pEvent->SetWindowSize (g_zoom);
  g_pEvent->ChangePhase (EV_PHASE_INTRO1);

  g_bTermInit = true;
  return EXIT_SUCCESS;
}

static void
initGettext ()
{
  setlocale (LC_ALL, "");
  textdomain ("planetblupi");
  bindtextdomain ("planetblupi", (GetShareDir () + "locale").c_str ());
  bind_textdomain_codeset ("planetblupi", "UTF-8");
}

int
main (int argc, char * argv[])
{
  initGettext ();

  int  res  = 0;
  bool exit = false;
  if ((res = DoInit (argc, argv, exit)) || exit)
    return res;

  SDL_TimerID updateTimer = SDL_AddTimer (
    g_timerInterval,
    [](Uint32 interval, void * param) -> Uint32 {
      CEvent::PushUserEvent (EV_UPDATE);
      return interval;
    },
    nullptr);

  SDL_Event event;
  while (SDL_WaitEvent (&event))
  {
    HandleEvent (event);
    if (event.type == SDL_QUIT)
      break;
  }

  SDL_RemoveTimer (updateTimer);
  FinishObjects ();

  if (g_renderer)
    SDL_DestroyRenderer (g_renderer);

  if (g_window)
    SDL_DestroyWindow (g_window);

  SDL_Quit ();

  if (g_updateThread)
  {
    g_updateAbort = true;
    g_updateThread->join ();
    delete (g_updateThread);
  }

  return 0;
}