Skip to content

Commit

Permalink
Use system clock instead of used CPU time. Fixes key repeat and some
Browse files Browse the repository at this point in the history
other stuff. close #206
  • Loading branch information
mniip committed Feb 12, 2014
1 parent d38b7b9 commit a105ed9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
15 changes: 13 additions & 2 deletions src/Misc.cpp
Expand Up @@ -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};
2 changes: 2 additions & 0 deletions src/Misc.h
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/gui/interface/Engine.cpp
@@ -1,9 +1,9 @@
#include <iostream>
#include <stack>
#include <cstdio>
#include <time.h>

#include "Config.h"
#include "Misc.h"
#include "gui/interface/Window.h"
#include "gui/interface/Platform.h"
#include "gui/interface/Engine.h"
Expand Down Expand Up @@ -181,7 +181,7 @@ void Engine::Tick()
state_->DoTick(dt);


lastTick = clock();
lastTick = gettime();
if(windowOpenState<1.0f)
{
if(lastBuffer)
Expand Down
8 changes: 4 additions & 4 deletions src/gui/interface/Textbox.cpp
@@ -1,8 +1,8 @@
#include <string>
#include <iostream>
#include <stdexcept>
#include <time.h>
#include "Config.h"
#include "Misc.h"
#include "gui/interface/Point.h"
#include "gui/interface/Textbox.h"
#include "gui/interface/Keys.h"
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/gui/search/SearchController.cpp
@@ -1,6 +1,5 @@
#include <string>
#include <sstream>
#include <time.h>
#include "SearchController.h"
#include "SearchModel.h"
#include "SearchView.h"
Expand All @@ -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"

Expand Down Expand Up @@ -61,7 +61,7 @@ void SearchController::ReleaseLoadedSave()

void SearchController::Update()
{
if(!nextQueryDone && nextQueryTime < clock())
if(!nextQueryDone && nextQueryTime < gettime())
{
nextQueryDone = true;
searchModel->UpdateSaveList(1, nextQuery);
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -9,6 +9,7 @@
#include "Format.h"
#include "LuaScriptInterface.h"
#include "LuaScriptHelper.h"
#include "Misc.h"
#include "PowderToy.h"

#include "gui/dialogues/ErrorMessage.h"
Expand All @@ -18,7 +19,6 @@
#include "gui/game/GameModel.h"
#include "simulation/Simulation.h"

#include <time.h>

#ifndef FFI
int luacon_partread(lua_State* l){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -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"
Expand All @@ -44,7 +45,6 @@ extern "C"
#endif
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include "socket/luasocket.h"
}
#include "socket/socket.lua.h"
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down

0 comments on commit a105ed9

Please sign in to comment.