summaryrefslogtreecommitdiffstats
path: root/src/app/system.cpp
blob: 73614aa84b378c96da8d96a2773aaabb71f4889f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.


#include "app/system.h"

#include "common/config.h"


#if defined(PLATFORM_WINDOWS)
#include "app/system_windows.h"

#elif defined(PLATFORM_LINUX)
#include "app/system_linux.h"

#else
#include "app/system_other.h"

#endif


#include <cassert>


/**
 * Displays a system dialog with info, error, question etc. message.
 *
 * \param type type of dialog
 * \param message text of message (in UTF-8)
 * \param title dialog title (in UTF-8)
 * \returns result (which button was clicked)
 */
SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message)
{
#if defined(PLATFORM_WINDOWS)
    return SystemDialog_Windows(type, title, message);
#elif defined(PLATFORM_LINUX)
    return SystemDialog_Linux(type, title, message);
#else
    return SystemDialog_Other(type, title, message);
#endif
}

SystemTimeStamp* CreateTimeStamp()
{
    return new SystemTimeStamp();
}

void DestroyTimeStamp(SystemTimeStamp *stamp)
{
    delete stamp;
}

void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src)
{
    *dst = *src;
}

void GetCurrentTimeStamp(SystemTimeStamp *stamp)
{
#if defined(PLATFORM_WINDOWS)
    GetCurrentTimeStamp_Windows(stamp);
#elif defined(PLATFORM_LINUX)
    GetCurrentTimeStamp_Linux(stamp);
#else
    GetCurrentTimeStamp_Other(stamp);
#endif
}

float GetTimeStampResolution(SystemTimeUnit unit)
{
    unsigned long long exact = 0;
#if defined(PLATFORM_WINDOWS)
    exact = GetTimeStampExactResolution_Windows();
#elif defined(PLATFORM_LINUX)
    exact = GetTimeStampExactResolution_Linux();
#else
    exact = GetTimeStampExactResolution_Other();
#endif
    float result = 0.0f;
    if (unit == STU_SEC)
        result = exact * 1e-9;
    else if (unit == STU_MSEC)
        result = exact * 1e-6;
    else if (unit == STU_USEC)
        result = exact * 1e-3;
    else
        assert(false);
    return result;
}

long long GetTimeStampExactResolution()
{
#if defined(PLATFORM_WINDOWS)
    return GetTimeStampExactResolution_Windows();
#elif defined(PLATFORM_LINUX)
    return GetTimeStampExactResolution_Linux();
#else
    return GetTimeStampExactResolution_Other();
#endif
}

float TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit)
{
    long long exact = 0;
#if defined(PLATFORM_WINDOWS)
    exact = TimeStampExactDiff_Windows(before, after);
#elif defined(PLATFORM_LINUX)
    exact = TimeStampExactDiff_Linux(before, after);
#else
    exact = TimeStampExactDiff_Other(before, after);
#endif
    float result = 0.0f;
    if (unit == STU_SEC)
        result = exact * 1e-9;
    else if (unit == STU_MSEC)
        result = exact * 1e-6;
    else if (unit == STU_USEC)
        result = exact * 1e-3;
    else
        assert(false);
    return result;
}

long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after)
{
#if defined(PLATFORM_WINDOWS)
    return TimeStampExactDiff_Windows(before, after);
#elif defined(PLATFORM_LINUX)
    return TimeStampExactDiff_Linux(before, after);
#else
    return TimeStampExactDiff_Other(before, after);
#endif
}