JNGL
init.hpp
Go to the documentation of this file.
1 // Copyright 2020-2022 Jan Niklas Hasse <[email protected]>
2 // For conditions of distribution and use, see copyright notice in LICENSE.txt
5 
6 #pragma once
7 
8 #include "App.hpp"
9 #include "AppParameters.hpp"
10 #include "main.hpp"
11 #include "screen.hpp"
12 #include "work.hpp"
13 
14 #include <cmath>
15 
16 #if defined(__has_include) && __has_include("filesystem")
17 #include <filesystem>
18 #endif
19 
20 namespace jngl {
21 class Work;
22 } // namespace jngl
23 
26 
27 #if !defined(__APPLE__) || !TARGET_OS_IPHONE // iOS
28 JNGL_MAIN_BEGIN { // NOLINT
29 #if !defined(ANDROID) && defined(__has_include) && __has_include(<filesystem>) && \
30  (!defined(TARGET_OS_IOS) || TARGET_OS_IOS == 0)
31  std::error_code err;
32  std::filesystem::current_path("data", err);
33  if (err) {
34  std::filesystem::current_path("../data", err); // move out of build/bin folder
35  if (err) {
36  std::filesystem::current_path("../../data", err); // move out of build/Debug folder
37  if (err) {
38  std::filesystem::current_path("../../../data", err); // move out of out\build\x64-Debug
39  }
40  }
41  }
42 #endif
43  jngl::AppParameters params = jnglInit();
44  auto& app = jngl::App::instance();
45  app.setDisplayName(params.displayName);
46  app.setPixelArt(params.pixelArt);
47  bool fullscreen = false;
48 #if defined(NDEBUG) || defined(__ANDROID__)
49  fullscreen = true;
50 #endif
51  std::pair<int, int> minAspectRatio{ 1, 3 };
52  std::pair<int, int> maxAspectRatio{ 3, 1 };
53  if (!params.screenSize) {
54  params.screenSize = { double(jngl::getDesktopWidth()), double(jngl::getDesktopHeight()) };
55  fullscreen = true;
56  }
57  if (fullscreen) {
58  const jngl::Vec2 desktopSize{ double(jngl::getDesktopWidth()),
59  double(jngl::getDesktopHeight()) };
60  if (desktopSize.x > 0 &&
61  desktopSize.y > 0) { // desktop size isn't available on some platforms (e.g. Android)
62  jngl::setScaleFactor(std::min(desktopSize.x / params.screenSize->x,
63  desktopSize.y / params.screenSize->y));
64  maxAspectRatio = minAspectRatio = std::pair<int, int>(
65  std::lround(params.screenSize->x), std::lround(params.screenSize->y));
66  }
67  } else {
68  // Make window as big as possible
69  const double scaleFactor = std::min((jngl::getDesktopWidth() - 99) / params.screenSize->x,
70  (jngl::getDesktopHeight() - 99) / params.screenSize->y);
71  if (scaleFactor > 1) {
72  jngl::setScaleFactor(std::floor(scaleFactor));
73  } else {
74  jngl::setScaleFactor(scaleFactor);
75  }
76  }
78  fullscreen ? jngl::getDesktopWidth()
79  : int(std::lround(params.screenSize->x * jngl::getScaleFactor())),
80  fullscreen ? jngl::getDesktopHeight()
81  : int(std::lround(params.screenSize->y * jngl::getScaleFactor())),
82  fullscreen, params.minAspectRatio ? *params.minAspectRatio : minAspectRatio,
83  params.maxAspectRatio ? *params.maxAspectRatio : maxAspectRatio);
84  jngl::setWork(params.start());
85  app.mainLoop();
86 }
87 JNGL_MAIN_END
88 #endif
Contains jngl::AppParameters struct.
Contains jngl::App class.
static App & instance()
Access the instance, creates it if it doesn't exist.
Two-dimensional vector.
Definition: Vec2.hpp:35
T current_path(T... args)
T floor(T... args)
jngl::AppParameters jnglInit()
Implement this function and set AppParameters::start.
Defines macros for the main entry point.
T min(T... args)
JNGL's main namespace.
Definition: App.hpp:10
double getScaleFactor()
Size of one screen pixel in actual pixels.
void showWindow(const std::string &title, int width, int height, bool fullscreen=false, std::pair< int, int > minAspectRatio={ 4, 3 }, std::pair< int, int > maxAspectRatio={ 16, 9 })
Creates the main window.
int getDesktopHeight()
Returns the height of the main display in actual pixels.
void setWork(std::shared_ptr< Work > work)
Sets the passed Work to be active in App::mainLoop()
int getDesktopWidth()
Returns the width of the main display in actual pixels.
T lround(T... args)
Parameters used to initialize the main window.
bool pixelArt
Activates pixel-perfect magnifying of textures (nearest-neighbor interpolation)
std::string displayName
Display name of the application which will be used in the window title for example.
std::function< std::shared_ptr< jngl::Work >)> start
A factory function which creates the first jngl::Work.
std::optional< jngl::Vec2 > screenSize
Size of the canvas in screen pixels, see jngl::getScreenSize()