1 /* 2 * Copyright (C) 2016 Google, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef GAME_H 18 #define GAME_H 19 20 #include <chrono> 21 #include <iostream> 22 #include <string> 23 #include <vector> 24 25 class Shell; 26 27 class Game { 28 public: 29 Game(const Game &game) = delete; 30 Game &operator=(const Game &game) = delete; ~Game()31 virtual ~Game() {} 32 33 struct Settings { 34 std::string name; 35 int initial_width; 36 int initial_height; 37 int queue_count; 38 int back_buffer_count; 39 int ticks_per_second; 40 bool vsync; 41 bool animate; 42 43 bool validate; 44 bool validate_verbose; 45 46 bool no_tick; 47 bool no_render; 48 bool no_present; 49 50 // Whether or not to use VkFlushMappedMemoryRanges 51 bool flush_buffers; 52 53 int max_frame_count; 54 }; settings()55 const Settings &settings() const { return settings_; } 56 attach_shell(Shell & shell)57 virtual void attach_shell(Shell &shell) { shell_ = &shell; } detach_shell()58 virtual void detach_shell() { shell_ = nullptr; } 59 attach_swapchain()60 virtual void attach_swapchain() {} detach_swapchain()61 virtual void detach_swapchain() {} 62 63 enum Key { 64 // virtual keys 65 KEY_SHUTDOWN, 66 // physical keys 67 KEY_UNKNOWN, 68 KEY_ESC, 69 KEY_UP, 70 KEY_DOWN, 71 KEY_SPACE, 72 }; on_key(Key key)73 virtual void on_key(Key key) {} on_tick()74 virtual void on_tick() {} 75 on_frame(float frame_pred)76 virtual void on_frame(float frame_pred) {} 77 78 void print_stats(); 79 void quit(); 80 81 protected: 82 int frame_count; 83 std::chrono::time_point<std::chrono::system_clock> start_time; 84 Game(const std::string & name,const std::vector<std::string> & args)85 Game(const std::string &name, const std::vector<std::string> &args) : settings_(), shell_(nullptr) { 86 settings_.name = name; 87 settings_.initial_width = 1280; 88 settings_.initial_height = 1024; 89 settings_.queue_count = 1; 90 settings_.back_buffer_count = 1; 91 settings_.ticks_per_second = 30; 92 settings_.vsync = true; 93 settings_.animate = true; 94 95 settings_.validate = false; 96 settings_.validate_verbose = false; 97 98 settings_.no_tick = false; 99 settings_.no_render = false; 100 settings_.no_present = false; 101 102 settings_.flush_buffers = false; 103 104 settings_.max_frame_count = -1; 105 106 parse_args(args); 107 108 frame_count = 0; 109 // Record start time for printing stats later 110 start_time = std::chrono::system_clock::now(); 111 } 112 113 Settings settings_; 114 Shell *shell_; 115 116 private: parse_args(const std::vector<std::string> & args)117 void parse_args(const std::vector<std::string> &args) { 118 for (auto it = args.begin(); it != args.end(); ++it) { 119 if (*it == "--b") { 120 settings_.vsync = false; 121 } else if (*it == "--w") { 122 ++it; 123 settings_.initial_width = std::stoi(*it); 124 } else if (*it == "--h") { 125 ++it; 126 settings_.initial_height = std::stoi(*it); 127 } else if (*it == "--v") { 128 settings_.validate = true; 129 } else if (*it == "--validate") { 130 settings_.validate = true; 131 } else if (*it == "--vv") { 132 settings_.validate = true; 133 settings_.validate_verbose = true; 134 } else if (*it == "--nt") { 135 settings_.no_tick = true; 136 } else if (*it == "--nr") { 137 settings_.no_render = true; 138 } else if (*it == "--np") { 139 settings_.no_present = true; 140 } else if (*it == "--flush") { 141 settings_.flush_buffers = true; 142 } else if (*it == "--c") { 143 ++it; 144 settings_.max_frame_count = std::stoi(*it); 145 } 146 } 147 } 148 }; 149 150 #endif // GAME_H 151