implement multithreading for windows, implement file uploading when saving

This commit is contained in:
2017-11-03 21:27:33 +00:00
parent cfc4312c89
commit a3276906f9
8 changed files with 140 additions and 4 deletions

View File

@@ -19,10 +19,74 @@ HDC hDC;
HGLRC hRC;
wchar_t* className;
bool keys[256];
std::mutex gl_mutex;
std::mutex async_mutex;
std::thread::id gl_thread;
int gl_count = 0;
#include <WbemCli.h>
#include "wacom.h"
//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
void async_lock()
{
//std::lock_guard<std::mutex> _lock(async_mutex);
if (gl_count == 0 || gl_thread != std::this_thread::get_id())
{
gl_mutex.lock();
bool ret = wglMakeCurrent(hDC, hRC);
if (ret == false)
LOG("FAILED wglMakeCurrent: %s", GetLastErrorAsString().c_str());
gl_thread = std::this_thread::get_id();
//LOG("lock");
}
gl_count++;
//assert(ret == true);
}
void async_swap()
{
SwapBuffers(hDC);
//LOG("swap");
}
void async_unlock()
{
//std::lock_guard<std::mutex> _lock(async_mutex);
gl_count--;
if (gl_count == 0)
{
//LOG("unlock");
wglMakeCurrent(0, 0);
gl_mutex.unlock();
}
}
struct async_locker
{
async_locker() { async_lock(); }
~async_locker() { async_unlock(); }
};
int read_WMI_info()
{
// see: http://win32easy.blogspot.co.uk/2011/03/wmi-in-c-query-everyting-from-your-os.html
@@ -303,9 +367,14 @@ int main(int argc, char** argv)
t0 = t1;
if (App::I.redraw)
{
async_lock();
App::I.redraw = true;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
App::I.clear();
App::I.update((float)(t1 - t0) / 1000.0f);
SwapBuffers(hDC);
async_unlock();
//LOG("swap main");
}
}
// else
@@ -323,6 +392,7 @@ int main(int argc, char** argv)
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
async_locker lock;
static bool leftDown = false;
static DWORD lastTime;
static POINT lastPoint;
@@ -368,7 +438,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
App::I.key_up(convert_key((int)wp));
break;
case WM_CHAR:
//App::I.key_char((int)wp);
App::I.key_char((int)wp);
break;
case WM_MOUSEMOVE:
//TODO: find a way to check if event is mouse/stylus. For now use Mouse for all