Parcourir la source

Using C++11 std::chrono for utils/time

Thomas Buck il y a 9 ans
Parent
révision
c33dc66af1
9 fichiers modifiés avec 43 ajouts et 61 suppressions
  1. 4
    0
      ChangeLog.md
  2. 0
    3
      include/config.h.in
  3. 0
    2
      include/utils/time.h
  4. 0
    4
      src/CMakeLists.txt
  5. 2
    3
      src/Console.cpp
  6. 6
    2
      src/UI.cpp
  7. 14
    13
      src/deps/imgui/imgui.cpp
  8. 6
    5
      src/deps/imgui/imgui.h
  9. 11
    29
      src/utils/time.cpp

+ 4
- 0
ChangeLog.md Voir le fichier

@@ -2,6 +2,10 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140909 ]
6
+    * utils/time now using std::chrono (C++11) for single cross-plattform implementation
7
+    * Updated imgui (my pull request was merged)
8
+
5 9
     [ 20140908 ]
6 10
     * Moved imgui Console into own (static) class
7 11
     * Added FPS display

+ 0
- 3
include/config.h.in Voir le fichier

@@ -31,8 +31,5 @@ extern const char *BUILD_HOST;
31 31
 #cmakedefine HAVE_STDLIB_H
32 32
 #cmakedefine HAVE_GETENV
33 33
 
34
-#cmakedefine HAVE_SYS_TIME_H
35
-#cmakedefine HAVE_GETTIMEOFDAY
36
-
37 34
 #endif
38 35
 

+ 0
- 2
include/utils/time.h Voir le fichier

@@ -8,8 +8,6 @@
8 8
 #ifndef _UTILS_TIME_H_
9 9
 #define _UTILS_TIME_H_
10 10
 
