From a105ed9df85e7660e46456adaf5236aa1f62f628 Mon Sep 17 00:00:00 2001 From: mniip Date: Mon, 3 Feb 2014 23:27:16 +0400 Subject: [PATCH] Use system clock instead of used CPU time. Fixes key repeat and some other stuff. close #206 --- src/Misc.cpp | 15 +++++++++++++-- src/Misc.h | 2 ++ src/gui/interface/Engine.cpp | 4 ++-- src/gui/interface/Textbox.cpp | 8 ++++---- src/gui/search/SearchController.cpp | 6 +++--- src/lua/LegacyLuaAPI.cpp | 14 +++++++------- src/lua/LuaScriptInterface.cpp | 6 +++--- 7 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index 149bb5060c..ff56500865 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -663,11 +663,22 @@ void millisleep(long int t) Sleep(t); #else struct timespec s; - s.tv_sec = t/1000; - s.tv_nsec = (t%1000)*10000000; + s.tv_sec = t / 1000; + s.tv_nsec = (t % 1000) * 10000000; nanosleep(&s, NULL); #endif } +long unsigned int gettime() +{ +#ifdef WIN + return GetTickCount(); +#else + struct timespec s; + clock_gettime(CLOCK_MONOTONIC, &s); + return s.tv_sec * 1000 + s.tv_nsec / 1000000; +#endif +} + vector2d v2d_zero = {0,0}; matrix2d m2d_identity = {1,0,0,1}; diff --git a/src/Misc.h b/src/Misc.h index 477083af36..ac0f05186b 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -90,6 +90,8 @@ int splitsign(const char* str, char * type = NULL); void millisleep(long int t); +long unsigned int gettime(); + // a b // c d diff --git a/src/gui/interface/Engine.cpp b/src/gui/interface/Engine.cpp index fb8b1c9c5c..8f10ae6c11 100644 --- a/src/gui/interface/Engine.cpp +++ b/src/gui/interface/Engine.cpp @@ -1,9 +1,9 @@ #include #include #include -#include #include "Config.h" +#include "Misc.h" #include "gui/interface/Window.h" #include "gui/interface/Platform.h" #include "gui/interface/Engine.h" @@ -181,7 +181,7 @@ void Engine::Tick() state_->DoTick(dt); - lastTick = clock(); + lastTick = gettime(); if(windowOpenState<1.0f) { if(lastBuffer) diff --git a/src/gui/interface/Textbox.cpp b/src/gui/interface/Textbox.cpp index 387bceafdb..1a4bcc7ba7 100644 --- a/src/gui/interface/Textbox.cpp +++ b/src/gui/interface/Textbox.cpp @@ -1,8 +1,8 @@ #include #include #include -#include #include "Config.h" +#include "Misc.h" #include "gui/interface/Point.h" #include "gui/interface/Textbox.h" #include "gui/interface/Keys.h" @@ -299,10 +299,10 @@ void Textbox::Tick(float dt) keyDown = 0; characterDown = 0; } - if((keyDown || characterDown) && repeatTime <= clock()) + if((keyDown || characterDown) && repeatTime <= gettime()) { OnVKeyPress(keyDown, characterDown, false, false, false); - repeatTime = clock()+(0.03 * CLOCKS_PER_SEC); + repeatTime = gettime()+30; } } @@ -316,7 +316,7 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool { characterDown = character; keyDown = key; - repeatTime = clock()+(0.3 * CLOCKS_PER_SEC); + repeatTime = gettime()+300; OnVKeyPress(key, character, shift, ctrl, alt); } diff --git a/src/gui/search/SearchController.cpp b/src/gui/search/SearchController.cpp index 65b0bac8f4..c655458939 100644 --- a/src/gui/search/SearchController.cpp +++ b/src/gui/search/SearchController.cpp @@ -1,6 +1,5 @@ #include #include -#include #include "SearchController.h" #include "SearchModel.h" #include "SearchView.h" @@ -9,6 +8,7 @@ #include "gui/dialogues/ErrorMessage.h" #include "gui/preview/PreviewController.h" #include "client/Client.h" +#include "Misc.h" #include "tasks/Task.h" #include "tasks/TaskWindow.h" @@ -61,7 +61,7 @@ void SearchController::ReleaseLoadedSave() void SearchController::Update() { - if(!nextQueryDone && nextQueryTime < clock()) + if(!nextQueryDone && nextQueryTime < gettime()) { nextQueryDone = true; searchModel->UpdateSaveList(1, nextQuery); @@ -107,7 +107,7 @@ void SearchController::DoSearch(std::string query, bool now) nextQuery = query; if(!now) { - nextQueryTime = clock()+(0.6 * CLOCKS_PER_SEC); + nextQueryTime = gettime()+600; nextQueryDone = false; } else diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index b56079bf83..3e02c20803 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -9,6 +9,7 @@ #include "Format.h" #include "LuaScriptInterface.h" #include "LuaScriptHelper.h" +#include "Misc.h" #include "PowderToy.h" #include "gui/dialogues/ErrorMessage.h" @@ -18,7 +19,6 @@ #include "gui/game/GameModel.h" #include "simulation/Simulation.h" -#include #ifndef FFI int luacon_partread(lua_State* l){ @@ -512,7 +512,7 @@ int luacon_keyevent(int key, int modifier, int event) { if (!strcmp(luacon_geterror(), "Error: Script not responding")) { - ui::Engine::Ref().LastTick(clock()); + ui::Engine::Ref().LastTick(gettime()); for(j=i;j<=c-1;j++) { lua_rawgeti(l, -2, j+1); @@ -564,7 +564,7 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel) { if (!strcmp(luacon_geterror(), "Error: Script not responding")) { - ui::Engine::Ref().LastTick(clock()); + ui::Engine::Ref().LastTick(gettime()); for(j=i;j<=c-1;j++) { lua_rawgeti(l, -2, j+1); @@ -625,7 +625,7 @@ int luacon_step(int mx, int my, std::string selectl, std::string selectr, std::s { if (!strcmp(luacon_geterror(), "Error: Script not responding")) { - ui::Engine::Ref().LastTick(clock()); + ui::Engine::Ref().LastTick(gettime()); for(j=i;j<=c-1;j++) { lua_rawgeti(l, -2, j+1); @@ -646,17 +646,17 @@ int luacon_step(int mx, int my, std::string selectl, std::string selectr, std::s int luacon_eval(const char *command){ - ui::Engine::Ref().LastTick(clock()); + ui::Engine::Ref().LastTick(gettime()); return luaL_dostring (luacon_ci->l, command); } void luacon_hook(lua_State * l, lua_Debug * ar) { - if(ar->event == LUA_HOOKCOUNT && clock()-ui::Engine::Ref().LastTick() > CLOCKS_PER_SEC*3) + if(ar->event == LUA_HOOKCOUNT && gettime()-ui::Engine::Ref().LastTick() > 3000) { if(ConfirmPrompt::Blocking("Script not responding", "The Lua script may have stopped responding. There might be an infinite loop. Press \"Stop\" to stop it", "Stop")) luaL_error(l, "Error: Script not responding"); - ui::Engine::Ref().LastTick(clock()); + ui::Engine::Ref().LastTick(gettime()); } } diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 5ca7ba1a9b..a8fbda0a59 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -21,6 +21,7 @@ #include "gui/game/Tool.h" #include "LuaScriptHelper.h" #include "client/HTTP.h" +#include "Misc.h" #include "PowderToy.h" #include "LuaBit.h" @@ -44,7 +45,6 @@ extern "C" #endif #include #include -#include #include "socket/luasocket.h" } #include "socket/socket.lua.h" @@ -2804,7 +2804,7 @@ void LuaScriptInterface::OnTick() lua_getglobal(l, "simulation"); lua_pushinteger(l, luacon_sim->NUM_PARTS); lua_setfield(l, -2, "NUM_PARTS"); lua_pop(l, 1); - ui::Engine::Ref().LastTick(clock()); + ui::Engine::Ref().LastTick(gettime()); if(luacon_mousedown) luacon_mouseevent(luacon_mousex, luacon_mousey, luacon_mousebutton, LUACON_MPRESS, 0); luacon_step(luacon_mousex, luacon_mousey, luacon_selectedl, luacon_selectedr, luacon_selectedalt, luacon_brushx, luacon_brushy); @@ -2829,7 +2829,7 @@ int LuaScriptInterface::Command(std::string command) lastCode += "\n"; lastCode += command; std::string tmp = "return " + lastCode; - ui::Engine::Ref().LastTick(clock()); + ui::Engine::Ref().LastTick(gettime()); luaL_loadbuffer(l, tmp.c_str(), tmp.length(), "@console"); if(lua_type(l, -1) != LUA_TFUNCTION) {