| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- /*
- * This source file is part of RmlUi, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://github.com/mikke89/RmlUi
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- * Copyright (c) 2019 The RmlUi Team, and contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include <RmlUi/Core.h>
- #include <RmlUi/Debugger.h>
- #include <RmlUi_Backend.h>
- #include <Shell.h>
- class DemoWindow
- {
- public:
- DemoWindow(const Rml::String &title, Rml::Context *context)
- {
- using namespace Rml;
- document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
- if (document != nullptr)
- {
- {
- document->GetElementById("title")->SetInnerRML(title);
- }
- document->Show();
- }
- }
- void performance_test()
- {
- RMLUI_ZoneScoped;
- if (!document)
- return;
- Rml::String rml;
- for (int i = 0; i < 50; i++)
- {
- int index = rand() % 1000;
- int route = rand() % 50;
- int max = (rand() % 40) + 10;
- int value = rand() % max;
- Rml::String rml_row = Rml::CreateString(1000, R"(
- <div class="row">
- <div class="col col1"><button class="expand" index="%d">+</button> <a>Route %d</a></div>
- <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
- <div class="col col4">Assigned</div>
- <select>
- <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
- </select>
- <div class="inrow unmark_collapse">
- <div class="col col123 assign_text">Assign to route</div>
- <div class="col col4">
- <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
- </div>
- </div>
- </div>)",
- index,
- route,
- max,
- value
- );
- rml += rml_row;
- }
- if (auto el = document->GetElementById("performance"))
- el->SetInnerRML(rml);
- }
- class SimpleEventListener : public Rml::EventListener {
- public:
- void ProcessEvent(Rml::Event& event) override {
- static int i = 0;
- event.GetTargetElement()->SetProperty("background-color", i++ % 2 == 0 ? "green" : "orange");
- }
- } simple_event_listener;
- ~DemoWindow()
- {
- if (document)
- {
- document->Close();
- }
- }
- Rml::ElementDocument * GetDocument() {
- return document;
- }
- private:
- Rml::ElementDocument *document;
- };
- bool run_loop = true;
- bool single_loop = true;
- bool run_update = true;
- bool single_update = true;
- class Event : public Rml::EventListener
- {
- public:
- Event(const Rml::String& value) : value(value) {}
- void ProcessEvent(Rml::Event& event) override
- {
- using namespace Rml;
- if(value == "exit")
- Backend::RequestExit();
- if (event == "keydown")
- {
- auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter< int >("key_identifier", 0);
- if (key_identifier == Rml::Input::KI_SPACE)
- {
- run_loop = !run_loop;
- }
- else if (key_identifier == Rml::Input::KI_DOWN)
- {
- run_loop = false;
- single_loop = true;
- }
- else if (key_identifier == Rml::Input::KI_RIGHT)
- {
- run_update = false;
- single_update = true;
- }
- else if (key_identifier == Rml::Input::KI_RETURN)
- {
- run_update = !run_update;
- }
- else if (key_identifier == Rml::Input::KI_ESCAPE)
- {
- Backend::RequestExit();
- }
- }
- }
- void OnDetach(Rml::Element* /*element*/) override { delete this; }
- private:
- Rml::String value;
- };
- class EventInstancer : public Rml::EventListenerInstancer
- {
- public:
- /// Instances a new event handle for Invaders.
- Rml::EventListener* InstanceEventListener(const Rml::String& value, Rml::Element* /*element*/) override
- {
- return new Event(value);
- }
- };
- #if defined RMLUI_PLATFORM_WIN32
- #include <RmlUi_Include_Windows.h>
- int APIENTRY WinMain(HINSTANCE /*instance_handle*/, HINSTANCE /*previous_instance_handle*/, char* /*command_line*/, int /*command_show*/)
- #else
- int main(int /*argc*/, char** /*argv*/)
- #endif
- {
- const int window_width = 1800;
- const int window_height = 1000;
- // Initializes the shell which provides common functionality used by the included samples.
- if (!Shell::Initialize())
- return -1;
- // Constructs the system and render interfaces, creates a window, and attaches the renderer.
- if (!Backend::Initialize("Benchmark Sample", window_width, window_height, true))
- {
- Shell::Shutdown();
- return -1;
- }
- // Install the custom interfaces constructed by the backend before initializing RmlUi.
- Rml::SetSystemInterface(Backend::GetSystemInterface());
- Rml::SetRenderInterface(Backend::GetRenderInterface());
- // RmlUi initialisation.
- Rml::Initialise();
- // Create the main RmlUi context.
- Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
- if (!context)
- {
- Rml::Shutdown();
- Backend::Shutdown();
- Shell::Shutdown();
- return -1;
- }
- Rml::Debugger::Initialise(context);
- EventInstancer event_listener_instancer;
- Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
- Shell::LoadFonts();
- Rml::UniquePtr<DemoWindow> window = Rml::MakeUnique<DemoWindow>("Benchmark sample", context);
- window->GetDocument()->AddEventListener(Rml::EventId::Keydown, new Event("hello"));
- window->GetDocument()->AddEventListener(Rml::EventId::Keyup, new Event("hello"));
- window->GetDocument()->AddEventListener(Rml::EventId::Animationend, new Event("hello"));
- bool running = true;
- while (running)
- {
- running = Backend::ProcessEvents(context, &Shell::ProcessKeyDownShortcuts);
- if (run_update || single_update)
- {
- single_update = false;
- window->performance_test();
- }
- if (run_loop || single_loop)
- {
- single_loop = false;
- context->Update();
- Backend::BeginFrame();
- context->Render();
- Backend::PresentFrame();
- }
- static constexpr int buffer_size = 200;
- static float fps_buffer[buffer_size] = {};
- static int buffer_index = 0;
- static double t_prev = 0.0f;
- double t = Rml::GetSystemInterface()->GetElapsedTime();
- float dt = float(t - t_prev);
- t_prev = t;
- static int count_frames = 0;
- count_frames += 1;
- float fps = 1.0f / dt;
- fps_buffer[buffer_index] = fps;
- buffer_index = ((buffer_index + 1) % buffer_size);
- if (window && count_frames > buffer_size / 8)
- {
- float fps_mean = 0;
- for (int i = 0; i < buffer_size; i++)
- fps_mean += fps_buffer[(buffer_index + i) % buffer_size];
- fps_mean = fps_mean / (float)buffer_size;
- auto el = window->GetDocument()->GetElementById("fps");
- count_frames = 0;
- el->SetInnerRML(Rml::CreateString(20, "FPS: %f", fps_mean));
- }
- }
- window.reset();
- // Shutdown RmlUi.
- Rml::Shutdown();
- Backend::Shutdown();
- Shell::Shutdown();
- return 0;
- }
|