Refactored platform-specific code

Moved functions from .h to .cpp files
dev-ui
Piotr Dziwinski 2013-03-19 23:07:39 +01:00
parent 4abcaae0f7
commit 025bedecfb
10 changed files with 410 additions and 263 deletions

View File

@ -55,11 +55,21 @@ if (${OPENAL_SOUND})
)
endif()
# Platform-dependent implementation of system.h
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(SYSTEM_CPP_MODULE "system_windows.cpp")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(SYSTEM_CPP_MODULE "system_linux.cpp")
else()
set(SYSTEM_CPP_MODULE "system_other.cpp")
endif()
# Source files
set(SOURCES
app/app.cpp
app/main.cpp
app/system.cpp
app/${SYSTEM_CPP_MODULE}
common/event.cpp
common/image.cpp
common/iman.cpp

79
src/app/system_linux.cpp Normal file
View File

@ -0,0 +1,79 @@
// * 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_linux.h"
#include <stdlib.h>
SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message)
{
std::string options = "";
switch (type)
{
case SDT_INFO:
default:
options = "--info";
break;
case SDT_WARNING:
options = "--warning";
break;
case SDT_ERROR:
options = "--error";
break;
case SDT_YES_NO:
options = "--question --ok-label=\"Yes\" --cancel-label=\"No\"";
break;
case SDT_OK_CANCEL:
options = "--question --ok-label=\"OK\" --cancel-label=\"Cancel\"";
break;
}
std::string command = "zenity " + options + " --text=\"" + message + "\" --title=\"" + title + "\"";
int code = system(command.c_str());
SystemDialogResult result = SDR_OK;
switch (type)
{
case SDT_YES_NO:
result = code ? SDR_NO : SDR_YES;
break;
case SDT_OK_CANCEL:
result = code ? SDR_CANCEL : SDR_OK;
break;
default:
break;
}
return result;
}
void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp)
{
clock_gettime(CLOCK_MONOTONIC, &stamp->clockTime);
}
long long GetTimeStampExactResolution_Linux()
{
return 1ll;
}
long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after)
{
return (after->clockTime.tv_nsec - before->clockTime.tv_nsec) +
(after->clockTime.tv_sec - before->clockTime.tv_sec) * 1000000000ll;
}

View File

@ -20,20 +20,11 @@
* \brief Linux-specific implementation of system functions
*/
/* NOTE: code is contained in this header;
* there is no separate .cpp module for simplicity */
#include "app/system.h"
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message);
void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp);
long long GetTimeStampExactResolution_Linux();
long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after);
struct SystemTimeStamp
{
timespec clockTime;
@ -45,60 +36,8 @@ struct SystemTimeStamp
};
SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message)
{
std::string options = "";
switch (type)
{
case SDT_INFO:
default:
options = "--info";
break;
case SDT_WARNING:
options = "--warning";
break;
case SDT_ERROR:
options = "--error";
break;
case SDT_YES_NO:
options = "--question --ok-label=\"Yes\" --cancel-label=\"No\"";
break;
case SDT_OK_CANCEL:
options = "--question --ok-label=\"OK\" --cancel-label=\"Cancel\"";
break;
}
SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message);
std::string command = "zenity " + options + " --text=\"" + message + "\" --title=\"" + title + "\"";
int code = system(command.c_str());
SystemDialogResult result = SDR_OK;
switch (type)
{
case SDT_YES_NO:
result = code ? SDR_NO : SDR_YES;
break;
case SDT_OK_CANCEL:
result = code ? SDR_CANCEL : SDR_OK;
break;
default:
break;
}
return result;
}
void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp)
{
clock_gettime(CLOCK_MONOTONIC, &stamp->clockTime);
}
long long GetTimeStampExactResolution_Linux()
{
return 1ll;
}
long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after)
{
return (after->clockTime.tv_nsec - before->clockTime.tv_nsec) +
(after->clockTime.tv_sec - before->clockTime.tv_sec) * 1000000000ll;
}
void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp);
long long GetTimeStampExactResolution_Linux();
long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after);

122
src/app/system_other.cpp Normal file
View File

@ -0,0 +1,122 @@
// * 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_other.h"
SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message)
{
switch (type)
{
case SDT_INFO:
std::cout << "INFO: ";
break;
case SDT_WARNING:
std::cout << "WARNING:";
break;
case SDT_ERROR:
std::cout << "ERROR: ";
break;
case SDT_YES_NO:
case SDT_OK_CANCEL:
std::cout << "QUESTION: ";
break;
}
std::cout << message << std::endl;
std::string line;
SystemDialogResult result = SDR_OK;
bool done = false;
while (!done)
{
switch (type)
{
case SDT_INFO:
case SDT_WARNING:
case SDT_ERROR:
std::cout << "Press ENTER to continue";
break;
case SDT_YES_NO:
std::cout << "Type 'Y' for Yes or 'N' for No";
break;
case SDT_OK_CANCEL:
std::cout << "Type 'O' for OK or 'C' for Cancel";
break;
}
std::getline(std::cin, line);
switch (type)
{
case SDT_INFO:
case SDT_WARNING:
case SDT_ERROR:
done = true;
break;
case SDT_YES_NO:
if (line == "Y" || line == "y")
{
result = SDR_YES;
done = true;
}
else if (line == "N" || line == "n")
{
result = SDR_NO;
done = true;
}
break;
case SDT_OK_CANCEL:
if (line == "O" || line == "o")
{
done = true;
result = SDR_OK;
}
else if (line == "C" || line == "c")
{
done = true;
result = SDR_CANCEL;
}
break;
}
}
return result;
}
void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp)
{
stamp->sdlTicks = SDL_GetTicks();
}
long long GetTimeStampExactResolution_Other()
{
return 1000000ll;
}
long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after)
{
return (after->sdlTicks - before->sdlTicks) * 1000000ll;
}

