Use system clock instead of used CPU time. Fixes key repeat and some

other stuff. close #206
This commit is contained in:
mniip 2014-02-03 23:27:16 +04:00
parent d38b7b9b11
commit a105ed9df8
7 changed files with 34 additions and 21 deletions

View File

@ -669,5 +669,16 @@ void millisleep(long int t)
#endif #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}; vector2d v2d_zero = {0,0};
matrix2d m2d_identity = {1,0,0,1}; matrix2d m2d_identity = {1,0,0,1};

View File

@ -90,6 +90,8 @@ int splitsign(const char* str, char * type = NULL);
void millisleep(long int t); void millisleep(long int t);
long unsigned int gettime();
// a b // a b
// c d // c d

View File

@ -1,9 +1,9 @@
#include <iostream> #include <iostream>
#include <stack> #include <stack>
#include <cstdio> #include <cstdio>
#include <time.h>
#include "Config.h" #include "Config.h"
#include "Misc.h"
#include "gui/interface/Window.h" #include "gui/interface/Window.h"
#include "gui/interface/Platform.h" #include "gui/interface/Platform.h"
#include "gui/interface/Engine.h" #include "gui/interface/Engine.h"
@ -181,7 +181,7 @@ void Engine::Tick()
state_->DoTick(dt); state_->DoTick(dt);
lastTick = clock(); lastTick = gettime();
if(windowOpenState<1.0f) if(windowOpenState<1.0f)
{ {
if(lastBuffer) if(lastBuffer)

View File

@ -1,8 +1,8 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <time.h>
#include "Config.h" #include "Config.h"
#include "Misc.h"
#include "gui/interface/Point.h" #include "gui/interface/Point.h"
#include "gui/interface/Textbox.h" #include "gui/interface/Textbox.h"
#include "gui/interface/Keys.h" #include "gui/interface/Keys.h"
@ -299,10 +299,10 @@ void Textbox::Tick(float dt)
keyDown = 0; keyDown = 0;
characterDown = 0; characterDown = 0;
} }
if((keyDown || characterDown) && repeatTime <= clock()) if((keyDown || characterDown) && repeatTime <= gettime())
{ {
OnVKeyPress(keyDown, characterDown, false, false, false); 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; characterDown = character;
keyDown = key; keyDown = key;
repeatTime = clock()+(0.3 * CLOCKS_PER_SEC); repeatTime = gettime()+300;
OnVKeyPress(key, character, shift, ctrl, alt); OnVKeyPress(key, character, shift, ctrl, alt);
} }

View File

@ -1,6 +1,5 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <time.h>
#include "SearchController.h" #include "SearchController.h"
#include "SearchModel.h" #include "SearchModel.h"
#include "SearchView.h" #include "SearchView.h"
@ -9,6 +8,7 @@
#include "gui/dialogues/ErrorMessage.h" #include "gui/dialogues/ErrorMessage.h"
#include "gui/preview/PreviewController.h" #include "gui/preview/PreviewController.h"
#include "client/Client.h" #include "client/Client.h"
#include "Misc.h"
#include "tasks/Task.h" #include "tasks/Task.h"
#include "tasks/TaskWindow.h" #include "tasks/TaskWindow.h"
@ -61,7 +61,7 @@ void SearchController::ReleaseLoadedSave()
void SearchController::Update() void SearchController::Update()
{ {
if(!nextQueryDone && nextQueryTime < clock()) if(!nextQueryDone && nextQueryTime < gettime())
{ {
nextQueryDone = true; nextQueryDone = true;
searchModel->UpdateSaveList(1, nextQuery); searchModel->UpdateSaveList(1, nextQuery);
@ -107,7 +107,7 @@ void SearchController::DoSearch(std::string query, bool now)
nextQuery = query; nextQuery = query;
if(!now) if(!now)
{ {
nextQueryTime = clock()+(0.6 * CLOCKS_PER_SEC); nextQueryTime = gettime()+600;
nextQueryDone = false; nextQueryDone = false;
} }
else else

View File

@ -9,6 +9,7 @@
#include "Format.h" #include "Format.h"
#include "LuaScriptInterface.h" #include "LuaScriptInterface.h"
#include "LuaScriptHelper.h" #include "LuaScriptHelper.h"
#include "Misc.h"
#include "PowderToy.h" #include "PowderToy.h"
#include "gui/dialogues/ErrorMessage.h" #include "gui/dialogues/ErrorMessage.h"
@ -18,7 +19,6 @@
#include "gui/game/GameModel.h" #include "gui/game/GameModel.h"
#include "simulation/Simulation.h" #include "simulation/Simulation.h"
#include <time.h>
#ifndef FFI #ifndef FFI
int luacon_partread(lua_State* l){ 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")) 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++) for(j=i;j<=c-1;j++)
{ {
lua_rawgeti(l, -2, j+1); 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")) 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++) for(j=i;j<=c-1;j++)
{ {
lua_rawgeti(l, -2, j+1); 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")) 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++) for(j=i;j<=c-1;j++)
{ {
lua_rawgeti(l, -2, j+1); 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){ int luacon_eval(const char *command){
ui::Engine::Ref().LastTick(clock()); ui::Engine::Ref().LastTick(gettime());
return luaL_dostring (luacon_ci->l, command); return luaL_dostring (luacon_ci->l, command);
} }
void luacon_hook(lua_State * l, lua_Debug * ar) 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")) 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"); luaL_error(l, "Error: Script not responding");
ui::Engine::Ref().LastTick(clock()); ui::Engine::Ref().LastTick(gettime());
} }
} }

View File

@ -21,6 +21,7 @@
#include "gui/game/Tool.h" #include "gui/game/Tool.h"
#include "LuaScriptHelper.h" #include "LuaScriptHelper.h"
#include "client/HTTP.h" #include "client/HTTP.h"
#include "Misc.h"
#include "PowderToy.h" #include "PowderToy.h"
#include "LuaBit.h" #include "LuaBit.h"
@ -44,7 +45,6 @@ extern "C"
#endif #endif
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <time.h>
#include "socket/luasocket.h" #include "socket/luasocket.h"
} }
#include "socket/socket.lua.h" #include "socket/socket.lua.h"
@ -2804,7 +2804,7 @@ void LuaScriptInterface::OnTick()
lua_getglobal(l, "simulation"); lua_getglobal(l, "simulation");
lua_pushinteger(l, luacon_sim->NUM_PARTS); lua_setfield(l, -2, "NUM_PARTS"); lua_pushinteger(l, luacon_sim->NUM_PARTS); lua_setfield(l, -2, "NUM_PARTS");
lua_pop(l, 1); lua_pop(l, 1);
ui::Engine::Ref().LastTick(clock()); ui::Engine::Ref().LastTick(gettime());
if(luacon_mousedown) if(luacon_mousedown)
luacon_mouseevent(luacon_mousex, luacon_mousey, luacon_mousebutton, LUACON_MPRESS, 0); 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); 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 += "\n";
lastCode += command; lastCode += command;
std::string tmp = "return " + lastCode; std::string tmp = "return " + lastCode;
ui::Engine::Ref().LastTick(clock()); ui::Engine::Ref().LastTick(gettime());
luaL_loadbuffer(l, tmp.c_str(), tmp.length(), "@console"); luaL_loadbuffer(l, tmp.c_str(), tmp.length(), "@console");
if(lua_type(l, -1) != LUA_TFUNCTION) if(lua_type(l, -1) != LUA_TFUNCTION)
{ {