Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not move a window #1423

Closed
Jimmyee opened this issue Nov 10, 2017 · 2 comments
Closed

Could not move a window #1423

Jimmyee opened this issue Nov 10, 2017 · 2 comments

Comments

@Jimmyee
Copy link

Jimmyee commented Nov 10, 2017

Hello again.
This time I'm here to show you an issue I keep having while using ImGUI with Allegro. I just can't move the window I have created around the screen.

Here's the code I'm using for main function and the window I execute:

// ImGui - standalone example application for Allegro 5
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.

#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <imgui.h>
#include "imgui_impl_a5.h"
#include <iostream>
#include <fstream>
#include <stdexcept>

#include "singleton.hpp"
#include "const.hpp"
#include "util.hpp"

int main(int, char**)
{
    const float FPS = 60.0f;
    std::array<int, 3> version = { 0, 0, 1 };

    std::cout << "Endless Online Awaken Client v" << version[0] << "." << version[1] << "." << version[2] << std::endl
    << std::endl;

    S &s = S::GetInstance();

    try
    {
        s.config = std::make_unique<Config>("./data/config.ini");

        std::vector<std::string> args = util::GetArgs(s.config->GetValue("Resolution"));
        int screen_width = std::atoi(args[0].c_str());
        int screen_height = std::atoi(args[1].c_str());

        int flags = s.config->GetValue("Fullscreen") == "yes"? ALLEGRO_FULLSCREEN : ALLEGRO_WINDOWED;

        al_init();
        al_install_keyboard();
        al_install_mouse();
        al_init_primitives_addon();
        al_init_image_addon();

        al_set_new_display_flags(flags);
        s.display = al_create_display(screen_width, screen_height);
        if(!s.display)
        {
            throw std::runtime_error("Could not create display!");
        }
        al_set_window_title(s.display, "game engine");

        s.event_queue = al_create_event_queue();
        if(!s.event_queue)
        {
            throw std::runtime_error("Could not create event queue!");
        }

        s.timer = al_create_timer(1.0 / FPS);
        if(!s.timer)
        {
            throw std::runtime_error("Could not create FPS timer!");
        }
        al_register_event_source(s.event_queue, al_get_display_event_source(s.display));
        al_register_event_source(s.event_queue, al_get_keyboard_event_source());
        al_register_event_source(s.event_queue, al_get_mouse_event_source());
        al_register_event_source(s.event_queue, al_get_timer_event_source(s.timer));
        al_start_timer(s.timer);

        ImGui_ImplA5_Init(s.display);

        float sx = screen_width / (float)640;
        float sy = screen_height / (float)480;

        ALLEGRO_TRANSFORM trans;
        al_identity_transform(&trans);
        al_scale_transform(&trans, sx, sy);
        al_use_transform(&trans);

        ALLEGRO_COLOR clear_color = al_map_rgb(11, 11, 11);

        s.gfx_loader = std::make_unique<GFXLoader>("./data/gfx/");
        s.gui = std::make_unique<GUI>();

        while (!s.call_exit)
        {
            ALLEGRO_EVENT event;
            ALLEGRO_TIMEOUT timeout;
            al_init_timeout(&timeout, 0.06);
            bool redraw = false;

            if(s.client.get()) s.client->Tick();

            bool get_event = al_wait_for_event_until(s.event_queue, &event, &timeout);
            if(get_event)
            {
                ImGui_ImplA5_ProcessEvent(&event);
                if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) S::GetInstance().call_exit = true;
                if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE)
                {
                    ImGui_ImplA5_InvalidateDeviceObjects();
                    al_acknowledge_resize(s.display);
                    Imgui_ImplA5_CreateDeviceObjects();
                }

                if(event.type == ALLEGRO_EVENT_KEY_DOWN || event.type == ALLEGRO_EVENT_KEY_UP)
                {
                    s.input_handler.Process(event);
                }

                if(event.type == ALLEGRO_EVENT_TIMER)
                {
                    redraw = true;
                }
            }

            ImGui_ImplA5_NewFrame();
            s.gui->Process();

            if(redraw && al_is_event_queue_empty(s.event_queue))
            {
                al_clear_to_color(clear_color);
                if(s.map.get()) s.map->Render();
                s.gui->Render();
                al_flip_display();
            }
        }

        ImGui_ImplA5_Shutdown();
        al_destroy_event_queue(s.event_queue);
        al_destroy_display(s.display);
    }
    catch(std::runtime_error rt_err)
    {
        std::cout << "Runtime error: " << rt_err.what() << std::endl;
        std::ofstream file("errorlog.txt", std::ios::trunc);
        if(file.is_open() && file.good())
        {
            file << rt_err.what() << std::endl;
            file.close();
        }
    }

    return 0;
}

// and how I execute the window (inside of s.gui->Process()):
void MapEditor::GUI()
{
    S &s = S::GetInstance();

    if(!ImGui::Begin("Map editor", 0, ImGuiWindowFlags_MenuBar)) { return; }

    if(ImGui::BeginMenuBar())
    {
        if(ImGui::BeginMenu("File"))
        {
            if(ImGui::MenuItem("New"))
            {
                ImGui::OpenPopup("FilePopup");

                if (ImGui::BeginPopup("FilePopup"))
                {
                    ImGui::Button("Test");
                    ImGui::EndPopup();
                }
            }

            if(ImGui::MenuItem("Load...")) {}
            if(ImGui::MenuItem("Save...")) {}
            if(ImGui::MenuItem("Save As...")) {}
            if(ImGui::MenuItem("Exit")) {}

            ImGui::EndMenu();
        }

        ImGui::EndMenuBar();
    }

    ImGui::End();
}

@ocornut
Copy link
Owner

ocornut commented Nov 10, 2017 via email

@Jimmyee
Copy link
Author

Jimmyee commented Nov 10, 2017

Thanks, it works correct now.

@ocornut ocornut closed this as completed Nov 10, 2017
ocornut added a commit that referenced this issue Nov 19, 2017
…. Exposed EndFrame(). Made it legal to call EndFrame() more than one. (#1423 etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants