rm core/ref; rm lovrAlloc; util does refcounting;

This commit is contained in:
bjorn 2021-02-08 17:23:18 -07:00
parent 1c3e29b3e5
commit 3ded60948f
51 changed files with 204 additions and 186 deletions

View File

@ -268,7 +268,6 @@ set(LOVR_SRC
src/core/arr.c src/core/arr.c
src/core/fs.c src/core/fs.c
src/core/map.c src/core/map.c
src/core/ref.c
src/core/utf.c src/core/utf.c
src/core/util.c src/core/util.c
src/core/zip.c src/core/zip.c

View File

@ -10,7 +10,6 @@ SRC += src/core/map.c
ifneq (@(PICO),y) ifneq (@(PICO),y)
SRC += src/core/os_$(PLATFORM).c SRC += src/core/os_$(PLATFORM).c
endif endif
SRC += src/core/ref.c
SRC += src/core/utf.c SRC += src/core/utf.c
SRC += src/core/util.c SRC += src/core/util.c
SRC += src/core/zip.c SRC += src/core/zip.c

View File

@ -1,6 +1,5 @@
#include "api.h" #include "api.h"
#include "core/os.h" #include "core/os.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>

View File

@ -3,7 +3,6 @@
#include "data/blob.h" #include "data/blob.h"
#include "data/soundData.h" #include "data/soundData.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>

View File

@ -4,7 +4,6 @@
#include "data/rasterizer.h" #include "data/rasterizer.h"
#include "data/soundData.h" #include "data/soundData.h"
#include "data/textureData.h" #include "data/textureData.h"
#include "core/ref.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -2,7 +2,6 @@
#include "event/event.h" #include "event/event.h"
#include "thread/thread.h" #include "thread/thread.h"
#include "core/os.h" #include "core/os.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -3,7 +3,6 @@
#include "data/blob.h" #include "data/blob.h"
#include "core/fs.h" #include "core/fs.h"
#include "core/os.h" #include "core/os.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -11,7 +11,6 @@
#include "data/rasterizer.h" #include "data/rasterizer.h"
#include "data/textureData.h" #include "data/textureData.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/ref.h"
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -1,7 +1,6 @@
#include "api.h" #include "api.h"
#include "graphics/canvas.h" #include "graphics/canvas.h"
#include "graphics/graphics.h" #include "graphics/graphics.h"
#include "core/ref.h"
#include <stdlib.h> #include <stdlib.h>
static int luax_checkattachment(lua_State* L, int index, Attachment* attachment) { static int luax_checkattachment(lua_State* L, int index, Attachment* attachment) {

View File

@ -4,7 +4,6 @@
#include "graphics/material.h" #include "graphics/material.h"
#include "graphics/mesh.h" #include "graphics/mesh.h"
#include "data/blob.h" #include "data/blob.h"
#include "core/ref.h"
#include <limits.h> #include <limits.h>
static int l_lovrMeshAttachAttributes(lua_State* L) { static int l_lovrMeshAttachAttributes(lua_State* L) {

View File

@ -6,7 +6,6 @@
#include "graphics/texture.h" #include "graphics/texture.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include <stdlib.h> #include <stdlib.h>
StringEntry lovrHeadsetDriver[] = { StringEntry lovrHeadsetDriver[] = {

View File

@ -4,7 +4,6 @@
#include "math/pool.h" #include "math/pool.h"
#include "math/randomGenerator.h" #include "math/randomGenerator.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>

View File

@ -1,6 +1,5 @@
#include "api.h" #include "api.h"
#include "physics/physics.h" #include "physics/physics.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
StringEntry lovrShapeType[] = { StringEntry lovrShapeType[] = {

View File

@ -1,6 +1,5 @@
#include "api.h" #include "api.h"
#include "physics/physics.h" #include "physics/physics.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdbool.h> #include <stdbool.h>

View File

@ -2,7 +2,6 @@
#include "event/event.h" #include "event/event.h"
#include "thread/thread.h" #include "thread/thread.h"
#include "thread/channel.h" #include "thread/channel.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <lualib.h> #include <lualib.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -1,10 +0,0 @@
#include "ref.h"
#include "util.h"
#include <stdlib.h>
void* _lovrAlloc(size_t size) {
char* ref = calloc(1, sizeof(size_t) + size);
lovrAssert(ref, "Out of memory");
*((Ref*) ref) = 1;
return ref + sizeof(size_t);
}

View File

@ -1,57 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#pragma once
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
#ifdef LOVR_DISABLE_THREAD
// Thread module is off, don't use atomics
typedef uint32_t Ref;
static inline uint32_t ref_inc(Ref* ref) { return ++*ref; }
static inline uint32_t ref_dec(Ref* ref) { return --*ref; }
#elif defined(_MSC_VER)
// MSVC atomics
#include <intrin.h>
typedef uint32_t Ref;
static inline uint32_t ref_inc(Ref* ref) { return _InterlockedIncrement((volatile long*) ref); }
static inline uint32_t ref_dec(Ref* ref) { return _InterlockedDecrement((volatile long*) ref); }
#elif (defined(__GNUC_MINOR__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))) \
|| (__has_builtin(__atomic_add_fetch) && __has_builtin(__atomic_sub_fetch))
// GCC/Clang atomics
typedef uint32_t Ref;
static inline uint32_t ref_inc(Ref* ref) { return __atomic_add_fetch(ref, 1, __ATOMIC_SEQ_CST); }
static inline uint32_t ref_dec(Ref* ref) { return __atomic_sub_fetch(ref, 1, __ATOMIC_SEQ_CST); }
#else
// No known compiler-specific atomics-- fall back to C11 atomics
// stdatomic.h doesn't work in c++ (except on Android, where it is fine)
#if !defined(__ANDROID__) && defined(__cplusplus)
#error "The header core/ref.h cannot currently be included from C++ when threading is enabled with this compiler. Either remove your ref.h include from any C++ files, or rebuild LOVR with -DLOVR_DISABLE_THREAD=ON"
#endif
#include <stdatomic.h>
typedef _Atomic(uint32_t) Ref;
static inline uint32_t ref_inc(Ref* ref) { return atomic_fetch_add(ref, 1) + 1; }
static inline uint32_t ref_dec(Ref* ref) { return atomic_fetch_sub(ref, 1) - 1; }
#endif
void* _lovrAlloc(size_t size);
#define toRef(o) ((Ref*) (((char*) (o)) - sizeof(size_t)))
#define lovrAlloc(T) (T*) _lovrAlloc(sizeof(T))
#define lovrRetain(o) if (o && !ref_inc(toRef(o))) { lovrThrow("Refcount overflow"); }
#define lovrRelease(T, o) if (o && !ref_dec(toRef(o))) lovr ## T ## Destroy(o), free(toRef(o));
#define _lovrRelease(o, f) if (o && !ref_dec(toRef(o))) f(o), free(toRef(o));

View File

@ -1,6 +1,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdatomic.h>
#pragma once #pragma once
@ -56,3 +57,9 @@ static inline uint64_t hash64(const void* data, size_t length) {
} }
return hash; return hash;
} }
// Refcounting
typedef atomic_uint ref_t;
#define lovrRetain(o) if (o) { atomic_fetch_add((ref_t*) (o), 1); }
#define lovrRelease(T, o) if (o && atomic_fetch_sub((ref_t*) (o), 1) == 1) lovr ## T ## Destroy(o), free(o);
#define _lovrRelease(o, f) if (o && atomic_fetch_sub((ref_t*) (o), 1) == 1) f(o), free(o);

View File

@ -2,7 +2,7 @@
#include "audio/spatializer.h" #include "audio/spatializer.h"
#include "data/soundData.h" #include "data/soundData.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/ref.h" #include "core/maf.h"
#include "core/os.h" #include "core/os.h"
#include "core/util.h" #include "core/util.h"
#include "lib/miniaudio/miniaudio.h" #include "lib/miniaudio/miniaudio.h"
@ -21,6 +21,7 @@ static const ma_format miniaudioFormats[] = {
#define BUFFER_SIZE 256 #define BUFFER_SIZE 256
struct Source { struct Source {
ref_t ref;
Source* next; Source* next;
SoundData* sound; SoundData* sound;
ma_data_converter* converter; ma_data_converter* converter;
@ -324,7 +325,9 @@ struct SoundData* lovrAudioGetCaptureStream() {
// Source // Source
Source* lovrSourceCreate(SoundData* sound, bool spatial) { Source* lovrSourceCreate(SoundData* sound, bool spatial) {
Source* source = lovrAlloc(Source); Source* source = calloc(1, sizeof(Source));
lovrAssert(source, "Out of memory");
source->ref = 1;
source->sound = sound; source->sound = sound;
lovrRetain(source->sound); lovrRetain(source->sound);

View File

@ -1,7 +1,10 @@
#include "data/blob.h" #include "data/blob.h"
#include <stdlib.h> #include <stdlib.h>
Blob* lovrBlobInit(Blob* blob, void* data, size_t size, const char* name) { Blob* lovrBlobCreate(void* data, size_t size, const char* name) {
Blob* blob = calloc(1, sizeof(Blob));
lovrAssert(blob, "Out of memory");
blob->ref = 1;
blob->data = data; blob->data = data;
blob->size = size; blob->size = size;
blob->name = name; blob->name = name;

View File

@ -1,13 +1,14 @@
#include <stddef.h> #include <stddef.h>
#include "core/util.h"
#pragma once #pragma once
typedef struct Blob { typedef struct Blob {
ref_t ref;
void* data; void* data;
size_t size; size_t size;
const char* name; const char* name;
} Blob; } Blob;
Blob* lovrBlobInit(Blob* blob, void* data, size_t size, const char* name); Blob* lovrBlobCreate(void* data, size_t size, const char* name);
#define lovrBlobCreate(...) lovrBlobInit(lovrAlloc(Blob), __VA_ARGS__)
void lovrBlobDestroy(void* ref); void lovrBlobDestroy(void* ref);

View File

@ -1,10 +1,13 @@
#include "data/modelData.h" #include "data/modelData.h"
#include "data/blob.h" #include "data/blob.h"
#include "data/textureData.h" #include "data/textureData.h"
#include "core/ref.h"
#include <stdlib.h> #include <stdlib.h>
ModelData* lovrModelDataInit(ModelData* model, Blob* source, ModelDataIO* io) { ModelData* lovrModelDataCreate(Blob* source, ModelDataIO* io) {
ModelData* model = calloc(1, sizeof(ModelData));
lovrAssert(model, "Out of memory");
model->ref = 1;
if (lovrModelDataInitGltf(model, source, io)) { if (lovrModelDataInitGltf(model, source, io)) {
return model; return model;
} else if (lovrModelDataInitObj(model, source, io)) { } else if (lovrModelDataInitObj(model, source, io)) {

View File

@ -179,6 +179,7 @@ typedef struct {
} ModelSkin; } ModelSkin;
typedef struct ModelData { typedef struct ModelData {
ref_t ref;
void* data; void* data;
struct Blob** blobs; struct Blob** blobs;
ModelBuffer* buffers; ModelBuffer* buffers;
@ -217,8 +218,7 @@ typedef struct ModelData {
typedef void* ModelDataIO(const char* filename, size_t* bytesRead); typedef void* ModelDataIO(const char* filename, size_t* bytesRead);
ModelData* lovrModelDataInit(ModelData* model, struct Blob* blob, ModelDataIO* io); ModelData* lovrModelDataCreate(struct Blob* blob, ModelDataIO* io);
#define lovrModelDataCreate(...) lovrModelDataInit(lovrAlloc(ModelData), __VA_ARGS__)
ModelData* lovrModelDataInitGltf(ModelData* model, struct Blob* blob, ModelDataIO* io); ModelData* lovrModelDataInitGltf(ModelData* model, struct Blob* blob, ModelDataIO* io);
ModelData* lovrModelDataInitObj(ModelData* model, struct Blob* blob, ModelDataIO* io); ModelData* lovrModelDataInitObj(ModelData* model, struct Blob* blob, ModelDataIO* io);
void lovrModelDataDestroy(void* ref); void lovrModelDataDestroy(void* ref);

View File

@ -2,7 +2,6 @@
#include "data/blob.h" #include "data/blob.h"
#include "data/textureData.h" #include "data/textureData.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include "lib/jsmn/jsmn.h" #include "lib/jsmn/jsmn.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -4,7 +4,6 @@
#include "core/arr.h" #include "core/arr.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/map.h" #include "core/map.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <float.h> #include <float.h>

View File

@ -2,7 +2,6 @@
#include "data/blob.h" #include "data/blob.h"
#include "data/textureData.h" #include "data/textureData.h"
#include "resources/VarelaRound.ttf.h" #include "resources/VarelaRound.ttf.h"
#include "core/ref.h"
#include "core/utf.h" #include "core/utf.h"
#include "lib/stb/stb_truetype.h" #include "lib/stb/stb_truetype.h"
#include <msdfgen-c.h> #include <msdfgen-c.h>
@ -10,7 +9,11 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
Rasterizer* lovrRasterizerInit(Rasterizer* rasterizer, Blob* blob, float size) { Rasterizer* lovrRasterizerCreate(Blob* blob, float size) {
Rasterizer* rasterizer = calloc(1, sizeof(Rasterizer));
lovrAssert(rasterizer, "Out of memory");
rasterizer->ref = 1;
stbtt_fontinfo* font = &rasterizer->font; stbtt_fontinfo* font = &rasterizer->font;
const unsigned char* data = blob ? blob->data : src_resources_VarelaRound_ttf; const unsigned char* data = blob ? blob->data : src_resources_VarelaRound_ttf;
if (!stbtt_InitFont(font, data, stbtt_GetFontOffsetForIndex(data, 0))) { if (!stbtt_InitFont(font, data, stbtt_GetFontOffsetForIndex(data, 0))) {

View File

@ -1,4 +1,5 @@
#include "lib/stb/stb_truetype.h" #include "lib/stb/stb_truetype.h"
#include "core/util.h"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@ -10,6 +11,7 @@ struct Blob;
struct TextureData; struct TextureData;
typedef struct Rasterizer { typedef struct Rasterizer {
ref_t ref;
stbtt_fontinfo font; stbtt_fontinfo font;
struct Blob* blob; struct Blob* blob;
float size; float size;
@ -34,8 +36,7 @@ typedef struct {
struct TextureData* data; struct TextureData* data;
} Glyph; } Glyph;
Rasterizer* lovrRasterizerInit(Rasterizer* rasterizer, struct Blob* blob, float size); Rasterizer* lovrRasterizerCreate(struct Blob* blob, float size);
#define lovrRasterizerCreate(...) lovrRasterizerInit(lovrAlloc(Rasterizer), __VA_ARGS__)
void lovrRasterizerDestroy(void* ref); void lovrRasterizerDestroy(void* ref);
bool lovrRasterizerHasGlyph(Rasterizer* fontData, uint32_t character); bool lovrRasterizerHasGlyph(Rasterizer* fontData, uint32_t character);
bool lovrRasterizerHasGlyphs(Rasterizer* fontData, const char* str); bool lovrRasterizerHasGlyphs(Rasterizer* fontData, const char* str);

View File

@ -1,7 +1,6 @@
#include "data/soundData.h" #include "data/soundData.h"
#include "data/blob.h" #include "data/blob.h"
#include "core/util.h" #include "core/util.h"
#include "core/ref.h"
#include "lib/stb/stb_vorbis.h" #include "lib/stb/stb_vorbis.h"
#include "lib/miniaudio/miniaudio.h" #include "lib/miniaudio/miniaudio.h"
#define MINIMP3_FLOAT_OUTPUT #define MINIMP3_FLOAT_OUTPUT
@ -16,6 +15,7 @@ static const ma_format miniaudioFormats[] = {
}; };
struct SoundData { struct SoundData {
ref_t ref;
uint32_t (*read)(SoundData* soundData, uint32_t offset, uint32_t count, void* data); uint32_t (*read)(SoundData* soundData, uint32_t offset, uint32_t count, void* data);
struct Blob* blob; struct Blob* blob;
void* decoder; void* decoder;
@ -73,7 +73,9 @@ static uint32_t lovrSoundDataReadMp3(SoundData* soundData, uint32_t offset, uint
// SoundData // SoundData
SoundData* lovrSoundDataCreateRaw(uint32_t frames, SampleFormat format, uint32_t channels, uint32_t sampleRate, struct Blob* blob) { SoundData* lovrSoundDataCreateRaw(uint32_t frames, SampleFormat format, uint32_t channels, uint32_t sampleRate, struct Blob* blob) {
SoundData* soundData = lovrAlloc(SoundData); SoundData* soundData = calloc(1, sizeof(SoundData));
lovrAssert(soundData, "Out of memory");
soundData->ref = 1;
soundData->frames = frames; soundData->frames = frames;
soundData->format = format; soundData->format = format;
soundData->channels = channels; soundData->channels = channels;
@ -92,7 +94,9 @@ SoundData* lovrSoundDataCreateRaw(uint32_t frames, SampleFormat format, uint32_t
} }
SoundData* lovrSoundDataCreateStream(uint32_t frames, SampleFormat format, uint32_t channels, uint32_t sampleRate) { SoundData* lovrSoundDataCreateStream(uint32_t frames, SampleFormat format, uint32_t channels, uint32_t sampleRate) {
SoundData* soundData = lovrAlloc(SoundData); SoundData* soundData = calloc(1, sizeof(SoundData));
lovrAssert(soundData, "Out of memory");
soundData->ref = 1;
soundData->frames = frames; soundData->frames = frames;
soundData->format = format; soundData->format = format;
soundData->channels = channels; soundData->channels = channels;
@ -110,7 +114,9 @@ SoundData* lovrSoundDataCreateStream(uint32_t frames, SampleFormat format, uint3
} }
SoundData* lovrSoundDataCreateFromFile(struct Blob* blob, bool decode) { SoundData* lovrSoundDataCreateFromFile(struct Blob* blob, bool decode) {
SoundData* soundData = lovrAlloc(SoundData); SoundData* soundData = calloc(1, sizeof(SoundData));
lovrAssert(soundData, "Out of memory");
soundData->ref = 1;
if (blob->size >= 4 && !memcmp(blob->data, "OggS", 4)) { if (blob->size >= 4 && !memcmp(blob->data, "OggS", 4)) {
soundData->decoder = stb_vorbis_open_memory(blob->data, (int) blob->size, NULL, NULL); soundData->decoder = stb_vorbis_open_memory(blob->data, (int) blob->size, NULL, NULL);

View File

@ -1,5 +1,4 @@
#include "data/textureData.h" #include "data/textureData.h"
#include "core/ref.h"
#include "lib/stb/stb_image.h" #include "lib/stb/stb_image.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
@ -453,7 +452,10 @@ static bool parseASTC(uint8_t* bytes, size_t size, TextureData* textureData) {
return true; return true;
} }
TextureData* lovrTextureDataInit(TextureData* textureData, uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format) { TextureData* lovrTextureDataCreate(uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format) {
TextureData* textureData = calloc(1, sizeof(TextureData));
lovrAssert(textureData, "Out of memory");
textureData->ref = 1;
size_t pixelSize = getPixelSize(format); size_t pixelSize = getPixelSize(format);
size_t size = width * height * pixelSize; size_t size = width * height * pixelSize;
lovrAssert(width > 0 && height > 0, "TextureData dimensions must be positive"); lovrAssert(width > 0 && height > 0, "TextureData dimensions must be positive");
@ -473,8 +475,11 @@ TextureData* lovrTextureDataInit(TextureData* textureData, uint32_t width, uint3
return textureData; return textureData;
} }
TextureData* lovrTextureDataInitFromBlob(TextureData* textureData, Blob* blob, bool flip) { TextureData* lovrTextureDataCreateFromBlob(Blob* blob, bool flip) {
textureData->blob = lovrAlloc(Blob); TextureData* textureData = calloc(1, sizeof(TextureData));
lovrAssert(textureData, "Out of memory");
textureData->ref = 1;
textureData->blob = lovrBlobCreate(NULL, 0, NULL);
if (parseDDS(blob->data, blob->size, textureData)) { if (parseDDS(blob->data, blob->size, textureData)) {
textureData->source = blob; textureData->source = blob;
lovrRetain(blob); lovrRetain(blob);

View File

@ -51,6 +51,7 @@ typedef struct {
} Mipmap; } Mipmap;
typedef struct TextureData { typedef struct TextureData {
ref_t ref;
Blob* blob; Blob* blob;
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
@ -60,10 +61,8 @@ typedef struct TextureData {
uint32_t mipmapCount; uint32_t mipmapCount;
} TextureData; } TextureData;
TextureData* lovrTextureDataInit(TextureData* textureData, uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format); TextureData* lovrTextureDataCreate(uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format);
TextureData* lovrTextureDataInitFromBlob(TextureData* textureData, Blob* blob, bool flip); TextureData* lovrTextureDataCreateFromBlob(Blob* blob, bool flip);
#define lovrTextureDataCreate(...) lovrTextureDataInit(lovrAlloc(TextureData), __VA_ARGS__)
#define lovrTextureDataCreateFromBlob(...) lovrTextureDataInitFromBlob(lovrAlloc(TextureData), __VA_ARGS__)
Color lovrTextureDataGetPixel(TextureData* textureData, uint32_t x, uint32_t y); Color lovrTextureDataGetPixel(TextureData* textureData, uint32_t x, uint32_t y);
void lovrTextureDataSetPixel(TextureData* textureData, uint32_t x, uint32_t y, Color color); void lovrTextureDataSetPixel(TextureData* textureData, uint32_t x, uint32_t y, Color color);
Blob* lovrTextureDataEncode(TextureData* textureData); Blob* lovrTextureDataEncode(TextureData* textureData);

View File

@ -2,7 +2,6 @@
#include "thread/thread.h" #include "thread/thread.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/os.h" #include "core/os.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include "core/utf.h" #include "core/utf.h"
#include <stdlib.h> #include <stdlib.h>

View File

@ -4,7 +4,6 @@
#include "data/textureData.h" #include "data/textureData.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/map.h" #include "core/map.h"
#include "core/ref.h"
#include "core/utf.h" #include "core/utf.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,6 +20,7 @@ typedef struct {
} FontAtlas; } FontAtlas;
struct Font { struct Font {
ref_t ref;
Rasterizer* rasterizer; Rasterizer* rasterizer;
Texture* texture; Texture* texture;
FontAtlas atlas; FontAtlas atlas;
@ -50,7 +50,10 @@ static void lovrFontExpandTexture(Font* font);
static void lovrFontCreateTexture(Font* font); static void lovrFontCreateTexture(Font* font);
Font* lovrFontCreate(Rasterizer* rasterizer) { Font* lovrFontCreate(Rasterizer* rasterizer) {
Font* font = lovrAlloc(Font); Font* font = calloc(1, sizeof(Font));
lovrAssert(font, "Out of memory");
font->ref = 1;
lovrRetain(rasterizer); lovrRetain(rasterizer);
font->rasterizer = rasterizer; font->rasterizer = rasterizer;
font->lineHeight = 1.f; font->lineHeight = 1.f;

View File

@ -9,7 +9,6 @@
#include "event/event.h" #include "event/event.h"
#include "math/math.h" #include "math/math.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -3,11 +3,14 @@
#include "graphics/shader.h" #include "graphics/shader.h"
#include "graphics/texture.h" #include "graphics/texture.h"
#include "resources/shaders.h" #include "resources/shaders.h"
#include "core/ref.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
Material* lovrMaterialInit(Material* material) { Material* lovrMaterialCreate() {
Material* material = calloc(1, sizeof(Material));
lovrAssert(material, "Out of memory");
material->ref = 1;
for (int i = 0; i < MAX_MATERIAL_SCALARS; i++) { for (int i = 0; i < MAX_MATERIAL_SCALARS; i++) {
material->scalars[i] = 1.f; material->scalars[i] = 1.f;
} }

View File

@ -7,14 +7,14 @@ struct Texture;
struct Shader; struct Shader;
typedef struct Material { typedef struct Material {
ref_t ref;
float scalars[MAX_MATERIAL_SCALARS]; float scalars[MAX_MATERIAL_SCALARS];
Color colors[MAX_MATERIAL_COLORS]; Color colors[MAX_MATERIAL_COLORS];
struct Texture* textures[MAX_MATERIAL_TEXTURES]; struct Texture* textures[MAX_MATERIAL_TEXTURES];
float transform[9]; float transform[9];
} Material; } Material;
Material* lovrMaterialInit(Material* material); Material* lovrMaterialCreate(void);
#define lovrMaterialCreate() lovrMaterialInit(lovrAlloc(Material))
void lovrMaterialDestroy(void* ref); void lovrMaterialDestroy(void* ref);
void lovrMaterialBind(Material* material, struct Shader* shader); void lovrMaterialBind(Material* material, struct Shader* shader);
float lovrMaterialGetScalar(Material* material, MaterialScalar scalarType); float lovrMaterialGetScalar(Material* material, MaterialScalar scalarType);

View File

@ -6,7 +6,6 @@
#include "graphics/texture.h" #include "graphics/texture.h"
#include "resources/shaders.h" #include "resources/shaders.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include <stdlib.h> #include <stdlib.h>
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
@ -16,6 +15,7 @@ typedef struct {
} NodeTransform; } NodeTransform;
struct Model { struct Model {
ref_t ref;
struct ModelData* data; struct ModelData* data;
struct Buffer** buffers; struct Buffer** buffers;
struct Mesh** meshes; struct Mesh** meshes;
@ -76,7 +76,9 @@ static void renderNode(Model* model, uint32_t nodeIndex, uint32_t instances) {
} }
Model* lovrModelCreate(ModelData* data) { Model* lovrModelCreate(ModelData* data) {
Model* model = lovrAlloc(Model); Model* model = calloc(1, sizeof(Model));
lovrAssert(model, "Out of memory");
model->ref = 1;
model->data = data; model->data = data;
lovrRetain(data); lovrRetain(data);

View File

@ -8,7 +8,6 @@
#include "resources/shaders.h" #include "resources/shaders.h"
#include "data/modelData.h" #include "data/modelData.h"
#include "math/math.h" #include "math/math.h"
#include "core/ref.h"
#include <math.h> #include <math.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
@ -40,6 +39,7 @@
#define LOVR_SHADER_DRAW_ID 7 #define LOVR_SHADER_DRAW_ID 7
struct Buffer { struct Buffer {
ref_t ref;
uint32_t id; uint32_t id;
void* data; void* data;
size_t size; size_t size;
@ -53,6 +53,7 @@ struct Buffer {
}; };
struct Texture { struct Texture {
ref_t ref;
GLuint id; GLuint id;
GLuint msaaId; GLuint msaaId;
GLenum target; GLenum target;
@ -74,6 +75,7 @@ struct Texture {
}; };
struct Canvas { struct Canvas {
ref_t ref;
uint32_t framebuffer; uint32_t framebuffer;
uint32_t resolveBuffer; uint32_t resolveBuffer;
uint32_t depthBuffer; uint32_t depthBuffer;
@ -89,6 +91,7 @@ struct Canvas {
}; };
struct ShaderBlock { struct ShaderBlock {
ref_t ref;
BlockType type; BlockType type;
arr_uniform_t uniforms; arr_uniform_t uniforms;
map_t uniformMap; map_t uniformMap;
@ -96,6 +99,7 @@ struct ShaderBlock {
}; };
struct Shader { struct Shader {
ref_t ref;
uint32_t program; uint32_t program;
ShaderType type; ShaderType type;
arr_uniform_t uniforms; arr_uniform_t uniforms;
@ -107,6 +111,7 @@ struct Shader {
}; };
struct Mesh { struct Mesh {
ref_t ref;
uint32_t vao; uint32_t vao;
uint32_t ibo; uint32_t ibo;
DrawMode mode; DrawMode mode;
@ -1665,13 +1670,15 @@ const GpuStats* lovrGpuGetStats() {
// Texture // Texture
Texture* lovrTextureCreate(TextureType type, TextureData** slices, uint32_t sliceCount, bool srgb, bool mipmaps, uint32_t msaa) { Texture* lovrTextureCreate(TextureType type, TextureData** slices, uint32_t sliceCount, bool srgb, bool mipmaps, uint32_t msaa) {
Texture* texture = lovrAlloc(Texture); Texture* texture = calloc(1, sizeof(Texture));
state.stats.textureCount++; lovrAssert(texture, "Out of memory");
texture->ref = 1;
texture->type = type; texture->type = type;
texture->srgb = srgb; texture->srgb = srgb;
texture->mipmaps = mipmaps; texture->mipmaps = mipmaps;
texture->target = convertTextureTarget(type); texture->target = convertTextureTarget(type);
texture->compareMode = COMPARE_NONE; texture->compareMode = COMPARE_NONE;
state.stats.textureCount++;
WrapMode wrap = type == TEXTURE_CUBE ? WRAP_CLAMP : WRAP_REPEAT; WrapMode wrap = type == TEXTURE_CUBE ? WRAP_CLAMP : WRAP_REPEAT;
glGenTextures(1, &texture->id); glGenTextures(1, &texture->id);
@ -1694,13 +1701,15 @@ Texture* lovrTextureCreate(TextureType type, TextureData** slices, uint32_t slic
} }
Texture* lovrTextureCreateFromHandle(uint32_t handle, TextureType type, uint32_t depth, uint32_t msaa) { Texture* lovrTextureCreateFromHandle(uint32_t handle, TextureType type, uint32_t depth, uint32_t msaa) {
Texture* texture = lovrAlloc(Texture); Texture* texture = calloc(1, sizeof(Texture));
state.stats.textureCount++; lovrAssert(texture, "Out of memory");
texture->ref = 1;
texture->type = type; texture->type = type;
texture->id = handle; texture->id = handle;
texture->target = convertTextureTarget(type); texture->target = convertTextureTarget(type);
texture->compareMode = COMPARE_NONE; texture->compareMode = COMPARE_NONE;
texture->native = true; texture->native = true;
state.stats.textureCount++;
int width, height; int width, height;
lovrGpuBindTexture(texture, 0); lovrGpuBindTexture(texture, 0);
@ -1978,7 +1987,10 @@ void lovrTextureSetWrap(Texture* texture, TextureWrap wrap) {
// Canvas // Canvas
Canvas* lovrCanvasCreate(uint32_t width, uint32_t height, CanvasFlags flags) { Canvas* lovrCanvasCreate(uint32_t width, uint32_t height, CanvasFlags flags) {
Canvas* canvas = lovrAlloc(Canvas); Canvas* canvas = calloc(1, sizeof(Canvas));
lovrAssert(canvas, "Out of memory");
canvas->ref = 1;
if (flags.stereo && state.singlepass != MULTIVIEW) { if (flags.stereo && state.singlepass != MULTIVIEW) {
width *= 2; width *= 2;
} }
@ -2023,7 +2035,9 @@ Canvas* lovrCanvasCreate(uint32_t width, uint32_t height, CanvasFlags flags) {
} }
Canvas* lovrCanvasCreateFromHandle(uint32_t width, uint32_t height, CanvasFlags flags, uint32_t framebuffer, uint32_t depthBuffer, uint32_t resolveBuffer, uint32_t attachmentCount, bool immortal) { Canvas* lovrCanvasCreateFromHandle(uint32_t width, uint32_t height, CanvasFlags flags, uint32_t framebuffer, uint32_t depthBuffer, uint32_t resolveBuffer, uint32_t attachmentCount, bool immortal) {
Canvas* canvas = lovrAlloc(Canvas); Canvas* canvas = calloc(1, sizeof(Canvas));
lovrAssert(canvas, "Out of memory");
canvas->ref = 1;
canvas->framebuffer = framebuffer; canvas->framebuffer = framebuffer;
canvas->depthBuffer = depthBuffer; canvas->depthBuffer = depthBuffer;
canvas->resolveBuffer = resolveBuffer; canvas->resolveBuffer = resolveBuffer;
@ -2200,7 +2214,10 @@ Texture* lovrCanvasGetDepthTexture(Canvas* canvas) {
// Buffer // Buffer
Buffer* lovrBufferCreate(size_t size, void* data, BufferType type, BufferUsage usage, bool readable) { Buffer* lovrBufferCreate(size_t size, void* data, BufferType type, BufferUsage usage, bool readable) {
Buffer* buffer = lovrAlloc(Buffer); Buffer* buffer = calloc(1, sizeof(Buffer));
lovrAssert(buffer, "Out of memory");
buffer->ref = 1;
state.stats.bufferCount++; state.stats.bufferCount++;
state.stats.bufferMemory += size; state.stats.bufferMemory += size;
buffer->size = size; buffer->size = size;
@ -2591,7 +2608,10 @@ static char* lovrShaderGetFlagCode(ShaderFlag* flags, uint32_t flagCount) {
} }
Shader* lovrShaderCreateGraphics(const char* vertexSource, int vertexSourceLength, const char* fragmentSource, int fragmentSourceLength, ShaderFlag* flags, uint32_t flagCount, bool multiview) { Shader* lovrShaderCreateGraphics(const char* vertexSource, int vertexSourceLength, const char* fragmentSource, int fragmentSourceLength, ShaderFlag* flags, uint32_t flagCount, bool multiview) {
Shader* shader = lovrAlloc(Shader); Shader* shader = calloc(1, sizeof(Shader));
lovrAssert(shader, "Out of memory");
shader->ref = 1;
#if defined(LOVR_WEBGL) || defined(LOVR_GLES) #if defined(LOVR_WEBGL) || defined(LOVR_GLES)
const char* version = "#version 300 es\n"; const char* version = "#version 300 es\n";
#else #else
@ -2687,7 +2707,9 @@ Shader* lovrShaderCreateDefault(DefaultShader type, ShaderFlag* flags, uint32_t
} }
Shader* lovrShaderCreateCompute(const char* source, int length, ShaderFlag* flags, uint32_t flagCount) { Shader* lovrShaderCreateCompute(const char* source, int length, ShaderFlag* flags, uint32_t flagCount) {
Shader* shader = lovrAlloc(Shader); Shader* shader = calloc(1, sizeof(Shader));
lovrAssert(shader, "Out of memory");
shader->ref = 1;
#ifdef LOVR_WEBGL #ifdef LOVR_WEBGL
lovrThrow("Compute shaders are not supported on this system"); lovrThrow("Compute shaders are not supported on this system");
#else #else
@ -2840,7 +2862,10 @@ size_t lovrShaderComputeUniformLayout(arr_uniform_t* uniforms) {
} }
ShaderBlock* lovrShaderBlockCreate(BlockType type, Buffer* buffer, arr_uniform_t* uniforms) { ShaderBlock* lovrShaderBlockCreate(BlockType type, Buffer* buffer, arr_uniform_t* uniforms) {
ShaderBlock* block = lovrAlloc(ShaderBlock); ShaderBlock* block = calloc(1, sizeof(ShaderBlock));
lovrAssert(block, "Out of memory");
block->ref = 1;
arr_init(&block->uniforms); arr_init(&block->uniforms);
map_init(&block->uniformMap, (uint32_t) uniforms->length); map_init(&block->uniformMap, (uint32_t) uniforms->length);
@ -2931,7 +2956,9 @@ Buffer* lovrShaderBlockGetBuffer(ShaderBlock* block) {
// Mesh // Mesh
Mesh* lovrMeshCreate(DrawMode mode, Buffer* vertexBuffer, uint32_t vertexCount) { Mesh* lovrMeshCreate(DrawMode mode, Buffer* vertexBuffer, uint32_t vertexCount) {
Mesh* mesh = lovrAlloc(Mesh); Mesh* mesh = calloc(1, sizeof(Mesh));
lovrAssert(mesh, "Out of memory");
mesh->ref = 1;
mesh->mode = mode; mesh->mode = mode;
mesh->vertexBuffer = vertexBuffer; mesh->vertexBuffer = vertexBuffer;
mesh->vertexCount = vertexCount; mesh->vertexCount = vertexCount;

View File

@ -6,7 +6,6 @@
#include "core/arr.h" #include "core/arr.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/map.h" #include "core/map.h"
#include "core/ref.h"
#include <OVR_CAPI.h> #include <OVR_CAPI.h>
#include <OVR_CAPI_GL.h> #include <OVR_CAPI_GL.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -23,7 +23,6 @@
#include "graphics/model.h" #include "graphics/model.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/os.h" #include "core/os.h"
#include "core/ref.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -618,7 +617,9 @@ static ModelData* openvr_newModelData(Device device, bool animated) {
} }
} }
ModelData* model = lovrAlloc(ModelData); ModelData* model = calloc(1, sizeof(ModelData));
lovrAssert(model, "Out of memory");
model->ref = 1;
model->blobCount = 2; model->blobCount = 2;
model->nodeCount = animated ? (1 + modelCount) : 1; model->nodeCount = animated ? (1 + modelCount) : 1;
model->bufferCount = 2 * modelCount; model->bufferCount = 2 * modelCount;

View File

@ -4,7 +4,6 @@
#include "graphics/graphics.h" #include "graphics/graphics.h"
#include "graphics/canvas.h" #include "graphics/canvas.h"
#include "graphics/texture.h" #include "graphics/texture.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>

View File

@ -5,7 +5,6 @@
#include "graphics/model.h" #include "graphics/model.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/os.h" #include "core/os.h"
#include "core/ref.h"
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -470,7 +469,9 @@ static struct ModelData* vrapi_newModelData(Device device, bool animated) {
return NULL; return NULL;
} }
ModelData* model = lovrAlloc(ModelData); ModelData* model = calloc(1, sizeof(ModelData));
lovrAssert(model, "Out of memory");
model->ref = 1;
model->blobCount = 2; model->blobCount = 2;
model->bufferCount = 6; model->bufferCount = 6;
model->attributeCount = 6; model->attributeCount = 6;

View File

@ -1,12 +1,12 @@
#include "math/curve.h" #include "math/curve.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
struct Curve { struct Curve {
ref_t ref;
arr_t(float) points; arr_t(float) points;
}; };
@ -51,7 +51,9 @@ static void evaluate(float* LOVR_RESTRICT P, size_t n, float t, vec3 p) {
} }
Curve* lovrCurveCreate(void) { Curve* lovrCurveCreate(void) {
Curve* curve = lovrAlloc(Curve); Curve* curve = calloc(1, sizeof(Curve));
lovrAssert(curve, "Out of memory");
curve->ref = 1;
arr_init(&curve->points); arr_init(&curve->points);
arr_reserve(&curve->points, 16); arr_reserve(&curve->points, 16);
return curve; return curve;

View File

@ -1,7 +1,6 @@
#include "math.h" #include "math.h"
#include "math/randomGenerator.h" #include "math/randomGenerator.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include "lib/noise1234/noise1234.h" #include "lib/noise1234/noise1234.h"
#include <math.h> #include <math.h>

View File

@ -10,7 +10,10 @@ static const size_t vectorComponents[] = {
[V_MAT4] = 16 [V_MAT4] = 16
}; };
Pool* lovrPoolInit(Pool* pool) { Pool* lovrPoolCreate() {
Pool* pool = calloc(1, sizeof(Pool));
lovrAssert(pool, "Out of memory");
pool->ref = 1;
lovrPoolGrow(pool, 1 << 12); lovrPoolGrow(pool, 1 << 12);
return pool; return pool;
} }

View File

@ -1,5 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "core/util.h"
#pragma once #pragma once
@ -24,14 +25,14 @@ typedef union {
} Vector; } Vector;
typedef struct Pool { typedef struct Pool {
ref_t ref;
float* data; float* data;
size_t count; size_t count;
size_t cursor; size_t cursor;
size_t generation; size_t generation;
} Pool; } Pool;
Pool* lovrPoolInit(Pool* pool); Pool* lovrPoolCreate(void);
#define lovrPoolCreate(...) lovrPoolInit(lovrAlloc(Pool))
void lovrPoolDestroy(void* ref); void lovrPoolDestroy(void* ref);
void lovrPoolGrow(Pool* pool, size_t count); void lovrPoolGrow(Pool* pool, size_t count);
Vector lovrPoolAllocate(Pool* pool, VectorType type, float** data); Vector lovrPoolAllocate(Pool* pool, VectorType type, float** data);

View File

@ -1,5 +1,4 @@
#include "math/randomGenerator.h" #include "math/randomGenerator.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
@ -7,6 +6,7 @@
#include <inttypes.h> #include <inttypes.h>
struct RandomGenerator { struct RandomGenerator {
ref_t ref;
Seed seed; Seed seed;
Seed state; Seed state;
double lastRandomNormal; double lastRandomNormal;
@ -30,7 +30,9 @@ static uint64_t wangHash64(uint64_t key) {
// Use an 'Xorshift*' variant, as shown here: http://xorshift.di.unimi.it // Use an 'Xorshift*' variant, as shown here: http://xorshift.di.unimi.it
RandomGenerator* lovrRandomGeneratorCreate(void) { RandomGenerator* lovrRandomGeneratorCreate(void) {
RandomGenerator* generator = lovrAlloc(RandomGenerator); RandomGenerator* generator = calloc(1, sizeof(RandomGenerator));
lovrAssert(generator, "Out of memory");
generator->ref = 1;
Seed seed = { .b32 = { .lo = 0xCBBF7A44, .hi = 0x0139408D } }; Seed seed = { .b32 = { .lo = 0xCBBF7A44, .hi = 0x0139408D } };
lovrRandomGeneratorSetSeed(generator, seed); lovrRandomGeneratorSetSeed(generator, seed);
generator->lastRandomNormal = HUGE_VAL; generator->lastRandomNormal = HUGE_VAL;

View File

@ -1,5 +1,4 @@
#include "physics.h" #include "physics.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
@ -54,7 +53,10 @@ void lovrPhysicsDestroy() {
initialized = false; initialized = false;
} }
World* lovrWorldInit(World* world, float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount) { World* lovrWorldCreate(float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount) {
World* world = calloc(1, sizeof(World));
lovrAssert(world, "Out of memory");
world->ref = 1;
world->id = dWorldCreate(); world->id = dWorldCreate();
world->space = dHashSpaceCreate(0); world->space = dHashSpaceCreate(0);
dHashSpaceSetLevels(world->space, -4, 8); dHashSpaceSetLevels(world->space, -4, 8);
@ -289,7 +291,10 @@ int lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const cha
return (world->masks[i] & (1 << j)) && (world->masks[j] & (1 << i)); return (world->masks[i] & (1 << j)) && (world->masks[j] & (1 << i));
} }
Collider* lovrColliderInit(Collider* collider, World* world, float x, float y, float z) { Collider* lovrColliderCreate(World* world, float x, float y, float z) {
Collider* collider = calloc(1, sizeof(Collider));
lovrAssert(collider, "Out of memory");
collider->ref = 1;
collider->body = dBodyCreate(world->id); collider->body = dBodyCreate(world->id);
collider->world = world; collider->world = world;
collider->friction = 0; collider->friction = 0;
@ -817,7 +822,10 @@ void lovrShapeGetAABB(Shape* shape, float aabb[6]) {
dGeomGetAABB(shape->id, aabb); dGeomGetAABB(shape->id, aabb);
} }
SphereShape* lovrSphereShapeInit(SphereShape* sphere, float radius) { SphereShape* lovrSphereShapeCreate(float radius) {
SphereShape* sphere = calloc(1, sizeof(SphereShape));
lovrAssert(sphere, "Out of memory");
sphere->ref = 1;
sphere->type = SHAPE_SPHERE; sphere->type = SHAPE_SPHERE;
sphere->id = dCreateSphere(0, radius); sphere->id = dCreateSphere(0, radius);
dGeomSetData(sphere->id, sphere); dGeomSetData(sphere->id, sphere);
@ -832,7 +840,10 @@ void lovrSphereShapeSetRadius(SphereShape* sphere, float radius) {
dGeomSphereSetRadius(sphere->id, radius); dGeomSphereSetRadius(sphere->id, radius);
} }
BoxShape* lovrBoxShapeInit(BoxShape* box, float x, float y, float z) { BoxShape* lovrBoxShapeCreate(float x, float y, float z) {
BoxShape* box = calloc(1, sizeof(BoxShape));
lovrAssert(box, "Out of memory");
box->ref = 1;
box->type = SHAPE_BOX; box->type = SHAPE_BOX;
box->id = dCreateBox(0, x, y, z); box->id = dCreateBox(0, x, y, z);
dGeomSetData(box->id, box); dGeomSetData(box->id, box);
@ -851,7 +862,10 @@ void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z) {
dGeomBoxSetLengths(box->id, x, y, z); dGeomBoxSetLengths(box->id, x, y, z);
} }
CapsuleShape* lovrCapsuleShapeInit(CapsuleShape* capsule, float radius, float length) { CapsuleShape* lovrCapsuleShapeCreate(float radius, float length) {
CapsuleShape* capsule = calloc(1, sizeof(CapsuleShape));
lovrAssert(capsule, "Out of memory");
capsule->ref = 1;
capsule->type = SHAPE_CAPSULE; capsule->type = SHAPE_CAPSULE;
capsule->id = dCreateCapsule(0, radius, length); capsule->id = dCreateCapsule(0, radius, length);
dGeomSetData(capsule->id, capsule); dGeomSetData(capsule->id, capsule);
@ -878,7 +892,10 @@ void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length) {
dGeomCapsuleSetParams(capsule->id, lovrCapsuleShapeGetRadius(capsule), length); dGeomCapsuleSetParams(capsule->id, lovrCapsuleShapeGetRadius(capsule), length);
} }
CylinderShape* lovrCylinderShapeInit(CylinderShape* cylinder, float radius, float length) { CylinderShape* lovrCylinderShapeCreate(float radius, float length) {
CylinderShape* cylinder = calloc(1, sizeof(CylinderShape));
lovrAssert(cylinder, "Out of memory");
cylinder->ref = 1;
cylinder->type = SHAPE_CYLINDER; cylinder->type = SHAPE_CYLINDER;
cylinder->id = dCreateCylinder(0, radius, length); cylinder->id = dCreateCylinder(0, radius, length);
dGeomSetData(cylinder->id, cylinder); dGeomSetData(cylinder->id, cylinder);
@ -905,7 +922,10 @@ void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length) {
dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), length); dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), length);
} }
MeshShape* lovrMeshShapeInit(MeshShape* mesh, int vertexCount, float vertices[], int indexCount, dTriIndex indices[]) { MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount, dTriIndex indices[]) {
MeshShape* mesh = calloc(1, sizeof(MeshShape));
lovrAssert(mesh, "Out of memory");
mesh->ref = 1;
dTriMeshDataID dataID = dGeomTriMeshDataCreate(); dTriMeshDataID dataID = dGeomTriMeshDataCreate();
dGeomTriMeshDataBuildSingle(dataID, vertices, 3 * sizeof(float), vertexCount, indices, indexCount, 3 * sizeof(dTriIndex)); dGeomTriMeshDataBuildSingle(dataID, vertices, 3 * sizeof(float), vertexCount, indices, indexCount, 3 * sizeof(dTriIndex));
dGeomTriMeshDataPreprocess2(dataID, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL); dGeomTriMeshDataPreprocess2(dataID, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL);
@ -964,8 +984,11 @@ void lovrJointSetEnabled(Joint* joint, bool enable) {
} }
} }
BallJoint* lovrBallJointInit(BallJoint* joint, Collider* a, Collider* b, float x, float y, float z) { BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float x, float y, float z) {
lovrAssert(a->world == b->world, "Joint bodies must exist in same World"); lovrAssert(a->world == b->world, "Joint bodies must exist in same World");
BallJoint* joint = calloc(1, sizeof(BallJoint));
lovrAssert(joint, "Out of memory");
joint->ref = 1;
joint->type = JOINT_BALL; joint->type = JOINT_BALL;
joint->id = dJointCreateBall(a->world->id, 0); joint->id = dJointCreateBall(a->world->id, 0);
dJointSetData(joint->id, joint); dJointSetData(joint->id, joint);
@ -1007,8 +1030,11 @@ void lovrBallJointSetTightness(Joint* joint, float tightness) {
dJointSetBallParam(joint->id, dParamERP, tightness); dJointSetBallParam(joint->id, dParamERP, tightness);
} }
DistanceJoint* lovrDistanceJointInit(DistanceJoint* joint, Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2) { DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2) {
lovrAssert(a->world == b->world, "Joint bodies must exist in same World"); lovrAssert(a->world == b->world, "Joint bodies must exist in same World");
DistanceJoint* joint = calloc(1, sizeof(DistanceJoint));
lovrAssert(joint, "Out of memory");
joint->ref = 1;
joint->type = JOINT_DISTANCE; joint->type = JOINT_DISTANCE;
joint->id = dJointCreateDBall(a->world->id, 0); joint->id = dJointCreateDBall(a->world->id, 0);
dJointSetData(joint->id, joint); dJointSetData(joint->id, joint);
@ -1059,8 +1085,11 @@ void lovrDistanceJointSetTightness(Joint* joint, float tightness) {
dJointSetDBallParam(joint->id, dParamERP, tightness); dJointSetDBallParam(joint->id, dParamERP, tightness);
} }
HingeJoint* lovrHingeJointInit(HingeJoint* joint, Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az) { HingeJoint* lovrHingeJointCreate(Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az) {
lovrAssert(a->world == b->world, "Joint bodies must exist in same World"); lovrAssert(a->world == b->world, "Joint bodies must exist in same World");
HingeJoint* joint = calloc(1, sizeof(HingeJoint));
lovrAssert(joint, "Out of memory");
joint->ref = 1;
joint->type = JOINT_HINGE; joint->type = JOINT_HINGE;
joint->id = dJointCreateHinge(a->world->id, 0); joint->id = dJointCreateHinge(a->world->id, 0);
dJointSetData(joint->id, joint); dJointSetData(joint->id, joint);
@ -1119,8 +1148,11 @@ void lovrHingeJointSetUpperLimit(HingeJoint* joint, float limit) {
dJointSetHingeParam(joint->id, dParamHiStop, limit); dJointSetHingeParam(joint->id, dParamHiStop, limit);
} }
SliderJoint* lovrSliderJointInit(SliderJoint* joint, Collider* a, Collider* b, float ax, float ay, float az) { SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float ax, float ay, float az) {
lovrAssert(a->world == b->world, "Joint bodies must exist in the same world"); lovrAssert(a->world == b->world, "Joint bodies must exist in the same world");
SliderJoint* joint = calloc(1, sizeof(SliderJoint));
lovrAssert(joint, "Out of memory");
joint->ref = 1;
joint->type = JOINT_SLIDER; joint->type = JOINT_SLIDER;
joint->id = dJointCreateSlider(a->world->id, 0); joint->id = dJointCreateSlider(a->world->id, 0);
dJointSetData(joint->id, joint); dJointSetData(joint->id, joint);

View File

@ -1,5 +1,6 @@
#include "core/arr.h" #include "core/arr.h"
#include "core/maf.h" #include "core/maf.h"
#include "core/util.h"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <ode/ode.h> #include <ode/ode.h>
@ -30,6 +31,7 @@ typedef struct Shape Shape;
typedef struct Joint Joint; typedef struct Joint Joint;
typedef struct { typedef struct {
ref_t ref;
dWorldID id; dWorldID id;
dSpaceID space; dSpaceID space;
dJointGroupID contactGroup; dJointGroupID contactGroup;
@ -40,6 +42,7 @@ typedef struct {
} World; } World;
struct Collider { struct Collider {
ref_t ref;
dBodyID body; dBodyID body;
World* world; World* world;
Collider* prev; Collider* prev;
@ -53,6 +56,7 @@ struct Collider {
}; };
struct Shape { struct Shape {
ref_t ref;
ShapeType type; ShapeType type;
dGeomID id; dGeomID id;
Collider* collider; Collider* collider;
@ -67,6 +71,7 @@ typedef Shape CylinderShape;
typedef Shape MeshShape; typedef Shape MeshShape;
struct Joint { struct Joint {
ref_t ref;
JointType type; JointType type;
dJointID id; dJointID id;
void* userdata; void* userdata;
@ -88,8 +93,7 @@ typedef struct {
bool lovrPhysicsInit(void); bool lovrPhysicsInit(void);
void lovrPhysicsDestroy(void); void lovrPhysicsDestroy(void);
World* lovrWorldInit(World* world, float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount); World* lovrWorldCreate(float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount);
#define lovrWorldCreate(...) lovrWorldInit(lovrAlloc(World), __VA_ARGS__)
void lovrWorldDestroy(void* ref); void lovrWorldDestroy(void* ref);
void lovrWorldDestroyData(World* world); void lovrWorldDestroyData(World* world);
void lovrWorldUpdate(World* world, float dt, CollisionResolver resolver, void* userdata); void lovrWorldUpdate(World* world, float dt, CollisionResolver resolver, void* userdata);
@ -115,8 +119,7 @@ int lovrWorldDisableCollisionBetween(World* world, const char* tag1, const char*
int lovrWorldEnableCollisionBetween(World* world, const char* tag1, const char* tag2); int lovrWorldEnableCollisionBetween(World* world, const char* tag1, const char* tag2);
int lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const char* tag); int lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const char* tag);
Collider* lovrColliderInit(Collider* collider, World* world, float x, float y, float z); Collider* lovrColliderCreate(World* world, float x, float y, float z);
#define lovrColliderCreate(...) lovrColliderInit(lovrAlloc(Collider), __VA_ARGS__)
void lovrColliderDestroy(void* ref); void lovrColliderDestroy(void* ref);
void lovrColliderDestroyData(Collider* collider); void lovrColliderDestroyData(Collider* collider);
void lovrColliderInitInertia(Collider* collider, Shape* shape); void lovrColliderInitInertia(Collider* collider, Shape* shape);
@ -186,36 +189,31 @@ void lovrShapeSetOrientation(Shape* shape, quat orientation);
void lovrShapeGetMass(Shape* shape, float density, float* cx, float* cy, float* cz, float* mass, float inertia[6]); void lovrShapeGetMass(Shape* shape, float density, float* cx, float* cy, float* cz, float* mass, float inertia[6]);
void lovrShapeGetAABB(Shape* shape, float aabb[6]); void lovrShapeGetAABB(Shape* shape, float aabb[6]);
SphereShape* lovrSphereShapeInit(SphereShape* sphere, float radius); SphereShape* lovrSphereShapeCreate(float radius);
#define lovrSphereShapeCreate(...) lovrSphereShapeInit(lovrAlloc(SphereShape), __VA_ARGS__)
#define lovrSphereShapeDestroy lovrShapeDestroy #define lovrSphereShapeDestroy lovrShapeDestroy
float lovrSphereShapeGetRadius(SphereShape* sphere); float lovrSphereShapeGetRadius(SphereShape* sphere);
void lovrSphereShapeSetRadius(SphereShape* sphere, float radius); void lovrSphereShapeSetRadius(SphereShape* sphere, float radius);
BoxShape* lovrBoxShapeInit(BoxShape* box, float x, float y, float z); BoxShape* lovrBoxShapeCreate(float x, float y, float z);
#define lovrBoxShapeCreate(...) lovrBoxShapeInit(lovrAlloc(BoxShape), __VA_ARGS__)
#define lovrBoxShapeDestroy lovrShapeDestroy #define lovrBoxShapeDestroy lovrShapeDestroy
void lovrBoxShapeGetDimensions(BoxShape* box, float* x, float* y, float* z); void lovrBoxShapeGetDimensions(BoxShape* box, float* x, float* y, float* z);
void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z); void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z);
CapsuleShape* lovrCapsuleShapeInit(CapsuleShape* capsule, float radius, float length); CapsuleShape* lovrCapsuleShapeCreate(float radius, float length);
#define lovrCapsuleShapeCreate(...) lovrCapsuleShapeInit(lovrAlloc(CapsuleShape), __VA_ARGS__)
#define lovrCapsuleShapeDestroy lovrShapeDestroy #define lovrCapsuleShapeDestroy lovrShapeDestroy
float lovrCapsuleShapeGetRadius(CapsuleShape* capsule); float lovrCapsuleShapeGetRadius(CapsuleShape* capsule);
void lovrCapsuleShapeSetRadius(CapsuleShape* capsule, float radius); void lovrCapsuleShapeSetRadius(CapsuleShape* capsule, float radius);
float lovrCapsuleShapeGetLength(CapsuleShape* capsule); float lovrCapsuleShapeGetLength(CapsuleShape* capsule);
void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length); void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length);
CylinderShape* lovrCylinderShapeInit(CylinderShape* cylinder, float radius, float length); CylinderShape* lovrCylinderShapeCreate(float radius, float length);
#define lovrCylinderShapeCreate(...) lovrCylinderShapeInit(lovrAlloc(CylinderShape), __VA_ARGS__)
#define lovrCylinderShapeDestroy lovrShapeDestroy #define lovrCylinderShapeDestroy lovrShapeDestroy
float lovrCylinderShapeGetRadius(CylinderShape* cylinder); float lovrCylinderShapeGetRadius(CylinderShape* cylinder);
void lovrCylinderShapeSetRadius(CylinderShape* cylinder, float radius); void lovrCylinderShapeSetRadius(CylinderShape* cylinder, float radius);
float lovrCylinderShapeGetLength(CylinderShape* cylinder); float lovrCylinderShapeGetLength(CylinderShape* cylinder);
void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length); void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length);
MeshShape* lovrMeshShapeInit(MeshShape* mesh, int vertexCount, float vertices[], int indexCount, dTriIndex indices[]); MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount, dTriIndex indices[]);
#define lovrMeshShapeCreate(...) lovrMeshShapeInit(lovrAlloc(MeshShape), __VA_ARGS__)
#define lovrMeshShapeDestroy lovrShapeDestroy #define lovrMeshShapeDestroy lovrShapeDestroy
void lovrJointDestroy(void* ref); void lovrJointDestroy(void* ref);
@ -231,8 +229,7 @@ void lovrJointSetUserData(Joint* joint, void* data);
bool lovrJointIsEnabled(Joint* joint); bool lovrJointIsEnabled(Joint* joint);
void lovrJointSetEnabled(Joint* joint, bool enable); void lovrJointSetEnabled(Joint* joint, bool enable);
BallJoint* lovrBallJointInit(BallJoint* joint, Collider* a, Collider* b, float x, float y, float z); BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float x, float y, float z);
#define lovrBallJointCreate(...) lovrBallJointInit(lovrAlloc(BallJoint), __VA_ARGS__)
#define lovrBallJointDestroy lovrJointDestroy #define lovrBallJointDestroy lovrJointDestroy
void lovrBallJointGetAnchors(BallJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2); void lovrBallJointGetAnchors(BallJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2);
void lovrBallJointSetAnchor(BallJoint* joint, float x, float y, float z); void lovrBallJointSetAnchor(BallJoint* joint, float x, float y, float z);
@ -241,8 +238,7 @@ void lovrBallJointSetResponseTime(Joint* joint, float responseTime);
float lovrBallJointGetTightness(Joint* joint); float lovrBallJointGetTightness(Joint* joint);
void lovrBallJointSetTightness(Joint* joint, float tightness); void lovrBallJointSetTightness(Joint* joint, float tightness);
DistanceJoint* lovrDistanceJointInit(DistanceJoint* joint, Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2); DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2);
#define lovrDistanceJointCreate(...) lovrDistanceJointInit(lovrAlloc(DistanceJoint), __VA_ARGS__)
#define lovrDistanceJointDestroy lovrJointDestroy #define lovrDistanceJointDestroy lovrJointDestroy
void lovrDistanceJointGetAnchors(DistanceJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2); void lovrDistanceJointGetAnchors(DistanceJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2);
void lovrDistanceJointSetAnchors(DistanceJoint* joint, float x1, float y1, float z1, float x2, float y2, float z2); void lovrDistanceJointSetAnchors(DistanceJoint* joint, float x1, float y1, float z1, float x2, float y2, float z2);
@ -253,8 +249,7 @@ void lovrDistanceJointSetResponseTime(Joint* joint, float responseTime);
float lovrDistanceJointGetTightness(Joint* joint); float lovrDistanceJointGetTightness(Joint* joint);
void lovrDistanceJointSetTightness(Joint* joint, float tightness); void lovrDistanceJointSetTightness(Joint* joint, float tightness);
HingeJoint* lovrHingeJointInit(HingeJoint* joint, Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az); HingeJoint* lovrHingeJointCreate(Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az);
#define lovrHingeJointCreate(...) lovrHingeJointInit(lovrAlloc(HingeJoint), __VA_ARGS__)
#define lovrHingeJointDestroy lovrJointDestroy #define lovrHingeJointDestroy lovrJointDestroy
void lovrHingeJointGetAnchors(HingeJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2); void lovrHingeJointGetAnchors(HingeJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2);
void lovrHingeJointSetAnchor(HingeJoint* joint, float x, float y, float z); void lovrHingeJointSetAnchor(HingeJoint* joint, float x, float y, float z);
@ -266,8 +261,7 @@ void lovrHingeJointSetLowerLimit(HingeJoint* joint, float limit);
float lovrHingeJointGetUpperLimit(HingeJoint* joint); float lovrHingeJointGetUpperLimit(HingeJoint* joint);
void lovrHingeJointSetUpperLimit(HingeJoint* joint, float limit); void lovrHingeJointSetUpperLimit(HingeJoint* joint, float limit);
SliderJoint* lovrSliderJointInit(SliderJoint* joint, Collider* a, Collider* b, float ax, float ay, float az); SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float ax, float ay, float az);
#define lovrSliderJointCreate(...) lovrSliderJointInit(lovrAlloc(SliderJoint), __VA_ARGS__)
#define lovrSliderJointDestroy lovrJointDestroy #define lovrSliderJointDestroy lovrJointDestroy
void lovrSliderJointGetAxis(SliderJoint* joint, float* x, float* y, float* z); void lovrSliderJointGetAxis(SliderJoint* joint, float* x, float* y, float* z);
void lovrSliderJointSetAxis(SliderJoint* joint, float x, float y, float z); void lovrSliderJointSetAxis(SliderJoint* joint, float x, float y, float z);

View File

@ -1,7 +1,6 @@
#include "thread/channel.h" #include "thread/channel.h"
#include "event/event.h" #include "event/event.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include "lib/tinycthread/tinycthread.h" #include "lib/tinycthread/tinycthread.h"
#include <stdlib.h> #include <stdlib.h>
@ -9,6 +8,7 @@
#include <math.h> #include <math.h>
struct Channel { struct Channel {
ref_t ref;
mtx_t lock; mtx_t lock;
cnd_t cond; cnd_t cond;
arr_t(Variant) messages; arr_t(Variant) messages;
@ -19,7 +19,9 @@ struct Channel {
}; };
Channel* lovrChannelCreate(uint64_t hash) { Channel* lovrChannelCreate(uint64_t hash) {
Channel* channel = lovrAlloc(Channel); Channel* channel = calloc(1, sizeof(Channel));
lovrAssert(channel, "Out of memory");
channel->ref = 1;
arr_init(&channel->messages); arr_init(&channel->messages);
mtx_init(&channel->lock, mtx_plain | mtx_timed); mtx_init(&channel->lock, mtx_plain | mtx_timed);
cnd_init(&channel->cond); cnd_init(&channel->cond);

View File

@ -2,7 +2,6 @@
#include "thread/channel.h" #include "thread/channel.h"
#include "core/arr.h" #include "core/arr.h"
#include "core/map.h" #include "core/map.h"
#include "core/ref.h"
#include "core/util.h" #include "core/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -53,11 +52,14 @@ Channel* lovrThreadGetChannel(const char* name) {
return entry.channel; return entry.channel;
} }
Thread* lovrThreadInit(Thread* thread, int (*runner)(void*), Blob* body) { Thread* lovrThreadCreate(int (*runner)(void*), Blob* body) {
lovrRetain(body); Thread* thread = calloc(1, sizeof(Thread));
lovrAssert(thread, "Out of memory");
thread->ref = 1;
thread->runner = runner; thread->runner = runner;
thread->body = body; thread->body = body;
mtx_init(&thread->lock, mtx_plain); mtx_init(&thread->lock, mtx_plain);
lovrRetain(body);
return thread; return thread;
} }

View File

@ -1,5 +1,6 @@
#include "data/blob.h" #include "data/blob.h"
#include "event/event.h" #include "event/event.h"
#include "core/util.h"
#include "lib/tinycthread/tinycthread.h" #include "lib/tinycthread/tinycthread.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -14,6 +15,7 @@
struct Channel; struct Channel;
typedef struct Thread { typedef struct Thread {
ref_t ref;
thrd_t handle; thrd_t handle;
mtx_t lock; mtx_t lock;
Blob* body; Blob* body;
@ -29,8 +31,7 @@ void lovrThreadModuleDestroy(void);
struct Channel* lovrThreadGetChannel(const char* name); struct Channel* lovrThreadGetChannel(const char* name);
void lovrThreadRemoveChannel(uint64_t hash); void lovrThreadRemoveChannel(uint64_t hash);
Thread* lovrThreadInit(Thread* thread, int (*runner)(void*), Blob* body); Thread* lovrThreadCreate(int (*runner)(void*), Blob* body);
#define lovrThreadCreate(...) lovrThreadInit(lovrAlloc(Thread), __VA_ARGS__)
void lovrThreadDestroy(void* ref); void lovrThreadDestroy(void* ref);
void lovrThreadStart(Thread* thread, Variant* arguments, uint32_t argumentCount); void lovrThreadStart(Thread* thread, Variant* arguments, uint32_t argumentCount);
void lovrThreadWait(Thread* thread); void lovrThreadWait(Thread* thread);