11
-extern unsigned long systemTimerStart;
12
-
13 11
 /*!
14 12
  * \brief Read the system timer
15 13
  * \returns number of ticks

+ 0
- 4
src/CMakeLists.txt Voir le fichier

@@ -132,10 +132,6 @@ check_function_exists (getcwd HAVE_GETCWD)
132 132
 check_include_files (stdlib.h HAVE_STDLIB_H)
133 133
 check_function_exists (getenv HAVE_GETENV)
134 134
 
135
-# gettimeofday() for timing
136
-check_include_files (sys/time.h HAVE_SYS_TIME_H)
137
-check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
138
-
139 135
 #################################################################
140 136
 
141 137
 # Configuration Header file

+ 2
- 3
src/Console.cpp Voir le fichier

@@ -32,9 +32,8 @@ void Console::display() {
32 32
         }
33 33
         ImGui::EndChild();
34 34
 
35
-        bool enter = false;
36
-        ImGui::InputText("Command", buffer, bufferLength, ImGuiInputTextFlags_AutoSelectAll, &enter);
37
-        if (enter) {
35
+        if (ImGui::InputText("Command", buffer, bufferLength,
36
+                    ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
38 37
             getLog() << "> " << buffer << Log::endl;
39 38
             int error = Command::command(buffer);
40 39
             if (error != 0) {

+ 6
- 2
src/UI.cpp Voir le fichier

@@ -172,8 +172,12 @@ void UI::display() {
172 172
 
173 173
     Console::display();
174 174
 
175
-    if (ImGui::Begin("Engine")) {
176
-        if (ImGui::CollapsingHeader("Debug", NULL, true, true)) {
175
+    if (ImGui::Begin("Engine/RT")) {
176
+        if (ImGui::CollapsingHeader("Engine", NULL, true, true)) {
177
+            ImGui::Text("Uptime: %lums", systemTimerGet());
178
+        }
179
+
180
+        if (ImGui::CollapsingHeader("Debug")) {
177 181
 
178 182
         }
179 183
 

+ 14
- 13
src/deps/imgui/imgui.cpp Voir le fichier

@@ -162,7 +162,6 @@
162 162
  - text edit: flag to disable live update of the user buffer. 
163 163
  - text edit: field resize behaviour - field could stretch when being edited? hover tooltip shows more text?
164 164
  - text edit: pasting text into a number box should filter the characters the same way direct input does
165
- - text edit: allow code to catch user pressing Return (perhaps through disable live edit? so only Return apply the final value, also allow catching Return if value didn't changed)
166 165
  - settings: write more decent code to allow saving/loading new fields
167 166
  - settings: api for per-tool simple persistant data (bool,int,float) in .ini file
168 167
  - log: be able to right-click and log a window or tree-node into tty/file/clipboard?
@@ -3280,7 +3279,7 @@ bool SliderFloat(const char* label, float* v, float v_min, float v_max, const ch
3280 3279
         g.ActiveId = g.SliderAsInputTextId;
3281 3280
         g.HoveredId = 0;
3282 3281
         window->FocusItemUnregister();      // Our replacement slider will override the focus ID (that we needed to declare previously to allow for a TAB focus to happen before we got selected)
3283
-        value_changed = ImGui::InputText(label, text_buf, IM_ARRAYSIZE(text_buf), ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_AlignCenter);
3282
+        value_changed = ImGui::InputText(label, text_buf, IM_ARRAYSIZE(text_buf), ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_AutoSelectAll);
3284 3283
         if (g.SliderAsInputTextId == 0)
3285 3284
         {
3286 3285
             // First frame
@@ -3835,7 +3834,7 @@ void ImGuiTextEditState::RenderTextScrolledClipped(ImFont font, float font_size,
3835 3834
 namespace ImGui
3836 3835
 {
3837 3836
 
3838
-bool InputFloat(const char* label, float *v, float step, float step_fast, int decimal_precision)
3837
+bool InputFloat(const char* label, float *v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags extra_flags)
3839 3838
 {
3840 3839
     ImGuiState& g = GImGui;
3841 3840
     ImGuiWindow* window = GetCurrentWindow();
@@ -3858,7 +3857,8 @@ bool InputFloat(const char* label, float *v, float step, float step_fast, int de
3858 3857
     else
3859 3858
         ImFormatString(buf, IM_ARRAYSIZE(buf), "%.*f", decimal_precision, *v);
3860 3859
     bool value_changed = false;
3861
-    if (ImGui::InputText("", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsDecimal|ImGuiInputTextFlags_AlignCenter|ImGuiInputTextFlags_AutoSelectAll))
3860
+	const ImGuiInputTextFlags flags = extra_flags | (ImGuiInputTextFlags_CharsDecimal|ImGuiInputTextFlags_AutoSelectAll);
3861
+    if (ImGui::InputText("", buf, IM_ARRAYSIZE(buf), flags))
3862 3862
     {
3863 3863
         ApplyNumericalTextInput(buf, v);
3864 3864
         value_changed = true;
@@ -3892,19 +3892,16 @@ bool InputFloat(const char* label, float *v, float step, float step_fast, int de
3892 3892
     return value_changed;
3893 3893
 }
3894 3894
 
3895
-bool InputInt(const char* label, int *v, int step, int step_fast)
3895
+bool InputInt(const char* label, int *v, int step, int step_fast, ImGuiInputTextFlags extra_flags)
3896 3896
 {
3897 3897
     float f = (float)*v;
3898
-    const bool value_changed = ImGui::InputFloat(label, &f, (float)step, (float)step_fast, 0);
3898
+    const bool value_changed = ImGui::InputFloat(label, &f, (float)step, (float)step_fast, 0, extra_flags);
3899 3899
     *v = (int)f;
3900 3900
     return value_changed;
3901 3901
 }
3902 3902
 
3903
-bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, bool *enter)
3903
+bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags)
3904 3904
 {
3905
-    if (enter != NULL)
3906
-        *enter = false;
3907
-
3908 3905
     ImGuiState& g = GImGui;
3909 3906
     ImGuiWindow* window = GetCurrentWindow();
3910 3907
     if (window->SkipItems)
@@ -3965,6 +3962,7 @@ bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlag
3965 3962
 
3966 3963
     bool value_changed = false;
3967 3964
     bool cancel_edit = false;
3965
+    bool enter_pressed = false;
3968 3966
     if (g.ActiveId == id)
3969 3967
     {
3970 3968
 		// Edit in progress
@@ -4004,7 +4002,7 @@ bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlag
4004 4002
         else if (IsKeyPressedMap(ImGuiKey_End))                 edit_state.OnKeyboardPressed(is_ctrl_down ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask);
4005 4003
         else if (IsKeyPressedMap(ImGuiKey_Delete))              edit_state.OnKeyboardPressed(STB_TEXTEDIT_K_DELETE | k_mask);
4006 4004
         else if (IsKeyPressedMap(ImGuiKey_Backspace))           edit_state.OnKeyboardPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask);
4007
-        else if (IsKeyPressedMap(ImGuiKey_Enter))               { g.ActiveId = 0; if (enter != NULL) *enter = true; }
4005
+        else if (IsKeyPressedMap(ImGuiKey_Enter))               { g.ActiveId = 0; enter_pressed = true; }
4008 4006
         else if (IsKeyPressedMap(ImGuiKey_Escape))              { g.ActiveId = 0; cancel_edit = true; }
4009 4007
         else if (is_ctrl_down && IsKeyPressedMap(ImGuiKey_Z))   edit_state.OnKeyboardPressed(STB_TEXTEDIT_K_UNDO);      // I don't want to use shortcuts but we should probably have an Input-catch stack
4010 4008
         else if (is_ctrl_down && IsKeyPressedMap(ImGuiKey_Y))   edit_state.OnKeyboardPressed(STB_TEXTEDIT_K_REDO);
@@ -4125,7 +4123,10 @@ bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlag
4125 4123
 
4126 4124
     RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
4127 4125
 
4128
-    return value_changed;
4126
+    if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0)
4127
+        return enter_pressed;
4128
+    else
4129
+        return value_changed;
4129 4130
 }
4130 4131
 
4131 4132
 static bool InputFloatN(const char* label, float* v, int components, int decimal_precision)
@@ -5871,7 +5872,7 @@ void ShowTestWindow(bool* open)
5871 5872
         bool goto_line = ImGui::Button("Goto");
5872 5873
         ImGui::SameLine(); 
5873 5874
         ImGui::PushItemWidth(100);
5874
-        ImGui::InputInt("##Line", &line, 0); 
5875
+        goto_line |= ImGui::InputInt("##Line", &line, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
5875 5876
         ImGui::PopItemWidth();
5876 5877
         ImGui::BeginChild("Sub1", ImVec2(ImGui::GetWindowWidth()*0.5f,300));
5877 5878
         for (int i = 0; i < 100; i++)

+ 6
- 5
src/deps/imgui/imgui.h Voir le fichier

@@ -215,12 +215,12 @@ namespace ImGui
215 215
     bool        CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value);
216 216
     bool        RadioButton(const char* label, bool active);
217 217
     bool        RadioButton(const char* label, int* v, int v_button);
218
-    bool        InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, int decimal_precision = -1);
218
+    bool        InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0);
219
+    bool        InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, int decimal_precision = -1, ImGuiInputTextFlags extra_flags = 0);
219 220
     bool        InputFloat2(const char* label, float v[2], int decimal_precision = -1);
220 221
     bool        InputFloat3(const char* label, float v[3], int decimal_precision = -1);
221 222
     bool        InputFloat4(const char* label, float v[4], int decimal_precision = -1);
222
-    bool        InputInt(const char* label, int* v, int step = 1, int step_fast = 100);
223
-    bool        InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, bool* enter = NULL);
223
+    bool        InputInt(const char* label, int* v, int step = 1, int step_fast = 100, ImGuiInputTextFlags extra_flags = 0);
224 224
     bool        Combo(const char* label, int* current_item, const char** items, int items_count, int popup_height_items = 7);
225 225
     bool        Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_height_items = 7);      // Separate items with \0, end item-list with \0\0
226 226
     bool        Combo(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_height_items = 7);
@@ -292,8 +292,9 @@ enum ImGuiInputTextFlags_
292 292
     // Default: 0
293 293
     ImGuiInputTextFlags_CharsDecimal        = 1 << 0,   // Allow 0123456789.+-*/
