Skip to content

Commit

Permalink
[glfw] MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Jun 26, 2024
1 parent 99d6693 commit 4b5102f
Show file tree
Hide file tree
Showing 12 changed files with 17,507 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ elseif((${SPINE_SDL}) OR (${CMAKE_CURRENT_BINARY_DIR} MATCHES "spine-sdl"))
add_subdirectory(spine-c)
add_subdirectory(spine-cpp)
add_subdirectory(spine-sdl)
elseif((${SPINE_GLFW}) OR (${CMAKE_CURRENT_BINARY_DIR} MATCHES "spine-glfw"))
add_subdirectory(spine-cpp)
add_subdirectory(spine-glfw)
else()
add_subdirectory(spine-c)
add_subdirectory(spine-cpp)
Expand Down
2 changes: 1 addition & 1 deletion spine-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ add_library(spine-cpp STATIC ${SOURCES} ${INCLUDES})
target_include_directories(spine-cpp PUBLIC spine-cpp/include)

add_library(spine-cpp-lite STATIC ${SOURCES} ${INCLUDES} spine-cpp-lite/spine-cpp-lite.cpp)
target_include_directories(spine-cpp-lite PUBLIC spine-cpp/include)
target_include_directories(spine-cpp-lite PUBLIC spine-cpp/include spine-cpp-lite)

