Silkworm/src/Silkworm.h

151 lines
3.8 KiB
C

/* Silkworm - Verlet cloth physics in C
*
* Copyright (c) 2021 Evan Hemsley
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Evan "cosmonaut" Hemsley <evan@moonside.games>
*
*/
#ifndef SILKWORM_H
#define SILKWORM_H
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef _WIN32
#define SILKWORMAPI __declspec(dllexport)
#define SILKWORMCALL __cdecl
#else
#define SILKWORMAPI
#define SILKWORMCALL
#endif
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef struct Silkworm_Vector2
{
float x;
float y;
} Silkworm_Vector2;
typedef struct Silkworm_Node
{
uint64_t id;
Silkworm_Vector2 position;
Silkworm_Vector2 previousPosition;
Silkworm_Vector2 velocity;
Silkworm_Vector2 acceleration;
float mass;
float friction;
float radius;
float pinned;
float pushFactor;
bool destroyable;
} Silkworm_Node;
typedef struct Silkworm_Link
{
uint64_t id;
Silkworm_Node *a;
Silkworm_Node *b;
float distance;
float tearThreshold;
} Silkworm_Link;
typedef enum Silkworm_ClothTriangleOrientation
{
UpperLeft,
BottomRight
} Silkworm_ClothTriangleOrientation;
typedef struct Silkworm_Triangle
{
uint64_t id;
Silkworm_Node* a;
Silkworm_Node* b;
Silkworm_Node* c;
uint32_t aHorizontalIndex;
uint32_t bHorizontalIndex;
uint32_t cHorizontalIndex;
uint32_t aVerticalIndex;
uint32_t bVerticalIndex;
uint32_t cVerticalIndex;
Silkworm_ClothTriangleOrientation orientation;
} Silkworm_Triangle;
typedef struct Silkworm_Cloth
{
uint64_t id;
float windFactor;
uint32_t horizontalNodeCount;
uint32_t verticalNodeCount;
uint64_t **nodeIndices; /* x by y grid of indices */
Silkworm_Triangle **triangles; /* array of pointers so we can use NULL */
uint32_t triangleCount;
} Silkworm_Cloth;
typedef struct Silkworm_Rope
{
uint32_t nodeCount;
uint32_t *nodeIndices;
} Silkworm_Rope;
/* Version API */
#define SILKWORM_ABI_VERSION 0
#define SILKWORM_MAJOR_VERSION 0
#define SILKWORM_MINOR_VERSION 1
#define SILKWORM_PATCH_VERSION 0
#define SILKWORM_COMPILED_VERSION ( \
(REFRESH_ABI_VERSION * 100 * 100 * 100) + \
(REFRESH_MAJOR_VERSION * 100 * 100) + \
(REFRESH_MINOR_VERSION * 100) + \
(REFRESH_PATCH_VERSION) \
)
SILKWORMAPI void Silkworm_Init();
SILKWORMAPI void Silkworm_Update(double delta);
SILKWORMAPI double Silkworm_CreateNode(double xPosition, double yPosition, double mass, double friction, double radius, double pushFactor);
SILKWORMAPI void Silkworm_NodeSetDestroyable(double nodeId);
SILKWORMAPI void Silkworm_NodePin(double nodeId);
SILKWORMAPI void Silkworm_NodeUnpin(double nodeId);
SILKWORMAPI double Silkworm_CreateLink(double aId, double bId, double distance, double tearThreshold);
SILKWORMAPI double Silkworm_CreateCloth(double xPosition, double yPosition, double horizontalNodeCount, double verticalNodeCount, double mass, double friction, double windFactor, double tearThreshold);
SILKWORMAPI void Silkworm_SetTriangleBuffer(const char* bufferId);
SILKWORMAPI double Silkworm_ClothFillTriangleBuffer(double clothId, double leftUV, double widthUV, double topUV, double heightUV);
#endif /* SILKWORM_H */