cmake config + optional SDL2 dependency

main
cosmonaut 2022-06-24 13:06:57 -07:00
parent 65f84abdae
commit 01d1848b11
4 changed files with 193 additions and 78 deletions

89
CMakeLists.txt Normal file
View File

@ -0,0 +1,89 @@
cmake_minimum_required(VERSION 2.8.12)
project(Silkworm2 C)
option(BUILD_SHARED_LIBS "Build shared library" ON)
option(USE_SDL2 "Use SDL2" ON)
SET(LIB_MAJOR_VERSION "0")
SET(LIB_MINOR_VERSION "1")
SET(LIB_REVISION "0")
SET(LIB_VERSION "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_REVISION}")
# Build Type
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
# By default, we use Release
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE "Release" CACHE
STRING "Choose the type of build." FORCE
)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
STRINGS "Debug" "Release" "RelWithDebInfo"
)
endif()
# Platform Flags
if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9)
set(LOBJC "objc")
elseif(WIN32)
# "Silkworm2.dll", not "libSilkworm2.dll"
set(CMAKE_SHARED_LIBRARY_PREFIX "")
endif()
if(UNIX)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH ${BIN_RPATH})
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
endif()
if(USE_SDL2)
add_definitions(-DUSE_SDL2)
endif()
add_library(Silkworm2
#Public header
src/Silkworm2.h
#Source
src/Silkworm2.c
)
# Build flags
if(NOT MSVC)
set_property(TARGET Silkworm2 PROPERTY COMPILE_FLAGS "-std=gnu99 -Wall -Wno-strict-aliasing -pedantic")
endif()
# includes
target_include_directories(Silkworm2 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
)
# Soname
set_target_properties(Silkworm2 PROPERTIES OUTPUT_NAME "Silkworm2"
VERSION ${LIB_VERSION}
SOVERSION ${LIB_MAJOR_VERSION}
)
# SDL2 Dependency
if(USE_SDL2)
if (DEFINED SDL2_INCLUDE_DIRS AND DEFINED SDL2_LIBRARIES)
message(STATUS "using pre-defined SDL2 variables SDL2_INCLUDE_DIRS and SDL2_LIBRARIES")
target_include_directories(Silkworm2 PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_link_libraries(Silkworm2 PUBLIC ${SDL2_LIBRARIES})
else()
# Only try to autodetect if both SDL2 variables aren't explicitly set
find_package(SDL2 CONFIG)
if (TARGET SDL2::SDL2)
message(STATUS "using TARGET SDL2::SDL2")
target_link_libraries(Silkworm2 PUBLIC SDL2::SDL2)
elseif (TARGET SDL2)
message(STATUS "using TARGET SDL2")
target_link_libraries(Silkworm2 PUBLIC SDL2)
else()
message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables")
target_include_directories(Silkworm2 PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_link_libraries(Silkworm2 PUBLIC ${SDL2_LIBRARIES})
endif()
endif()
endif()

View File

@ -4,6 +4,10 @@ Silkworm2 is a cloth physics simulation system that was designed for the game Sa
What if we weren't constrained by the absolute garbage FFI system of Game Maker Studio 2? This library answers that question. What if we weren't constrained by the absolute garbage FFI system of Game Maker Studio 2? This library answers that question.
## Dependencies
Silkworm2 optionally depends on SDL2, but you can use the standard library if you so choose by disabling USE_SDL2.
## License ## License
This library is licensed under the zlib license. See LICENSE file for details. This library is licensed under the zlib license. See LICENSE file for details.

View File

@ -26,9 +26,31 @@
#include "Silkworm2.h" #include "Silkworm2.h"
/* Function defines */
#ifdef USE_SDL2
#include <SDL.h>
#define Silkworm_malloc SDL_malloc
#define Silkworm_realloc SDL_realloc
#define Silkworm_free SDL_free
#define Silkworm_sin SDL_sin
#define Silkworm_fabs SDL_fabs
#else
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define Silkworm_malloc malloc
#define Silkworm_realloc realloc
#define Silkworm_free free
#define Silkworm_sin sinf
#define Silkworm_fabs fabs
#endif
#define PI 3.14159265358979323846 #define PI 3.14159265358979323846
typedef struct Silkworm_Vector2 typedef struct Silkworm_Vector2
@ -37,14 +59,6 @@ typedef struct Silkworm_Vector2
float y; float y;
} Silkworm_Vector2; } Silkworm_Vector2;
static inline Silkworm_Vector2 Vector2_Rotate(Silkworm_Vector2 vector, float angle)
{
Silkworm_Vector2 rotated;
rotated.x = vector.x * cosf(angle) - vector.y * sinf(angle);
rotated.y = vector.x * sinf(angle) + vector.y * cosf(angle);
return rotated;
}
static inline Silkworm_Vector2 Vector2_Normalize(Silkworm_Vector2 vector) static inline Silkworm_Vector2 Vector2_Normalize(Silkworm_Vector2 vector)
{ {
float length = sqrtf(vector.x * vector.x + vector.y * vector.y); float length = sqrtf(vector.x * vector.x + vector.y * vector.y);
@ -184,7 +198,7 @@ static inline void NodeTriangleHashTable_Insert(
{ {
if (arr->elements[i].key == key) if (arr->elements[i].key == key)
{ {
arr->elements[i].indexArray = realloc(arr->elements[i].indexArray, sizeof(uint32_t) * (arr->elements[i].indexArrayCount + 1)); arr->elements[i].indexArray = Silkworm_realloc(arr->elements[i].indexArray, sizeof(uint32_t) * (arr->elements[i].indexArrayCount + 1));
arr->elements[i].indexArray[arr->elements[i].indexArrayCount] = index; arr->elements[i].indexArray[arr->elements[i].indexArrayCount] = index;
arr->elements[i].indexArrayCount += 1; arr->elements[i].indexArrayCount += 1;
foundKey = true; foundKey = true;
@ -194,10 +208,10 @@ static inline void NodeTriangleHashTable_Insert(
if (!foundKey) if (!foundKey)
{ {
arr->elements = realloc(arr->elements, sizeof(NodeTriangleHashMap) * (arr->count + 1)); arr->elements = Silkworm_realloc(arr->elements, sizeof(NodeTriangleHashMap) * (arr->count + 1));
arr->elements[arr->count].key = key; arr->elements[arr->count].key = key;
arr->elements[arr->count].indexArray = malloc(sizeof(uint32_t)); arr->elements[arr->count].indexArray = Silkworm_malloc(sizeof(uint32_t));
arr->elements[arr->count].indexArray[0] = index; arr->elements[arr->count].indexArray[0] = index;
arr->elements[arr->count].indexArrayCount = 1; arr->elements[arr->count].indexArrayCount = 1;
@ -266,7 +280,7 @@ static inline void LinkTriangleHashTable_Insert(
{ {
if (arr->elements[i].key == key) if (arr->elements[i].key == key)
{ {
arr->elements[i].indexArray = realloc(arr->elements[i].indexArray, sizeof(uint32_t) * (arr->elements[i].indexArrayCount + 1)); arr->elements[i].indexArray = Silkworm_realloc(arr->elements[i].indexArray, sizeof(uint32_t) * (arr->elements[i].indexArrayCount + 1));
arr->elements[i].indexArray[arr->elements[i].indexArrayCount] = index; arr->elements[i].indexArray[arr->elements[i].indexArrayCount] = index;
arr->elements[i].indexArrayCount += 1; arr->elements[i].indexArrayCount += 1;
foundKey = true; foundKey = true;
@ -276,10 +290,10 @@ static inline void LinkTriangleHashTable_Insert(
if (!foundKey) if (!foundKey)
{ {
arr->elements = realloc(arr->elements, sizeof(LinkTriangleHashMap) * (arr->count + 1)); arr->elements = Silkworm_realloc(arr->elements, sizeof(LinkTriangleHashMap) * (arr->count + 1));
arr->elements[arr->count].key = key; arr->elements[arr->count].key = key;
arr->elements[arr->count].indexArray = malloc(sizeof(uint32_t)); arr->elements[arr->count].indexArray = Silkworm_malloc(sizeof(uint32_t));
arr->elements[arr->count].indexArray[0] = index; arr->elements[arr->count].indexArray[0] = index;
arr->elements[arr->count].indexArrayCount = 1; arr->elements[arr->count].indexArrayCount = 1;
@ -368,7 +382,7 @@ static Silkworm_Context *context = NULL;
void Silkworm_Init() void Silkworm_Init()
{ {
context = malloc(sizeof(Silkworm_Context)); context = Silkworm_malloc(sizeof(Silkworm_Context));
context->nodes = NULL; context->nodes = NULL;
context->nodeCount = 0; context->nodeCount = 0;
@ -380,15 +394,15 @@ void Silkworm_Init()
context->clothCount = 0; context->clothCount = 0;
context->nodeIndexStackCapacity = 16; context->nodeIndexStackCapacity = 16;
context->nodeIndexStack = malloc(sizeof(uint64_t) * context->nodeIndexStackCapacity); context->nodeIndexStack = Silkworm_malloc(sizeof(uint64_t) * context->nodeIndexStackCapacity);
context->nodeIndexStackCount = 0; context->nodeIndexStackCount = 0;
context->linkIndexStackCapacity = 16; context->linkIndexStackCapacity = 16;
context->linkIndexStack = malloc(sizeof(uint64_t) * context->linkIndexStackCapacity); context->linkIndexStack = Silkworm_malloc(sizeof(uint64_t) * context->linkIndexStackCapacity);
context->linkIndexStackCount = 0; context->linkIndexStackCount = 0;
context->clothIndexStackCapacity = 16; context->clothIndexStackCapacity = 16;
context->clothIndexStack = malloc(sizeof(uint64_t) * context->clothIndexStackCapacity); context->clothIndexStack = Silkworm_malloc(sizeof(uint64_t) * context->clothIndexStackCapacity);
context->clothIndexStackCount = 0; context->clothIndexStackCount = 0;
context->gravity = 200; context->gravity = 200;
@ -398,15 +412,15 @@ void Silkworm_Init()
context->timeElapsed = 0; context->timeElapsed = 0;
context->nodeDestructionDataCapacity = 16; context->nodeDestructionDataCapacity = 16;
context->nodeDestructionData = malloc(sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity); context->nodeDestructionData = Silkworm_malloc(sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity);
context->nodeDestructionDataCount = 0; context->nodeDestructionDataCount = 0;
context->linkDestructionDataCapacity = 16; context->linkDestructionDataCapacity = 16;
context->linkDestructionData = malloc(sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity); context->linkDestructionData = Silkworm_malloc(sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity);
context->linkDestructionDataCount = 0; context->linkDestructionDataCount = 0;
context->vertexCapacity = 1024; context->vertexCapacity = 1024;
context->vertexBuffer = malloc(sizeof(Silkworm_Vertex) * context->vertexCapacity); context->vertexBuffer = Silkworm_malloc(sizeof(Silkworm_Vertex) * context->vertexCapacity);
} }
static inline Silkworm_Node* LookupNode(uint64_t nodeId) static inline Silkworm_Node* LookupNode(uint64_t nodeId)
@ -452,7 +466,7 @@ void Silkworm_DestroyLink(Silkworm_Link* link)
if (context->linkDestructionDataCount >= context->linkDestructionDataCapacity) if (context->linkDestructionDataCount >= context->linkDestructionDataCapacity)
{ {
context->linkDestructionDataCapacity *= 2; context->linkDestructionDataCapacity *= 2;
context->linkDestructionData = realloc(context->linkDestructionData, sizeof(Silkworm_Vector2) * context->linkDestructionDataCapacity); context->linkDestructionData = Silkworm_realloc(context->linkDestructionData, sizeof(Silkworm_Vector2) * context->linkDestructionDataCapacity);
} }
Silkworm_Vector2 position; Silkworm_Vector2 position;
@ -476,11 +490,11 @@ void Silkworm_ClothDestroy(void* clothPtr)
{ {
for (j = 0; j < cloth->nodeHash.buckets[i].count; j += 1) for (j = 0; j < cloth->nodeHash.buckets[i].count; j += 1)
{ {
free(cloth->nodeHash.buckets[i].elements[j].indexArray); Silkworm_free(cloth->nodeHash.buckets[i].elements[j].indexArray);
} }
if (cloth->nodeHash.buckets[i].elements != NULL) if (cloth->nodeHash.buckets[i].elements != NULL)
{ {
free(cloth->nodeHash.buckets[i].elements); Silkworm_free(cloth->nodeHash.buckets[i].elements);
} }
} }
@ -488,11 +502,11 @@ void Silkworm_ClothDestroy(void* clothPtr)
{ {
for (j = 0; j < cloth->linkHash.buckets[i].count; j += 1) for (j = 0; j < cloth->linkHash.buckets[i].count; j += 1)
{ {
free(cloth->linkHash.buckets[i].elements[j].indexArray); Silkworm_free(cloth->linkHash.buckets[i].elements[j].indexArray);
} }
if (cloth->linkHash.buckets[i].elements != NULL) if (cloth->linkHash.buckets[i].elements != NULL)
{ {
free(cloth->linkHash.buckets[i].elements); Silkworm_free(cloth->linkHash.buckets[i].elements);
} }
} }
@ -507,29 +521,29 @@ void Silkworm_ClothDestroy(void* clothPtr)
Silkworm_DestroyNode(node); Silkworm_DestroyNode(node);
} }
} }
free(cloth->nodes[i]); Silkworm_free(cloth->nodes[i]);
} }
free(cloth->nodes); Silkworm_free(cloth->nodes);
for (i = 0; i < cloth->triangleCount; i += 1) for (i = 0; i < cloth->triangleCount; i += 1)
{ {
if (cloth->triangles[i] != NULL) if (cloth->triangles[i] != NULL)
{ {
free(cloth->triangles[i]); Silkworm_free(cloth->triangles[i]);
} }
} }
free(cloth->triangles); Silkworm_free(cloth->triangles);
if (context->clothIndexStackCount >= context->clothIndexStackCapacity) if (context->clothIndexStackCount >= context->clothIndexStackCapacity)
{ {
context->clothIndexStackCapacity *= 2; context->clothIndexStackCapacity *= 2;
context->nodeIndexStack = realloc(context->nodeIndexStack, sizeof(uint64_t) * context->nodeIndexStackCapacity); context->nodeIndexStack = Silkworm_realloc(context->nodeIndexStack, sizeof(uint64_t) * context->nodeIndexStackCapacity);
} }
context->clothIndexStack[context->clothIndexStackCount] = cloth->id; context->clothIndexStack[context->clothIndexStackCount] = cloth->id;
context->clothIndexStackCount += 1; context->clothIndexStackCount += 1;
free(cloth); Silkworm_free(cloth);
} }
} }
@ -559,7 +573,7 @@ void Silkworm_PerformDestroys()
if (cloth->triangles[triangleIndex] != NULL) if (cloth->triangles[triangleIndex] != NULL)
{ {
free(cloth->triangles[triangleIndex]); Silkworm_free(cloth->triangles[triangleIndex]);
cloth->triangles[triangleIndex] = NULL; cloth->triangles[triangleIndex] = NULL;
} }
} }
@ -582,13 +596,13 @@ void Silkworm_PerformDestroys()
} }
} }
free(context->links[i]); Silkworm_free(context->links[i]);
context->links[i] = NULL; context->links[i] = NULL;
if (context->linkIndexStackCount >= context->linkIndexStackCapacity) if (context->linkIndexStackCount >= context->linkIndexStackCapacity)
{ {
context->linkIndexStackCapacity *= 2; context->linkIndexStackCapacity *= 2;
context->linkIndexStack = realloc(context->linkIndexStack, sizeof(uint64_t) * context->linkIndexStackCapacity); context->linkIndexStack = Silkworm_realloc(context->linkIndexStack, sizeof(uint64_t) * context->linkIndexStackCapacity);
} }
context->linkIndexStack[context->linkIndexStackCount] = i; context->linkIndexStack[context->linkIndexStackCount] = i;
@ -615,7 +629,7 @@ void Silkworm_PerformDestroys()
if (cloth->triangles[triangleIndex] != NULL) if (cloth->triangles[triangleIndex] != NULL)
{ {
free(cloth->triangles[triangleIndex]); Silkworm_free(cloth->triangles[triangleIndex]);
cloth->triangles[triangleIndex] = NULL; cloth->triangles[triangleIndex] = NULL;
} }
} }
@ -623,14 +637,14 @@ void Silkworm_PerformDestroys()
cloth->nodes[node->clothReference.horizontalIndex][node->clothReference.verticalIndex] = NULL; cloth->nodes[node->clothReference.horizontalIndex][node->clothReference.verticalIndex] = NULL;
} }
free(context->nodes[i]->links); Silkworm_free(context->nodes[i]->links);
free(context->nodes[i]); Silkworm_free(context->nodes[i]);
context->nodes[i] = NULL; context->nodes[i] = NULL;
if (context->nodeIndexStackCount >= context->nodeIndexStackCapacity) if (context->nodeIndexStackCount >= context->nodeIndexStackCapacity)
{ {
context->nodeIndexStackCapacity *= 2; context->nodeIndexStackCapacity *= 2;
context->nodeIndexStack = realloc(context->nodeIndexStack, sizeof(uint64_t) * context->nodeIndexStackCapacity); context->nodeIndexStack = Silkworm_realloc(context->nodeIndexStack, sizeof(uint64_t) * context->nodeIndexStackCapacity);
} }
context->nodeIndexStack[context->nodeIndexStackCount] = i; context->nodeIndexStack[context->nodeIndexStackCount] = i;
@ -657,8 +671,8 @@ void Silkworm_Update(float delta, float windSpeedX, float windSpeedY)
{ {
if (!node->pinned) if (!node->pinned)
{ {
float windFactorX = 0.5f * sinf(context->timeElapsed * 4 + node->position.y / 2) + 0.5f; float windFactorX = 0.5f * Silkworm_sin(context->timeElapsed * 4 + node->position.y / 2) + 0.5f;
float windFactorY = 0.5f * sinf(context->timeElapsed * 4 + node->position.x / 3) + 0.5f; float windFactorY = 0.5f * Silkworm_sin(context->timeElapsed * 4 + node->position.x / 3) + 0.5f;
node->position.x += windFactorX * windSpeedX * delta * 0.05f * node->windFactor; node->position.x += windFactorX * windSpeedX * delta * 0.05f * node->windFactor;
node->position.y += windFactorY * windSpeedY * delta * 0.05f * node->windFactor; node->position.y += windFactorY * windSpeedY * delta * 0.05f * node->windFactor;
} }
@ -731,7 +745,7 @@ void Silkworm_Update(float delta, float windSpeedX, float windSpeedY)
node->velocity.x = velocityX * delta; node->velocity.x = velocityX * delta;
node->velocity.y = velocityY * delta; node->velocity.y = velocityY * delta;
if (fabs(node->position.x) > context->xBound || fabs(node->position.x) > context->yBound) if (Silkworm_fabs(node->position.x) > context->xBound || Silkworm_fabs(node->position.x) > context->yBound)
{ {
Silkworm_DestroyNode(node); Silkworm_DestroyNode(node);
} }
@ -746,7 +760,7 @@ void Silkworm_Update(float delta, float windSpeedX, float windSpeedY)
void* Silkworm_CreateNode(Silkworm_NodeCreateInfo* nodeCreateInfo) void* Silkworm_CreateNode(Silkworm_NodeCreateInfo* nodeCreateInfo)
{ {
Silkworm_Node* node = malloc(sizeof(Silkworm_Node)); Silkworm_Node* node = Silkworm_malloc(sizeof(Silkworm_Node));
uint64_t id; uint64_t id;
if (context->nodeIndexStackCount > 0) if (context->nodeIndexStackCount > 0)
@ -758,7 +772,7 @@ void* Silkworm_CreateNode(Silkworm_NodeCreateInfo* nodeCreateInfo)
{ {
id = context->nodeCount; id = context->nodeCount;
context->nodes = realloc(context->nodes, sizeof(Silkworm_Node*) * (context->nodeCount + 1)); context->nodes = Silkworm_realloc(context->nodes, sizeof(Silkworm_Node*) * (context->nodeCount + 1));
context->nodeCount += 1; context->nodeCount += 1;
} }
@ -824,7 +838,7 @@ void* Silkworm_CreateLink(void* nodeAPtr, void* nodeBPtr, float distance, float
uint64_t id; uint64_t id;
Silkworm_Link* link = malloc(sizeof(Silkworm_Link)); Silkworm_Link* link = Silkworm_malloc(sizeof(Silkworm_Link));
if (context->linkIndexStackCount > 0) if (context->linkIndexStackCount > 0)
{ {
@ -835,7 +849,7 @@ void* Silkworm_CreateLink(void* nodeAPtr, void* nodeBPtr, float distance, float
{ {
id = context->linkCount; id = context->linkCount;
context->links = realloc(context->links, sizeof(Silkworm_Link*) * (context->linkCount + 1)); context->links = Silkworm_realloc(context->links, sizeof(Silkworm_Link*) * (context->linkCount + 1));
context->linkCount += 1; context->linkCount += 1;
} }
@ -848,11 +862,11 @@ void* Silkworm_CreateLink(void* nodeAPtr, void* nodeBPtr, float distance, float
link->tearThreshold = (float)tearThreshold; link->tearThreshold = (float)tearThreshold;
link->markedForDestroy = false; link->markedForDestroy = false;
link->a->links = realloc(link->a->links, sizeof(Silkworm_Link*) * (link->a->linkCount + 1)); link->a->links = Silkworm_realloc(link->a->links, sizeof(Silkworm_Link*) * (link->a->linkCount + 1));
link->a->links[link->a->linkCount] = link; link->a->links[link->a->linkCount] = link;
link->a->linkCount += 1; link->a->linkCount += 1;
link->b->links = realloc(link->b->links, sizeof(Silkworm_Link*) * (link->b->linkCount + 1)); link->b->links = Silkworm_realloc(link->b->links, sizeof(Silkworm_Link*) * (link->b->linkCount + 1));
link->b->links[link->b->linkCount] = link; link->b->links[link->b->linkCount] = link;
link->b->linkCount += 1; link->b->linkCount += 1;
@ -864,12 +878,12 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo)
int32_t i, j, k, m; int32_t i, j, k, m;
Silkworm_NodeCreateInfo nodeCreateInfo; Silkworm_NodeCreateInfo nodeCreateInfo;
Silkworm_Cloth* cloth = malloc(sizeof(Silkworm_Cloth)); Silkworm_Cloth* cloth = Silkworm_malloc(sizeof(Silkworm_Cloth));
cloth->horizontalNodeCount = (uint32_t)clothCreateInfo->horizontalNodeCount; cloth->horizontalNodeCount = (uint32_t)clothCreateInfo->horizontalNodeCount;
cloth->verticalNodeCount = (uint32_t)clothCreateInfo->verticalNodeCount; cloth->verticalNodeCount = (uint32_t)clothCreateInfo->verticalNodeCount;
cloth->nodes = malloc(sizeof(Silkworm_Node**) * cloth->horizontalNodeCount); cloth->nodes = Silkworm_malloc(sizeof(Silkworm_Node**) * cloth->horizontalNodeCount);
uint64_t id; uint64_t id;
@ -884,7 +898,7 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo)
{ {
id = context->clothCount; id = context->clothCount;
context->cloths = realloc(context->cloths, sizeof(Silkworm_Cloth*) * (context->clothCount + 1)); context->cloths = Silkworm_realloc(context->cloths, sizeof(Silkworm_Cloth*) * (context->clothCount + 1));
context->cloths[id] = cloth; context->cloths[id] = cloth;
context->clothCount += 1; context->clothCount += 1;
} }
@ -905,7 +919,7 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo)
for (i = 0; i < cloth->horizontalNodeCount; i += 1) for (i = 0; i < cloth->horizontalNodeCount; i += 1)
{ {
cloth->nodes[i] = malloc(sizeof(Silkworm_Node*) * cloth->verticalNodeCount); cloth->nodes[i] = Silkworm_malloc(sizeof(Silkworm_Node*) * cloth->verticalNodeCount);
for (j = 0; j < cloth->verticalNodeCount; j += 1) for (j = 0; j < cloth->verticalNodeCount; j += 1)
{ {
@ -931,7 +945,7 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo)
} }
} }
cloth->triangles = malloc(sizeof(Silkworm_Triangle*) * cloth->horizontalNodeCount * cloth->verticalNodeCount * 2); cloth->triangles = Silkworm_malloc(sizeof(Silkworm_Triangle*) * cloth->horizontalNodeCount * cloth->verticalNodeCount * 2);
uint32_t triangleIndex = 0; uint32_t triangleIndex = 0;
for (i = 0; i < cloth->horizontalNodeCount; i += 1) for (i = 0; i < cloth->horizontalNodeCount; i += 1)
@ -940,7 +954,7 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo)
{ {
if (i + 1 < cloth->horizontalNodeCount && j + 1 < cloth->verticalNodeCount) if (i + 1 < cloth->horizontalNodeCount && j + 1 < cloth->verticalNodeCount)
{ {
cloth->triangles[triangleIndex] = malloc(sizeof(Silkworm_Triangle)); cloth->triangles[triangleIndex] = Silkworm_malloc(sizeof(Silkworm_Triangle));
cloth->triangles[triangleIndex]->a = cloth->nodes[i][j]; cloth->triangles[triangleIndex]->a = cloth->nodes[i][j];
cloth->triangles[triangleIndex]->b = cloth->nodes[i + 1][j]; cloth->triangles[triangleIndex]->b = cloth->nodes[i + 1][j];
@ -965,7 +979,7 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo)
if (i - 1 >= 0 && j - 1 >= 0) if (i - 1 >= 0 && j - 1 >= 0)
{ {
cloth->triangles[triangleIndex] = malloc(sizeof(Silkworm_Triangle)); cloth->triangles[triangleIndex] = Silkworm_malloc(sizeof(Silkworm_Triangle));
cloth->triangles[triangleIndex]->a = cloth->nodes[i][j]; cloth->triangles[triangleIndex]->a = cloth->nodes[i][j];
cloth->triangles[triangleIndex]->b = cloth->nodes[i - 1][j]; cloth->triangles[triangleIndex]->b = cloth->nodes[i - 1][j];
@ -1069,7 +1083,7 @@ uint32_t Silkworm_ClothRender(void **pVertexBuffer, uint32_t *pVertexBufferLengt
if (vertexCount + (cloth->triangleCount * 3) > context->vertexCapacity) if (vertexCount + (cloth->triangleCount * 3) > context->vertexCapacity)
{ {
context->vertexCapacity *= 2; context->vertexCapacity *= 2;
context->vertexBuffer = realloc(context->vertexBuffer, sizeof(Silkworm_Vertex) * context->vertexCapacity); context->vertexBuffer = Silkworm_realloc(context->vertexBuffer, sizeof(Silkworm_Vertex) * context->vertexCapacity);
} }
for (i = 0; i < cloth->triangleCount; i += 1) for (i = 0; i < cloth->triangleCount; i += 1)
@ -1155,8 +1169,8 @@ void Silkworm_PushNodesInRadius(float x, float y, float radius, float xDirection
if (node != NULL && !node->pinned) if (node != NULL && !node->pinned)
{ {
float xDistance = (float)fabs(x - node->position.x); float xDistance = (float)Silkworm_fabs(x - node->position.x);
float yDistance = (float)fabs(y - node->position.y); float yDistance = (float)Silkworm_fabs(y - node->position.y);
float squareDistance = xDistance * xDistance + yDistance * yDistance; float squareDistance = xDistance * xDistance + yDistance * yDistance;
@ -1181,8 +1195,8 @@ void Silkworm_PinNodesInRadius(float x, float y, float radius)
if (node != NULL) if (node != NULL)
{ {
float xDistance = (float)fabs((float)x - node->position.x); float xDistance = (float)Silkworm_fabs((float)x - node->position.x);
float yDistance = (float)fabs((float)y - node->position.y); float yDistance = (float)Silkworm_fabs((float)y - node->position.y);
float squareDistance = xDistance * xDistance + yDistance * yDistance; float squareDistance = xDistance * xDistance + yDistance * yDistance;
@ -1206,8 +1220,8 @@ void Silkworm_UnpinNodesInRadius(float x, float y, float radius)
if (node != NULL) if (node != NULL)
{ {
float xDistance = (float)fabs((float)x - node->position.x); float xDistance = (float)Silkworm_fabs((float)x - node->position.x);
float yDistance = (float)fabs((float)y - node->position.y); float yDistance = (float)Silkworm_fabs((float)y - node->position.y);
float squareDistance = xDistance * xDistance + yDistance * yDistance; float squareDistance = xDistance * xDistance + yDistance * yDistance;
@ -1231,8 +1245,8 @@ void Silkworm_DestroyNodesInRadius(float x, float y, float radius)
if (node != NULL && node->destroyable) if (node != NULL && node->destroyable)
{ {
float xDistance = (float)fabs(x - node->position.x); float xDistance = (float)Silkworm_fabs(x - node->position.x);
float yDistance = (float)fabs(y - node->position.y); float yDistance = (float)Silkworm_fabs(y - node->position.y);
float squareDistance = xDistance * xDistance + yDistance * yDistance; float squareDistance = xDistance * xDistance + yDistance * yDistance;
@ -1243,7 +1257,7 @@ void Silkworm_DestroyNodesInRadius(float x, float y, float radius)
if (context->nodeDestructionDataCount >= context->nodeDestructionDataCapacity) if (context->nodeDestructionDataCount >= context->nodeDestructionDataCapacity)
{ {
context->nodeDestructionDataCapacity *= 2; context->nodeDestructionDataCapacity *= 2;
context->nodeDestructionData = realloc(context->nodeDestructionData, sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity); context->nodeDestructionData = Silkworm_realloc(context->nodeDestructionData, sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity);
} }
@ -1279,7 +1293,7 @@ void Silkworm_DestroyNodesInRectangle(Silkworm_Rectangle* rectangle)
if (context->nodeDestructionDataCount >= context->nodeDestructionDataCapacity) if (context->nodeDestructionDataCount >= context->nodeDestructionDataCapacity)
{ {
context->nodeDestructionDataCapacity *= 2; context->nodeDestructionDataCapacity *= 2;
context->nodeDestructionData = realloc(context->nodeDestructionData, sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity); context->nodeDestructionData = Silkworm_realloc(context->nodeDestructionData, sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity);
} }
@ -1302,8 +1316,8 @@ void* Silkworm_FindClothInRadius(float x, float y, float radius)
if (node != NULL) if (node != NULL)
{ {
float xDistance = (float)fabs(x - node->position.x); float xDistance = (float)Silkworm_fabs(x - node->position.x);
float yDistance = (float)fabs(y - node->position.y); float yDistance = (float)Silkworm_fabs(y - node->position.y);
float squareDistance = xDistance * xDistance + yDistance * yDistance; float squareDistance = xDistance * xDistance + yDistance * yDistance;
@ -1343,19 +1357,19 @@ void Silkworm_Finish()
{ {
Silkworm_ClearAll(); Silkworm_ClearAll();
free(context->nodes); Silkworm_free(context->nodes);
free(context->links); Silkworm_free(context->links);
free(context->cloths); Silkworm_free(context->cloths);
free(context->nodeIndexStack); Silkworm_free(context->nodeIndexStack);
free(context->linkIndexStack); Silkworm_free(context->linkIndexStack);
free(context->clothIndexStack); Silkworm_free(context->clothIndexStack);
free(context->nodeDestructionData); Silkworm_free(context->nodeDestructionData);
free(context->linkDestructionData); Silkworm_free(context->linkDestructionData);
free(context->vertexBuffer); Silkworm_free(context->vertexBuffer);
free(context); Silkworm_free(context);
context = NULL; context = NULL;
} }

View File

@ -59,9 +59,13 @@
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\SDL2\include;</AdditionalIncludeDirectories>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">_WINDLL;USE_SDL2;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>DebugFull</GenerateDebugInformation> <GenerateDebugInformation>DebugFull</GenerateDebugInformation>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">SDL2.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\SDL2\lib\$(PlatformShortName);</AdditionalLibraryDirectories>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'"> <ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
@ -70,10 +74,14 @@
<Optimization>MaxSpeed</Optimization> <Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">_WINDLL;USE_SDL2;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Release|x64'">..\..\SDL2\include;</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|x64'">SDL2.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories Condition="'$(Configuration)|$(Platform)'=='Release|x64'">..\..\SDL2\lib\$(PlatformShortName);</AdditionalLibraryDirectories>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>