]> git.cworth.org Git - lmno.games/blob - flutterempires/linux/main.cc
Add all linux files generated with flutter create .
[lmno.games] / flutterempires / linux / main.cc
1 #include <flutter/flutter_window_controller.h>
2 #include <linux/limits.h>
3 #include <unistd.h>
4
5 #include <cstdlib>
6 #include <iostream>
7 #include <memory>
8 #include <vector>
9
10 #include "flutter/generated_plugin_registrant.h"
11 #include "window_configuration.h"
12
13 namespace {
14
15 // Returns the path of the directory containing this executable, or an empty
16 // string if the directory cannot be found.
17 std::string GetExecutableDirectory() {
18   char buffer[PATH_MAX + 1];
19   ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer));
20   if (length > PATH_MAX) {
21     std::cerr << "Couldn't locate executable" << std::endl;
22     return "";
23   }
24   std::string executable_path(buffer, length);
25   size_t last_separator_position = executable_path.find_last_of('/');
26   if (last_separator_position == std::string::npos) {
27     std::cerr << "Unabled to find parent directory of " << executable_path
28               << std::endl;
29     return "";
30   }
31   return executable_path.substr(0, last_separator_position);
32 }
33
34 }  // namespace
35
36 int main(int argc, char **argv) {
37   // Resources are located relative to the executable.
38   std::string base_directory = GetExecutableDirectory();
39   if (base_directory.empty()) {
40     base_directory = ".";
41   }
42   std::string data_directory = base_directory + "/data";
43   std::string assets_path = data_directory + "/flutter_assets";
44   std::string icu_data_path = data_directory + "/icudtl.dat";
45
46   // Arguments for the Flutter Engine.
47   std::vector<std::string> arguments;
48
49   flutter::FlutterWindowController flutter_controller(icu_data_path);
50   flutter::WindowProperties window_properties = {};
51   window_properties.title = kFlutterWindowTitle;
52   window_properties.width = kFlutterWindowWidth;
53   window_properties.height = kFlutterWindowHeight;
54
55   // Start the engine.
56   if (!flutter_controller.CreateWindow(window_properties, assets_path,
57                                        arguments)) {
58     return EXIT_FAILURE;
59   }
60   RegisterPlugins(&flutter_controller);
61
62   // Run until the window is closed.
63   while (flutter_controller.RunEventLoopWithTimeout(
64       std::chrono::milliseconds::max())) {
65   }
66   return EXIT_SUCCESS;
67 }