294 294
     ImGuiInputTextFlags_CharsHexadecimal    = 1 << 1,   // Allow 0123456789ABCDEFabcdef
295
-    ImGuiInputTextFlags_AutoSelectAll       = 1 << 2,
296
-    ImGuiInputTextFlags_AlignCenter         = 1 << 3,
295
+    ImGuiInputTextFlags_AutoSelectAll       = 1 << 2,   // Select entire text when first taking focus
296
+    ImGuiInputTextFlags_EnterReturnsTrue    = 1 << 3,   // Return 'true' when Enter is pressed (as opposed to when the value was modified)
297
+    //ImGuiInputTextFlags_AlignCenter       = 1 << 3,
297 298
 };
298 299
 
299 300
 // User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array

+ 11
- 29
src/utils/time.cpp Voir le fichier

@@ -5,42 +5,24 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include <chrono>
9
+
8 10
 #include "global.h"
9 11
 #include "utils/time.h"
10 12
 
11
-unsigned long systemTimerStart = 0;
12
-
13
-#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
14
-
15
-#include <sys/time.h>
16
-
17
-unsigned long systemTimerGet() {
18
-    struct timeval time;
19
-    struct timezone zone;
20
-    gettimeofday(&time, &zone);
21
-    return ((time.tv_sec * 1000) + (time.tv_usec / 1000)) - systemTimerStart;
22
-}
23
-
24
-void systemTimerReset() {
25
-    struct timeval time;
26
-    struct timezone zone;
27
-    gettimeofday(&time, &zone);
28
-    systemTimerStart = (time.tv_sec * 1000) + (time.tv_usec / 1000);
29
-}
30
-
31
-#elif defined(_WIN32)
32
-
33
-#include <Windows.h>
13
+static unsigned long systemTimerStart = 0;
34 14
 
35 15
 unsigned long systemTimerGet() {
36
-    return GetTickCount() - systemTimerStart;
16
+    auto tp = std::chrono::steady_clock::now();
17
+    auto dtn = tp.time_since_epoch();
18
+    return (dtn.count() * 1000 * std::chrono::steady_clock::period::num
19
+            / std::chrono::steady_clock::period::den) - systemTimerStart;
37 20
 }
38 21
 
39 22
 void systemTimerReset() {
40
-    systemTimerStart = GetTickCount();
23
+    auto tp = std::chrono::steady_clock::now();
24
+    auto dtn = tp.time_since_epoch();
25
+    systemTimerStart = dtn.count() * 1000 * std::chrono::steady_clock::period::num
26
+            / std::chrono::steady_clock::period::den;
41 27
 }
42 28
 
43
-#else
44
-#error "No support for timer on this platform!"
45
-#endif
46
-

Chargement…
Annuler
Enregistrer