FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
environment.h
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPLBASE_ENVIRONMENT_H
16 #define FPLBASE_ENVIRONMENT_H
17 
18 #include <string>
19 
20 #include "fplbase/config.h" // Must come first.
21 
22 #include "mathfu/glsl_mappings.h"
23 
24 namespace fplbase {
25 
26 // OpenGL ES feature level we are able to obtain.
27 enum FeatureLevel {
28  kFeatureLevel20, // 2.0: Our fallback.
29  kFeatureLevel30, // 3.0: We request this by default.
30 };
31 
32 enum WindowMode {
33  // Doesn't use all of the screen, typically not available on mobile. If used
34  // on a device that has no windows (mobile), will do the same as
35  // kWindowModeFullscreenNative.
36  kWindowModeWindowedNative,
37  // Doesn't use all of the screen, typically not available on mobile. If used
38  // on a device that has no windows (mobile), it will do the same thing as
39  // kWindowModeFullscreenScaled.
40  kWindowModeWindowedScaled,
41  // Uses all of the display at the native resolution of the device. Any size
42  // supplied is ignored.
43  kWindowModeFullscreenNative,
44  // Uses all of the display, tries to scale from supplied size as best as
45  // possible.
46  kWindowModeFullscreenScaled,
47 };
48 
49 // Any backend stores its data in an object inherited from this.
51  virtual ~EnvironmentHandles() {}
52 };
53 
54 // An environment is responsible for managing the window context and rendering
55 // context (e.g. OpenGL context), if any.
56 class Environment {
57  public:
58  Environment()
59  : feature_level_(kFeatureLevel20),
60  window_size_(mathfu::vec2i(800, 600)) // Overwritten elsewhere.
61  {}
62 
63  // The following functions are implemented differently for each rendering
64  // backend.
65  bool Initialize(const mathfu::vec2i &window_size, const char *window_title,
66  WindowMode window_mode = kWindowModeWindowedScaled);
67  void ShutDown();
68  void AdvanceFrame(bool minimized);
69 
70  // This is typically called by backends when they detect a size change.
71  // Should typically be called in between frames to keep rendering consistent.
72  void SetWindowSize(const mathfu::vec2i &window_size) {
73  window_size_ = window_size;
74  }
75 
76  mathfu::vec2i GetViewportSize() const;
77 
78  FeatureLevel feature_level() const { return feature_level_; }
79 
80  const mathfu::vec2i &window_size() const { return window_size_; }
81  mathfu::vec2i &window_size() { return window_size_; }
82 
83  const std::string &last_error() const { return last_error_; }
84 
85  private:
86  FeatureLevel feature_level_;
87  mathfu::vec2i window_size_;
88  std::string last_error_;
89  std::unique_ptr<EnvironmentHandles> handles_;
90 };
91 
92 #define LOOKUP_GL_FUNCTION(type, name, required, lookup_fn) \
93  union { \
94  void *data; \
95  type function; \
96  } data_function_union_##name; \
97  data_function_union_##name.data = (void *)lookup_fn(#name); \
98  if (required && !data_function_union_##name.data) { \
99  last_error_ = "could not retrieve GL function pointer " #name; \
100  return false; \
101  } \
102  name = data_function_union_##name.function;
103 
104 
105 } // namespace fplbase
106 
107 #endif // FPLBASE_ENVIRONMENT_H
Definition: environment.h:50
Definition: environment.h:56