start 380 project5
changes to 380 project 4 changes to 350 project 2 Signed-off-by: Amrit Panesar <apanesar@4195tech.com>
This commit is contained in:
1 parent
40ac78074b
commit
39ee821f54
174 files changed
+13356
-80
No files matched your search
@@ -0,0 +1,285 @@
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// GambleKit Copyright (C) 2014 Ivan Donev
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// contact: gamblekit@gmail.com
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include "GkFileSystem.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "..\Utils\GkStrings.h"
|
||||
|
||||
std::wstring GkFileSystem::source;
|
||||
//wchar_t *GkFileSystem::source;
|
||||
FileSystemMode GkFileSystem::mode;
|
||||
GkFileSystem::Init GkFileSystem::init;
|
||||
|
||||
GkFileSystem::Init::Init(void)
|
||||
{
|
||||
//GkFileSystem::source = GkStrings::Copy(L"\0");
|
||||
}
|
||||
|
||||
GkFileSystem::Init::~Init(void)
|
||||
{
|
||||
//delete[] GkFileSystem::source;
|
||||
}
|
||||
|
||||
GkFileSystem::GkFileSystem(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
GkFileSystem::~GkFileSystem(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void GkFileSystem::SetSource(const wchar_t *source, FileSystemMode mode)
|
||||
{
|
||||
//delete[] GkFileSystem::source;
|
||||
|
||||
//if (source == 0)
|
||||
// GkFileSystem::source = GkStrings::Copy(L"\0");
|
||||
//else
|
||||
// GkFileSystem::source = GkStrings::Copy(source);
|
||||
|
||||
if (source == 0)
|
||||
GkFileSystem::source = std::wstring(L"\0");
|
||||
else
|
||||
GkFileSystem::source = std::wstring(source);
|
||||
|
||||
|
||||
|
||||
GkFileSystem::mode = mode;
|
||||
}
|
||||
|
||||
bool GkFileSystem::LoadFile(const wchar_t *fileName, void **fileData, unsigned int *fileSize)
|
||||
{
|
||||
if (GkFileSystem::mode == FileSystemMode_Physical)
|
||||
return GkFileSystem::LoadPhysicalFile(fileName, fileData, fileSize);
|
||||
|
||||
if (GkFileSystem::mode == FileSystemMode_Virtual)
|
||||
return GkFileSystem::LoadVirtualFile(fileName, fileData, fileSize);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void GkFileSystem::CreateVirtualFileSystem(const wchar_t* directory, const wchar_t* fileName)
|
||||
{
|
||||
std::ofstream fileStream(fileName, std::ios::out | std::ios::binary);
|
||||
|
||||
GkFileSystem::WriteVirtualFileSystem(directory, &fileStream);
|
||||
|
||||
fileStream.close();
|
||||
}
|
||||
|
||||
bool GkFileSystem::LoadVirtualFile(const wchar_t *fileName, void **fileData, unsigned int *fileSize)
|
||||
{
|
||||
std::vector<wchar_t*> pathElements;
|
||||
|
||||
int elementStart = 0;
|
||||
int i = 0;
|
||||
bool done;
|
||||
do
|
||||
{
|
||||
done = *((char*)&fileName[i]) == 0;
|
||||
|
||||
if ((fileName[i] == L'\\') || done)
|
||||
{
|
||||
int l = i - elementStart;
|
||||
wchar_t *element = new wchar_t[l + 1];
|
||||
CopyMemory(element, &fileName[elementStart], l * sizeof(wchar_t));
|
||||
element[l] = 0;
|
||||
pathElements.push_back(element);
|
||||
elementStart = i + 1;
|
||||
}
|
||||
|
||||
i++;
|
||||
} while (!done);
|
||||
|
||||
|
||||
std::ifstream file(GkFileSystem::source, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
int pathElementIndex = 0;
|
||||
|
||||
int dirLimit = (int)file.tellg();
|
||||
file.seekg(0);
|
||||
|
||||
done = false;
|
||||
while (file && !done)
|
||||
{
|
||||
wchar_t *vfn = GkFileSystem::ReadString(&file);
|
||||
int size;
|
||||
file.read((char*)&size, sizeof(int));
|
||||
|
||||
if (wcscmp(vfn, pathElements[pathElementIndex]) == 0)
|
||||
{
|
||||
pathElementIndex++;
|
||||
dirLimit = (int)file.tellg() + size;
|
||||
|
||||
if (pathElementIndex == pathElements.size())
|
||||
{
|
||||
*fileSize = size;
|
||||
*fileData = new char[size];
|
||||
file.read((char*)*fileData, size);
|
||||
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
file.seekg((int)file.tellg() + size);
|
||||
}
|
||||
|
||||
delete vfn;
|
||||
|
||||
if (file.tellg() >= dirLimit)
|
||||
break;
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
|
||||
for (int i = 0; i < (int)pathElements.size(); i++)
|
||||
{
|
||||
delete pathElements[i];
|
||||
}
|
||||
|
||||
return done;
|
||||
}
|
||||
|
||||
bool GkFileSystem::LoadPhysicalFile(const wchar_t *fileName, void **fileData, unsigned int *fileSize)
|
||||
{
|
||||
// TODO: fix this static string
|
||||
std::wstring fn(GkFileSystem::source);
|
||||
fn.append(fileName);
|
||||
|
||||
std::ifstream file(fn.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||
|
||||
if (!file)
|
||||
return false;
|
||||
|
||||
*fileSize = (unsigned int)file.tellg();
|
||||
*fileData = new char[*fileSize];
|
||||
|
||||
file.seekg(0);
|
||||
file.read((char*)*fileData, *fileSize);
|
||||
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
wchar_t* GkFileSystem::ReadString(std::ifstream* fileStream)
|
||||
{
|
||||
int l;
|
||||
fileStream->read((char*)&l, sizeof(int));
|
||||
|
||||
wchar_t *result = new wchar_t[l + 1];
|
||||
fileStream->read((char*)result, l * sizeof(wchar_t));
|
||||
result[l] = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void GkFileSystem::WriteVirtualFileSystem(const wchar_t* directory, std::ofstream* fileStream)
|
||||
{
|
||||
std::wstring fullSearchDir(directory);
|
||||
fullSearchDir.append(L"\\*");
|
||||
|
||||
WIN32_FIND_DATAW findData;
|
||||
|
||||
HANDLE findHandle = INVALID_HANDLE_VALUE;
|
||||
findHandle = FindFirstFileW(fullSearchDir.c_str(), &findData);
|
||||
|
||||
if (findHandle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int chunkSize, chunkSizePos;
|
||||
do
|
||||
{
|
||||
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
{
|
||||
if ((wcscmp(findData.cFileName, L".") != 0) && (wcscmp(findData.cFileName, L"..") != 0))
|
||||
{
|
||||
std::wstring fullSearchSubDir(directory);
|
||||
fullSearchSubDir.append(L"\\");
|
||||
fullSearchSubDir.append(findData.cFileName);
|
||||
|
||||
GkFileSystem::WriteString(findData.cFileName, fileStream);
|
||||
|
||||
chunkSizePos = (int)fileStream->tellp();
|
||||
fileStream->write((char*)&chunkSize, sizeof(int));
|
||||
|
||||
GkFileSystem::WriteVirtualFileSystem(fullSearchSubDir.c_str(), fileStream);
|
||||
|
||||
int endPos = (int)fileStream->tellp();
|
||||
chunkSize = endPos - chunkSizePos - 4;
|
||||
fileStream->seekp(chunkSizePos);
|
||||
fileStream->write((char*)&chunkSize, sizeof(int));
|
||||
fileStream->seekp(endPos);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::wstring fullFileName(directory);
|
||||
fullFileName.append(L"\\");
|
||||
fullFileName.append(findData.cFileName);
|
||||
|
||||
GkFileSystem::WriteString(findData.cFileName, fileStream);
|
||||
|
||||
chunkSizePos = (int)fileStream->tellp();
|
||||
fileStream->write((char*)&chunkSize, sizeof(int));
|
||||
|
||||
GkFileSystem::WriteFile(fullFileName.c_str(), fileStream);
|
||||
|
||||
int endPos = (int)fileStream->tellp();
|
||||
chunkSize = endPos - chunkSizePos - 4;
|
||||
fileStream->seekp(chunkSizePos);
|
||||
fileStream->write((char*)&chunkSize, sizeof(int));
|
||||
fileStream->seekp(endPos);
|
||||
}
|
||||
|
||||
}
|
||||
while (FindNextFileW(findHandle, &findData) != 0);
|
||||
|
||||
FindClose(findHandle);
|
||||
}
|
||||
|
||||
void GkFileSystem::WriteString(const wchar_t* s, std::ofstream* fileStream)
|
||||
{
|
||||
int l = 0;
|
||||
while (s[l] != 0)
|
||||
l++;
|
||||
|
||||
fileStream->write((char*)&l, sizeof(int));
|
||||
fileStream->write((char*)s, l * sizeof(wchar_t));
|
||||
}
|
||||
|
||||
void GkFileSystem::WriteFile(const wchar_t* fileName, std::ofstream* fileStream)
|
||||
{
|
||||
std::ifstream file(fileName, std::ios::in | std::ios::binary);
|
||||
|
||||
const int bufferSize = 2048;
|
||||
char buffer[bufferSize];
|
||||
|
||||
for (;;)
|
||||
{
|
||||
file.read(buffer, bufferSize);
|
||||
fileStream->write(buffer, file.gcount());
|
||||
|
||||
if (!file)
|
||||
break;
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include "..\GkDefinitions.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
enum FileSystemMode
|
||||
{
|
||||
FileSystemMode_Physical,
|
||||
FileSystemMode_Virtual
|
||||
};
|
||||
|
||||
class GKDLLCLASS GkFileSystem
|
||||
{
|
||||
private:
|
||||
static class GKDLLCLASS Init
|
||||
{
|
||||
public:
|
||||
Init(void);
|
||||
~Init(void);
|
||||
} init;
|
||||
|
||||
static std::wstring source;
|
||||
//static wchar_t *source;
|
||||
static FileSystemMode mode;
|
||||
|
||||
static wchar_t* ReadString(std::ifstream* fileStream);
|
||||
|
||||
static void WriteVirtualFileSystem(const wchar_t* directory, std::ofstream* fileStream);
|
||||
|
||||
static void WriteString(const wchar_t* s, std::ofstream* fileStream);
|
||||
static void WriteFile(const wchar_t* fileName, std::ofstream* fileStream);
|
||||
|
||||
static bool LoadPhysicalFile(const wchar_t *fileName, void **fileData, unsigned int *fileSize);
|
||||
static bool LoadVirtualFile(const wchar_t *fileName, void **fileData, unsigned int *fileSize);
|
||||
|
||||
public:
|
||||
GkFileSystem(void);
|
||||
~GkFileSystem(void);
|
||||
|
||||
// Sets the source and mode of the virtual file system.
|
||||
// If mode is FileSystemMode_Virtual, the source should be the name of the file, containing the virtual file system. Such file can be produced by the CreateVirtualFileSystem method.
|
||||
// If mode is FileSystemMode_Physical, the source can be a root directory for the game's resources. Usually, this root directory is used to create a virtual file system from.
|
||||
static void SetSource(const wchar_t *source, FileSystemMode mode);
|
||||
|
||||
// Allocates memory in fileData and loads the requested file in it, returning the size of the file in fileSize
|
||||
static bool LoadFile(const wchar_t *fileName, void **fileData, unsigned int *fileSize);
|
||||
|
||||
// Writes the contents of a directory and all its subdirectories into a virtual file with the given name. The file can then be used as a virtual file system file.
|
||||
static void CreateVirtualFileSystem(const wchar_t* directory, const wchar_t* fileName);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// GambleKit Copyright (C) 2014 Ivan Donev
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// contact: gamblekit@gmail.com
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include "GkOSUtils.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
GkOSUtils::GkOSUtils(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
GkOSUtils::~GkOSUtils(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void* GkOSUtils::GetMem(int size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void GkOSUtils::FreeMem(void* data)
|
||||
{
|
||||
free(data);
|
||||
}
|
||||
|
||||
void GkOSUtils::ZeroMem(void* data, int size)
|
||||
{
|
||||
memset(data, 0, size);
|
||||
}
|
||||
|
||||
void GkOSUtils::CopyMem(void* destination, void* source, int size)
|
||||
{
|
||||
memcpy(destination, source, size);
|
||||
}
|
||||
|
||||
int GkOSUtils::CompareMem(void* block1, void* block2, int size)
|
||||
{
|
||||
return memcmp(block1, block2, size);
|
||||
}
|
||||
|
||||
long long GkOSUtils::GetPerformanceCounter()
|
||||
{
|
||||
LARGE_INTEGER result;
|
||||
|
||||
if (QueryPerformanceCounter(&result))
|
||||
return result.QuadPart;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
long long GkOSUtils::GetPerformanceCounterFrequency()
|
||||
{
|
||||
LARGE_INTEGER result;
|
||||
|
||||
if (QueryPerformanceFrequency(&result))
|
||||
return result.QuadPart;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
void GkOSUtils::ShowMessage(const char *text, HWND windowHandle)
|
||||
{
|
||||
MessageBox(windowHandle, text, "Message", MB_OK);
|
||||
}
|
||||
|
||||
int GkOSUtils::GetScreenWidth()
|
||||
{
|
||||
return GetSystemMetrics(SM_CXSCREEN);
|
||||
}
|
||||
|
||||
int GkOSUtils::GetScreenHeight()
|
||||
{
|
||||
return GetSystemMetrics(SM_CYSCREEN);
|
||||
}
|
||||
|
||||
|
||||
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
|
||||
{
|
||||
int *Count = (int*)dwData;
|
||||
(*Count)++;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int GkOSUtils::GetNumberOfScreens()
|
||||
{
|
||||
int Count = 0;
|
||||
if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&Count))
|
||||
return Count;
|
||||
return -1;//signals an error
|
||||
}
|
||||
|
||||
DateTime GkOSUtils::GetDateTime()
|
||||
{
|
||||
time_t currentTime;
|
||||
time(¤tTime);
|
||||
|
||||
tm localTime;
|
||||
localtime_s(&localTime, ¤tTime);
|
||||
|
||||
DateTime result;
|
||||
result.day = localTime.tm_mday;
|
||||
result.month = 1 + localTime.tm_mon;
|
||||
result.year = 1900 + localTime.tm_year;
|
||||
result.hour = localTime.tm_hour;
|
||||
result.minute = localTime.tm_min;
|
||||
result.second = localTime.tm_sec;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool GkOSUtils::SetDateTime(DateTime &dateTime)
|
||||
{
|
||||
SYSTEMTIME sysTime;
|
||||
sysTime.wDay = dateTime.day;
|
||||
sysTime.wMonth = dateTime.month;
|
||||
sysTime.wYear = dateTime.year;
|
||||
sysTime.wHour = dateTime.hour;
|
||||
sysTime.wMinute = dateTime.minute;
|
||||
sysTime.wSecond = dateTime.second;
|
||||
sysTime.wMilliseconds = 0;
|
||||
|
||||
return SetLocalTime(&sysTime) != 0;
|
||||
}
|
||||
|
||||
int GkOSUtils::GetSecondsElapsed(DateTime &since)
|
||||
{
|
||||
//TODO
|
||||
|
||||
DateTime now = GkOSUtils::GetDateTime();
|
||||
return ((now.hour - since.hour) * 60 + (now.minute - since.minute)) * 60 + (now.second - since.second);
|
||||
}
|
||||
|
||||
DateTime GkOSUtils::GetFileModificationDateTime(const wchar_t *fileName)
|
||||
{
|
||||
DateTime result;
|
||||
|
||||
HANDLE handle = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
FILETIME created, accessed, modified;
|
||||
|
||||
GetFileTime(handle, &created, &accessed, &modified);
|
||||
|
||||
FILETIME localFileTime;
|
||||
FileTimeToLocalFileTime(&modified, &localFileTime);
|
||||
|
||||
SYSTEMTIME localTime;
|
||||
FileTimeToSystemTime(&localFileTime, &localTime);
|
||||
|
||||
result.day = localTime.wDay;
|
||||
result.month = localTime.wMonth;
|
||||
result.year = localTime.wYear;
|
||||
result.hour = localTime.wHour;
|
||||
result.minute = localTime.wMinute;
|
||||
result.second = localTime.wSecond;
|
||||
}
|
||||
CloseHandle(handle);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string GkOSUtils::GetApplicationName()
|
||||
{
|
||||
TCHAR buffer[MAX_PATH]={0};
|
||||
GetModuleFileName(NULL, buffer, MAX_PATH);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
std::string GkOSUtils::GetApplicationPath()
|
||||
{
|
||||
std::string applicationName = GkOSUtils::GetApplicationName();
|
||||
int nameLength = applicationName.length();
|
||||
char path[MAX_PATH];
|
||||
int pathLast = 0;
|
||||
for (int i = 0; i < nameLength; i++)
|
||||
{
|
||||
path[i] = applicationName[i];
|
||||
if (path[i] == '\\')
|
||||
pathLast = i;
|
||||
}
|
||||
path[pathLast + 1] = 0;
|
||||
|
||||
return std::string(path);
|
||||
}
|
||||
|
||||
void GkOSUtils::Execute(const char *command)
|
||||
{
|
||||
ShellExecuteA(GetDesktopWindow(), "open", command, NULL, NULL, SW_SHOWNORMAL);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include "..\GkDefinitions.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <string>
|
||||
|
||||
struct DateTime
|
||||
{
|
||||
int day, month, year,
|
||||
hour, minute, second;
|
||||
};
|
||||
|
||||
class GKDLLCLASS GkOSUtils
|
||||
{
|
||||
public:
|
||||
GkOSUtils(void);
|
||||
~GkOSUtils(void);
|
||||
|
||||
static void* GkOSUtils::GetMem(int size);
|
||||
static void GkOSUtils::FreeMem(void* data);
|
||||
static void GkOSUtils::ZeroMem(void* data, int size);
|
||||
static void GkOSUtils::CopyMem(void* destination, void* source, int size);
|
||||
static int GkOSUtils::CompareMem(void* block1, void* block2, int size);
|
||||
|
||||
static long long GkOSUtils::GetPerformanceCounter();
|
||||
static long long GkOSUtils::GetPerformanceCounterFrequency();
|
||||
|
||||
static void GkOSUtils::ShowMessage(const char *text, HWND windowHandle = 0);
|
||||
|
||||
static int GetScreenWidth();
|
||||
static int GetScreenHeight();
|
||||
|
||||
static int GetNumberOfScreens();
|
||||
|
||||
static DateTime GetDateTime();
|
||||
static bool SetDateTime(DateTime &dateTime);
|
||||
static int GetSecondsElapsed(DateTime &since);
|
||||
|
||||
static DateTime GetFileModificationDateTime(const wchar_t *fileName);
|
||||
|
||||
static std::string GetApplicationName();
|
||||
static std::string GetApplicationPath();
|
||||
|
||||
static void Execute(const char *command);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// GambleKit Copyright (C) 2014 Ivan Donev
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// contact: gamblekit@gmail.com
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include "GkOpenGLWindowsForm.h"
|
||||
|
||||
|
||||
GkOpenGLWindowsForm::GkOpenGLWindowsForm(HGLRC renderContext)
|
||||
{
|
||||
PIXELFORMATDESCRIPTOR d =
|
||||
{
|
||||
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
|
||||
1, // Version Number
|
||||
PFD_DRAW_TO_WINDOW | // Format Must Support Window
|
||||
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
|
||||
PFD_DOUBLEBUFFER, // Must Support Double Buffering
|
||||
PFD_TYPE_RGBA, // Request An RGBA Format
|
||||
32, // Select Our Color Depth
|
||||
0, 0, 0, 0, 0, 0, // Color Bits Ignored
|
||||
0, // No Alpha Buffer
|
||||
0, // Shift Bit Ignored
|
||||
0, // No Accumulation Buffer
|
||||
0, 0, 0, 0, // Accumulation Bits Ignored
|
||||
16, // 16Bit Z-Buffer (Depth Buffer)
|
||||
0, // No Stencil Buffer
|
||||
0, // No Auxiliary Buffer
|
||||
PFD_MAIN_PLANE, // Main Drawing Layer
|
||||
0, // Reserved
|
||||
0, 0, 0 // Layer Masks Ignored
|
||||
};
|
||||
this->pixelFormatDescriptor = d;
|
||||
|
||||
this->CreateContext(renderContext);
|
||||
}
|
||||
|
||||
|
||||
GkOpenGLWindowsForm::~GkOpenGLWindowsForm(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool GkOpenGLWindowsForm::CreateContext(HGLRC renderContext)
|
||||
{
|
||||
this->deviceContext = GetDC(this->handle);
|
||||
if (this->deviceContext == 0)
|
||||
return false;
|
||||
|
||||
this->pixelFormat = ChoosePixelFormat(this->deviceContext, &this->pixelFormatDescriptor);
|
||||
if (this->pixelFormat == 0)
|
||||
return false;
|
||||
|
||||
if(!SetPixelFormat(this->deviceContext, this->pixelFormat, &this->pixelFormatDescriptor))
|
||||
return false;
|
||||
|
||||
if (renderContext == 0)
|
||||
{
|
||||
this->renderContext = wglCreateContext(this->deviceContext);
|
||||
if (this->renderContext == 0)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->renderContext = renderContext;
|
||||
}
|
||||
|
||||
if (!this->MakeCurrent())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GkOpenGLWindowsForm::Present()
|
||||
{
|
||||
return SwapBuffers(this->deviceContext) != 0;
|
||||
}
|
||||
|
||||
bool GkOpenGLWindowsForm::MakeCurrent()
|
||||
{
|
||||
return wglMakeCurrent(this->deviceContext, this->renderContext) != 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "..\GkDefinitions.h"
|
||||
|
||||
#include "GkWindowsForm.h"
|
||||
|
||||
class GKDLLCLASS GkOpenGLWindowsForm :
|
||||
public GkWindowsForm
|
||||
{
|
||||
public:
|
||||
HDC deviceContext;
|
||||
HGLRC renderContext;
|
||||
|
||||
PIXELFORMATDESCRIPTOR pixelFormatDescriptor;
|
||||
unsigned int pixelFormat;
|
||||
|
||||
GkOpenGLWindowsForm(HGLRC renderContext = 0);
|
||||
virtual ~GkOpenGLWindowsForm(void);
|
||||
|
||||
bool GkOpenGLWindowsForm::CreateContext(HGLRC renderContext = 0);
|
||||
bool GkOpenGLWindowsForm::Present();
|
||||
bool GkOpenGLWindowsForm::MakeCurrent();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,397 @@
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// GambleKit Copyright (C) 2014 Ivan Donev
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// contact: gamblekit@gmail.com
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include "GkWindowsForm.h"
|
||||
|
||||
#include <windowsx.h>
|
||||
|
||||
const char* GkWindowsForm::FormClassName = "MyWindowsForm";
|
||||
|
||||
//std::vector<GkWindowsForm*> GkWindowsForm::instances;
|
||||
GkList<GkWindowsForm*> GkWindowsForm::instances;
|
||||
|
||||
LRESULT CALLBACK GkWindowsForm::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
GkWindowsForm* instance = GkWindowsForm::GetInstance(hWnd);
|
||||
|
||||
if (instance != NULL)
|
||||
{
|
||||
switch (uMsg)
|
||||
{
|
||||
case WM_SYSCOMMAND:
|
||||
{
|
||||
switch (wParam)
|
||||
{
|
||||
case SC_SCREENSAVE:
|
||||
{
|
||||
if (instance->suppressScreenSaver)
|
||||
return 0;
|
||||
}
|
||||
case SC_MONITORPOWER:
|
||||
{
|
||||
if (instance->suppressMonitorPowerSave)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_ACTIVATE:
|
||||
{
|
||||
// Minimization state
|
||||
if (!HIWORD(wParam))
|
||||
{
|
||||
instance->active=TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
instance->active=FALSE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_DESTROY:
|
||||
{
|
||||
if (instance->Close())
|
||||
{
|
||||
GkWindowsForm::instances.Remove(instance);
|
||||
|
||||
if (GkWindowsForm::instances.size == 0)
|
||||
PostQuitMessage(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_SIZE:
|
||||
{
|
||||
int width = LOWORD(lParam);
|
||||
int height = HIWORD(lParam);
|
||||
|
||||
instance->Resize(width, height);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_PAINT:
|
||||
{
|
||||
if (instance->Paint())
|
||||
{
|
||||
ValidateRect(instance->handle, NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_KEYDOWN:
|
||||
{
|
||||
int key = wParam;
|
||||
|
||||
instance->KeyDown(key);
|
||||
|
||||
if ((key >=0 ) && (key < 256))
|
||||
instance->keys[key] = TRUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_KEYUP:
|
||||
{
|
||||
int key = wParam;
|
||||
|
||||
instance->KeyUp(key);
|
||||
|
||||
if ((key >=0 ) && (key < 256))
|
||||
instance->keys[key] = FALSE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_LBUTTONDOWN:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
|
||||
instance->MouseDown(x, y, GkWindowsForm::MouseButton_Left);
|
||||
instance->mouseButtons |= GkWindowsForm::MouseButton_Left;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_LBUTTONUP:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
|
||||
instance->MouseUp(x, y, GkWindowsForm::MouseButton_Left);
|
||||
instance->mouseButtons &= !GkWindowsForm::MouseButton_Left;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_RBUTTONDOWN:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
|
||||
instance->MouseDown(x, y, GkWindowsForm::MouseButton_Right);
|
||||
instance->mouseButtons |= GkWindowsForm::MouseButton_Right;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_RBUTTONUP:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
|
||||
instance->MouseUp(x, y, GkWindowsForm::MouseButton_Right);
|
||||
instance->mouseButtons &= !GkWindowsForm::MouseButton_Right;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_MBUTTONDOWN:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
|
||||
instance->MouseDown(x, y, GkWindowsForm::MouseButton_Middle);
|
||||
instance->mouseButtons |= GkWindowsForm::MouseButton_Middle;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_MBUTTONUP:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
|
||||
instance->MouseUp(x, y, GkWindowsForm::MouseButton_Middle);
|
||||
instance->mouseButtons &= !GkWindowsForm::MouseButton_Middle;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_MOUSEMOVE:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
|
||||
instance->MouseMove(x, y);
|
||||
|
||||
instance->mouseX = x;
|
||||
instance->mouseY = y;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_MOUSEHWHEEL:
|
||||
{
|
||||
int x = GET_X_LPARAM(lParam);
|
||||
int y = GET_Y_LPARAM(lParam);
|
||||
int deltaFactor = GET_WHEEL_DELTA_WPARAM(wParam);
|
||||
|
||||
instance->MouseWheel(x, y, deltaFactor, WHEEL_DELTA);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int result;
|
||||
if (instance->HandleMessage(uMsg, wParam, lParam, &result))
|
||||
return result;
|
||||
}
|
||||
|
||||
return DefWindowProc(hWnd,uMsg,wParam,lParam);
|
||||
}
|
||||
|
||||
GkWindowsForm* GkWindowsForm::GetInstance(HWND instanceHandle)
|
||||
{
|
||||
for (int i = 0; i < GkWindowsForm::instances.size; i++)
|
||||
{
|
||||
if (GkWindowsForm::instances[i]->handle == instanceHandle)
|
||||
{
|
||||
return GkWindowsForm::instances[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool GkWindowsForm::ProcessMessages()
|
||||
{
|
||||
MSG msg;
|
||||
|
||||
bool result = false;
|
||||
|
||||
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int GkWindowsForm::PerformFormLoop()
|
||||
{
|
||||
MSG msg;
|
||||
|
||||
bool done = false;
|
||||
while(!done) // Loop That Runs While done=FALSE
|
||||
{
|
||||
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // Is There A Message Waiting?
|
||||
{
|
||||
if (msg.message == WM_QUIT) // Have We Received A Quit Message?
|
||||
{
|
||||
done = true; // If So done=TRUE
|
||||
}
|
||||
else // If Not, Deal With Window Messages
|
||||
{
|
||||
TranslateMessage(&msg); // Translate The Message
|
||||
DispatchMessage(&msg); // Dispatch The Message
|
||||
}
|
||||
}
|
||||
|
||||
if (!done)
|
||||
{
|
||||
for (int i = 0; i < GkWindowsForm::instances.size; i++)
|
||||
{
|
||||
GkWindowsForm::instances[i]->MainLoop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (int)msg.wParam;
|
||||
|
||||
//while (GetMessage(&msg, NULL, 0, 0))
|
||||
// {
|
||||
// TranslateMessage(&msg);
|
||||
// DispatchMessage(&msg);
|
||||
// }
|
||||
|
||||
// return (int)msg.wParam;
|
||||
}
|
||||
|
||||
GkWindowsForm::GkWindowsForm(void)
|
||||
{
|
||||
this->applicationInstance = GetModuleHandle(NULL);
|
||||
ZeroMemory(keys, 256);
|
||||
|
||||
this->left = 0;
|
||||
this->top = 0;
|
||||
this->width = 640;
|
||||
this->height = 480;
|
||||
this->caption = "Window Title";
|
||||
//this->windowMode = GkWindowsForm::WindowMode_Windowed;
|
||||
this->windowMode = GkWindowsForm::WindowMode_FullScreen;
|
||||
|
||||
GkWindowsForm::instances.Add(this);
|
||||
//GkWindowsForm::instances.push_back(this);
|
||||
|
||||
this->RegisterWindowClass();
|
||||
this->CreateFormWindow();
|
||||
this->ShowFormWindow();
|
||||
}
|
||||
|
||||
|
||||
GkWindowsForm::~GkWindowsForm(void)
|
||||
{
|
||||
this->DestroyFormWindow();
|
||||
//GkWindowsForm::instances.Remove(this);
|
||||
//if (GkWindowsForm::instances.size == 0)
|
||||
// PostMessage(this->handle, WM_CLOSE, 0, 0);
|
||||
}
|
||||
|
||||
bool GkWindowsForm::RegisterWindowClass()
|
||||
{
|
||||
//LRESULT (CALLBACK GkWindowsForm::*memfun)(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
||||
//memfun = &GkWindowsForm::WndProc;
|
||||
//this->ex.lpfnWndProc = *((WNDPROC*) (&memfun));
|
||||
|
||||
this->windowClass.cbSize = sizeof(WNDCLASSEX);
|
||||
this->windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
|
||||
this->windowClass.lpfnWndProc = (WNDPROC)GkWindowsForm::WndProc;
|
||||
this->windowClass.cbClsExtra = 0;
|
||||
this->windowClass.cbWndExtra = 0;
|
||||
this->windowClass.hInstance = this->applicationInstance;
|
||||
this->windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
||||
this->windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
this->windowClass.hbrBackground = NULL;
|
||||
this->windowClass.lpszMenuName = NULL;
|
||||
this->windowClass.hIconSm = NULL;
|
||||
this->windowClass.lpszClassName = GkWindowsForm::FormClassName;
|
||||
|
||||
return RegisterClassEx(&this->windowClass) != 0;
|
||||
}
|
||||
|
||||
bool GkWindowsForm::CreateFormWindow()
|
||||
{
|
||||
long wndStyle = this->windowMode == GkWindowsForm::WindowMode_FullScreen ? WS_POPUP : WS_OVERLAPPEDWINDOW;
|
||||
wndStyle |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
|
||||
|
||||
// Translate from client rect to window rect
|
||||
RECT wRect = { this->left, this->top, this->left + this->width, this->top + this->height };
|
||||
AdjustWindowRect(&wRect, wndStyle, false);
|
||||
|
||||
this->handle = CreateWindow(GkWindowsForm::FormClassName, this->caption,
|
||||
wndStyle,
|
||||
this->left, this->top, wRect.right - wRect.left, wRect.bottom - wRect.top,
|
||||
NULL, NULL,
|
||||
this->applicationInstance,
|
||||
NULL);
|
||||
|
||||
return this->handle != 0;
|
||||
}
|
||||
|
||||
void GkWindowsForm::ShowFormWindow()
|
||||
{
|
||||
ShowWindow(this->handle,SW_SHOW);
|
||||
//UpdateWindow(this->handle);
|
||||
SetForegroundWindow(this->handle); // Slightly Higher Priority
|
||||
SetFocus(this->handle); // Sets Keyboard Focus To The Window
|
||||
}
|
||||
|
||||
void GkWindowsForm::DestroyFormWindow()
|
||||
{
|
||||
PostMessage(this->handle, WM_DESTROY, 0, 0);
|
||||
}
|
||||
|
||||
void GkWindowsForm::SetPosition(int left, int top)
|
||||
{
|
||||
this->left = left;
|
||||
this->top = top;
|
||||
|
||||
SetWindowPos(this->handle, HWND_TOP, this->left, this->top, this->width, this->height, SWP_NOSIZE);
|
||||
}
|
||||
|
||||
void GkWindowsForm::SetSize(int width, int height)
|
||||
{
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
|
||||
SetWindowPos(this->handle, HWND_TOP, this->left, this->top, this->width, this->height, SWP_NOMOVE);
|
||||
}
|
||||
|
||||
void GkWindowsForm::SetStyle()
|
||||
{
|
||||
SetWindowLong(this->handle, GWL_STYLE, WS_POPUP | WS_MINIMIZEBOX);
|
||||
SetWindowLong(this->handle, GWL_EXSTYLE, 0);
|
||||
//SetWindowLong(this->handle, GWL_STYLE, WS_POPUP);
|
||||
//SetWindowLong(this->handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
|
||||
}
|
||||
|
||||
void GkWindowsForm::SetActive()
|
||||
{
|
||||
SetActiveWindow(this->handle);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include "..\GkDefinitions.h"
|
||||
|
||||
#include "..\Utils\GkList.h"
|
||||
//#include <vector>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
class GKDLLCLASS GkWindowsForm
|
||||
{
|
||||
public:
|
||||
static const int WindowMode_Windowed = 0,
|
||||
WindowMode_FullScreen = 1,
|
||||
|
||||
MouseButton_None = 0,
|
||||
MouseButton_Left = 1,
|
||||
MouseButton_Right = 2,
|
||||
MouseButton_Middle = 4;
|
||||
|
||||
static const char* FormClassName;
|
||||
|
||||
static GkList<GkWindowsForm*> instances;
|
||||
//static std::vector<GkWindowsForm*> instances;
|
||||
|
||||
|
||||
HINSTANCE applicationInstance;
|
||||
WNDCLASSEX windowClass;
|
||||
HWND handle;
|
||||
|
||||
bool keys[256];
|
||||
int mouseX, mouseY, mouseButtons;
|
||||
bool suppressScreenSaver, suppressMonitorPowerSave;
|
||||
|
||||
bool active;
|
||||
int windowMode;
|
||||
int left, top, width, height;
|
||||
char *caption;
|
||||
|
||||
|
||||
GkWindowsForm(void);
|
||||
virtual ~GkWindowsForm(void);
|
||||
|
||||
bool GkWindowsForm::RegisterWindowClass();
|
||||
bool GkWindowsForm::CreateFormWindow();
|
||||
void GkWindowsForm::ShowFormWindow();
|
||||
void GkWindowsForm::DestroyFormWindow();
|
||||
void GkWindowsForm::SetPosition(int left, int top);
|
||||
void GkWindowsForm::SetSize(int width, int height);
|
||||
void GkWindowsForm::SetStyle();
|
||||
void GkWindowsForm::SetActive();
|
||||
|
||||
virtual bool GkWindowsForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, int *result) { return false; };
|
||||
virtual void GkWindowsForm::MainLoop() { };
|
||||
virtual void GkWindowsForm::Created() { };
|
||||
virtual bool GkWindowsForm::Close() { return true; };
|
||||
virtual bool GkWindowsForm::Paint() { return true; };
|
||||
virtual void GkWindowsForm::KeyDown(int keyCode) { };
|
||||
virtual void GkWindowsForm::KeyUp(int keyCode) { };
|
||||
virtual void GkWindowsForm::Resize(int width, int height) { };
|
||||
virtual void GkWindowsForm::MouseMove(int x, int y) { };
|
||||
virtual void GkWindowsForm::MouseDown(int x, int y, int mouseButton) { };
|
||||
virtual void GkWindowsForm::MouseUp(int x, int y, int mouseButton) { };
|
||||
virtual void GkWindowsForm::MouseWheel(int x, int y, int deltaFactor, int delta) { };
|
||||
|
||||
static LRESULT CALLBACK GkWindowsForm::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
||||
static GkWindowsForm* GkWindowsForm::GetInstance(HWND instanceHandle);
|
||||
static bool GkWindowsForm::ProcessMessages();
|
||||
static int GkWindowsForm::PerformFormLoop();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// GambleKit Copyright (C) 2014 Ivan Donev
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// contact: gamblekit@gmail.com
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include "GkWindowsMutex.h"
|
||||
|
||||
|
||||
GkWindowsMutex::GkWindowsMutex(void)
|
||||
{
|
||||
this->handle = CreateMutex(NULL, FALSE, NULL);
|
||||
}
|
||||
|
||||
|
||||
GkWindowsMutex::~GkWindowsMutex(void)
|
||||
{
|
||||
if(this->handle != NULL)
|
||||
CloseHandle(this->handle);
|
||||
}
|
||||
|
||||
bool GkWindowsMutex::Lock()
|
||||
{
|
||||
return WaitForSingleObject(this->handle, INFINITE) == WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
void GkWindowsMutex::Unlock()
|
||||
{
|
||||
ReleaseMutex(this->handle);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "..\GkDefinitions.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
class GKDLLCLASS GkWindowsMutex
|
||||
{
|
||||
public:
|
||||
HANDLE handle;
|
||||
|
||||
GkWindowsMutex(void);
|
||||
~GkWindowsMutex(void);
|
||||
|
||||
bool Lock();
|
||||
void Unlock();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// GambleKit Copyright (C) 2014 Ivan Donev
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// contact: gamblekit@gmail.com
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include "GkWindowsThread.h"
|
||||
|
||||
|
||||
DWORD WINAPI GkWindowsThread::ExecuteThread(LPVOID lpParam)
|
||||
{
|
||||
GkWindowsThread *instance = (GkWindowsThread*)lpParam;
|
||||
|
||||
int result = instance->Execute();
|
||||
instance->exited = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void GkWindowsThread::SleepX(int milliseconds)
|
||||
{
|
||||
Sleep(milliseconds);
|
||||
}
|
||||
|
||||
GkWindowsThread::GkWindowsThread(void)
|
||||
{
|
||||
this->handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) GkWindowsThread::ExecuteThread, this, 0, &this->id);
|
||||
this->terminated = this->handle == NULL;
|
||||
this->exited = terminated;
|
||||
}
|
||||
|
||||
|
||||
GkWindowsThread::~GkWindowsThread(void)
|
||||
{
|
||||
if(this->handle == NULL)
|
||||
return;
|
||||
|
||||
this->terminated = true;
|
||||
|
||||
while (!this->exited)
|
||||
Sleep(0);
|
||||
|
||||
CloseHandle(this->handle);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "..\GkDefinitions.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
class GKDLLCLASS GkWindowsThread
|
||||
{
|
||||
public:
|
||||
HANDLE handle;
|
||||
DWORD id;
|
||||
|
||||
bool terminated, exited;
|
||||
|
||||
GkWindowsThread(void);
|
||||
virtual ~GkWindowsThread(void);
|
||||
|
||||
virtual int Execute() = 0;
|
||||
|
||||
void Lock();
|
||||
void Unlock();
|
||||
|
||||
static void SleepX(int milliseconds);
|
||||
static DWORD WINAPI ExecuteThread(LPVOID lpParam);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user