View File

@ -20,20 +20,13 @@
* \brief Fallback code for other systems
*/
/* NOTE: code is contained in this header;
* there is no separate .cpp module for simplicity */
#include "app/system.h"
#include <SDL/SDL.h>
#include <iostream>
SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message);
void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp);
long long GetTimeStampExactResolution_Other();
long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after);
struct SystemTimeStamp
{
Uint32 sdlTicks;
@ -45,105 +38,8 @@ struct SystemTimeStamp
};
SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message)
{
switch (type)
{
case SDT_INFO:
std::cout << "INFO: ";
break;
case SDT_WARNING:
std::cout << "WARNING:";
break;
case SDT_ERROR:
std::cout << "ERROR: ";
break;
case SDT_YES_NO:
case SDT_OK_CANCEL:
std::cout << "QUESTION: ";
break;
}
SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message);
std::cout << message << std::endl;
std::string line;
SystemDialogResult result = SDR_OK;
bool done = false;
while (!done)
{
switch (type)
{
case SDT_INFO:
case SDT_WARNING:
case SDT_ERROR:
std::cout << "Press ENTER to continue";
break;
case SDT_YES_NO:
std::cout << "Type 'Y' for Yes or 'N' for No";
break;
case SDT_OK_CANCEL:
std::cout << "Type 'O' for OK or 'C' for Cancel";
break;
}
std::getline(std::cin, line);
switch (type)
{
case SDT_INFO:
case SDT_WARNING:
case SDT_ERROR:
done = true;
break;
case SDT_YES_NO:
if (line == "Y" || line == "y")
{
result = SDR_YES;
done = true;
}
else if (line == "N" || line == "n")
{
result = SDR_NO;
done = true;
}
break;
case SDT_OK_CANCEL:
if (line == "O" || line == "o")
{
done = true;
result = SDR_OK;
}
else if (line == "C" || line == "c")
{
done = true;
result = SDR_CANCEL;
}
break;
}
}
return result;
}
void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp)
{
stamp->sdlTicks = SDL_GetTicks();
}
long long GetTimeStampExactResolution_Other()
{
return 1000000ll;
}
long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after)
{
return (after->sdlTicks - before->sdlTicks) * 1000000ll;
}
void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp);
long long GetTimeStampExactResolution_Other();
long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after);

View File

@ -0,0 +1,98 @@
// * 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_windows.h"
// Convert a wide Unicode string to an UTF8 string
std::string UTF8_Encode_Windows(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;
}
// Convert an UTF8 string to a wide Unicode String
std::wstring UTF8_Decode_Windows(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;
}
SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message)
{
unsigned int windowsType = 0;
std::wstring windowsMessage = UTF8_Decode_Windows(message);
std::wstring windowsTitle = UTF8_Decode_Windows(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 GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp)
{
GetSystemTimeAsFileTime(&stamp->fileTime);
}
long long GetTimeStampExactResolution_Windows()
{
return 100ll;
}
long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after)
{
long long tH = (1ll << 32) * (after->fileTime.dwHighDateTime - before->fileTime.dwHighDateTime);
long long tL = after->fileTime.dwLowDateTime - before->fileTime.dwLowDateTime;
return (tH + tL) * 100ll;
}

View File

@ -20,20 +20,11 @@
* \brief Windows-specific implementation of system functions
*/
/* NOTE: code is contained in this header;
* there is no separate .cpp module for simplicity */
#include "app/system.h"
#include <windows.h>
std::string UTF8_Encode_Windows(const std::wstring &wstr);
std::wstring UTF8_Decode_Windows(const std::string &str);
SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message);
void GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp);
long long GetTimeStampExactResolution_Windows();
long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after);
struct SystemTimeStamp
{
FILETIME fileTime;
@ -44,82 +35,10 @@ struct SystemTimeStamp
}
};
std::string UTF8_Encode_Windows(const std::wstring &wstr);
std::wstring UTF8_Decode_Windows(const std::string &str);
SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message);
// Convert a wide Unicode string to an UTF8 string
std::string UTF8_Encode_Windows(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;
}
// Convert an UTF8 string to a wide Unicode String
std::wstring UTF8_Decode_Windows(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;
}
SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message)
{
unsigned int windowsType = 0;
std::wstring windowsMessage = UTF8_Decode_Windows(message);
std::wstring windowsTitle = UTF8_Decode_Windows(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 GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp)
{
GetSystemTimeAsFileTime(&stamp->fileTime);
}
long long GetTimeStampExactResolution_Windows()
{
return 100ll;
}
long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after)
{
long long tH = (1ll << 32) * (after->fileTime.dwHighDateTime - before->fileTime.dwHighDateTime);
long long tL = after->fileTime.dwLowDateTime - before->fileTime.dwLowDateTime;
return (tH + tL) * 100ll;
}
void GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp);
long long GetTimeStampExactResolution_Windows();
long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after);

