Home | History | Annotate | Download | only in smoke
      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 <string>
     21 #include <vector>
     22 
     23 class Shell;
     24 
     25 class Game {
     26 public:
     27     Game(const Game &game) = delete;
     28     Game &operator=(const Game &game) = delete;
     29     virtual ~Game() {}
     30 
     31     struct Settings {
     32         std::string name;
     33         int initial_width;
     34         int initial_height;
     35         int queue_count;
     36         int back_buffer_count;
     37         int ticks_per_second;
     38         bool vsync;
     39         bool animate;
     40 
     41         bool validate;
     42         bool validate_verbose;
     43 
     44         bool no_tick;
     45         bool no_render;
     46         bool no_present;
     47     };
     48     const Settings &settings() const { return settings_; }
     49 
     50     virtual void attach_shell(Shell &shell) { shell_ = &shell; }
     51     virtual void detach_shell() { shell_ = nullptr; }
     52 
     53     virtual void attach_swapchain() {}
     54     virtual void detach_swapchain() {}
     55 
     56     enum Key {
     57         // virtual keys
     58         KEY_SHUTDOWN,
     59         // physical keys
     60         KEY_UNKNOWN,
     61         KEY_ESC,
     62         KEY_UP,
     63         KEY_DOWN,
     64         KEY_SPACE,
     65     };
     66     virtual void on_key(Key key) {}
     67     virtual void on_tick() {}
     68 
     69     virtual void on_frame(float frame_pred) {}
     70 
     71 protected:
     72     Game(const std::string &name, const std::vector<std::string> &args)
     73         : settings_(), shell_(nullptr)
     74     {
     75         settings_.name = name;
     76         settings_.initial_width = 1280;
     77         settings_.initial_height = 1024;
     78         settings_.queue_count = 1;
     79         settings_.back_buffer_count = 1;
     80         settings_.ticks_per_second = 30;
     81         settings_.vsync = true;
     82         settings_.animate = true;
     83 
     84         settings_.validate = false;
     85         settings_.validate_verbose = false;
     86 
     87         settings_.no_tick = false;
     88         settings_.no_render = false;
     89         settings_.no_present = false;
     90 
     91         parse_args(args);
     92     }
     93 
     94     Settings settings_;
     95     Shell *shell_;
     96 
     97 private:
     98     void parse_args(const std::vector<std::string> &args)
     99     {
    100         for (auto it = args.begin(); it != args.end(); ++it) {
    101             if (*it == "-b") {
    102                 settings_.vsync = false;
    103             } else if (*it == "-w") {
    104                 ++it;
    105                 settings_.initial_width = std::stoi(*it);
    106             } else if (*it == "-h") {
    107                 ++it;
    108                 settings_.initial_height = std::stoi(*it);
    109             } else if (*it == "-v") {
    110                 settings_.validate = true;
    111             } else if (*it == "--validate") {
    112                 settings_.validate = true;
    113             } else if (*it == "-vv") {
    114                 settings_.validate = true;
    115                 settings_.validate_verbose = true;
    116             } else if (*it == "-nt") {
    117                 settings_.no_tick = true;
    118             } else if (*it == "-nr") {
    119                 settings_.no_render = true;
    120             } else if (*it == "-np") {
    121                 settings_.no_present = true;
    122             }
    123         }
    124     }
    125 };
    126 
    127 #endif // GAME_H
    128