summaryrefslogtreecommitdiffstats
path: root/src/app/system_windows.cpp
blob: a2ea18f620fa32aa43210455e8586a16d8b00fb6 (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
/*
 * This file is part of the Colobot: Gold Edition source code
 * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
 * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
 *
 * 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://gnu.org/licenses
 */

#include "app/system_windows.h"

#include "common/logger.h"

#include <windows.h>


void CSystemUtilsWindows::Init()
{
    LARGE_INTEGER freq;
    QueryPerformanceFrequency(&freq);
    m_counterFrequency = freq.QuadPart;

    assert(m_counterFrequency != 0);
}

SystemDialogResult CSystemUtilsWindows::SystemDialog(SystemDialogType type, const std::string& title, const std::string& message)
{
    unsigned int windowsType = 0;
    std::wstring windowsMessage = UTF8_Decode(message);
    std::wstring windowsTitle = UTF8_Decode(title);

    switch (type)
    {
        case SDT_INFO:
        default:
            windowsType = MB_ICONINFORMATION|MB_OK;
            break;
        case SDT_WARNING:
            windowsType = MB_ICONWARNING|MB_OK;
            break;
        case SDT_ERROR:
            windowsType = MB_ICONERROR|MB_OK;
            break;
        case SDT_YES_NO:
            windowsType = MB_ICONQUESTION|MB_YESNO;
            break;
        case SDT_OK_CANCEL:
            windowsType = MB_ICONWARNING|MB_OKCANCEL;
            break;
    }

    switch (MessageBoxW(NULL, windowsMessage.c_str(), windowsTitle.c_str(), windowsType))
    {
        case IDOK:
            return SDR_OK;
        case IDCANCEL:
            return SDR_CANCEL;
        case IDYES:
            return SDR_YES;
        case IDNO:
            return SDR_NO;
        default:
            break;
    }

    return SDR_OK;
}

void CSystemUtilsWindows::GetCurrentTimeStamp(SystemTimeStamp* stamp)
{
    LARGE_INTEGER value;
    QueryPerformanceCounter(&value);
    stamp->counterValue = value.QuadPart;
}

long long int CSystemUtilsWindows::GetTimeStampExactResolution()
{
    return 1000000000ll / m_counterFrequency;
}

long long int CSystemUtilsWindows::TimeStampExactDiff(SystemTimeStamp* before, SystemTimeStamp* after)
{
    float floatValue = static_cast<double>(after->counterValue - before->counterValue) * (1e9 / static_cast<double>(m_counterFrequency));
    return static_cast<long long>(floatValue);
}

//! Converts a wide Unicode string to an UTF8 string
std::string CSystemUtilsWindows::UTF8_Encode(const std::wstring& wstr)
{
    int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(wstr.size()), NULL, 0, NULL, NULL);
    std::string strTo(size_needed, 0);
    WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(wstr.size()), &strTo[0], size_needed, NULL, NULL);
    return strTo;
}

//! Converts an UTF8 string to a wide Unicode String
std::wstring CSystemUtilsWindows::UTF8_Decode(const std::string& str)
{
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast<int>(str.size()), NULL, 0);
    std::wstring wstrTo(size_needed, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast<int>(str.size()), &wstrTo[0], size_needed);
    return wstrTo;

}

std::string CSystemUtilsWindows::GetSaveDir()
{
    std::string savegameDir;

    char* envUSERPROFILE = getenv("USERPROFILE");
    if (envUSERPROFILE == NULL)
    {
        savegameDir = "save";
    }
    else
    {
        savegameDir = std::string(envUSERPROFILE) + "\\colobot";
    }
    GetLogger()->Trace("Saved game files are going to %s\n", savegameDir.c_str());

    return savegameDir;
}