1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 /**
21  * \file common/system/system.h
22  * \brief System functions: time stamps, info dialogs, etc.
23  */
24 
25 #pragma once
26 
27 #include "common/config.h"
28 
29 #include <memory>
30 #include <string>
31 #include <vector>
32 
33 /**
34  * \enum SystemDialogType
35  * \brief Type of system dialog
36  */
37 enum SystemDialogType
38 {
39     //! Information message
40     SDT_INFO,
41     //! Warning message
42     SDT_WARNING,
43     //! Error message
44     SDT_ERROR,
45     //! Yes/No question
46     SDT_YES_NO,
47     //! Ok/Cancel question
48     SDT_OK_CANCEL
49 };
50 
51 /**
52  * \enum SystemDialogResult
53  * \brief Result of system dialog
54  *
55  * Means which button was pressed.
56  */
57 enum SystemDialogResult
58 {
59     SDR_OK,
60     SDR_CANCEL,
61     SDR_YES,
62     SDR_NO
63 };
64 
65 /**
66  * \enum SystemTimeUnit
67  * \brief Time unit
68  */
69 enum SystemTimeUnit
70 {
71     //! seconds
72     STU_SEC,
73     //! milliseconds
74     STU_MSEC,
75     //! microseconds
76     STU_USEC
77 };
78 
79 /*
80  * Forward declaration of time stamp struct
81  * SystemTimeStamp should only be used in a pointer context.
82  * The implementation details are hidden because of platform dependence.
83  */
84 struct SystemTimeStamp;
85 
86 /**
87  * \class CSystemUtils
88  * \brief Platform-specific utils
89  *
90  * This class provides system-specific utilities like displaying user dialogs and
91  * querying system timers for exact timestamps.
92  */
93 class CSystemUtils
94 {
95 public:
96     virtual ~CSystemUtils();
97 
98     //! Creates system utils for specific platform
99     static std::unique_ptr<CSystemUtils> Create();
100 
101     //! Performs platform-specific initialization
102     virtual void Init() = 0;
103 
104     //! Displays a system dialog
105     virtual SystemDialogResult SystemDialog(SystemDialogType, const std::string &title, const std::string &message) = 0;
106 
107     //! Displays a fallback system dialog using console
108     TEST_VIRTUAL SystemDialogResult ConsoleSystemDialog(SystemDialogType type, const std::string& title, const std::string& message);
109 
110     //! Creates a new time stamp object
111     TEST_VIRTUAL SystemTimeStamp* CreateTimeStamp();
112 
113     //! Destroys a time stamp object
114     TEST_VIRTUAL void DestroyTimeStamp(SystemTimeStamp *stamp);
115 
116     //! Copies the time stamp from \a src to \a dst
117     TEST_VIRTUAL void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src);
118 
119     //! Interpolates between two timestamps. If i=0 then dst=a. If i=1 then dst=b. If i=0.5 then dst is halfway between.
120     virtual void InterpolateTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *a, SystemTimeStamp *b, float i) = 0;
121 
122     //! Returns a time stamp associated with current time
123     virtual void GetCurrentTimeStamp(SystemTimeStamp *stamp) = 0;
124 
125     //! Returns a difference between two timestamps in given time unit
126     /** The difference is \a after - \a before. */
127     TEST_VIRTUAL float TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit = STU_SEC);
128 
129     //! Returns the exact (in nanosecond units) difference between two timestamps
130     /** The difference is \a after - \a before. */
131     virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) = 0;
132 
133     //! Returns the path where the executable binary is located (ends with the path separator)
134     virtual std::string GetBasePath();
135 
136     //! Returns the data path (containing textures, levels, helpfiles, etc)
137     virtual std::string GetDataPath();
138 
139     //! Returns the translations path
140     virtual std::string GetLangPath();
141 
142     //! Returns the save dir location
143     virtual std::string GetSaveDir();
144 
145     //! Returns the environment variable with the given name or an empty string if it does not exist
146     virtual std::string GetEnvVar(const std::string &name);
147 
148     //! Opens a path with default file browser
149     /** \returns true if successful */
150     virtual bool OpenPath(const std::string& path);
151 
152     //! Opens a website with default web browser
153     /** \returns true if successful */
154     virtual bool OpenWebsite(const std::string& url);
155 
156     //! Sleep for given amount of microseconds
157     virtual void Usleep(int usecs) = 0;
158 
159 private:
160     std::string m_basePath;
161     std::vector<std::unique_ptr<SystemTimeStamp>> m_timeStamps;
162 };
163