View File

@ -2,6 +2,14 @@ set(SRC_DIR ${colobot_SOURCE_DIR}/src)
configure_file(${SRC_DIR}/common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/config.h)
# Platform-dependent implementation of system.h
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(SYSTEM_CPP_MODULE "system_windows.cpp")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(SYSTEM_CPP_MODULE "system_linux.cpp")
else()
set(SYSTEM_CPP_MODULE "system_other.cpp")
endif()
set(TEXTURE_SOURCES
${SRC_DIR}/graphics/opengl/gldevice.cpp
@ -17,6 +25,7 @@ ${SRC_DIR}/common/logger.cpp
${SRC_DIR}/common/image.cpp
${SRC_DIR}/common/stringutils.cpp
${SRC_DIR}/app/system.cpp
${SRC_DIR}/app/${SYSTEM_CPP_MODULE}
model_test.cpp
)
@ -25,6 +34,7 @@ ${SRC_DIR}/graphics/opengl/gldevice.cpp
${SRC_DIR}/common/logger.cpp
${SRC_DIR}/common/image.cpp
${SRC_DIR}/app/system.cpp
${SRC_DIR}/app/${SYSTEM_CPP_MODULE}
transform_test.cpp
)
@ -33,6 +43,7 @@ ${SRC_DIR}/graphics/opengl/gldevice.cpp
${SRC_DIR}/common/logger.cpp
${SRC_DIR}/common/image.cpp
${SRC_DIR}/app/system.cpp
${SRC_DIR}/app/${SYSTEM_CPP_MODULE}
light_test.cpp
)

View File

@ -15,10 +15,20 @@ endif()
# Configure file
configure_file(${SRC_DIR}/common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/config.h)
# Platform-dependent implementation of system.h
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(SYSTEM_CPP_MODULE "system_windows.cpp")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(SYSTEM_CPP_MODULE "system_linux.cpp")
else()
set(SYSTEM_CPP_MODULE "system_other.cpp")
endif()
# Code sources
set(COLOBOT_SOURCES
${SRC_DIR}/app/app.cpp
${SRC_DIR}/app/system.cpp
${SRC_DIR}/app/${SYSTEM_CPP_MODULE}
${SRC_DIR}/common/event.cpp
${SRC_DIR}/common/image.cpp
${SRC_DIR}/common/iman.cpp
@ -159,12 +169,24 @@ if (${OPENAL_SOUND})
endif()
endif()
# Platform-dependent tests
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
#TODO: set(PLATFORM_TESTS app/system_windows_test.cpp)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(PLATFORM_TESTS app/system_linux_test.cpp)
else()
#TODO: set(PLATFORM_TESTS app/system_other_test.cpp)
endif()
# Tests
set(UT_SOURCES
main.cpp
graphics/engine/lightman_test.cpp
math/geometry_test.cpp
math/matrix_test.cpp
math/vector_test.cpp
${PLATFORM_TESTS}
)
include_directories(

View File

@ -0,0 +1,51 @@
#include "app/system.h"
#include "app/system_linux.h"
#include <gtest/gtest.h>
TEST(SystemLinuxTest, TimeStampDiff)
{
const long long SEC = 1000000000;
SystemTimeStamp before, after;
before.clockTime.tv_sec = 1;
before.clockTime.tv_nsec = 100;
after.clockTime.tv_sec = 1;
after.clockTime.tv_nsec = 900;
long long tDiff = TimeStampExactDiff_Linux(&before, &after);
EXPECT_EQ( 800, tDiff);
tDiff = TimeStampExactDiff_Linux(&after, &before);
EXPECT_EQ(-800, tDiff);
// -------
before.clockTime.tv_sec = 2;
before.clockTime.tv_nsec = 200;
after.clockTime.tv_sec = 3;
after.clockTime.tv_nsec = 500;
tDiff = TimeStampExactDiff_Linux(&before, &after);
EXPECT_EQ( SEC + 300, tDiff);
tDiff = TimeStampExactDiff_Linux(&after, &before);
EXPECT_EQ(-SEC - 300, tDiff);
// -------
before.clockTime.tv_sec = 3;
before.clockTime.tv_nsec = 200;
after.clockTime.tv_sec = 4;
after.clockTime.tv_nsec = 100;
tDiff = TimeStampExactDiff_Linux(&before, &after);
EXPECT_EQ( SEC - 100, tDiff);
tDiff = TimeStampExactDiff_Linux(&after, &before);
EXPECT_EQ(-SEC + 100, tDiff);
}