diff --git a/ECTrainer2/BaseProcess.cpp b/ECTrainer2/BaseProcess.cpp index 56bc67b..5b4dd51 100644 --- a/ECTrainer2/BaseProcess.cpp +++ b/ECTrainer2/BaseProcess.cpp @@ -1,6 +1,7 @@ #include "BaseProcess.h" #include "ECTrainer.h" -#include "MyWinUtils.h" +#include "HPTimer.h" +#include "nkcWinUtils.h" // コンストラクタ BaseProcess::BaseProcess(ECTrainer* pEct) @@ -33,7 +34,7 @@ // メッセージループ MSG msg; - mwut::HPTimer timer; + nkc::HPTimer timer; int fpsCounter = 0; const int fpsCount = 100; while (1) { @@ -51,8 +52,8 @@ if (++fpsCounter >= fpsCount) { fpsCounter = 0; double fps = fpsCount * 1000.0 / timer.Interval(); - mwut::DebugPrintf(_T("%s : %.1f fps\n"), - mwut::Multi2Wide(this->ClassName()).c_str(), fps); + nkc::wut::DebugPrintf(_T("%s : %.1f fps\n"), + nkc::wut::Multi2Wide(this->ClassName()).c_str(), fps); } } } diff --git a/ECTrainer2/BaseProcess.h b/ECTrainer2/BaseProcess.h index a6339e4..cc75776 100644 --- a/ECTrainer2/BaseProcess.h +++ b/ECTrainer2/BaseProcess.h @@ -7,7 +7,7 @@ enum class ECTMSG; -// 処理の基本クラス +// マルチスレッド繰り返し処理の基本クラス class BaseProcess { protected: diff --git a/ECTrainer2/BitalMonitor.cpp b/ECTrainer2/BitalMonitor.cpp index 131cba0..3f7b3a7 100644 --- a/ECTrainer2/BitalMonitor.cpp +++ b/ECTrainer2/BitalMonitor.cpp @@ -1,7 +1,6 @@ #include "BitalMonitor.h" #include "ECTrainer.h" #include "Worker.h" -#include "myWinUtils.h" // コンストラクタ BitalMonitor::BitalMonitor(ECTrainer* pEct) @@ -26,7 +25,6 @@ char comBuf[BUF_LEN] = { '\0' }; DWORD readBytes = _Com.Receive((BYTE*)comBuf, BUF_LEN - 1); if (readBytes < 1) continue; - //mwut::DebugPrintf(_T("check com %d\n"), com); if ((*comBuf >= '0' && *comBuf <= '9') || *comBuf == '#' || *comBuf == LF) { _pEct->PWorker()->EventLog(_T("Bital device connected.")); _useDevice = true; diff --git a/ECTrainer2/DShowMovie.cpp b/ECTrainer2/DShowMovie.cpp index a0a682d..9b7358d 100644 --- a/ECTrainer2/DShowMovie.cpp +++ b/ECTrainer2/DShowMovie.cpp @@ -1,5 +1,5 @@ #include "DShowMovie.h" -#include "myWinUtils.h" +#include "nkcWinUtils.h" #pragma comment(lib, "dxguid.lib") #pragma comment(lib, "d3d9.lib") @@ -63,9 +63,9 @@ CHECK(_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, _hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &_pD3DDev)); - mwut::DebugPrintf(_T("Software DirectX\n")); + nkc::wut::DebugPrintf(_T("Software DirectX\n")); } else { - mwut::DebugPrintf(_T("Hardware DirectX\n")); + nkc::wut::DebugPrintf(_T("Hardware DirectX\n")); } // COMの初期化 diff --git a/ECTrainer2/ECTrainer.cpp b/ECTrainer2/ECTrainer.cpp index fc684f1..d83d284 100644 --- a/ECTrainer2/ECTrainer.cpp +++ b/ECTrainer2/ECTrainer.cpp @@ -11,7 +11,7 @@ #include "Worker.h" #include "KeepAlive.h" #include "DShowMovie.h" -#include "myWinUtils.h" +#include "nkcWinUtils.h" // コンストラクタ @@ -45,20 +45,20 @@ // プロセス初期化 for (int i = 0; i < (int)PROC::NUM; i++) { if (!_pProcs[i]->Init()) return false; - //mwut::DebugPrintf(_T("Thread %d initialized.\n"), i); + //nkc::wut::DebugPrintf(_T("Thread %d initialized.\n"), i); } // スレッド開始 for (int i = 0; i < (int)PROC::NUM; i++) { if (i != (int)PROC::GUI) _pProcs[i]->Launch(); - //mwut::DebugPrintf(_T("Thread %d start.\n"), i); + //nkc::wut::DebugPrintf(_T("Thread %d start.\n"), i); } _pProcs[(int)PROC::GUI]->MainLoop(); // スレッド終了 for (int i = (int)PROC::REST; i < (int)PROC::NUM; i++) { auto rv = _pProcs[i]->WaitForExit(); - //mwut::DebugPrintf(_T("Process %d exit %s\n"), i, rv ? _T("ok") : _T("ng")); + //nkc::wut::DebugPrintf(_T("Process %d exit %s\n"), i, rv ? _T("ok") : _T("ng")); } return true; diff --git a/ECTrainer2/ECTrainer2.vcxproj b/ECTrainer2/ECTrainer2.vcxproj index c990c87..036b2c8 100644 --- a/ECTrainer2/ECTrainer2.vcxproj +++ b/ECTrainer2/ECTrainer2.vcxproj @@ -150,13 +150,14 @@ + - + @@ -172,12 +173,13 @@ + - + diff --git a/ECTrainer2/ECTrainer2.vcxproj.filters b/ECTrainer2/ECTrainer2.vcxproj.filters index 673a60d..f78d800 100644 --- a/ECTrainer2/ECTrainer2.vcxproj.filters +++ b/ECTrainer2/ECTrainer2.vcxproj.filters @@ -60,9 +60,6 @@ ソース ファイル - - ソース ファイル - ソース ファイル @@ -72,6 +69,12 @@ ソース ファイル + + ソース ファイル + + + ソース ファイル + @@ -125,9 +128,6 @@ ヘッダー ファイル - - ヘッダー ファイル - ヘッダー ファイル @@ -137,6 +137,12 @@ ヘッダー ファイル + + ヘッダー ファイル + + + ヘッダー ファイル + diff --git a/ECTrainer2/ECTrainerGUI.cpp b/ECTrainer2/ECTrainerGUI.cpp index 4d14b03..1608786 100644 --- a/ECTrainer2/ECTrainerGUI.cpp +++ b/ECTrainer2/ECTrainerGUI.cpp @@ -9,7 +9,7 @@ #include "TobiiREST.h" #include "Stimulus.h" #include "Marker.h" -#include "myWinUtils.h" +#include "nkcWinUtils.h" #pragma comment(lib,"winmm.lib") #define CVUI_IMPLEMENTATION @@ -29,7 +29,7 @@ _MainFrame.create(MAIN_FRAME_SIZE, CV_8UC3); // 刺激提示ウインドウ - std::vector displays = mwut::GetDisplayInfo(); // 実行環境の情報取得 + std::vector displays = nkc::wut::GetDisplayInfo(); // 実行環境の情報取得 if (displays.size() > 1 && !FORCE_SINGLE_MONITOR) { // マルチモニタの場合 // サブ画面に操作パネル表示 @@ -187,7 +187,7 @@ ((BaseProcess*)_pEct->PWorker())->PostMsg(ECTMSG::SOFTWARE_END); #endif } - mwut::DebugPrintf(_T("Key '%c'\n"), msg.wParam); + nkc::wut::DebugPrintf(_T("Key '%c'\n"), msg.wParam); break; case (int)ECTMSG::MOVIE_START: diff --git a/ECTrainer2/HPTimer.cpp b/ECTrainer2/HPTimer.cpp new file mode 100644 index 0000000..52c9e91 --- /dev/null +++ b/ECTrainer2/HPTimer.cpp @@ -0,0 +1,47 @@ +// 高精度タイマークラス + +#include "HPTimer.h" + +namespace nkc { + +// コンストラクタ +HPTimer::HPTimer() { + QueryPerformanceFrequency(&_nFreq); + QueryPerformanceCounter(&_nBegin); + _nLast = _nBegin; +} + +// 経過時間算出 +double HPTimer::Calc(LARGE_INTEGER before) { + LARGE_INTEGER cur; + QueryPerformanceCounter(&cur); + double elapse = (cur.QuadPart - before.QuadPart) * 1000.0 / _nFreq.QuadPart; + _nLast = cur; + return elapse; +} + +// 開始時からの経過時間(ms) +double HPTimer::Elapse() { + return this->Calc(_nBegin); +} + +// 前回からの経過時間(ms) +double HPTimer::Interval() { + return this->Calc(_nLast); +} + +// 開始時間リセット +void HPTimer::Reset() { + QueryPerformanceCounter(&_nBegin); +} + +// 高精度Sleep +void HPTimer::HPSleep(double delayms) { + HPTimer timer; + while (1) { + if (timer.Elapse() > delayms) break; + Sleep(0); + } +} + +}; // namespace nkc diff --git a/ECTrainer2/HPTimer.h b/ECTrainer2/HPTimer.h new file mode 100644 index 0000000..0971883 --- /dev/null +++ b/ECTrainer2/HPTimer.h @@ -0,0 +1,25 @@ +// 高精度タイマークラス + +#pragma once + +#include + + +namespace nkc { + +// 高精度タイマークラス +class HPTimer { + LARGE_INTEGER _nFreq; // 周波数 + LARGE_INTEGER _nBegin; // 開始時間 + LARGE_INTEGER _nLast; // 前回問い合わせ時間 + double Calc(LARGE_INTEGER before); + +public: + HPTimer(); + double Elapse(); // 開始時からの経過時間(ms) + double Interval(); // 前回からの経過時間(ms) + void Reset(); // 開始時間リセット + static void HPSleep(double delayms); // 高精度Sleep +}; + +}; // namespace nkc diff --git a/ECTrainer2/KeepAlive.cpp b/ECTrainer2/KeepAlive.cpp index d15423b..25ed735 100644 --- a/ECTrainer2/KeepAlive.cpp +++ b/ECTrainer2/KeepAlive.cpp @@ -3,7 +3,7 @@ #include #include "KeepAlive.h" #include "ECTrainer.h" -#include "myWinUtils.h" +#include "nkcWinUtils.h" // コンストラクタ KeepAlive::KeepAlive(ECTrainer* pEct) : BaseProcess(pEct) @@ -20,13 +20,13 @@ // Initialize winsock WSADATA wsa; if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { - mwut::DebugPrintf(_T("Initialising Winsock Failed with error Code : %d\n"), WSAGetLastError()); + nkc::wut::DebugPrintf(_T("Initialising Winsock Failed with error Code : %d\n"), WSAGetLastError()); return false; } // Create socket if ((_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) { - mwut::DebugPrintf(_T("Creating socket failed with error Code : %d\n"), WSAGetLastError()); + nkc::wut::DebugPrintf(_T("Creating socket failed with error Code : %d\n"), WSAGetLastError()); return false; } @@ -52,7 +52,7 @@ _count = 0; if (sendto(_socket, message, (int)strlen(message), 0, (struct sockaddr*)&_socketAddr, _socketAddrLen) == SOCKET_ERROR) { - mwut::DebugPrintf(_T("Keep Alive Data Sent Error\n")); + nkc::wut::DebugPrintf(_T("Keep Alive Data Sent Error\n")); } else { _sent = true; } diff --git a/ECTrainer2/Marker.cpp b/ECTrainer2/Marker.cpp index 89ac6b2..4f09410 100644 --- a/ECTrainer2/Marker.cpp +++ b/ECTrainer2/Marker.cpp @@ -1,7 +1,7 @@  #include "Marker.h" #include "ECTrainer.h" -#include "myWinUtils.h" +#include "nkcWinUtils.h" // コンストラクタ Marker::Marker() @@ -98,7 +98,7 @@ // マーカーを描画する void Marker::DrawMarker(cv::Mat& img) { if (_Corners.size() < 1) { - mwut::DebugPrintf(_T("Marker not generated")); + nkc::wut::DebugPrintf(_T("Marker not generated")); return; } diff --git a/ECTrainer2/MovieObject.cpp b/ECTrainer2/MovieObject.cpp index 82745ce..b20cc32 100644 --- a/ECTrainer2/MovieObject.cpp +++ b/ECTrainer2/MovieObject.cpp @@ -9,7 +9,7 @@ } // テーブルからデータをセット -int MovieObject::SetData(mwut::STR_TABLE table) { +int MovieObject::SetData(nkc::STR_TABLE table) { _frame.clear(); int addLines = 0; @@ -53,8 +53,8 @@ // ファイル名からデータをセット int MovieObject::SetData(const std::string& filename) { - mwut::STR_TABLE table; - if (!mwut::ReadTable(filename, table)) return 0; + nkc::STR_TABLE table; + if (!nkc::wut::ReadTable(filename, table)) return 0; return SetData(table); } diff --git a/ECTrainer2/MovieObject.h b/ECTrainer2/MovieObject.h index 5af72b0..f7de34c 100644 --- a/ECTrainer2/MovieObject.h +++ b/ECTrainer2/MovieObject.h @@ -2,8 +2,7 @@ #include #include - -#include "myWinUtils.h" +#include "nkcWinUtils.h" // 構成要素 @@ -38,7 +37,7 @@ // コンストラクタ MovieObject(); // テーブルからデータをセット - int SetData(mwut::STR_TABLE table); + int SetData(nkc::STR_TABLE table); // ファイル名からデータをセット int SetData(const std::string& filename); // 指定時間のオブジェクト情報を返す diff --git a/ECTrainer2/MyWinUtils.h b/ECTrainer2/MyWinUtils.h deleted file mode 100644 index 7c2a8c2..0000000 --- a/ECTrainer2/MyWinUtils.h +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace mwut { - - typedef std::vector> STR_TABLE; - - // スワップクラス - template - class SWAP { - public: - static void DO(T& a, T& b) { - T tmp = a; - a = b; - b = tmp; - } - }; - - // マルチバイト文字列をワイド文字列(UNICODE)に変換 - std::wstring Multi2Wide(std::string const& src); - - // 書式付きデバッグ出力 - int DebugPrintf(LPCTSTR format, ...); - - // テーブルを読み込み - bool ReadTable(const std::string& filename, STR_TABLE& table, const char delimiter = ','); - - // マルチディスプレイの情報を取得 - // 要素0:メインウインドウ - std::vector GetDisplayInfo(); - - // ディスプレイ情報取得コールバック - BOOL CALLBACK MonitorEnumProc( - HMONITOR hMon, HDC hdcMon, LPRECT lpMon, LPARAM dwData); - - // 安全なデリート - void SafeDelete(void** ptr); - - // 日付時刻文字列取得 - std::string DateTimeStr(); - - // 高精度タイマークラス - class HPTimer { - LARGE_INTEGER _nFreq; // 周波数 - LARGE_INTEGER _nBegin; // 開始時間 - LARGE_INTEGER _nLast; // 前回問い合わせ時間 - double Calc(LARGE_INTEGER before); - - public: - HPTimer(); - double Elapse(); // 開始時からの経過時間(ms) - double Interval(); // 前回からの経過時間(ms) - void Reset(); // 開始時間リセット - static void HPSleep(double delayms); // 高精度Sleep - }; -} diff --git a/ECTrainer2/SceneCamera.cpp b/ECTrainer2/SceneCamera.cpp index 1f33dff..1633843 100644 --- a/ECTrainer2/SceneCamera.cpp +++ b/ECTrainer2/SceneCamera.cpp @@ -6,7 +6,7 @@ #include "SceneCamera.h" #include "Worker.h" #include "ECTrainerGUI.h" -#include "MyWinUtils.h" +//#include "nkcWinUtils.h" // コンストラクタ SceneCamera::SceneCamera(ECTrainer* pEct) : BaseProcess(pEct) diff --git a/ECTrainer2/Stimulus.cpp b/ECTrainer2/Stimulus.cpp index 69d2555..b7e1d3c 100644 --- a/ECTrainer2/Stimulus.cpp +++ b/ECTrainer2/Stimulus.cpp @@ -16,7 +16,7 @@ } Stimulus::~Stimulus() { - mwut::SafeDelete((void**)&_pMovieObject); + nkc::wut::SafeDelete((void**)&_pMovieObject); } // 初期化 @@ -24,9 +24,9 @@ _Display.Put(cv::imread(OPENING_FILE)); // 設定ファイルをテーブルに読み込み - mwut::STR_TABLE table; - if (!mwut::ReadTable(STIM_CONFIG_FILE, table)) { - mwut::DebugPrintf(_T("Can't open config file : %d\n"), STIM_CONFIG_FILE.c_str()); + nkc::STR_TABLE table; + if (!nkc::wut::ReadTable(STIM_CONFIG_FILE, table)) { + nkc::wut::DebugPrintf(_T("Can't open config file : %d\n"), STIM_CONFIG_FILE.c_str()); return false; } @@ -148,12 +148,12 @@ _cap.open(_StimInfoSet[newStimNo].smallmovie); if (!_cap.isOpened()) { _pEct->PWorker()->EventLog((_T("Can't open small movie file: ") + - mwut::Multi2Wide(_StimInfoSet[newStimNo].filepath)).c_str()); + nkc::wut::Multi2Wide(_StimInfoSet[newStimNo].filepath)).c_str()); ((BaseProcess*)_pEct->PWorker())->PostMsg(ECTMSG::SYSTEM_ERROR); return false; } - _Movie.Put(mwut::Multi2Wide(_StimInfoSet[newStimNo].filepath)); + _Movie.Put(nkc::wut::Multi2Wide(_StimInfoSet[newStimNo].filepath)); ((BaseProcess*)_pEct->PECTrainerGUI())->PostMsg(ECTMSG::MOVIE_START); } @@ -167,7 +167,7 @@ auto img = cv::imread(imageFile); if (img.empty()) { _pEct->PWorker()->EventLog((_T("Can't open image file: ") + - mwut::Multi2Wide(imageFile)).c_str()); + nkc::wut::Multi2Wide(imageFile)).c_str()); ((BaseProcess*)_pEct->PWorker())->PostMsg(ECTMSG::SYSTEM_ERROR); return false; } @@ -179,8 +179,8 @@ void Stimulus::LoadMovieObject(int stimNo) { int nFrames = _pMovieObject->SetData(_StimInfoSet[stimNo].csvfile); - mwut::DebugPrintf(_T("Movie object '%s' - %d frames read.\n"), - mwut::Multi2Wide(_StimInfoSet[stimNo].csvfile).c_str(), nFrames); + nkc::wut::DebugPrintf(_T("Movie object '%s' - %d frames read.\n"), + nkc::wut::Multi2Wide(_StimInfoSet[stimNo].csvfile).c_str(), nFrames); } // ターゲット情報を取得 diff --git a/ECTrainer2/Stimulus.h b/ECTrainer2/Stimulus.h index aeb88b6..21a53cf 100644 --- a/ECTrainer2/Stimulus.h +++ b/ECTrainer2/Stimulus.h @@ -7,7 +7,7 @@ #include "nkcOpenCV.h" #include "RingBuffer.h" #include "MovieObject.h" -#include "myWinUtils.h" +#include "HPTimer.h" class BaseProcess; class Marker; @@ -39,7 +39,7 @@ std::vector _StimInfoSet; // 実験設定 MovieObject* _pMovieObject; // ターゲット情報 int _StimNo; // 刺激データ番号 -1:停止 - mwut::HPTimer _StimTimer; // 刺激提示タイマー + nkc::HPTimer _StimTimer; // 刺激提示タイマー RingBuffer _Display; // 表示画像 RingBuffer _Movie; // 動画情報 cv::VideoCapture _cap; // 動画オブジェクト diff --git a/ECTrainer2/TobiiREST.cpp b/ECTrainer2/TobiiREST.cpp index 0f08be1..65ce0f4 100644 --- a/ECTrainer2/TobiiREST.cpp +++ b/ECTrainer2/TobiiREST.cpp @@ -2,7 +2,7 @@ #include "TobiiREST.h" #include "ECTrainerGUI.h" #include "Worker.h" -#include "myWinUtils.h" +#include "nkcWinUtils.h" // コンストラクタ TobiiREST::TobiiREST(ECTrainer* pEct) : BaseProcess(pEct) @@ -93,7 +93,7 @@ catch (const std::exception& e) { UNREFERENCED_PARAMETER(e); // e.what() がマルチバイト文字列なので使えない - mwut::DebugPrintf(_T("Error in TobiiREST::StartCalib()\n")); + nkc::wut::DebugPrintf(_T("Error in TobiiREST::StartCalib()\n")); return false; } } @@ -124,7 +124,7 @@ { UNREFERENCED_PARAMETER(e); // e.what() がマルチバイト文字列なので使えない ((BaseProcess*)_pEct->PWorker())->PostMsg(ECTMSG::CALIB_ERR); - mwut::DebugPrintf(_T("Error in TobiiREST::QueryCalibResult()\n")); + nkc::wut::DebugPrintf(_T("Error in TobiiREST::QueryCalibResult()\n")); return false; } } @@ -143,7 +143,7 @@ return true; } catch (const std::exception & e) { UNREFERENCED_PARAMETER(e); // e.what() がマルチバイト文字列なので使えない - mwut::DebugPrintf(_T("Error in TobiiREST::QueryBatteryLevel()\n")); + nkc::wut::DebugPrintf(_T("Error in TobiiREST::QueryBatteryLevel()\n")); return false; } } diff --git a/ECTrainer2/Worker.cpp b/ECTrainer2/Worker.cpp index fc213db..6e45852 100644 --- a/ECTrainer2/Worker.cpp +++ b/ECTrainer2/Worker.cpp @@ -6,7 +6,8 @@ #include "BitalMonitor.h" #include "Marker.h" #include "MovieObject.h" -#include "myWinUtils.h" +#include "nkcWinUtils.h" +#include "HPTimer.h" // コンストラクタ Worker::Worker(ECTrainer* pEct) : BaseProcess(pEct) @@ -18,14 +19,14 @@ , _FullScreenImage() , _FBLevel(1) { - _pExpTimer = new mwut::HPTimer(); - _pContactTimer = new mwut::HPTimer(); + _pExpTimer = new nkc::HPTimer(); + _pContactTimer = new nkc::HPTimer(); } // デストラクタ Worker::~Worker() { - mwut::SafeDelete((void**)&_pExpTimer); - mwut::SafeDelete((void**)&_pContactTimer); + nkc::wut::SafeDelete((void**)&_pExpTimer); + nkc::wut::SafeDelete((void**)&_pContactTimer); } // 初期化 @@ -246,7 +247,7 @@ void Worker::OpenDataLog() { this->CloseDataLog(); // 念のため閉じる - auto filename = DATA_LOG_FILE + mwut::Multi2Wide(mwut::DateTimeStr()) + _T(".csv"); + auto filename = DATA_LOG_FILE + nkc::wut::Multi2Wide(nkc::wut::DateTimeStr()) + _T(".csv"); if (_tfopen_s(&_fpLogData, filename.c_str(), _T("w")) != 0) { _pEct->MsgBox(_T("Can't open data log file."), MB_ICONERROR); @@ -278,9 +279,9 @@ if (_tcslen(msg) < 1) { _ftprintf(fp, _T("\n")); } else { - std::wstring datestr = mwut::Multi2Wide(mwut::DateTimeStr()); + std::wstring datestr = nkc::wut::Multi2Wide(nkc::wut::DateTimeStr()); _ftprintf(fp, _T("%s %s\n"), datestr.c_str(), msg); - mwut::DebugPrintf(_T("%s %s\n"), datestr.c_str(), msg); + nkc::wut::DebugPrintf(_T("%s %s\n"), datestr.c_str(), msg); } fclose(fp); diff --git a/ECTrainer2/Worker.h b/ECTrainer2/Worker.h index 3f27d11..7e37f0e 100644 --- a/ECTrainer2/Worker.h +++ b/ECTrainer2/Worker.h @@ -5,11 +5,8 @@ #include #include "nkcOpenCV.h" #include "RingBuffer.h" -#include "myWinUtils.h" +#include "HPTimer.h" -namespace mwut { - class HPTimer; -}; class BaseProcess; class ECTrainer; @@ -33,8 +30,8 @@ //const int NOHIT_TIME = 100; // 視線が外れたと判定する時間 msec APP_STATUS _AppStatus; // アプリケーション状態 FILE* _fpLogData; // データログファイルポインタ - mwut::HPTimer* _pExpTimer; // 実験経過タイマー - mwut::HPTimer* _pContactTimer; // 目標コンタクトタイマー + nkc::HPTimer* _pExpTimer; // 実験経過タイマー + nkc::HPTimer* _pContactTimer; // 目標コンタクトタイマー //mwut::HPTimer* _pNohitTimer; // 視線外れ時間 double _ContactTime; // コンタクト累積時間 RingBuffer _TargetImage; // ターゲット画像 diff --git a/ECTrainer2/myWinUtils.cpp b/ECTrainer2/myWinUtils.cpp deleted file mode 100644 index b75299d..0000000 --- a/ECTrainer2/myWinUtils.cpp +++ /dev/null @@ -1,166 +0,0 @@ -#include "MyWinUtils.h" - -#include -#include -#include -#include - -namespace mwut { - - // マルチバイト文字列をワイド文字列(UNICODE)に変換 - std::wstring Multi2Wide(std::string const& src) { - auto const dest_size = ::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, nullptr, 0U); - std::vector dest(dest_size, L'\0'); - if (::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, dest.data(), (int)dest.size()) == 0) { - throw std::system_error{ static_cast(::GetLastError()), std::system_category() }; - } - dest.resize(std::char_traits::length(dest.data())); - dest.shrink_to_fit(); - return std::wstring(dest.begin(), dest.end()); - } - - // 書式付きデバッグ出力 - int DebugPrintf(LPCTSTR format, ...) { - va_list args; - va_start(args, format); - - int len = _vsctprintf(format, args) + 1; - TCHAR* buf = (TCHAR*)malloc(len * sizeof(TCHAR)); - len = _vstprintf_s(buf, len, format, args); - - OutputDebugString(buf); - free(buf); - - return len; - } - - // テーブルを読み込み - bool ReadTable(const std::string& filename, STR_TABLE& table, const char delimiter) { - // ファイルを開く - std::fstream filestream(filename); - if (!filestream.is_open()) return false; // ファイルが開けなかった場合は終了 - - // ファイルを読み込む - while (!filestream.eof()) { - // 1行読み込む - std::string buffer; - filestream >> buffer; - - // ファイルから読み込んだ1行の文字列を区切り文字で分けてリストに追加する - std::vector record; // 1行分の文字列のリスト - std::istringstream streambuffer(buffer); // 文字列ストリーム - std::string token; // 1セル分の文字列 - while (std::getline(streambuffer, token, delimiter)) { - // 1セル分の文字列をリストに追加する - record.push_back(token); - } - - // 1行分の文字列を出力引数のリストに追加する - table.push_back(record); - } - - return true; - } - - // マルチディスプレイの情報を取得 - // 要素0:メインウインドウ - std::vector GetDisplayInfo() { - // ディスプレイ情報取得 - std::vector displays; - EnumDisplayMonitors(NULL, NULL, - (MONITORENUMPROC)MonitorEnumProc, (LPARAM)&displays); - - // メインモニタを先頭要素に移動 - int mainMonitor = 0; - for (int i = 0; i < displays.size(); i++) { - if (displays[i].left == 0 && displays[i].top == 0) mainMonitor = i; - } - if (mainMonitor != 0) { - SWAP::DO(displays[0], displays[mainMonitor]); - } - -#ifdef _DEBUG - for (int i = 0; i < displays.size(); i++) { - DebugPrintf(_T("Display %d%s L %d T %d R %d B %d\n"), - i, i ? _T("") : _T("(main)"), displays[i].left, displays[i].top, - displays[i].right, displays[i].bottom); - //std::cout << "Monitor" << i << " : " - // << displays[i].left << "," << displays[i].top << "," - // << displays[i].right << "," << displays[i].bottom << std::endl; - } -#endif // _DEBUG - - return displays; - } - - // ディスプレイ情報取得コールバック - BOOL CALLBACK MonitorEnumProc( - HMONITOR hMon, HDC hdcMon, LPRECT lpMon, LPARAM dwData) { - - std::vector* displays = (std::vector*)dwData; - displays->push_back(*lpMon); - return TRUE; - } - - // 安全なデリート - void SafeDelete(void** ptr) { - if (*ptr) { - delete* ptr; - *ptr = NULL; - } - } - - // 日付時刻文字列取得 (C++20になったら std::format 使いたい) - std::string DateTimeStr() { - SYSTEMTIME st; - ::GetLocalTime(&st); - - char buf[20]; - sprintf_s(buf, 19, "%04d%02d%02d_%02d%02d%02d", - st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); - std::string str = buf; - return str; - } - - // 高精度タイマークラス - - // コンストラクタ - HPTimer::HPTimer() { - QueryPerformanceFrequency(&_nFreq); - QueryPerformanceCounter(&_nBegin); - _nLast = _nBegin; - } - - // 経過時間算出 - double HPTimer::Calc(LARGE_INTEGER before) { - LARGE_INTEGER cur; - QueryPerformanceCounter(&cur); - double elapse = (cur.QuadPart - before.QuadPart) * 1000.0 / _nFreq.QuadPart; - _nLast = cur; - return elapse; - } - - // 開始時からの経過時間(ms) - double HPTimer::Elapse() { - return this->Calc(_nBegin); - } - - // 前回からの経過時間(ms) - double HPTimer::Interval() { - return this->Calc(_nLast); - } - - // 開始時間リセット - void HPTimer::Reset() { - QueryPerformanceCounter(&_nBegin); - } - - // 高精度Sleep - void HPTimer::HPSleep(double delayms) { - HPTimer timer; - while (1) { - if (timer.Elapse() > delayms) break; - Sleep(0); - } - } -} diff --git a/ECTrainer2/nkcWinUtils.cpp b/ECTrainer2/nkcWinUtils.cpp new file mode 100644 index 0000000..4c7fba0 --- /dev/null +++ b/ECTrainer2/nkcWinUtils.cpp @@ -0,0 +1,127 @@ +#include "nkcWinUtils.h" + +#include +#include +#include +#include + +namespace nkc { +namespace wut { + +// マルチバイト文字列をワイド文字列(UNICODE)に変換 +std::wstring Multi2Wide(std::string const& src) { + auto const dest_size = ::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, nullptr, 0U); + std::vector dest(dest_size, L'\0'); + if (::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, dest.data(), (int)dest.size()) == 0) { + throw std::system_error{ static_cast(::GetLastError()), std::system_category() }; + } + dest.resize(std::char_traits::length(dest.data())); + dest.shrink_to_fit(); + return std::wstring(dest.begin(), dest.end()); +} + +// 書式付きデバッグ出力 +int DebugPrintf(LPCTSTR format, ...) { + va_list args; + va_start(args, format); + + int len = _vsctprintf(format, args) + 1; + TCHAR* buf = (TCHAR*)malloc(len * sizeof(TCHAR)); + len = _vstprintf_s(buf, len, format, args); + + OutputDebugString(buf); + free(buf); + + return len; +} + +// テーブルを読み込み +bool ReadTable(const std::string& filename, STR_TABLE& table, const char delimiter) { + // ファイルを開く + std::fstream filestream(filename); + if (!filestream.is_open()) return false; // ファイルが開けなかった場合は終了 + + // ファイルを読み込む + while (!filestream.eof()) { + // 1行読み込む + std::string buffer; + filestream >> buffer; + + // ファイルから読み込んだ1行の文字列を区切り文字で分けてリストに追加する + std::vector record; // 1行分の文字列のリスト + std::istringstream streambuffer(buffer); // 文字列ストリーム + std::string token; // 1セル分の文字列 + while (std::getline(streambuffer, token, delimiter)) { + // 1セル分の文字列をリストに追加する + record.push_back(token); + } + + // 1行分の文字列を出力引数のリストに追加する + table.push_back(record); + } + + return true; +} + +// マルチディスプレイの情報を取得 +// 要素0:メインウインドウ +std::vector GetDisplayInfo() { + // ディスプレイ情報取得 + std::vector displays; + EnumDisplayMonitors(NULL, NULL, + (MONITORENUMPROC)MonitorEnumProc, (LPARAM)&displays); + + // メインモニタを先頭要素に移動 + int mainMonitor = 0; + for (int i = 0; i < displays.size(); i++) { + if (displays[i].left == 0 && displays[i].top == 0) mainMonitor = i; + } + if (mainMonitor != 0) { + SWAP::DO(displays[0], displays[mainMonitor]); + } + +#ifdef _DEBUG + for (int i = 0; i < displays.size(); i++) { + DebugPrintf(_T("Display %d%s L %d T %d R %d B %d\n"), + i, i ? _T("") : _T("(main)"), displays[i].left, displays[i].top, + displays[i].right, displays[i].bottom); + //std::cout << "Monitor" << i << " : " + // << displays[i].left << "," << displays[i].top << "," + // << displays[i].right << "," << displays[i].bottom << std::endl; + } +#endif // _DEBUG + + return displays; +} + +// ディスプレイ情報取得コールバック +BOOL CALLBACK MonitorEnumProc( + HMONITOR hMon, HDC hdcMon, LPRECT lpMon, LPARAM dwData) { + + std::vector* displays = (std::vector*)dwData; + displays->push_back(*lpMon); + return TRUE; +} + +// 安全なデリート +void SafeDelete(void** ptr) { + if (*ptr) { + delete* ptr; + *ptr = NULL; + } +} + +// 日付時刻文字列取得 (C++20になったら std::format 使いたい) +std::string DateTimeStr() { + SYSTEMTIME st; + ::GetLocalTime(&st); + + char buf[20]; + sprintf_s(buf, 19, "%04d%02d%02d_%02d%02d%02d", + st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); + std::string str = buf; + return str; +} + +}; // namespace wut +}; // namespace nkc diff --git a/ECTrainer2/nkcWinUtils.h b/ECTrainer2/nkcWinUtils.h new file mode 100644 index 0000000..4089381 --- /dev/null +++ b/ECTrainer2/nkcWinUtils.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include +#include + +namespace nkc { + +// スワップクラス +template +class SWAP { +public: + static void DO(T& a, T& b) { + T tmp = a; + a = b; + b = tmp; + } +}; + +typedef std::vector> STR_TABLE; + +namespace wut { + +// マルチバイト文字列をワイド文字列(UNICODE)に変換 +std::wstring Multi2Wide(std::string const& src); + +// 書式付きデバッグ出力 +int DebugPrintf(LPCTSTR format, ...); + +// テーブルを読み込み +bool ReadTable(const std::string& filename, STR_TABLE& table, const char delimiter = ','); + +// マルチディスプレイの情報を取得 +// 要素0:メインウインドウ +std::vector GetDisplayInfo(); + +// ディスプレイ情報取得コールバック +BOOL CALLBACK MonitorEnumProc( + HMONITOR hMon, HDC hdcMon, LPRECT lpMon, LPARAM dwData); + +// 安全なデリート +void SafeDelete(void** ptr); + +// 日付時刻文字列取得 +std::string DateTimeStr(); + +}; // namespace wut +}; // namespace nkc