bool Imgui_ImplA5_CreateDeviceObjects()
{
    ImGuiIO &io = ImGui::GetIO();

    // Build texture
    unsigned char *pixels;
    int width, height;
    io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);

    // Create texture
    int flags = al_get_new_bitmap_flags();
    int fmt = al_get_new_bitmap_format();
    al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP|ALLEGRO_MIN_LINEAR|ALLEGRO_MAG_LINEAR);
    al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
    ALLEGRO_BITMAP* img = al_create_bitmap(width, height);
    al_set_new_bitmap_flags(flags);
    al_set_new_bitmap_format(fmt);
    if (!img) 
        return false;

    ALLEGRO_LOCKED_REGION *locked_img = al_lock_bitmap(img, al_get_bitmap_format(img), ALLEGRO_LOCK_WRITEONLY);
    if (!locked_img) 
    {
        al_destroy_bitmap(img);
        return false;
    }
    memcpy(locked_img->data, pixels, sizeof(int)*width*height);
    al_unlock_bitmap(img);

    // Convert software texture to hardware texture.
    ALLEGRO_BITMAP* cloned_img = al_clone_bitmap(img);
    al_destroy_bitmap(img);
    if (!cloned_img) 
        return false;

    // Store our identifier
    io.Fonts->TexID = (void*)cloned_img;
    g_Texture = cloned_img;

    // Cleanup (don't clear the input data if you want to append new fonts later)
    io.Fonts->ClearInputData();
    io.Fonts->ClearTexData();

    // Create an invisible mouse cursor
    // Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
    ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8,8);
    g_MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0);
    al_destroy_bitmap(mouse_cursor);

    return true;
}
Beispiel #2
0
void Mouse::SetImage(Sprite* image, int focusX, int focusY) {
    if(_image) {
        al_destroy_mouse_cursor(_cursor);
    }
    if(image) {
        delete _image;
        _image = nullptr;
        _image = Sprite::CreateSprite(*image);
    } else {
        focusX = 0;
        focusY = 0;
    }
    _focus = Vector2D(focusX, focusY);
    if(image) {
        _cursor = al_create_mouse_cursor(_image->GetImage(), _focus.GetX(), _focus.GetY());
    } else {
        al_set_system_mouse_cursor(_parent_display, _cursor_id);
    }
}
Beispiel #3
0
int main(int argc, char** argv)
{
    time_t t;

    srand((unsigned) time(&t));

    if (!al_init())
    {
        std::cout << "Failed to start Allegro.";
        return -1;
    }

    if (!al_init_image_addon())
    {
        std::cout << "Failed to start Allegro Imagea person who gives information to the police or to some other authority about the bad behavior or criminal activity of someone else Addon.\n";
        return -1;
    }

    if (!al_init_primitives_addon())
    {
        std::cout << "Failed to start Allegro Primitives Addon.\n";
        return -1;
    }

    if (!al_init_font_addon())
    {
        std::cout << "Failed to start Allegro Font Addon.\n";
        return -1;
    }

    if (!al_init_ttf_addon())
    {
        std::cout << "Failed to start Allegro TTF Addon.\n";
        return -1;
    }

    ALLEGRO_DISPLAY* main_window = al_create_display(800, 600);
    if (!main_window)
    {
        std::cout << "Failed to create display.";
        return -1;
    }

    ALLEGRO_EVENT_QUEUE* eq = al_create_event_queue();
    if (!eq)
    {
        std::cout << "Failed to create event queue.";
        return -1;
    }

    ALLEGRO_TIMER* fps_timer = al_create_timer(1.0 / 60);
    if (!fps_timer)
    {
        std::cout << "Failed to create timer.";
        return -1;
    }

    ALLEGRO_TIMER* count_timer = al_create_timer(1.0);
    if (!count_timer)
    {
        std::cout << "Failed to create count timer.";
        return -1;
    }

    if (!al_install_keyboard())
    {
        std::cout << "Failed to install keyboard.";
        return -1;
    }


    if (!al_install_mouse() || !al_set_mouse_cursor(main_window, al_create_mouse_cursor(al_load_bitmap("resources/sprites/UI/cursor/clicker.png"), 16, 31)))
    {
        std::cout << "Failed to install mouse.";
        return -1;
    }


    al_register_event_source(eq, al_get_timer_event_source(fps_timer));
    al_register_event_source(eq, al_get_timer_event_source(count_timer));
    al_register_event_source(eq, al_get_keyboard_event_source());
    al_register_event_source(eq, al_get_mouse_event_source());
    al_register_event_source(eq, al_get_display_event_source(main_window));

    Game base;
    if (!base.init())
    {
        std::cout << "Failed to initialise game! Quitting...\n";
        return -1;
    }

    bool ready_to_draw = false;
    bool ready_to_draw_fps = false;
    al_start_timer(fps_timer);
    al_start_timer(count_timer);
    int fps_count = 0;

    ALLEGRO_FONT* fps_font = al_load_ttf_font("resources/fonts/MontereyFLF.ttf", 11, 0);
    if (!fps_font)
    {
        std::cout << "Failed to load font for fps counter!";
        return -1;
    }

    while (true)
    {
        while (!al_event_queue_is_empty(eq))
        {
            ALLEGRO_EVENT ev;
            al_get_next_event(eq, &ev);
            if (ev.type == ALLEGRO_EVENT_TIMER)
            {
                if (ev.timer.source == fps_timer)
                {
                    ready_to_draw = true;
                }
                else if (ev.timer.source == count_timer)
                {
                    ready_to_draw_fps = true;
                }
            }
            else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
            {
                std::cout << "Safely quitting.";
                al_destroy_display(main_window);
                al_destroy_event_queue(eq);
                al_destroy_timer(fps_timer);
                return 0;
            }
            else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) //current keyboard input section
                {
                    std::cout << "Safely quitting.";
                    al_destroy_display(main_window);
                    al_destroy_event_queue(eq);
                    al_destroy_timer(fps_timer);
                    return 0;
                }
            }
            base.runEvents(ev);
        }

        if (ready_to_draw)
        {
            fps_count++;
            base.runLogic();
            base.runDisplay();
            char buffer[25];
            if (ready_to_draw_fps)
            {
                sprintf(buffer, "%d", fps_count);
                fps_count = 0;
                ready_to_draw_fps = false;
            }
            al_draw_text(fps_font, al_map_rgb(0,255,0), 5, 5, 0, buffer);
            al_flip_display();
            ready_to_draw = false;
        }

    }
}