-
Notifications
You must be signed in to change notification settings - Fork 9
/
vector2.c
498 lines (404 loc) · 16.3 KB
/
vector2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* If defined, the following flags inhibit definition of the indicated items.*/
#define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
#define NOVIRTUALKEYCODES // VK_*
#define NOWINMESSAGES // WM_*, EM_*, LB_*, CB_*
#define NOWINSTYLES // WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
#define NOSYSMETRICS // SM_*
#define NOMENUS // MF_*
#define NOICONS // IDI_*
#define NOKEYSTATES // MK_*
#define NOSYSCOMMANDS // SC_*
#define NORASTEROPS // Binary and Tertiary raster ops
#define NOSHOWWINDOW // SW_*
#define OEMRESOURCE // OEM Resource values
#define NOATOM // Atom Manager routines
#define NOCLIPBOARD // Clipboard routines
#define NOCOLOR // Screen colors
#define NOCTLMGR // Control and Dialog routines
#define NODRAWTEXT // DrawText() and DT_*
#define NOGDI // All GDI defines and routines
#define NOKERNEL // All KERNEL defines and routines
#define NOUSER // All USER defines and routines
/*#define NONLS // All NLS defines and routines*/
#define NOMB // MB_* and MessageBox()
#define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
#define NOMETAFILE // typedef METAFILEPICT
#define NOMINMAX // Macros min(a,b) and max(a,b)
#define NOMSG // typedef MSG and associated routines
#define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
#define NOSCROLL // SB_* and scrolling routines
#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
#define NOSOUND // Sound driver routines
#define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
#define NOWH // SetWindowsHook and WH_*
#define NOWINOFFSETS // GWL_*, GCL_*, associated routines
#define NOCOMM // COMM driver routines
#define NOKANJI // Kanji support stuff.
#define NOHELP // Help engine interface.
#define NOPROFILER // Profiler interface.
#define NODEFERWINDOWPOS // DeferWindowPos routines
#define NOMCX // Modem Configuration Extensions
#define VC_EXTRALEAN // Maybe fix?
#define WIN32_LEAN_AND_MEAN // Maybe fix?
/* Type required before windows.h inclusion */
typedef struct tagMSG* LPMSG;
#include "php.h"
#undef LOG_INFO
#undef LOG_WARNING
#undef LOG_DEBUG
#include "raylib.h"
#include "include/hashmap.h"
#ifdef PHP_WIN32
#include <Wincrypt.h>
#endif
#include "vector2.h"
//-- Custom RayLib Struct Containers
static unsigned int RL_VECTOR2_OBJECT_ID = 0;
static unsigned char RL_VECTOR2_INIT = 0;
static const unsigned int RL_VECTOR2_MAX_OBJECTS = 999999;
char* RL_Vector2_Hash_Id(char *str, size_t size) {
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const int charset_size = sizeof(charset) - 1;
for (size_t i = 0; i < size - 1; i++) {
#ifdef PHP_WIN32
// On Windows, use CryptGenRandom to generate random bytes
HCRYPTPROV hCryptProv;
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
fprintf(stderr, "CryptAcquireContext failed (%lu)\n", GetLastError());
return NULL;
}
if (!CryptGenRandom(hCryptProv, 1, (BYTE *)&str[i])) {
fprintf(stderr, "CryptGenRandom failed (%lu)\n", GetLastError());
return NULL;
}
CryptReleaseContext(hCryptProv, 0);
#else
// On other platforms, use arc4random to generate random bytes
str[i] = charset[arc4random_uniform(charset_size)];
#endif
}
str[size-1] = '\0';
return str;
}
struct RL_Vector2* RL_Vector2_Create() {
//-- Create the initial data structures
if (RL_VECTOR2_INIT == 0) {
RL_Vector2_Object_List = (struct RL_Vector2**) malloc(0);
RL_Vector2_Object_Map = hashmap_create();
RL_VECTOR2_INIT = 1;
}
//-- Create the container object
struct RL_Vector2* object = (struct RL_Vector2*) malloc(sizeof(struct RL_Vector2));
object->id = RL_VECTOR2_OBJECT_ID++;
object->guid = calloc(33, sizeof(char));
object->guid = RL_Vector2_Hash_Id(object->guid, sizeof(object->guid)); // Generate hash ID
object->data.v = (Vector2) {0};
object->type = RL_VECTOR2_IS_VALUE;
object->refCount = 1;
object->deleted = 0;
//-- Push to the dynamic array list
RL_Vector2_Object_List = (struct RL_Vector2**) realloc(RL_Vector2_Object_List, RL_VECTOR2_OBJECT_ID * sizeof(struct RL_Vector2*));
RL_Vector2_Object_List[object->id] = object;
//-- Add to hashmap
hashmap_set(RL_Vector2_Object_Map, object->guid, sizeof(object->guid) - 1, object);
return object;
}
void RL_Vector2_Delete(struct RL_Vector2* object, int index) {
if (index < 0 || index >= RL_VECTOR2_OBJECT_ID) {
// Error: invalid index
return;
}
hashmap_remove(RL_Vector2_Object_Map, object->guid, sizeof(object->guid) -1);
// Free the memory for the element being deleted
free(RL_Vector2_Object_List[index]);
// Shift the remaining elements over by one
memmove(&RL_Vector2_Object_List[index], &RL_Vector2_Object_List[index + 1], (RL_VECTOR2_OBJECT_ID - index - 1) * sizeof(struct RL_Vector2 *));
// Decrement the count and resize the array
RL_VECTOR2_OBJECT_ID--;
RL_Vector2_Object_List = (struct RL_Vector2 **)realloc(RL_Vector2_Object_List, (RL_VECTOR2_OBJECT_ID) * sizeof(struct RL_Vector2 *));
}
void RL_Vector2_Free(struct RL_Vector2* object) {
free(object);
}
//------------------------------------------------------------------------------------------------------
//-- raylib Vector2 PHP Custom Object
//------------------------------------------------------------------------------------------------------
zend_object_handlers php_raylib_vector2_object_handlers;
static HashTable php_raylib_vector2_prop_handlers;
typedef double (*raylib_vector2_read_float_t)(php_raylib_vector2_object *obj);
typedef int (*raylib_vector2_write_float_t)(php_raylib_vector2_object *obj, zval *value);
typedef struct _raylib_vector2_prop_handler {
raylib_vector2_read_float_t read_float_func;
raylib_vector2_write_float_t write_float_func;
} raylib_vector2_prop_handler;
static void php_raylib_vector2_register_prop_handler(HashTable *prop_handler, char *name,
raylib_vector2_read_float_t read_float_func,
raylib_vector2_write_float_t write_float_func) /* {{{ */
{
raylib_vector2_prop_handler hnd;
hnd.read_float_func = read_float_func;
hnd.write_float_func = write_float_func;
zend_hash_str_add_mem(prop_handler, name, strlen(name), &hnd, sizeof(raylib_vector2_prop_handler));
/* Register for reflection */
zend_declare_property_null(php_raylib_vector2_ce, name, strlen(name), ZEND_ACC_PUBLIC);
}
/* }}} */
static zval *php_raylib_vector2_property_reader(php_raylib_vector2_object *obj, raylib_vector2_prop_handler *hnd, zval *rv) /* {{{ */
{
if (obj != NULL && hnd->read_float_func) {
ZVAL_DOUBLE(rv, hnd->read_float_func(obj));
}
return rv;
}
/* }}} */
static zval *php_raylib_vector2_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
{
php_raylib_vector2_object *obj;
zval *retval = NULL;
raylib_vector2_prop_handler *hnd = NULL;
obj = php_raylib_vector2_fetch_object(object);
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}
if (hnd == NULL) {
retval = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
}
return retval;
}
/* }}} */
static zval *php_raylib_vector2_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
{
php_raylib_vector2_object *obj;
zval *retval = NULL;
raylib_vector2_prop_handler *hnd = NULL;
obj = php_raylib_vector2_fetch_object(object);
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}
if (hnd) {
retval = php_raylib_vector2_property_reader(obj, hnd, rv);
} else {
retval = zend_std_read_property(object, name, type, cache_slot, rv);
}
return retval;
}
/* }}} */
static zval *php_raylib_vector2_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
{
php_raylib_vector2_object *obj;
raylib_vector2_prop_handler *hnd;
obj = php_raylib_vector2_fetch_object(object);
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, member);
}
if (hnd && hnd->write_float_func) {
hnd->write_float_func(obj, value);
} else {
value = zend_std_write_property(object, member, value, cache_slot);
}
return value;
}
/* }}} */
static int php_raylib_vector2_has_property(zend_object *object, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
{
php_raylib_vector2_object *obj;
raylib_vector2_prop_handler *hnd = NULL;
int ret = 0;
if ((hnd = zend_hash_find_ptr(obj->prop_handler, name)) != NULL) {
switch (has_set_exists) {
case ZEND_PROPERTY_EXISTS:
ret = 1;
break;
case ZEND_PROPERTY_NOT_EMPTY: {
zval rv;
zval *value = php_raylib_vector2_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
if (value != &EG(uninitialized_zval)) {
convert_to_boolean(value);
ret = Z_TYPE_P(value) == IS_TRUE ? 1 : 0;
}
break;
}
case ZEND_PROPERTY_ISSET: {
zval rv;
zval *value = php_raylib_vector2_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
if (value != &EG(uninitialized_zval)) {
ret = Z_TYPE_P(value) != IS_NULL? 1 : 0;
zval_ptr_dtor(value);
}
break;
}
EMPTY_SWITCH_DEFAULT_CASE();
}
} else {
ret = zend_std_has_property(object, name, has_set_exists, cache_slot);
}
return ret;
}
/* }}} */
static HashTable *php_raylib_vector2_get_gc(zend_object *object, zval **gc_data, int *gc_data_count) /* {{{ */
{
*gc_data = NULL;
*gc_data_count = 0;
return zend_std_get_properties(object);
}
/* }}} */
static HashTable *php_raylib_vector2_get_properties(zend_object *object)/* {{{ */
{
php_raylib_vector2_object *obj;
HashTable *props;
raylib_vector2_prop_handler *hnd;
zend_string *key;
obj = php_raylib_vector2_fetch_object(object);
props = zend_std_get_properties(object);
if (obj->prop_handler == NULL) {
return NULL;
}
ZEND_HASH_FOREACH_STR_KEY_PTR(obj->prop_handler, key, hnd) {
zval *ret, val;
ret = php_raylib_vector2_property_reader(obj, hnd, &val);
if (ret == NULL) {
ret = &EG(uninitialized_zval);
}
zend_hash_update(props, key, ret);
} ZEND_HASH_FOREACH_END();
return props;
}
/* }}} */
void php_raylib_vector2_free_storage(zend_object *object)/* {{{ */
{
php_raylib_vector2_object *intern = php_raylib_vector2_fetch_object(object);
intern->vector2->refCount--;
if (intern->vector2->refCount < 1) {
RL_Vector2_Free(intern->vector2);
}
zend_object_std_dtor(&intern->std);
}
/* }}} */
static void php_raylib_vector2_free_prop_handler(zval *el) /* {{{ */
{
pefree(Z_PTR_P(el), 1);
} /* }}} */
zend_object * php_raylib_vector2_new_ex(zend_class_entry *ce, zend_object *orig)/* {{{ */
{
php_raylib_vector2_object *intern;
intern = zend_object_alloc(sizeof(php_raylib_vector2_object), ce);
intern->prop_handler = &php_raylib_vector2_prop_handlers;
if (orig) {
php_raylib_vector2_object *other = php_raylib_vector2_fetch_object(orig);
*php_raylib_vector2_fetch_data(intern) = (Vector2) {
.x = php_raylib_vector2_fetch_data(other)->x,
.y = php_raylib_vector2_fetch_data(other)->y
};
} else {
intern->vector2 = RL_Vector2_Create();
*php_raylib_vector2_fetch_data(intern) = (Vector2) {
.x = 0,
.y = 0
};
}
zend_object_std_init(&intern->std, ce);
object_properties_init(&intern->std, ce);
intern->std.handlers = &php_raylib_vector2_object_handlers;
return &intern->std;
}
/* }}} */
zend_object *php_raylib_vector2_new(zend_class_entry *class_type) /* {{{ */
{
return php_raylib_vector2_new_ex(class_type, NULL);
}
/* }}} */
static zend_object *php_raylib_vector2_clone(zend_object *old_object) /* {{{ */
{
zend_object *new_object;
new_object = php_raylib_vector2_new_ex(old_object->ce, old_object);
zend_objects_clone_members(new_object, old_object);
return new_object;
}
/* }}} */
// PHP object handling
ZEND_BEGIN_ARG_INFO_EX(arginfo_vector2__construct, 0, 0, 0)
ZEND_ARG_TYPE_MASK(0, x, MAY_BE_DOUBLE|MAY_BE_NULL, "0")
ZEND_ARG_TYPE_MASK(0, y, MAY_BE_DOUBLE|MAY_BE_NULL, "0")
ZEND_END_ARG_INFO()
PHP_METHOD(Vector2, __construct)
{
double x;
bool x_is_null = 1;
double y;
bool y_is_null = 1;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE_OR_NULL(x, x_is_null)
Z_PARAM_DOUBLE_OR_NULL(y, y_is_null)
ZEND_PARSE_PARAMETERS_END();
php_raylib_vector2_object *intern = Z_VECTOR2_OBJ_P(ZEND_THIS);
if (x_is_null) {
x = 0.0f;
}
if (y_is_null) {
y = 0.0f;
}
*php_raylib_vector2_fetch_data(intern) = (Vector2) {
.x = (float) x,
.y = (float) y
};
}
static double php_raylib_vector2_get_x(php_raylib_vector2_object *obj) /* {{{ */
{
return (double) php_raylib_vector2_fetch_data(obj)->x;
}
/* }}} */
static double php_raylib_vector2_get_y(php_raylib_vector2_object *obj) /* {{{ */
{
return (double) php_raylib_vector2_fetch_data(obj)->y;
}
/* }}} */
static int php_raylib_vector2_set_x(php_raylib_vector2_object *obj, zval *newval) /* {{{ */
{
int ret = SUCCESS;
if (Z_TYPE_P(newval) == IS_NULL) {
php_raylib_vector2_fetch_data(obj)->x = 0;
return ret;
}
php_raylib_vector2_fetch_data(obj)->x = (float) zval_get_double(newval);
return ret;
}
/* }}} */
static int php_raylib_vector2_set_y(php_raylib_vector2_object *obj, zval *newval) /* {{{ */
{
int ret = SUCCESS;
if (Z_TYPE_P(newval) == IS_NULL) {
php_raylib_vector2_fetch_data(obj)->y = 0;
return ret;
}
php_raylib_vector2_fetch_data(obj)->y = (float) zval_get_double(newval);
return ret;
}
/* }}} */
const zend_function_entry php_raylib_vector2_methods[] = {
PHP_ME(Vector2, __construct, arginfo_vector2__construct, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void php_raylib_vector2_startup(INIT_FUNC_ARGS)
{
zend_class_entry ce;
memcpy(&php_raylib_vector2_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
php_raylib_vector2_object_handlers.offset = XtOffsetOf(php_raylib_vector2_object, std);
php_raylib_vector2_object_handlers.free_obj = &php_raylib_vector2_free_storage;
php_raylib_vector2_object_handlers.clone_obj = php_raylib_vector2_clone;
// Props
php_raylib_vector2_object_handlers.get_property_ptr_ptr = php_raylib_vector2_get_property_ptr_ptr;
php_raylib_vector2_object_handlers.get_gc = php_raylib_vector2_get_gc;
php_raylib_vector2_object_handlers.get_properties = php_raylib_vector2_get_properties;
php_raylib_vector2_object_handlers.read_property = php_raylib_vector2_read_property;
php_raylib_vector2_object_handlers.write_property = php_raylib_vector2_write_property;
php_raylib_vector2_object_handlers.has_property = php_raylib_vector2_has_property;
// Init
INIT_NS_CLASS_ENTRY(ce, "raylib", "Vector2", php_raylib_vector2_methods);
php_raylib_vector2_ce = zend_register_internal_class(&ce);
php_raylib_vector2_ce->create_object = php_raylib_vector2_new;
// Props
zend_hash_init(&php_raylib_vector2_prop_handlers, 0, NULL, php_raylib_vector2_free_prop_handler, 1);
php_raylib_vector2_register_prop_handler(&php_raylib_vector2_prop_handlers, "x", php_raylib_vector2_get_x, php_raylib_vector2_set_x);
php_raylib_vector2_register_prop_handler(&php_raylib_vector2_prop_handlers, "y", php_raylib_vector2_get_y, php_raylib_vector2_set_y);
}