1 // Aseprite
2 // Copyright (C) 2001-2016  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "app/app.h"
12 #include "app/cli/app_options.h"
13 #include "app/console.h"
14 #include "app/resource_finder.h"
15 #include "app/send_crash.h"
16 #include "base/exception.h"
17 #include "base/memory.h"
18 #include "base/memory_dump.h"
19 #include "base/system_console.h"
20 #include "she/error.h"
21 #include "she/scoped_handle.h"
22 #include "she/system.h"
23 
24 #include <clocale>
25 #include <cstdlib>
26 #include <ctime>
27 #include <iostream>
28 
29 #ifdef _WIN32
30   #include <windows.h>
31 #endif
32 
33 namespace {
34 
35   // Memory leak detector wrapper
36   class MemLeak {
37   public:
38 #ifdef LAF_MEMLEAK
MemLeak()39     MemLeak() { base_memleak_init(); }
~MemLeak()40     ~MemLeak() { base_memleak_exit(); }
41 #else
42     MemLeak() { }
43 #endif
44   };
45 
46 }
47 
48 // Aseprite entry point. (Called from she library.)
app_main(int argc,char * argv[])49 int app_main(int argc, char* argv[])
50 {
51   // Initialize the locale. Aseprite isn't ready to handle numeric
52   // fields with other locales (e.g. we expect strings like "10.32" be
53   // used in std::strtod(), not something like "10,32").
54   std::setlocale(LC_ALL, "en-US");
55   ASSERT(std::strtod("10.32", nullptr) == 10.32);
56 
57   // Initialize the random seed.
58   std::srand(static_cast<unsigned int>(std::time(nullptr)));
59 
60 #ifdef _WIN32
61   ::CoInitialize(NULL);
62 #endif
63 
64   try {
65     base::MemoryDump memoryDump;
66     MemLeak memleak;
67     base::SystemConsole systemConsole;
68     app::AppOptions options(argc, const_cast<const char**>(argv));
69     she::ScopedHandle<she::System> system(she::create_system());
70     app::App app;
71 
72     // Change the name of the memory dump file
73     {
74       std::string filename = app::memory_dump_filename();
75       if (!filename.empty())
76         memoryDump.setFileName(filename);
77     }
78 
79     app.initialize(options);
80 
81     if (options.startShell())
82       systemConsole.prepareShell();
83 
84     app.run();
85     return 0;
86   }
87   catch (std::exception& e) {
88     std::cerr << e.what() << '\n';
89     she::error_message(e.what());
90     return 1;
91   }
92 }
93