Dreikblack Posted February 27 Share Posted February 27 #include "UltraEngine.h" using namespace UltraEngine; shared_ptr<Window> window; shared_ptr<Framebuffer> framebuffer; shared_ptr<World> world; shared_ptr<Interface> ui; shared_ptr<Camera> uiCamera; shared_ptr<Widget> btn; static bool EventCallback(const Event& e, shared_ptr<Object> extra) { Print("Callback"); return true; } void initGui() { auto default_font = LoadFont("Fonts\\arial.ttf"); ui = CreateInterface(world, default_font, framebuffer->GetSize()); ui->SetRenderLayers(2); ui->root->SetColor(0.0f, 0.0f, 0.0f, 0.0f); uiCamera = CreateCamera(world, PROJECTION_ORTHOGRAPHIC); uiCamera->SetPosition((float)framebuffer->GetSize().x * 0.5f, (float)framebuffer->GetSize().y * 0.5f, 0); uiCamera->SetRenderLayers(2); uiCamera->SetClearMode(CLEAR_DEPTH); btn = CreateButton("Btn", 20, 20, 100, 100, ui->root); } int main(int argc, const char* argv[]) { //Get the displays auto displays = GetDisplays(); //Create a window window = CreateWindow("Ultra Engine", 0, 0, 300, 300, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR); //Create a world world = CreateWorld(); //Create a framebuffer framebuffer = CreateFramebuffer(window); //Create a camera auto camera = CreateCamera(world); camera->SetClearColor(0, 0, 0); initGui(); ListenEvent(EVENT_DATA, NULL, EventCallback, btn); //Main loop while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false) { if (window->KeyDown(KEY_SPACE)) { Print(btn.use_count()); EmitEvent(EVENT_DATA, btn); Print(btn.use_count()); } world->Update(); world->Render(framebuffer); } return 0; } Quote Link to comment Share on other sites More sharing options...
Solution Josh Posted March 12 Solution Share Posted March 12 This makes sense. The button is being stored as part of the emitted event. You can clear the events in the main loop to get rid of them like this: //Main loop while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false) { while (PeekEvent()) WaitEvent();// Clears event queue if (window->KeyDown(KEY_SPACE)) { Print(int(btn.use_count())); EmitEvent(EVENT_DATA, btn); Print(int(btn.use_count())); } world->Update(); world->Render(framebuffer); } Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Dreikblack Posted March 12 Author Share Posted March 12 Should not ListenEvent clear event then? Also i would prefer events to keep weak_ptr. I have PeekEvent and WaitEvent in main project and still had some problems with extra shared_ptr held by events. I will investigate again what exact problems i had with it later to make new example in case if it can't be fixed with current engine build. Quote Link to comment Share on other sites More sharing options...
Dreikblack Posted March 12 Author Share Posted March 12 Tried to do clearing - still does not look right. Not increasing now every time but still to many extra uses. #include "UltraEngine.h" using namespace UltraEngine; shared_ptr<Window> window; shared_ptr<Framebuffer> framebuffer; shared_ptr<World> world; shared_ptr<Interface> ui; shared_ptr<Camera> uiCamera; shared_ptr<Widget> btn; static bool EventCallback(const Event& e, shared_ptr<Object> extra) { Print("In callback:" + WString(btn.use_count())); return true; } void initGui() { auto default_font = LoadFont("Fonts\\arial.ttf"); ui = CreateInterface(world, default_font, framebuffer->GetSize()); ui->SetRenderLayers(2); ui->root->SetColor(0.0f, 0.0f, 0.0f, 0.0f); uiCamera = CreateCamera(world, PROJECTION_ORTHOGRAPHIC); uiCamera->SetPosition((float)framebuffer->GetSize().x * 0.5f, (float)framebuffer->GetSize().y * 0.5f, 0); uiCamera->SetRenderLayers(2); uiCamera->SetClearMode(CLEAR_DEPTH); btn = CreateButton("Btn", 20, 20, 100, 100, ui->root); } int main(int argc, const char* argv[]) { //Get the displays auto displays = GetDisplays(); //Create a window window = CreateWindow("Ultra Engine", 0, 0, 300, 300, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR); //Create a world world = CreateWorld(); //Create a framebuffer framebuffer = CreateFramebuffer(window); //Create a camera auto camera = CreateCamera(world); camera->SetClearColor(0, 0, 0); initGui(); ListenEvent(EVENT_DATA, NULL, EventCallback, btn); //Main loop while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false) { while (PeekEvent()) WaitEvent();// Clears event queue if (window->KeyHit(KEY_SPACE)) { Print("Before:" + WString(btn.use_count())); EmitEvent(EVENT_DATA, btn); Print("After:" + WString(btn.use_count())); } world->Update(); world->Render(framebuffer); } return 0; } Quote Link to comment Share on other sites More sharing options...
Dreikblack Posted March 12 Author Share Posted March 12 Ah, extra 1 is because it's root have it. Maybe it's not big problem as i thought. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 12 Share Posted March 12 Mark as solution when you feel it is resolved. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Dreikblack Posted March 12 Author Share Posted March 12 It's not fully resolved but probably it's ok as it is for now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.