# Install target
install(TARGETS spine-cpp EXPORT spine-cpp_TARGETS DESTINATION dist/lib)
Expand Down
34 changes: 33 additions & 1 deletion spine-cpp/spine-cpp-lite/spine-cpp-lite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ typedef struct _spine_render_command {
float *positions;
float *uvs;
int32_t *colors;
int32_t *darkColors;
int32_t numVertices;
uint16_t *indices;
int32_t numIndices;
Expand Down Expand Up @@ -653,6 +654,7 @@ static _spine_render_command *spine_render_command_create(BlockAllocator &alloca
cmd->positions = allocator.allocate<float>(numVertices << 1);
cmd->uvs = allocator.allocate<float>(numVertices << 1);
cmd->colors = allocator.allocate<int32_t>(numVertices);
cmd->darkColors = allocator.allocate<int32_t>(numVertices);
cmd->numVertices = numVertices;
cmd->indices = allocator.allocate<uint16_t>(numIndices);
cmd->numIndices = numIndices;
Expand Down Expand Up @@ -697,19 +699,22 @@ static _spine_render_command *batch_sub_commands(BlockAllocator &allocator, Vect
float *positions = batched->positions;
float *uvs = batched->uvs;
int32_t *colors = batched->colors;
int32_t *darkColors = batched->darkColors;
uint16_t *indices = batched->indices;
int indicesOffset = 0;
for (int i = first; i <= last; i++) {
_spine_render_command *cmd = commands[i];
memcpy(positions, cmd->positions, sizeof(float) * 2 * cmd->numVertices);
memcpy(uvs, cmd->uvs, sizeof(float) * 2 * cmd->numVertices);
memcpy(colors, cmd->colors, sizeof(int32_t) * cmd->numVertices);
memcpy(darkColors, cmd->darkColors, sizeof(int32_t) * cmd->numVertices);
for (int ii = 0; ii < cmd->numIndices; ii++)
indices[ii] = cmd->indices[ii] + indicesOffset;
indicesOffset += cmd->numVertices;
positions += 2 * cmd->numVertices;
uvs += 2 * cmd->numVertices;
colors += cmd->numVertices;
darkColors += cmd->numVertices;
indices += cmd->numIndices;
}
return batched;
Expand All @@ -728,9 +733,16 @@ static _spine_render_command *batch_commands(BlockAllocator &allocator, Vector<_
int numIndices = first->numIndices;
while (i <= (int) commands.size()) {
_spine_render_command *cmd = i < (int) commands.size() ? commands[i] : nullptr;

if (cmd && cmd->numVertices == 0 && cmd->numIndices == 0) {
i++;
continue;
}

if (cmd != nullptr && cmd->atlasPage == first->atlasPage &&
cmd->blendMode == first->blendMode &&
cmd->colors[0] == first->colors[0] &&
cmd->darkColors[0] == first->darkColors[0] &&
numIndices + cmd->numIndices < 0xffff) {
numVertices += cmd->numVertices;
numIndices += cmd->numIndices;
Expand Down Expand Up @@ -836,6 +848,11 @@ spine_render_command spine_skeleton_drawable_render(spine_skeleton_drawable draw
uint8_t b = static_cast<uint8_t>(skeleton->getColor().b * slot.getColor().b * attachmentColor->b * 255);
uint8_t a = static_cast<uint8_t>(skeleton->getColor().a * slot.getColor().a * attachmentColor->a * 255);
uint32_t color = (a << 24) | (r << 16) | (g << 8) | b;
uint32_t darkColor = 0xff000000;
if (slot.hasDarkColor()) {
Color &slotDarkColor = slot.getDarkColor();
darkColor = 0xff000000 | (static_cast<uint8_t>(slotDarkColor.r * 255) << 16)| (static_cast<uint8_t>(slotDarkColor.g * 255) << 8)| static_cast<uint8_t>(slotDarkColor.b * 255);
}

if (clipper.isClipping()) {
clipper.clipTriangles(*worldVertices, *indices, *uvs, 2);
Expand All @@ -850,7 +867,10 @@ spine_render_command spine_skeleton_drawable_render(spine_skeleton_drawable draw
_drawable->renderCommands.add(cmd);
memcpy(cmd->positions, vertices->buffer(), (verticesCount << 1) * sizeof(float));
memcpy(cmd->uvs, uvs->buffer(), (verticesCount << 1) * sizeof(float));
for (int ii = 0; ii < verticesCount; ii++) cmd->colors[ii] = color;
for (int ii = 0; ii < verticesCount; ii++) {
cmd->colors[ii] = color;
cmd->darkColors[ii] = darkColor;
}
memcpy(cmd->indices, indices->buffer(), indices->size() * sizeof(uint16_t));
clipper.clipEnd(slot);
}
Expand Down Expand Up @@ -895,6 +915,11 @@ int32_t *spine_render_command_get_colors(spine_render_command command) {
return ((_spine_render_command *) command)->colors;
}

int32_t *spine_render_command_get_dark_colors(spine_render_command command) {
if (!command) return nullptr;
return ((_spine_render_command *) command)->darkColors;
}

int32_t spine_render_command_get_num_vertices(spine_render_command command) {
if (!command) return 0;
return ((_spine_render_command *) command)->numVertices;
Expand Down Expand Up @@ -1697,6 +1722,13 @@ void spine_skeleton_set_y(spine_skeleton skeleton, float y) {
_skeleton->setY(y);
}

void spine_skeleton_set_scale(spine_skeleton skeleton, float scaleX, float scaleY) {
if (skeleton == nullptr) return;
Skeleton *_skeleton = (Skeleton *) skeleton;
_skeleton->setScaleX(scaleX);
_skeleton->setScaleY(scaleY);
}

float spine_skeleton_get_scale_x(spine_skeleton skeleton) {
if (skeleton == nullptr) return 0;
Skeleton *_skeleton = (Skeleton *) skeleton;
Expand Down
3 changes: 3 additions & 0 deletions spine-cpp/spine-cpp-lite/spine-cpp-lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ SPINE_CPP_LITE_EXPORT float *spine_render_command_get_positions(spine_render_com
SPINE_CPP_LITE_EXPORT float *spine_render_command_get_uvs(spine_render_command command);
// @ignore
SPINE_CPP_LITE_EXPORT int32_t *spine_render_command_get_colors(spine_render_command command);
// @ignore
SPINE_CPP_LITE_EXPORT int32_t *spine_render_command_get_dark_colors(spine_render_command command);
SPINE_CPP_LITE_EXPORT int32_t spine_render_command_get_num_vertices(spine_render_command command);
SPINE_CPP_LITE_EXPORT uint16_t *spine_render_command_get_indices(spine_render_command command);
SPINE_CPP_LITE_EXPORT int32_t spine_render_command_get_num_indices(spine_render_command command);
Expand Down Expand Up @@ -452,6 +454,7 @@ SPINE_CPP_LITE_EXPORT float spine_skeleton_get_x(spine_skeleton skeleton);
SPINE_CPP_LITE_EXPORT void spine_skeleton_set_x(spine_skeleton skeleton, float x);
SPINE_CPP_LITE_EXPORT float spine_skeleton_get_y(spine_skeleton skeleton);
SPINE_CPP_LITE_EXPORT void spine_skeleton_set_y(spine_skeleton skeleton, float y);
SPINE_CPP_LITE_EXPORT void spine_skeleton_set_scale(spine_skeleton skeleton, float scaleX, float scaleY);
SPINE_CPP_LITE_EXPORT float spine_skeleton_get_scale_x(spine_skeleton skeleton);
SPINE_CPP_LITE_EXPORT void spine_skeleton_set_scale_x(spine_skeleton skeleton, float scaleX);
SPINE_CPP_LITE_EXPORT float spine_skeleton_get_scale_y(spine_skeleton skeleton);
Expand Down
61 changes: 61 additions & 0 deletions spine-glfw/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.10)
project(spine-glfw)
if(MSVC)
message("MSCV detected")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS")
else()
message("${CMAKE_C_FLAGS}")
message("${CMAKE_CXX_FLAGS}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wno-unused-parameter -std=c99")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -pedantic -Wno-unused-parameter -std=c++11 -fno-exceptions -fno-rtti")
endif()

include(FetchContent)

# Fetch GLFW
FetchContent_Declare(
glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG latest
)
FetchContent_MakeAvailable(glfw)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
set(GLFW_INSTALL OFF CACHE BOOL " " FORCE)

# Fetch glbinding
FetchContent_Declare(
glbinding
GIT_REPOSITORY https://github.com/cginternals/glbinding.git
GIT_TAG master
)
FetchContent_MakeAvailable(glbinding)
set(OPTION_BUILD_DOCS OFF CACHE BOOL " " FORCE)
set(OPTION_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
set(OPTION_BUILD_TEST OFF CACHE BOOL " " FORCE)
set(OPTION_BUILD_TOOLS OFF CACHE BOOL " " FORCE)

include_directories(${glbinding_SOURCE_DIR}/include)
include_directories(src)

# Find local OpenGL lib
find_package(OpenGL REQUIRED)

# spine-glfw library
add_library(spine-glfw STATIC src/spine-glfw.cpp src/spine-glfw.h src/stb_image.h)
target_link_libraries(spine-glfw PRIVATE glbinding::glbinding)
target_link_libraries(spine-glfw LINK_PUBLIC spine-cpp spine-cpp-lite)

# Example
add_executable(spine-glfw-example example/main.cpp)
target_link_libraries(spine-glfw-example PRIVATE glfw)
target_link_libraries(spine-glfw-example PRIVATE OpenGL::GL)
target_link_libraries(spine-glfw-example LINK_PUBLIC spine-glfw)
target_link_libraries(spine-glfw-example PRIVATE glbinding::glbinding)

# copy data to build directory
add_custom_command(TARGET spine-glfw-example PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_LIST_DIR}/data $<TARGET_FILE_DIR:spine-glfw-example>/data)
95 changes: 95 additions & 0 deletions spine-glfw/data/spineboy-pma.atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
spineboy-pma.png
size: 1024, 256
filter: Linear, Linear
pma: true
scale: 0.5
crosshair
bounds: 352, 7, 45, 45
eye-indifferent
bounds: 862, 105, 47, 45
eye-surprised
bounds: 505, 79, 47, 45
front-bracer
bounds: 826, 66, 29, 40
front-fist-closed
bounds: 786, 65, 38, 41
front-fist-open
bounds: 710, 51, 43, 44
rotate: 90
front-foot
bounds: 210, 6, 63, 35
front-shin
bounds: 665, 128, 41, 92
rotate: 90
front-thigh
bounds: 2, 2, 23, 56
rotate: 90
front-upper-arm
bounds: 250, 205, 23, 49
goggles
bounds: 665, 171, 131, 83
gun
bounds: 798, 152, 105, 102
head
bounds: 2, 27, 136, 149
hoverboard-board
bounds: 2, 178, 246, 76
hoverboard-thruster
bounds: 722, 96, 30, 32
rotate: 90
hoverglow-small
bounds: 275, 81, 137, 38
mouth-grind
bounds: 614, 97, 47, 30
mouth-oooo
bounds: 612, 65, 47, 30
mouth-smile
bounds: 661, 64, 47, 30
muzzle-glow
bounds: 382, 54, 25, 25
muzzle-ring
bounds: 275, 54, 25, 105
rotate: 90
muzzle01
bounds: 911, 95, 67, 40
rotate: 90
muzzle02
bounds: 792, 108, 68, 42
muzzle03
bounds: 956, 171, 83, 53
rotate: 90
muzzle04
bounds: 275, 7, 75, 45
muzzle05
bounds: 140, 3, 68, 38
neck
bounds: 250, 182, 18, 21
portal-bg
bounds: 140, 43, 133, 133
portal-flare1
bounds: 554, 65, 56, 30
portal-flare2
bounds: 759, 112, 57, 31
rotate: 90
portal-flare3
bounds: 554, 97, 58, 30
portal-shade
bounds: 275, 121, 133, 133
portal-streaks1
bounds: 410, 126, 126, 128
portal-streaks2
bounds: 538, 129, 125, 125
rear-bracer
bounds: 857, 67, 28, 36
rear-foot
bounds: 663, 96, 57, 30
rear-shin
bounds: 414, 86, 38, 89
rotate: 90
rear-thigh
bounds: 756, 63, 28, 47
rear-upper-arm
bounds: 60, 5, 20, 44
rotate: 90
torso
bounds: 905, 164, 49, 90
Binary file added spine-glfw/data/spineboy-pma.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4b5102f

Please sign in to comment.