Home | History | Annotate | Download | only in smoke
      1 /*
      2  * Copyright (C) 2016 Google, Inc.
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice shall be included
     12  * in all copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     20  * DEALINGS IN THE SOFTWARE.
     21  */
     22 
     23 #ifndef GAME_H
     24 #define GAME_H
     25 
     26 #include <string>
     27 #include <vector>
     28 
     29 class Shell;
     30 
     31 class Game {
     32 public:
     33     Game(const Game &game) = delete;
     34     Game &operator=(const Game &game) = delete;
     35     virtual ~Game() {}
     36 
     37     struct Settings {
     38         std::string name;
     39         int initial_width;
     40         int initial_height;
     41         int queue_count;
     42         int back_buffer_count;
     43         int ticks_per_second;
     44         bool vsync;
     45         bool animate;
     46 
     47         bool validate;
     48         bool validate_verbose;
     49 
     50         bool no_tick;
     51         bool no_render;
     52         bool no_present;
     53     };
     54     const Settings &settings() const { return settings_; }
     55 
     56     virtual void attach_shell(Shell &shell) { shell_ = &shell; }
     57     virtual void detach_shell() { shell_ = nullptr; }
     58 
     59     virtual void attach_swapchain() {}
     60     virtual void detach_swapchain() {}
     61 
     62     enum Key {
     63         // virtual keys
     64         KEY_SHUTDOWN,
     65         // physical keys
     66         KEY_UNKNOWN,
     67         KEY_ESC,
     68         KEY_UP,
     69         KEY_DOWN,
     70         KEY_SPACE,
     71     };
     72     virtual void on_key(Key key) {}
     73     virtual void on_tick() {}
     74 
     75     virtual void on_frame(float frame_pred) {}
     76 
     77 protected:
     78     Game(const std::string &name, const std::vector<std::string> &args)
     79         : settings_(), shell_(nullptr)
     80     {
     81         settings_.name = name;
     82         settings_.initial_width = 1280;
     83         settings_.initial_height = 1024;
     84         settings_.queue_count = 1;
     85         settings_.back_buffer_count = 1;
     86         settings_.ticks_per_second = 30;
     87         settings_.vsync = true;
     88         settings_.animate = true;
     89 
     90         settings_.validate = false;
     91         settings_.validate_verbose = false;
     92 
     93         settings_.no_tick = false;
     94         settings_.no_render = false;
     95         settings_.no_present = false;
     96 
     97         parse_args(args);
     98     }
     99 
    100     Settings settings_;
    101     Shell *shell_;
    102 
    103 private:
    104     void parse_args(const std::vector<std::string> &args)
    105     {
    106         for (auto it = args.begin(); it != args.end(); ++it) {
    107             if (*it == "-b") {
    108                 settings_.vsync = false;
    109             } else if (*it == "-w") {
    110                 ++it;
    111                 settings_.initial_width = std::stoi(*it);
    112             } else if (*it == "-h") {
    113                 ++it;
    114                 settings_.initial_height = std::stoi(*it);
    115             } else if (*it == "-v") {
    116                 settings_.validate = true;
    117             } else if (*it == "--validate") {
    118                 settings_.validate = true;
    119             } else if (*it == "-vv") {
    120                 settings_.validate = true;
    121                 settings_.validate_verbose = true;
    122             } else if (*it == "-nt") {
    123                 settings_.no_tick = true;
    124             } else if (*it == "-nr") {
    125                 settings_.no_render = true;
    126             } else if (*it == "-np") {
    127                 settings_.no_present = true;
    128             }
    129         }
    130     }
    131 };
    132 
    133 #endif // GAME_H
    134