mirror of
https://gitlab.com/dosowisko.net/libsuperderpy.git
synced 2024-12-05 00:38:00 +01:00
Dear ImGui integration
This commit is contained in:
parent
52030b67e3
commit
440e6c41e6
13 changed files with 890 additions and 12 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "src/3rdparty/cimgui"]
|
||||
path = src/3rdparty/cimgui
|
||||
url = https://gitlab.com/dosowisko.net/cimgui.git
|
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
project(libsuperderpy C)
|
||||
project(libsuperderpy C CXX)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
A multiplatform game engine for games written in C and Allegro 5. Supports GNU/Linux, Android, FreeBSD, macOS, Windows and asm.js
|
||||
|
||||
Used by such games as:
|
||||
- [Animatch](https://gitlab.com/HolyPangolin/animatch)
|
||||
- [Spray Cheese](https://gitlab.com/dosowisko.net/spraycheese)
|
||||
- [ODLOT](https://gitlab.com/HolyPangolin/odlot)
|
||||
- Rumina
|
||||
- [Non-Competitive Singing Potatoes](https://github.com/dos1/potatoes)
|
||||
|
|
|
@ -8,18 +8,29 @@ if (NOT LIBSUPERDERPY_CONFIG_INCLUDED)
|
|||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED true)
|
||||
set(CMAKE_CXX_STANDARD 98)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -ffast-math")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -ffast-math")
|
||||
|
||||
if(MAEMO5)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")
|
||||
endif(MAEMO5)
|
||||
|
||||
if(ANDROID)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
|
||||
endif(ANDROID)
|
||||
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O1 -fno-optimize-sibling-calls -fno-omit-frame-pointer -fsanitize=leak -DLEAK_SANITIZER=1 -fno-common -fsanitize-recover=all")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O1 -fno-optimize-sibling-calls -fno-omit-frame-pointer -fsanitize=leak -DLEAK_SANITIZER=1 -fno-common -fsanitize-recover=all")
|
||||
|
||||
if(APPLE)
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-undefined,error")
|
||||
else(APPLE)
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
|
||||
endif(APPLE)
|
||||
|
||||
set(USE_CLANG_TIDY "yes" CACHE STRING "Analyze the code with clang-tidy" )
|
||||
option(USE_CLANG_TIDY "Analyze the code with clang-tidy" ON)
|
||||
if(USE_CLANG_TIDY AND NOT MINGW)
|
||||
find_program(CLANG_TIDY_EXE NAMES "clang-tidy" DOC "Path to clang-tidy executable")
|
||||
if(NOT CLANG_TIDY_EXE)
|
||||
|
@ -29,13 +40,18 @@ if (NOT LIBSUPERDERPY_CONFIG_INCLUDED)
|
|||
endif()
|
||||
endif()
|
||||
|
||||
set(LIBSUPERDERPY_STATIC "no" CACHE STRING "Compile and link libsuperderpy as a static library." )
|
||||
option(LIBSUPERDERPY_STATIC "Compile and link libsuperderpy as a static library." OFF)
|
||||
|
||||
set(LIBSUPERDERPY_STATIC_DEPS "no" CACHE STRING "Link dependencies (e.g. Allegro) statically." )
|
||||
option(LIBSUPERDERPY_STATIC_DEPS "Link dependencies (e.g. Allegro) statically." OFF)
|
||||
if(LIBSUPERDERPY_STATIC_DEPS)
|
||||
SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a)
|
||||
endif(LIBSUPERDERPY_STATIC_DEPS)
|
||||
|
||||
option(LIBSUPERDERPY_IMGUI "Compile with Dear ImGui support." OFF)
|
||||
if (LIBSUPERDERPY_IMGUI)
|
||||
add_definitions(-DLIBSUPERDERPY_IMGUI)
|
||||
endif (LIBSUPERDERPY_IMGUI)
|
||||
|
||||
execute_process(
|
||||
COMMAND git rev-parse --short HEAD
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/libsuperderpy
|
||||
|
@ -91,7 +107,7 @@ if (NOT LIBSUPERDERPY_CONFIG_INCLUDED)
|
|||
|
||||
set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib/${LIBSUPERDERPY_GAMENAME}:\$ORIGIN/gamestates:\$ORIGIN:\$ORIGIN/../lib:\$ORIGIN/lib:\$ORIGIN/bin")
|
||||
|
||||
if(EMSCRIPTEN)
|
||||
if(EMSCRIPTEN)
|
||||
set(CMAKE_EXECUTABLE_SUFFIX ".bc")
|
||||
set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
|
||||
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s SIDE_MODULE=1")
|
||||
|
@ -111,7 +127,7 @@ if(EMSCRIPTEN)
|
|||
set(CMAKE_SHARED_MODULE_SUFFIX ".js")
|
||||
endif()
|
||||
|
||||
set(LIBSUPERDERPY_USE_WEBGL2 NO CACHE BOOL "Use WebGL 2 context")
|
||||
option(LIBSUPERDERPY_USE_WEBGL2 "Use WebGL 2 context" OFF)
|
||||
if(LIBSUPERDERPY_USE_WEBGL2)
|
||||
set(EMSCRIPTEN_FLAGS ${EMSCRIPTEN_FLAGS} -s USE_WEBGL2=1)
|
||||
endif(LIBSUPERDERPY_USE_WEBGL2)
|
||||
|
@ -123,13 +139,13 @@ if(EMSCRIPTEN)
|
|||
set(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}")
|
||||
set(SHARE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
|
||||
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(ANDROID OR EMSCRIPTEN)
|
||||
add_definitions(-DLIBSUPERDERPY_SINGLE_THREAD=1)
|
||||
endif()
|
||||
if(ANDROID OR EMSCRIPTEN)
|
||||
add_definitions(-DLIBSUPERDERPY_SINGLE_THREAD)
|
||||
endif()
|
||||
|
||||
find_package(Allegro5 REQUIRED)
|
||||
find_package(Allegro5 REQUIRED)
|
||||
find_package(Allegro5Font REQUIRED)
|
||||
find_package(Allegro5TTF REQUIRED)
|
||||
find_package(Allegro5Primitives REQUIRED)
|
||||
|
|
|
@ -17,6 +17,10 @@ set(CMAKE_C_COMPILER ${OSXCROSS_PATH}/bin/o64-clang)
|
|||
set(CMAKE_CXX_COMPILER ${OSXCROSS_PATH}/bin/o64-clang++)
|
||||
set(CMAKE_LINKER ${OSXCROSS_PATH}/bin/x86_64-apple-darwin15-ld)
|
||||
set(CMAKE_AR ${OSXCROSS_PATH}/bin/x86_64-apple-darwin15-ar)
|
||||
set(CMAKE_RANLIB ${OSXCROSS_PATH}/bin/x86_64-apple-darwin15-ranlib)
|
||||
set(CMAKE_STRIP ${OSXCROSS_PATH}/bin/x86_64-apple-darwin15-strip)
|
||||
set(CMAKE_NM ${OSXCROSS_PATH}/bin/x86_64-apple-darwin15-nm)
|
||||
set(CMAKE_INSTALL_NAME_TOOL ${OSXCROSS_PATH}/bin/x86_64-apple-darwin15-install_name_tool)
|
||||
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
|
|
1
src/3rdparty/cimgui
vendored
Submodule
1
src/3rdparty/cimgui
vendored
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit e5ac5a203bbf47e120ef4cd773071f4d7274a70d
|
|
@ -16,9 +16,17 @@ if (EMSCRIPTEN)
|
|||
list(APPEND SRC_LIST emscripten-audio-stream.c)
|
||||
endif()
|
||||
|
||||
if (LIBSUPERDERPY_IMGUI)
|
||||
set(IMGUI_STATIC ON CACHE STRING "" FORCE)
|
||||
add_subdirectory(3rdparty/cimgui EXCLUDE_FROM_ALL)
|
||||
set_property(TARGET "cimgui" PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
list(APPEND SRC_LIST imgui/imgui_impl_allegro5.c)
|
||||
endif (LIBSUPERDERPY_IMGUI)
|
||||
|
||||
if (LIBSUPERDERPY_STATIC)
|
||||
add_library("libsuperderpy" STATIC ${SRC_LIST})
|
||||
set_property(TARGET libsuperderpy PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
set_property(TARGET "libsuperderpy" PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
else (LIBSUPERDERPY_STATIC)
|
||||
add_library("libsuperderpy" SHARED ${SRC_LIST})
|
||||
endif (LIBSUPERDERPY_STATIC)
|
||||
|
@ -27,6 +35,10 @@ SET_TARGET_PROPERTIES("libsuperderpy" PROPERTIES PREFIX "")
|
|||
|
||||
target_link_libraries("libsuperderpy" ${ALLEGRO5_LIBRARIES} ${ALLEGRO5_FONT_LIBRARIES} ${ALLEGRO5_TTF_LIBRARIES} ${ALLEGRO5_PRIMITIVES_LIBRARIES} ${ALLEGRO5_AUDIO_LIBRARIES} ${ALLEGRO5_ACODEC_LIBRARIES} ${ALLEGRO5_VIDEO_LIBRARIES} ${ALLEGRO5_IMAGE_LIBRARIES} m dl)
|
||||
|
||||
if (LIBSUPERDERPY_IMGUI)
|
||||
target_link_libraries("libsuperderpy" cimgui)
|
||||
endif (LIBSUPERDERPY_IMGUI)
|
||||
|
||||
if (ANDROID)
|
||||
target_link_libraries("libsuperderpy" log)
|
||||
endif (ANDROID)
|
||||
|
|
380
src/imgui/imgui_impl_allegro5.c
Normal file
380
src/imgui/imgui_impl_allegro5.c
Normal file
|
@ -0,0 +1,380 @@
|
|||
// dear imgui: Renderer + Platform Binding for Allegro 5
|
||||
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
|
||||
// [X] Platform: Clipboard support (from Allegro 5.1.12)
|
||||
// [X] Platform: Mouse cursor shape and visibility. Disable with 'io->ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||
// Issues:
|
||||
// [ ] Renderer: The renderer is suboptimal as we need to unindex our buffers and convert vertices manually.
|
||||
// [ ] Platform: Missing gamepad support.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
||||
// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
|
||||
// https://github.com/ocornut/imgui, Original Allegro 5 code by @birthggd
|
||||
|
||||
// CHANGELOG
|
||||
// (minor and older changes stripped away, please see git history for details)
|
||||
// 2018-06-13: Platform: Added clipboard support (from Allegro 5.1.12).
|
||||
// 2018-06-13: Renderer: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||
// 2018-06-13: Renderer: Backup/restore transform and clipping rectangle.
|
||||
// 2018-06-11: Misc: Setup io->BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
|
||||
// 2018-04-18: Misc: Renamed file from imgui_impl_a5.cpp to imgui_impl_allegro5.cpp.
|
||||
// 2018-04-18: Misc: Added support for 32-bits vertex indices to avoid conversion at runtime. Added imconfig_allegro5.h to enforce 32-bit indices when included from imgui.h.
|
||||
// 2018-02-16: Misc: Obsoleted the io->RenderDrawListsFn callback and exposed ImGui_ImplAllegro5_RenderDrawData() in the .h file so you can call it yourself.
|
||||
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
||||
|
||||
#include "imgui/imgui_impl_allegro5.h"
|
||||
#include "internal.h"
|
||||
#include <float.h>
|
||||
#include <stdint.h> // uint64_t
|
||||
#include <string.h> // memcpy
|
||||
|
||||
#define ALLEGRO_HAS_CLIPBOARD (ALLEGRO_VERSION_INT >= ((5 << 24) | (1 << 16) | (12 << 8))) // Clipboard only supported from Allegro 5.1.12
|
||||
|
||||
// Visual Studio warnings
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable : 4127) // condition expression is constant
|
||||
#endif
|
||||
|
||||
// Data
|
||||
static ALLEGRO_DISPLAY* g_Display = NULL;
|
||||
static ALLEGRO_BITMAP* g_Texture = NULL;
|
||||
static double g_Time = 0.0;
|
||||
static ALLEGRO_MOUSE_CURSOR* g_MouseCursorInvisible = NULL;
|
||||
static ALLEGRO_VERTEX_DECL* g_VertexDecl = NULL;
|
||||
static char* g_ClipboardTextData = NULL;
|
||||
|
||||
struct ImDrawVertAllegro {
|
||||
ImVec2 pos;
|
||||
ImVec2 uv;
|
||||
ALLEGRO_COLOR col;
|
||||
};
|
||||
typedef struct ImDrawVertAllegro ImDrawVertAllegro;
|
||||
|
||||
// Render function.
|
||||
// (this used to be set in io->RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
|
||||
SYMBOL_EXPORT void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data) {
|
||||
// Backup Allegro state that will be modified
|
||||
ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
|
||||
ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform();
|
||||
int last_clip_x, last_clip_y, last_clip_w, last_clip_h;
|
||||
al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h);
|
||||
int last_blender_op, last_blender_src, last_blender_dst;
|
||||
al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst);
|
||||
|
||||
// Setup render state
|
||||
al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
|
||||
|
||||
// Setup orthographic projection matrix
|
||||
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
|
||||
{
|
||||
float L = draw_data->DisplayPos.x;
|
||||
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||
float T = draw_data->DisplayPos.y;
|
||||
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||
ALLEGRO_TRANSFORM transform;
|
||||
al_identity_transform(&transform);
|
||||
al_use_transform(&transform);
|
||||
al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f);
|
||||
al_use_projection_transform(&transform);
|
||||
}
|
||||
|
||||
for (int n = 0; n < draw_data->CmdListsCount; n++) {
|
||||
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
||||
|
||||
// Allegro's implementation of al_draw_indexed_prim() for DX9 is completely broken. Unindex our buffers ourselves.
|
||||
// FIXME-OPT: Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 float as well..
|
||||
//static ImVector<ImDrawVertAllegro> vertices;
|
||||
ImDrawVertAllegro* vertices = al_malloc(sizeof(ImDrawVertAllegro) * cmd_list->IdxBuffer.Size);
|
||||
for (int i = 0; i < cmd_list->IdxBuffer.Size; i++) {
|
||||
const ImDrawVert* src_v = &cmd_list->VtxBuffer.Data[cmd_list->IdxBuffer.Data[i]];
|
||||
ImDrawVertAllegro* dst_v = &vertices[i];
|
||||
dst_v->pos = src_v->pos;
|
||||
dst_v->uv = src_v->uv;
|
||||
const unsigned char* c = (const unsigned char*)&src_v->col;
|
||||
dst_v->col = al_map_rgba(c[0], c[1], c[2], c[3]);
|
||||
}
|
||||
|
||||
int* indices = NULL;
|
||||
bool cleanup = false;
|
||||
if (sizeof(ImDrawIdx) == sizeof(short)) {
|
||||
// FIXME-OPT: Unfortunately Allegro doesn't support 16-bit indices.. You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices.
|
||||
// Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful.
|
||||
int* indices_converted = al_malloc(sizeof(int) * cmd_list->IdxBuffer.Size);
|
||||
for (int i = 0; i < cmd_list->IdxBuffer.Size; ++i) {
|
||||
indices_converted[i] = cmd_list->IdxBuffer.Data[i];
|
||||
}
|
||||
indices = indices_converted;
|
||||
cleanup = true;
|
||||
} else {
|
||||
indices = (void*)cmd_list->IdxBuffer.Data;
|
||||
}
|
||||
|
||||
int idx_offset = 0;
|
||||
ImVec2 pos = draw_data->DisplayPos;
|
||||
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
|
||||
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer.Data[cmd_i];
|
||||
if (pcmd->UserCallback) {
|
||||
pcmd->UserCallback(cmd_list, pcmd);
|
||||
} else {
|
||||
ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->TextureId;
|
||||
al_set_clipping_rectangle((int)(pcmd->ClipRect.x - pos.x), (int)(pcmd->ClipRect.y - pos.y), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
|
||||
al_draw_prim(&vertices[0], g_VertexDecl, texture, idx_offset, idx_offset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
|
||||
}
|
||||
idx_offset += pcmd->ElemCount;
|
||||
}
|
||||
|
||||
al_free(vertices);
|
||||
if (cleanup) {
|
||||
al_free(indices);
|
||||
}
|
||||
}
|
||||
|
||||
// Restore modified Allegro state
|
||||
al_set_blender(last_blender_op, last_blender_src, last_blender_dst);
|
||||
al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h);
|
||||
al_use_transform(&last_transform);
|
||||
al_use_projection_transform(&last_projection_transform);
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT bool ImGui_ImplAllegro5_CreateDeviceObjects() {
|
||||
// Build texture atlas
|
||||
ImGuiIO* io = igGetIO();
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
ImFontAtlas_GetTexDataAsRGBA32(io->Fonts, &pixels, &width, &height, NULL);
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT void ImGui_ImplAllegro5_InvalidateDeviceObjects() {
|
||||
if (g_Texture) {
|
||||
al_destroy_bitmap(g_Texture);
|
||||
igGetIO()->Fonts->TexID = NULL;
|
||||
g_Texture = NULL;
|
||||
}
|
||||
if (g_MouseCursorInvisible) {
|
||||
al_destroy_mouse_cursor(g_MouseCursorInvisible);
|
||||
g_MouseCursorInvisible = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#if ALLEGRO_HAS_CLIPBOARD
|
||||
static const char* ImGui_ImplAllegro5_GetClipboardText(void* data) {
|
||||
if (g_ClipboardTextData) {
|
||||
al_free(g_ClipboardTextData);
|
||||
}
|
||||
g_ClipboardTextData = al_get_clipboard_text(g_Display);
|
||||
return g_ClipboardTextData;
|
||||
}
|
||||
|
||||
static void ImGui_ImplAllegro5_SetClipboardText(void* data, const char* text) {
|
||||
al_set_clipboard_text(g_Display, text);
|
||||
}
|
||||
#endif
|
||||
|
||||
SYMBOL_EXPORT bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display) {
|
||||
g_Display = display;
|
||||
|
||||
// Setup back-end capabilities flags
|
||||
ImGuiIO* io = igGetIO();
|
||||
io->BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||
|
||||
// Create custom vertex declaration.
|
||||
// Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 floats.
|
||||
// We still use a custom declaration to use 'ALLEGRO_PRIM_TEX_COORD' instead of 'ALLEGRO_PRIM_TEX_COORD_PIXEL' else we can't do a reliable conversion.
|
||||
ALLEGRO_VERTEX_ELEMENT elems[] =
|
||||
{
|
||||
{ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, pos)},
|
||||
{ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, uv)},
|
||||
{ALLEGRO_PRIM_COLOR_ATTR, 0, offsetof(ImDrawVertAllegro, col)},
|
||||
{0, 0, 0}};
|
||||
g_VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
|
||||
|
||||
io->KeyMap[ImGuiKey_Tab] = ALLEGRO_KEY_TAB;
|
||||
io->KeyMap[ImGuiKey_LeftArrow] = ALLEGRO_KEY_LEFT;
|
||||
io->KeyMap[ImGuiKey_RightArrow] = ALLEGRO_KEY_RIGHT;
|
||||
io->KeyMap[ImGuiKey_UpArrow] = ALLEGRO_KEY_UP;
|
||||
io->KeyMap[ImGuiKey_DownArrow] = ALLEGRO_KEY_DOWN;
|
||||
io->KeyMap[ImGuiKey_PageUp] = ALLEGRO_KEY_PGUP;
|
||||
io->KeyMap[ImGuiKey_PageDown] = ALLEGRO_KEY_PGDN;
|
||||
io->KeyMap[ImGuiKey_Home] = ALLEGRO_KEY_HOME;
|
||||
io->KeyMap[ImGuiKey_End] = ALLEGRO_KEY_END;
|
||||
io->KeyMap[ImGuiKey_Insert] = ALLEGRO_KEY_INSERT;
|
||||
io->KeyMap[ImGuiKey_Delete] = ALLEGRO_KEY_DELETE;
|
||||
io->KeyMap[ImGuiKey_Backspace] = ALLEGRO_KEY_BACKSPACE;
|
||||
io->KeyMap[ImGuiKey_Space] = ALLEGRO_KEY_SPACE;
|
||||
io->KeyMap[ImGuiKey_Enter] = ALLEGRO_KEY_ENTER;
|
||||
io->KeyMap[ImGuiKey_Escape] = ALLEGRO_KEY_ESCAPE;
|
||||
io->KeyMap[ImGuiKey_A] = ALLEGRO_KEY_A;
|
||||
io->KeyMap[ImGuiKey_C] = ALLEGRO_KEY_C;
|
||||
io->KeyMap[ImGuiKey_V] = ALLEGRO_KEY_V;
|
||||
io->KeyMap[ImGuiKey_X] = ALLEGRO_KEY_X;
|
||||
io->KeyMap[ImGuiKey_Y] = ALLEGRO_KEY_Y;
|
||||
io->KeyMap[ImGuiKey_Z] = ALLEGRO_KEY_Z;
|
||||
|
||||
#if ALLEGRO_HAS_CLIPBOARD
|
||||
io->SetClipboardTextFn = ImGui_ImplAllegro5_SetClipboardText;
|
||||
io->GetClipboardTextFn = ImGui_ImplAllegro5_GetClipboardText;
|
||||
io->ClipboardUserData = NULL;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT void ImGui_ImplAllegro5_Shutdown() {
|
||||
ImGui_ImplAllegro5_InvalidateDeviceObjects();
|
||||
g_Display = NULL;
|
||||
|
||||
// Destroy last known clipboard data
|
||||
if (g_ClipboardTextData) {
|
||||
al_free(g_ClipboardTextData);
|
||||
}
|
||||
g_ClipboardTextData = NULL;
|
||||
}
|
||||
|
||||
// You can read the io->WantCaptureMouse, io->WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||
// - When io->WantCaptureMouse is true, do not dispatch mouse input data to your main application.
|
||||
// - When io->WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
|
||||
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||
SYMBOL_EXPORT bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* ev) {
|
||||
ImGuiIO* io = igGetIO();
|
||||
|
||||
switch (ev->type) {
|
||||
case ALLEGRO_EVENT_MOUSE_AXES:
|
||||
io->MouseWheel += ev->mouse.dz;
|
||||
io->MouseWheelH += ev->mouse.dw;
|
||||
return true;
|
||||
case ALLEGRO_EVENT_KEY_CHAR:
|
||||
if (ev->keyboard.display == g_Display) {
|
||||
if (ev->keyboard.unichar > 0 && ev->keyboard.unichar < 0x10000) {
|
||||
ImGuiIO_AddInputCharacter(io, (unsigned short)ev->keyboard.unichar);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
case ALLEGRO_EVENT_KEY_DOWN:
|
||||
case ALLEGRO_EVENT_KEY_UP:
|
||||
if (ev->keyboard.display == g_Display) {
|
||||
io->KeysDown[ev->keyboard.keycode] = (ev->type == ALLEGRO_EVENT_KEY_DOWN);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void ImGui_ImplAllegro5_UpdateMouseCursor() {
|
||||
ImGuiIO* io = igGetIO();
|
||||
if (io->ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) {
|
||||
return;
|
||||
}
|
||||
|
||||
ImGuiMouseCursor imgui_cursor = igGetMouseCursor();
|
||||
if (io->MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) {
|
||||
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
||||
al_set_mouse_cursor(g_Display, g_MouseCursorInvisible);
|
||||
} else {
|
||||
ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
|
||||
switch (imgui_cursor) {
|
||||
case ImGuiMouseCursor_TextInput:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeAll:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeNS:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeEW:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeNESW:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeNWSE:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW;
|
||||
break;
|
||||
}
|
||||
al_set_system_mouse_cursor(g_Display, cursor_id);
|
||||
}
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT void ImGui_ImplAllegro5_NewFrame() {
|
||||
if (!g_Texture) {
|
||||
ImGui_ImplAllegro5_CreateDeviceObjects();
|
||||
}
|
||||
|
||||
ImGuiIO* io = igGetIO();
|
||||
|
||||
// Setup display size (every frame to accommodate for window resizing)
|
||||
int w, h;
|
||||
w = al_get_display_width(g_Display);
|
||||
h = al_get_display_height(g_Display);
|
||||
io->DisplaySize = (ImVec2){.x = w, .y = h};
|
||||
|
||||
// Setup time step
|
||||
double current_time = al_get_time();
|
||||
io->DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (1.0f / 60.0f);
|
||||
g_Time = current_time;
|
||||
|
||||
// Setup inputs
|
||||
ALLEGRO_KEYBOARD_STATE keys;
|
||||
al_get_keyboard_state(&keys);
|
||||
io->KeyCtrl = al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL);
|
||||
io->KeyShift = al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT);
|
||||
io->KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR);
|
||||
io->KeySuper = al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN);
|
||||
|
||||
ALLEGRO_MOUSE_STATE mouse;
|
||||
if (keys.display == g_Display) {
|
||||
al_get_mouse_state(&mouse);
|
||||
io->MousePos = (ImVec2){.x = mouse.x, .y = mouse.y};
|
||||
} else {
|
||||
io->MousePos = (ImVec2){.x = -FLT_MAX, .y = -FLT_MAX};
|
||||
}
|
||||
|
||||
al_get_mouse_state(&mouse);
|
||||
io->MouseDown[0] = mouse.buttons & (1 << 0);
|
||||
io->MouseDown[1] = mouse.buttons & (1 << 1);
|
||||
io->MouseDown[2] = mouse.buttons & (1 << 2);
|
||||
|
||||
ImGui_ImplAllegro5_UpdateMouseCursor();
|
||||
}
|
364
src/imgui/imgui_impl_allegro5.cpp
Normal file
364
src/imgui/imgui_impl_allegro5.cpp
Normal file
|
@ -0,0 +1,364 @@
|
|||
// dear imgui: Renderer + Platform Binding for Allegro 5
|
||||
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
|
||||
// [X] Platform: Clipboard support (from Allegro 5.1.12)
|
||||
// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||
// Issues:
|
||||
// [ ] Renderer: The renderer is suboptimal as we need to unindex our buffers and convert vertices manually.
|
||||
// [ ] Platform: Missing gamepad support.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
||||
// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
|
||||
// https://github.com/ocornut/imgui, Original Allegro 5 code by @birthggd
|
||||
|
||||
// CHANGELOG
|
||||
// (minor and older changes stripped away, please see git history for details)
|
||||
// 2018-06-13: Platform: Added clipboard support (from Allegro 5.1.12).
|
||||
// 2018-06-13: Renderer: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||
// 2018-06-13: Renderer: Backup/restore transform and clipping rectangle.
|
||||
// 2018-06-11: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
|
||||
// 2018-04-18: Misc: Renamed file from imgui_impl_a5.cpp to imgui_impl_allegro5.cpp.
|
||||
// 2018-04-18: Misc: Added support for 32-bits vertex indices to avoid conversion at runtime. Added imconfig_allegro5.h to enforce 32-bit indices when included from imgui.h.
|
||||
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplAllegro5_RenderDrawData() in the .h file so you can call it yourself.
|
||||
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
||||
|
||||
#include "internal.h"
|
||||
#include <float.h>
|
||||
#include <stdint.h> // uint64_t
|
||||
#include <string.h> // memcpy
|
||||
|
||||
#include "imgui/imgui_impl_allegro5.h"
|
||||
|
||||
#define ALLEGRO_HAS_CLIPBOARD (ALLEGRO_VERSION_INT >= ((5 << 24) | (1 << 16) | (12 << 8))) // Clipboard only supported from Allegro 5.1.12
|
||||
|
||||
// Visual Studio warnings
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable : 4127) // condition expression is constant
|
||||
#endif
|
||||
|
||||
// Data
|
||||
static ALLEGRO_DISPLAY* g_Display = NULL;
|
||||
static ALLEGRO_BITMAP* g_Texture = NULL;
|
||||
static double g_Time = 0.0;
|
||||
static ALLEGRO_MOUSE_CURSOR* g_MouseCursorInvisible = NULL;
|
||||
static ALLEGRO_VERTEX_DECL* g_VertexDecl = NULL;
|
||||
static char* g_ClipboardTextData = NULL;
|
||||
|
||||
struct ImDrawVertAllegro {
|
||||
ImVec2 pos;
|
||||
ImVec2 uv;
|
||||
ALLEGRO_COLOR col;
|
||||
};
|
||||
|
||||
// Render function.
|
||||
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
|
||||
void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data) {
|
||||
// Backup Allegro state that will be modified
|
||||
ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
|
||||
ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform();
|
||||
int last_clip_x, last_clip_y, last_clip_w, last_clip_h;
|
||||
al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h);
|
||||
int last_blender_op, last_blender_src, last_blender_dst;
|
||||
al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst);
|
||||
|
||||
// Setup render state
|
||||
al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
|
||||
|
||||
// Setup orthographic projection matrix
|
||||
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
|
||||
{
|
||||
float L = draw_data->DisplayPos.x;
|
||||
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||
float T = draw_data->DisplayPos.y;
|
||||
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||
ALLEGRO_TRANSFORM transform;
|
||||
al_identity_transform(&transform);
|
||||
al_use_transform(&transform);
|
||||
al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f);
|
||||
al_use_projection_transform(&transform);
|
||||
}
|
||||
|
||||
for (int n = 0; n < draw_data->CmdListsCount; n++) {
|
||||
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
||||
|
||||
// Allegro's implementation of al_draw_indexed_prim() for DX9 is completely broken. Unindex our buffers ourselves.
|
||||
// FIXME-OPT: Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 float as well..
|
||||
static ImVector<ImDrawVertAllegro> vertices;
|
||||
vertices.resize(cmd_list->IdxBuffer.Size);
|
||||
for (int i = 0; i < cmd_list->IdxBuffer.Size; i++) {
|
||||
const ImDrawVert* src_v = &cmd_list->VtxBuffer[cmd_list->IdxBuffer[i]];
|
||||
ImDrawVertAllegro* dst_v = &vertices[i];
|
||||
dst_v->pos = src_v->pos;
|
||||
dst_v->uv = src_v->uv;
|
||||
unsigned char* c = (unsigned char*)&src_v->col;
|
||||
dst_v->col = al_map_rgba(c[0], c[1], c[2], c[3]);
|
||||
}
|
||||
|
||||
const int* indices = NULL;
|
||||
if (sizeof(ImDrawIdx) == 2) {
|
||||
// FIXME-OPT: Unfortunately Allegro doesn't support 16-bit indices.. You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices.
|
||||
// Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful.
|
||||
static ImVector<int> indices_converted;
|
||||
indices_converted.resize(cmd_list->IdxBuffer.Size);
|
||||
for (int i = 0; i < cmd_list->IdxBuffer.Size; ++i)
|
||||
indices_converted[i] = (int)cmd_list->IdxBuffer.Data[i];
|
||||
indices = indices_converted.Data;
|
||||
} else if (sizeof(ImDrawIdx) == 4) {
|
||||
indices = (const int*)cmd_list->IdxBuffer.Data;
|
||||
}
|
||||
|
||||
int idx_offset = 0;
|
||||
ImVec2 pos = draw_data->DisplayPos;
|
||||
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
|
||||
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
|
||||
if (pcmd->UserCallback) {
|
||||
pcmd->UserCallback(cmd_list, pcmd);
|
||||
} else {
|
||||
ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->TextureId;
|
||||
al_set_clipping_rectangle(pcmd->ClipRect.x - pos.x, pcmd->ClipRect.y - pos.y, pcmd->ClipRect.z - pcmd->ClipRect.x, pcmd->ClipRect.w - pcmd->ClipRect.y);
|
||||
al_draw_prim(&vertices[0], g_VertexDecl, texture, idx_offset, idx_offset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
|
||||
}
|
||||
idx_offset += pcmd->ElemCount;
|
||||
}
|
||||
}
|
||||
|
||||
// Restore modified Allegro state
|
||||
al_set_blender(last_blender_op, last_blender_src, last_blender_dst);
|
||||
al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h);
|
||||
al_use_transform(&last_transform);
|
||||
al_use_projection_transform(&last_projection_transform);
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT bool ImGui_ImplAllegro5_CreateDeviceObjects() {
|
||||
// Build texture atlas
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
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;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT void ImGui_ImplAllegro5_InvalidateDeviceObjects() {
|
||||
if (g_Texture) {
|
||||
al_destroy_bitmap(g_Texture);
|
||||
ImGui::GetIO().Fonts->TexID = NULL;
|
||||
g_Texture = NULL;
|
||||
}
|
||||
if (g_MouseCursorInvisible) {
|
||||
al_destroy_mouse_cursor(g_MouseCursorInvisible);
|
||||
g_MouseCursorInvisible = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#if ALLEGRO_HAS_CLIPBOARD
|
||||
static const char* ImGui_ImplAllegro5_GetClipboardText(void*) {
|
||||
if (g_ClipboardTextData)
|
||||
al_free(g_ClipboardTextData);
|
||||
g_ClipboardTextData = al_get_clipboard_text(g_Display);
|
||||
return g_ClipboardTextData;
|
||||
}
|
||||
|
||||
static void ImGui_ImplAllegro5_SetClipboardText(void*, const char* text) {
|
||||
al_set_clipboard_text(g_Display, text);
|
||||
}
|
||||
#endif
|
||||
|
||||
SYMBOL_EXPORT bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display) {
|
||||
g_Display = display;
|
||||
|
||||
// Setup back-end capabilities flags
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||
|
||||
// Create custom vertex declaration.
|
||||
// Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 floats.
|
||||
// We still use a custom declaration to use 'ALLEGRO_PRIM_TEX_COORD' instead of 'ALLEGRO_PRIM_TEX_COORD_PIXEL' else we can't do a reliable conversion.
|
||||
ALLEGRO_VERTEX_ELEMENT elems[] =
|
||||
{
|
||||
{ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, IM_OFFSETOF(ImDrawVertAllegro, pos)},
|
||||
{ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, IM_OFFSETOF(ImDrawVertAllegro, uv)},
|
||||
{ALLEGRO_PRIM_COLOR_ATTR, 0, IM_OFFSETOF(ImDrawVertAllegro, col)},
|
||||
{0, 0, 0}};
|
||||
g_VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
|
||||
|
||||
io.KeyMap[ImGuiKey_Tab] = ALLEGRO_KEY_TAB;
|
||||
io.KeyMap[ImGuiKey_LeftArrow] = ALLEGRO_KEY_LEFT;
|
||||
io.KeyMap[ImGuiKey_RightArrow] = ALLEGRO_KEY_RIGHT;
|
||||
io.KeyMap[ImGuiKey_UpArrow] = ALLEGRO_KEY_UP;
|
||||
io.KeyMap[ImGuiKey_DownArrow] = ALLEGRO_KEY_DOWN;
|
||||
io.KeyMap[ImGuiKey_PageUp] = ALLEGRO_KEY_PGUP;
|
||||
io.KeyMap[ImGuiKey_PageDown] = ALLEGRO_KEY_PGDN;
|
||||
io.KeyMap[ImGuiKey_Home] = ALLEGRO_KEY_HOME;
|
||||
io.KeyMap[ImGuiKey_End] = ALLEGRO_KEY_END;
|
||||
io.KeyMap[ImGuiKey_Insert] = ALLEGRO_KEY_INSERT;
|
||||
io.KeyMap[ImGuiKey_Delete] = ALLEGRO_KEY_DELETE;
|
||||
io.KeyMap[ImGuiKey_Backspace] = ALLEGRO_KEY_BACKSPACE;
|
||||
io.KeyMap[ImGuiKey_Space] = ALLEGRO_KEY_SPACE;
|
||||
io.KeyMap[ImGuiKey_Enter] = ALLEGRO_KEY_ENTER;
|
||||
io.KeyMap[ImGuiKey_Escape] = ALLEGRO_KEY_ESCAPE;
|
||||
io.KeyMap[ImGuiKey_A] = ALLEGRO_KEY_A;
|
||||
io.KeyMap[ImGuiKey_C] = ALLEGRO_KEY_C;
|
||||
io.KeyMap[ImGuiKey_V] = ALLEGRO_KEY_V;
|
||||
io.KeyMap[ImGuiKey_X] = ALLEGRO_KEY_X;
|
||||
io.KeyMap[ImGuiKey_Y] = ALLEGRO_KEY_Y;
|
||||
io.KeyMap[ImGuiKey_Z] = ALLEGRO_KEY_Z;
|
||||
|
||||
#if ALLEGRO_HAS_CLIPBOARD
|
||||
io.SetClipboardTextFn = ImGui_ImplAllegro5_SetClipboardText;
|
||||
io.GetClipboardTextFn = ImGui_ImplAllegro5_GetClipboardText;
|
||||
io.ClipboardUserData = NULL;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT void ImGui_ImplAllegro5_Shutdown() {
|
||||
ImGui_ImplAllegro5_InvalidateDeviceObjects();
|
||||
g_Display = NULL;
|
||||
|
||||
// Destroy last known clipboard data
|
||||
if (g_ClipboardTextData)
|
||||
al_free(g_ClipboardTextData);
|
||||
g_ClipboardTextData = NULL;
|
||||
}
|
||||
|
||||
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
|
||||
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
|
||||
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||
SYMBOL_EXPORT bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* ev) {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
switch (ev->type) {
|
||||
case ALLEGRO_EVENT_MOUSE_AXES:
|
||||
io.MouseWheel += ev->mouse.dz;
|
||||
io.MouseWheelH += ev->mouse.dw;
|
||||
return true;
|
||||
case ALLEGRO_EVENT_KEY_CHAR:
|
||||
if (ev->keyboard.display == g_Display)
|
||||
if (ev->keyboard.unichar > 0 && ev->keyboard.unichar < 0x10000)
|
||||
io.AddInputCharacter((unsigned short)ev->keyboard.unichar);
|
||||
return true;
|
||||
case ALLEGRO_EVENT_KEY_DOWN:
|
||||
case ALLEGRO_EVENT_KEY_UP:
|
||||
if (ev->keyboard.display == g_Display)
|
||||
io.KeysDown[ev->keyboard.keycode] = (ev->type == ALLEGRO_EVENT_KEY_DOWN);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void ImGui_ImplAllegro5_UpdateMouseCursor() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
||||
return;
|
||||
|
||||
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
|
||||
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) {
|
||||
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
||||
al_set_mouse_cursor(g_Display, g_MouseCursorInvisible);
|
||||
} else {
|
||||
ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
|
||||
switch (imgui_cursor) {
|
||||
case ImGuiMouseCursor_TextInput:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeAll:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeNS:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeEW:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeNESW:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE;
|
||||
break;
|
||||
case ImGuiMouseCursor_ResizeNWSE:
|
||||
cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW;
|
||||
break;
|
||||
}
|
||||
al_set_system_mouse_cursor(g_Display, cursor_id);
|
||||
}
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT void ImGui_ImplAllegro5_NewFrame() {
|
||||
if (!g_Texture)
|
||||
ImGui_ImplAllegro5_CreateDeviceObjects();
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
// Setup display size (every frame to accommodate for window resizing)
|
||||
int w, h;
|
||||
w = al_get_display_width(g_Display);
|
||||
h = al_get_display_height(g_Display);
|
||||
io.DisplaySize = ImVec2((float)w, (float)h);
|
||||
|
||||
// Setup time step
|
||||
double current_time = al_get_time();
|
||||
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
|
||||
g_Time = current_time;
|
||||
|
||||
// Setup inputs
|
||||
ALLEGRO_KEYBOARD_STATE keys;
|
||||
al_get_keyboard_state(&keys);
|
||||
io.KeyCtrl = al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL);
|
||||
io.KeyShift = al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT);
|
||||
io.KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR);
|
||||
io.KeySuper = al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN);
|
||||
|
||||
ALLEGRO_MOUSE_STATE mouse;
|
||||
if (keys.display == g_Display) {
|
||||
al_get_mouse_state(&mouse);
|
||||
io.MousePos = ImVec2((float)mouse.x, (float)mouse.y);
|
||||
} else {
|
||||
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
|
||||
}
|
||||
|
||||
al_get_mouse_state(&mouse);
|
||||
io.MouseDown[0] = mouse.buttons & (1 << 0);
|
||||
io.MouseDown[1] = mouse.buttons & (1 << 1);
|
||||
io.MouseDown[2] = mouse.buttons & (1 << 2);
|
||||
|
||||
ImGui_ImplAllegro5_UpdateMouseCursor();
|
||||
}
|
36
src/imgui/imgui_impl_allegro5.h
Normal file
36
src/imgui/imgui_impl_allegro5.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
// dear imgui: Renderer + Platform Binding for Allegro 5
|
||||
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
|
||||
// [X] Platform: Clipboard support (from Allegro 5.1.12)
|
||||
// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||
// Issues:
|
||||
// [ ] Renderer: The renderer is suboptimal as we need to unindex our buffers and convert vertices manually.
|
||||
// [ ] Platform: Missing gamepad support.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
||||
// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
|
||||
// https://github.com/ocornut/imgui, Original Allegro 5 code by @birthggd
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include "3rdparty/cimgui/imgui/imgui.h"
|
||||
#else
|
||||
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS 1
|
||||
#endif
|
||||
|
||||
#include "3rdparty/cimgui/cimgui.h"
|
||||
#include "libsuperderpy.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display);
|
||||
void ImGui_ImplAllegro5_Shutdown(void);
|
||||
void ImGui_ImplAllegro5_NewFrame(void);
|
||||
void ImGui_ImplAllegro5_RenderDrawData(struct ImDrawData* draw_data);
|
||||
bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* event);
|
||||
|
||||
// Use if you want to reset your rendering device without losing ImGui state.
|
||||
bool ImGui_ImplAllegro5_CreateDeviceObjects(void);
|
||||
void ImGui_ImplAllegro5_InvalidateDeviceObjects(void);
|
|
@ -375,6 +375,18 @@ SYMBOL_EXPORT int libsuperderpy_start(struct Game* game) {
|
|||
game->_priv.timestamp = al_get_time();
|
||||
game->_priv.paused = false;
|
||||
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
igCreateContext(NULL);
|
||||
ImGuiIO* io = igGetIO();
|
||||
io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
|
||||
// Setup Platform/Renderer bindings
|
||||
ImGui_ImplAllegro5_Init(game->display);
|
||||
|
||||
// Setup Style
|
||||
igStyleColorsDark(NULL);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -407,6 +419,11 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game* game) {
|
|||
SYMBOL_EXPORT void libsuperderpy_destroy(struct Game* game) {
|
||||
game->shutting_down = true;
|
||||
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
ImGui_ImplAllegro5_Shutdown();
|
||||
igDestroyContext(NULL);
|
||||
#endif
|
||||
|
||||
ClearGarbage(game);
|
||||
|
||||
struct Gamestate *tmp = game->_priv.gamestates, *pom;
|
||||
|
|
|
@ -73,6 +73,10 @@ struct GamestateResources;
|
|||
#include "emscripten-audio-stream.h"
|
||||
#endif
|
||||
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
#include "imgui/imgui_impl_allegro5.h"
|
||||
#endif
|
||||
|
||||
#define LIBSUPERDERPY_BITMAP_HASHMAP_BUCKETS 16
|
||||
|
||||
struct Viewport {
|
||||
|
|
|
@ -42,7 +42,13 @@ static inline void HandleEvent(struct Game* game, ALLEGRO_EVENT* ev) {
|
|||
break;
|
||||
|
||||
case ALLEGRO_EVENT_DISPLAY_RESIZE:
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
ImGui_ImplAllegro5_InvalidateDeviceObjects();
|
||||
#endif
|
||||
al_acknowledge_resize(game->display);
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
ImGui_ImplAllegro5_CreateDeviceObjects();
|
||||
#endif
|
||||
SetupViewport(game, game->viewport_config);
|
||||
break;
|
||||
|
||||
|
@ -334,10 +340,21 @@ static inline bool MainloopTick(struct Game* game) {
|
|||
delta *= ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer) / (1 / 60.f));
|
||||
game->time += delta;
|
||||
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
ImGui_ImplAllegro5_NewFrame();
|
||||
igNewFrame();
|
||||
#endif
|
||||
|
||||
LogicGamestates(game, delta);
|
||||
DrawGamestates(game);
|
||||
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
igRender();
|
||||
ImGui_ImplAllegro5_RenderDrawData(igGetDrawData());
|
||||
#endif
|
||||
|
||||
DrawConsole(game);
|
||||
|
||||
al_flip_display();
|
||||
return true;
|
||||
}
|
||||
|
@ -353,6 +370,28 @@ static inline bool MainloopEvents(struct Game* game) {
|
|||
break;
|
||||
}
|
||||
|
||||
#ifdef LIBSUPERDERPY_IMGUI
|
||||
ImGui_ImplAllegro5_ProcessEvent(&ev);
|
||||
switch (ev.type) {
|
||||
case ALLEGRO_EVENT_KEY_CHAR:
|
||||
case ALLEGRO_EVENT_KEY_DOWN:
|
||||
case ALLEGRO_EVENT_KEY_UP:
|
||||
if (igGetIO()->WantCaptureKeyboard) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case ALLEGRO_EVENT_MOUSE_AXES:
|
||||
case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
|
||||
case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
|
||||
if (igGetIO()->WantCaptureMouse) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (game->handlers.event) {
|
||||
if ((*game->handlers.event)(game, &ev)) {
|
||||
continue;
|
||||
|
|
Loading…
Reference in a new issue