cpu start
This commit is contained in:
@@ -5,11 +5,46 @@
|
||||
#include "imgui_impl_sdl2.h"
|
||||
#include "imgui_impl_sdlsurface2.h"
|
||||
#include <SDL.h>
|
||||
#include <chrono>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include "../libs/emscripten/emscripten_mainloop_stub.h"
|
||||
#endif
|
||||
|
||||
struct ExampleTimingHistory
|
||||
{
|
||||
double Samples[120];
|
||||
int Count;
|
||||
int Offset;
|
||||
|
||||
ExampleTimingHistory() : Count(0), Offset(0) { memset(Samples, 0, sizeof(Samples)); }
|
||||
|
||||
void AddSample(double value_ms)
|
||||
{
|
||||
Samples[Offset] = value_ms;
|
||||
Offset = (Offset + 1) % IM_ARRAYSIZE(Samples);
|
||||
if (Count < IM_ARRAYSIZE(Samples))
|
||||
Count++;
|
||||
}
|
||||
|
||||
double GetAverageMs() const
|
||||
{
|
||||
if (Count == 0)
|
||||
return 0.0;
|
||||
double total = 0.0;
|
||||
for (int i = 0; i < Count; i++)
|
||||
total += Samples[i];
|
||||
return total / (double)Count;
|
||||
}
|
||||
};
|
||||
|
||||
static double ExampleGetTimeMs()
|
||||
{
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
return std::chrono::duration<double, std::milli>(Clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
// Setup SDL
|
||||
@@ -79,6 +114,8 @@ int main(int, char**)
|
||||
bool show_demo_window = true;
|
||||
bool show_another_window = false;
|
||||
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
ExampleTimingHistory raster_times;
|
||||
ExampleTimingHistory present_times;
|
||||
|
||||
// Main loop
|
||||
bool done = false;
|
||||
@@ -133,6 +170,10 @@ int main(int, char**)
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
int logical_w = 0;
|
||||
int logical_h = 0;
|
||||
SDL_GetWindowSize(window, &logical_w, &logical_h);
|
||||
|
||||
// 1. Show the big demo window
|
||||
if (show_demo_window)
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
@@ -156,6 +197,8 @@ int main(int, char**)
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("counter = %d", counter);
|
||||
|
||||
ImGui::Text("Raster %.3f ms | Present %.3f ms", raster_times.GetAverageMs(), present_times.GetAverageMs());
|
||||
ImGui::Text("Window %d x %d | Framebuffer %d x %d", logical_w, logical_h, framebuffer->w, framebuffer->h);
|
||||
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::End();
|
||||
}
|
||||
@@ -173,6 +216,7 @@ int main(int, char**)
|
||||
// Rendering
|
||||
ImGui::Render();
|
||||
|
||||
const double raster_start_ms = ExampleGetTimeMs();
|
||||
SDL_FillRect(framebuffer, nullptr, SDL_MapRGBA(framebuffer->format,
|
||||
(Uint8)(clear_color.x * 255),
|
||||
(Uint8)(clear_color.y * 255),
|
||||
@@ -180,9 +224,12 @@ int main(int, char**)
|
||||
(Uint8)(clear_color.w * 255)));
|
||||
|
||||
ImGui_ImplSDLSurface2_RenderDrawData(ImGui::GetDrawData());
|
||||
raster_times.AddSample(ExampleGetTimeMs() - raster_start_ms);
|
||||
|
||||
const double present_start_ms = ExampleGetTimeMs();
|
||||
SDL_BlitSurface(framebuffer, nullptr, window_surface, nullptr);
|
||||
SDL_UpdateWindowSurface(window);
|
||||
present_times.AddSample(ExampleGetTimeMs() - present_start_ms);
|
||||
}
|
||||
#ifdef __EMSCRIPTEN__
|
||||
EMSCRIPTEN_MAINLOOP_END;
|
||||
|
||||
Reference in New Issue
Block a user