JNGL
init.hpp
Go to the documentation of this file.
1 // Copyright 2020-2021 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 "main.hpp"
10 #include "screen.hpp"
11 #include "work.hpp"
12 
13 #include <boost/math/special_functions/round.hpp>
14 #include <cmath>
15 #include <optional>
16 
17 namespace jngl {
18 class Work;
19 
21 struct AppParameters {
24 
29 
31  bool pixelArt = false;
32 };
33 
34 } // namespace jngl
35 
38 
39 #if !defined(__APPLE__) || !TARGET_OS_IPHONE // iOS
40 JNGL_MAIN_BEGIN { // NOLINT
41  jngl::AppParameters params;
42  auto workFactory = jnglInit(params);
43  auto& app = jngl::App::instance();
44  app.setDisplayName(params.displayName);
45  app.setPixelArt(params.pixelArt);
46  bool fullscreen = false;
47 #ifdef NDEBUG
48  fullscreen = true;
49 #endif
50  std::pair<int, int> minAspectRatio{ 1, 3 };
51  std::pair<int, int> maxAspectRatio{ 3, 1 };
52  if (!params.screenSize) {
53  params.screenSize = { double(jngl::getDesktopWidth()), double(jngl::getDesktopHeight()) };
54  fullscreen = true;
55  }
56  if (fullscreen) {
57  const jngl::Vec2 desktopSize{ double(jngl::getDesktopWidth()),
58  double(jngl::getDesktopHeight()) };
59  if (desktopSize.x > 0 &&
60  desktopSize.y > 0) { // desktop size isn't available on some platforms (e.g. Android)
61  jngl::setScaleFactor(std::min(desktopSize.x / params.screenSize->x,
62  desktopSize.y / params.screenSize->y));
63  maxAspectRatio = minAspectRatio =
64  std::pair<int, int>(params.screenSize->x, params.screenSize->y);
65  }
66  } else {
67  // Make window as big as possible
68  const float scaleFactor = std::min((jngl::getDesktopWidth() - 50) / params.screenSize->x,
69  (jngl::getDesktopHeight() - 50) / params.screenSize->y);
70  if (scaleFactor > 1) {
71  jngl::setScaleFactor(std::floor(scaleFactor));
72  } else {
73  jngl::setScaleFactor(scaleFactor);
74  }
75  }
77  params.displayName,
78  fullscreen ? jngl::getDesktopWidth()
79  : boost::math::iround(params.screenSize->x * jngl::getScaleFactor()),
80  fullscreen ? jngl::getDesktopHeight()
81  : boost::math::iround(params.screenSize->y * jngl::getScaleFactor()),
82  fullscreen, minAspectRatio, maxAspectRatio);
83  jngl::setWork(workFactory());
84  app.mainLoop();
85 }
86 JNGL_MAIN_END
87 #endif
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 floor(T... args)
std::function< std::shared_ptr< jngl::Work >)> jnglInit(jngl::AppParameters &)
Implement this function and return a factory function which creates the first jngl::Work.
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.
Parameters used to initialize the main window.
Definition: init.hpp:21
bool pixelArt
Activates pixel-perfect magnifying of textures (nearest-neighbor interpolation)
Definition: init.hpp:31
std::string displayName
Display name of the application which will be used in the window title for example.
Definition: init.hpp:23
std::optional< jngl::Vec2 > screenSize
Size of the canvas in screen pixels, see jngl::getScreenSize()
Definition: init.hpp:28