Skip to content

Commit 5ccde4d

Browse files
feat: additional low hanging bindings
1 parent 2c00e2e commit 5ccde4d

16 files changed

Lines changed: 1417 additions & 49 deletions

‎extensions/pl_camera_ext_m.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,27 @@ camera_init(PyObject* self, PyObject* args, PyObject* kwargs)
542542
Py_RETURN_NONE;
543543
}
544544

545+
static PyObject*
546+
camera_set_orthographic_size(PyObject* self, PyObject* args, PyObject* kwargs)
547+
{
548+
static const char* apcKeywords[] = {"camera", "width", "height", NULL};
549+
PyObject* ptPyCamera = NULL;
550+
float fWidth = 0.0f;
551+
float fHeight = 0.0f;
552+
if(!pl_parse_args("Off", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &ptPyCamera, &fWidth, &fHeight))
553+
return NULL;
554+
if(!PyObject_TypeCheck(ptPyCamera, (PyTypeObject*)gptCameraType))
555+
{
556+
PyErr_SetString(PyExc_TypeError, "expected plCamera");
557+
return NULL;
558+
}
559+
gptCamera->set_orthographic_size(((pyplCamera*)ptPyCamera)->ptCamera, fWidth, fHeight);
560+
Py_RETURN_NONE;
561+
}
562+
545563
static PyMethodDef gatCommandsplCameraI[] =
546564
{
565+
{"set_orthographic_size", (PyCFunction)camera_set_orthographic_size, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
547566
{"init", (PyCFunction)camera_init, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
548567
{"look_at", (PyCFunction)camera_look_at, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
549568
{"set_depth_mode", (PyCFunction)camera_set_depth_mode, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},

‎extensions/pl_platform_ext_m.c‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,169 @@ window_set_title_bar(PyObject* self, PyObject* args, PyObject* kwargs)
376376
return PyBool_FromLong(gptWindow->set_title_bar(ptWindow, &tDesc));
377377
}
378378

379+
// window attribute values by type:
380+
// SIZE -> (width, height) (uint)
381+
// POSITION -> (x, y) (int)
382+
// SIZE_LIMITS -> (minWidth, minHeight, maxWidth, maxHeight) (uint)
383+
// others -> bool
384+
static PyObject*
385+
window_set_attribute(PyObject* self, PyObject* args, PyObject* kwargs)
386+
{
387+
static const char* apcKeywords[] = {"window", "attribute", "value", NULL};
388+
PyObject* ptPyWindow = NULL;
389+
int iAttribute = 0;
390+
PyObject* ptValue = NULL;
391+
if(!pl_parse_args("OiO", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &ptPyWindow, &iAttribute, &ptValue))
392+
return NULL;
393+
plWindow* ptWindow = PyCapsule_GetPointer(ptPyWindow, "plWindow");
394+
if(ptWindow == NULL)
395+
return NULL;
396+
397+
plWindowAttributeValue tValue = {0};
398+
switch(iAttribute)
399+
{
400+
case PL_WINDOW_ATTRIBUTE_SIZE:
401+
if(!PyArg_ParseTuple(ptValue, "II;SIZE expects (width, height)", &tValue.tuVec2.x, &tValue.tuVec2.y))
402+
return NULL;
403+
break;
404+
case PL_WINDOW_ATTRIBUTE_POSITION:
405+
if(!PyArg_ParseTuple(ptValue, "ii;POSITION expects (x, y)", &tValue.tiVec2.x, &tValue.tiVec2.y))
406+
return NULL;
407+
break;
408+
case PL_WINDOW_ATTRIBUTE_SIZE_LIMITS:
409+
if(!PyArg_ParseTuple(ptValue, "IIII;SIZE_LIMITS expects (minWidth, minHeight, maxWidth, maxHeight)",
410+
&tValue.tuVec4.x, &tValue.tuVec4.y, &tValue.tuVec4.z, &tValue.tuVec4.w))
411+
return NULL;
412+
break;
413+
default:
414+
{
415+
if(iAttribute < 0 || iAttribute >= PL_WINDOW_ATTRIBUTE_COUNT)
416+
{
417+
PyErr_Format(PyExc_ValueError, "unknown window attribute %d", iAttribute);
418+
return NULL;
419+
}
420+
const int iTruth = PyObject_IsTrue(ptValue);
421+
if(iTruth < 0)
422+
return NULL;
423+
tValue.bValue = iTruth != 0;
424+
break;
425+
}
426+
}
427+
return PyBool_FromLong(gptWindow->set_attribute(ptWindow, iAttribute, &tValue));
428+
}
429+
430+
// returns the value (see window_set_attribute) or None if unsupported
431+
static PyObject*
432+
window_get_attribute(PyObject* self, PyObject* args, PyObject* kwargs)
433+
{
434+
static const char* apcKeywords[] = {"window", "attribute", NULL};
435+
PyObject* ptPyWindow = NULL;
436+
int iAttribute = 0;
437+
if(!pl_parse_args("Oi", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &ptPyWindow, &iAttribute))
438+
return NULL;
439+
plWindow* ptWindow = PyCapsule_GetPointer(ptPyWindow, "plWindow");
440+
if(ptWindow == NULL)
441+
return NULL;
442+
if(iAttribute < 0 || iAttribute >= PL_WINDOW_ATTRIBUTE_COUNT)
443+
{
444+
PyErr_Format(PyExc_ValueError, "unknown window attribute %d", iAttribute);
445+
return NULL;
446+
}
447+
448+
plWindowAttributeValue tValue = {0};
449+
if(!gptWindow->get_attribute(ptWindow, iAttribute, &tValue))
450+
Py_RETURN_NONE;
451+
switch(iAttribute)
452+
{
453+
case PL_WINDOW_ATTRIBUTE_SIZE: return Py_BuildValue("(II)", tValue.tuVec2.x, tValue.tuVec2.y);
454+
case PL_WINDOW_ATTRIBUTE_POSITION: return Py_BuildValue("(ii)", tValue.tiVec2.x, tValue.tiVec2.y);
455+
case PL_WINDOW_ATTRIBUTE_SIZE_LIMITS: return Py_BuildValue("(IIII)", tValue.tuVec4.x, tValue.tuVec4.y, tValue.tuVec4.z, tValue.tuVec4.w);
456+
default: return PyBool_FromLong(tValue.bValue);
457+
}
458+
}
459+
460+
static PyObject*
461+
window_set_cursor_mode(PyObject* self, PyObject* args, PyObject* kwargs)
462+
{
463+
static const char* apcKeywords[] = {"window", "mode", NULL};
464+
PyObject* ptPyWindow = NULL;
465+
int iMode = 0;
466+
if(!pl_parse_args("Oi", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &ptPyWindow, &iMode))
467+
return NULL;
468+
plWindow* ptWindow = PyCapsule_GetPointer(ptPyWindow, "plWindow");
469+
if(ptWindow == NULL)
470+
return NULL;
471+
return PyBool_FromLong(gptWindow->set_cursor_mode(ptWindow, iMode));
472+
}
473+
474+
static PyObject*
475+
window_get_cursor_mode(PyObject* self, PyObject* arg)
476+
{
477+
plWindow* ptWindow = PyCapsule_GetPointer(arg, "plWindow");
478+
if(ptWindow == NULL)
479+
return NULL;
480+
return PyLong_FromLong((long)gptWindow->get_cursor_mode(ptWindow));
481+
}
482+
483+
static PyObject*
484+
window_set_raw_mouse_input(PyObject* self, PyObject* args, PyObject* kwargs)
485+
{
486+
static const char* apcKeywords[] = {"window", "enabled", NULL};
487+
PyObject* ptPyWindow = NULL;
488+
int iEnabled = 0;
489+
if(!pl_parse_args("Op", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &ptPyWindow, &iEnabled))
490+
return NULL;
491+
plWindow* ptWindow = PyCapsule_GetPointer(ptPyWindow, "plWindow");
492+
if(ptWindow == NULL)
493+
return NULL;
494+
return PyBool_FromLong(gptWindow->set_raw_mouse_input(ptWindow, iEnabled != 0));
495+
}
496+
497+
// desc: plFullScreenDesc (tMode, iMonitor; -1 = automatic)
498+
static PyObject*
499+
window_set_fullscreen(PyObject* self, PyObject* args, PyObject* kwargs)
500+
{
501+
static const char* apcKeywords[] = {"window", "desc", NULL};
502+
PyObject* ptPyWindow = NULL;
503+
PyObject* ptPyDesc = NULL;
504+
if(!pl_parse_args("OO", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &ptPyWindow, &ptPyDesc))
505+
return NULL;
506+
plWindow* ptWindow = PyCapsule_GetPointer(ptPyWindow, "plWindow");
507+
if(ptWindow == NULL)
508+
return NULL;
509+
uint32_t uMode = 0;
510+
if(!pl__get_u32_attr(ptPyDesc, "tMode", &uMode))
511+
return NULL;
512+
plFullScreenDesc tDesc = {.tMode = (plFullScreenMode)uMode, .iMonitor = -1};
513+
if(PyObject_HasAttrString(ptPyDesc, "iMonitor"))
514+
{
515+
PyObject* ptMonitor = PyObject_GetAttrString(ptPyDesc, "iMonitor");
516+
if(ptMonitor == NULL)
517+
return NULL;
518+
tDesc.iMonitor = (int)PyLong_AsLong(ptMonitor);
519+
Py_DECREF(ptMonitor);
520+
if(PyErr_Occurred())
521+
return NULL;
522+
}
523+
return PyBool_FromLong(gptWindow->set_fullscreen(ptWindow, &tDesc));
524+
}
525+
526+
//-----------------------------------------------------------------------------
527+
// [SECTION] timer api
528+
//-----------------------------------------------------------------------------
529+
530+
static PyObject*
531+
timer_get_time(PyObject* self, PyObject* unused)
532+
{
533+
return PyFloat_FromDouble(gptTimer->get_time());
534+
}
535+
536+
static PyObject*
537+
timer_get_raw_time(PyObject* self, PyObject* unused)
538+
{
539+
return PyFloat_FromDouble(gptTimer->get_raw_time());
540+
}
541+
379542
//-----------------------------------------------------------------------------
380543
// [SECTION] file api
381544
//-----------------------------------------------------------------------------
@@ -495,11 +658,25 @@ static PyMethodDef gatCommandsplWindowI[] =
495658
{"get_capabilities", (PyCFunction)window_get_capabilities, METH_NOARGS | METH_STATIC, NULL},
496659
{"set_icon", (PyCFunction)window_set_icon, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
497660
{"set_title_bar", (PyCFunction)window_set_title_bar, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
661+
{"set_attribute", (PyCFunction)window_set_attribute, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
662+
{"get_attribute", (PyCFunction)window_get_attribute, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
663+
{"set_cursor_mode", (PyCFunction)window_set_cursor_mode, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
664+
{"get_cursor_mode", (PyCFunction)window_get_cursor_mode, METH_O | METH_STATIC, NULL},
665+
{"set_raw_mouse_input", (PyCFunction)window_set_raw_mouse_input, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
666+
{"set_fullscreen", (PyCFunction)window_set_fullscreen, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
667+
{NULL, NULL, 0, NULL}
668+
};
669+
670+
static PyMethodDef gatCommandsplTimerI[] =
671+
{
672+
{"get_time", (PyCFunction)timer_get_time, METH_NOARGS | METH_STATIC, NULL},
673+
{"get_raw_time", (PyCFunction)timer_get_raw_time, METH_NOARGS | METH_STATIC, NULL},
498674
{NULL, NULL, 0, NULL}
499675
};
500676

501677
PL_NEW_PYTHON_API(plWindowI)
502678
PL_NEW_PYTHON_API(plFileI)
679+
PL_NEW_PYTHON_API(plTimerI)
503680

504681
//-----------------------------------------------------------------------------
505682
// [SECTION] constants
@@ -520,4 +697,22 @@ plPythonIntConstantPair gatPlatformIntPairs[] = {
520697
PL_ADD_INT_CONSTANT(PL_WINDOW_CAPABILITY_FLAGS_APP_ICON),
521698
PL_ADD_INT_CONSTANT(PL_WINDOW_CAPABILITY_FLAGS_CUSTOM_TITLE_BAR),
522699
PL_ADD_INT_CONSTANT(PL_WINDOW_CAPABILITY_FLAGS_TITLE_BAR_TEXT),
700+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_SIZE),
701+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_POSITION),
702+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_RESIZABLE),
703+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_DECORATED),
704+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_TOP_MOST),
705+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_MINIMIZED),
706+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_MAXIMIZED),
707+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_VISIBLE),
708+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_FOCUSED),
709+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_HOVERED),
710+
PL_ADD_INT_CONSTANT(PL_WINDOW_ATTRIBUTE_SIZE_LIMITS),
711+
PL_ADD_INT_CONSTANT(PL_CURSOR_MODE_NORMAL),
712+
PL_ADD_INT_CONSTANT(PL_CURSOR_MODE_HIDDEN),
713+
PL_ADD_INT_CONSTANT(PL_CURSOR_MODE_CAPTURED),
714+
PL_ADD_INT_CONSTANT(PL_CURSOR_MODE_DISABLED),
715+
PL_ADD_INT_CONSTANT(PL_FULLSCREEN_MODE_NONE),
716+
PL_ADD_INT_CONSTANT(PL_FULLSCREEN_MODE_EXCLUSIVE),
717+
PL_ADD_INT_CONSTANT(PL_FULLSCREEN_MODE_BORDERLESS),
523718
};

‎extensions/pl_resource_ext_m.c‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,93 @@ resource_get_texture(PyObject* self, PyObject* args)
122122
return PyLong_FromUInt32(tTextureHandle.uData);
123123
}
124124

125+
// fileData: optional bytes-like file contents (used during the call; copied if
126+
// PL_RESOURCE_LOAD_FLAG_RETAIN_FILE_DATA is set)
127+
static PyObject*
128+
resource_load_ex(PyObject* self, PyObject* args, PyObject* kwargs)
129+
{
130+
static const char* apcKeywords[] = {"file", "flags", "fileData", "containerFileName", "fileBytesOffset", NULL};
131+
const char* pcFile = NULL;
132+
int iFlags = PL_RESOURCE_LOAD_FLAG_NONE;
133+
Py_buffer tData = {0};
134+
const char* pcContainer = NULL;
135+
Py_ssize_t szOffset = 0;
136+
if(!pl_parse_args("s|iz*zn", (const char**)apcKeywords, args, kwargs, __FUNCTION__,
137+
&pcFile, &iFlags, &tData, &pcContainer, &szOffset))
138+
return NULL;
139+
if(szOffset < 0)
140+
{
141+
PyBuffer_Release(&tData);
142+
PyErr_SetString(PyExc_ValueError, "fileBytesOffset must be >= 0");
143+
return NULL;
144+
}
145+
plResourceHandle tHandle = gptResource->load_ex(pcFile, iFlags, (uint8_t*)tData.buf, tData.buf ? (size_t)tData.len : 0, pcContainer, (size_t)szOffset);
146+
PyBuffer_Release(&tData);
147+
return PyLong_FromUnsignedLongLong(tHandle.ulData);
148+
}
149+
150+
// single "handle" argument
151+
static bool
152+
pl__resource_parse_handle(PyObject* args, PyObject* kwargs, plResourceHandle* ptHandleOut)
153+
{
154+
static const char* apcKeywords[] = {"handle", NULL};
155+
unsigned long long ulData = 0;
156+
if(!pl_parse_args("K", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &ulData))
157+
return false;
158+
ptHandleOut->ulData = ulData;
159+
return true;
160+
}
161+
162+
static PyObject*
163+
resource_unload(PyObject* self, PyObject* args, PyObject* kwargs)
164+
{
165+
plResourceHandle tHandle = {0};
166+
if(!pl__resource_parse_handle(args, kwargs, &tHandle))
167+
return NULL;
168+
gptResource->unload(tHandle);
169+
Py_RETURN_NONE;
170+
}
171+
172+
static PyObject*
173+
resource_is_valid(PyObject* self, PyObject* args, PyObject* kwargs)
174+
{
175+
plResourceHandle tHandle = {0};
176+
if(!pl__resource_parse_handle(args, kwargs, &tHandle))
177+
return NULL;
178+
return PyBool_FromLong(gptResource->is_valid(tHandle));
179+
}
180+
181+
static PyObject*
182+
resource_is_loaded(PyObject* self, PyObject* args, PyObject* kwargs)
183+
{
184+
static const char* apcKeywords[] = {"file", NULL};
185+
const char* pcFile = NULL;
186+
if(!pl_parse_args("s", (const char**)apcKeywords, args, kwargs, __FUNCTION__, &pcFile))
187+
return NULL;
188+
return PyBool_FromLong(gptResource->is_loaded(pcFile));
189+
}
190+
191+
// retained file data (PL_RESOURCE_LOAD_FLAG_RETAIN_FILE_DATA) as bytes, or None
192+
static PyObject*
193+
resource_get_file_data(PyObject* self, PyObject* args, PyObject* kwargs)
194+
{
195+
plResourceHandle tHandle = {0};
196+
if(!pl__resource_parse_handle(args, kwargs, &tHandle))
197+
return NULL;
198+
size_t szSize = 0;
199+
const uint8_t* puData = gptResource->get_file_data(tHandle, &szSize);
200+
if(puData == NULL)
201+
Py_RETURN_NONE;
202+
return PyBytes_FromStringAndSize((const char*)puData, (Py_ssize_t)szSize);
203+
}
204+
125205
static PyMethodDef gatCommandsplResourceI[] =
126206
{
207+
{"load_ex", (PyCFunction)resource_load_ex, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
208+
{"unload", (PyCFunction)resource_unload, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
209+
{"is_valid", (PyCFunction)resource_is_valid, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
210+
{"is_loaded", (PyCFunction)resource_is_loaded, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
211+
{"get_file_data", (PyCFunction)resource_get_file_data, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL},
127212
{"new_frame", (PyCFunction)resource_new_frame, METH_NOARGS | METH_STATIC, NULL},
128213
{"cleanup", (PyCFunction)resource_cleanup, METH_NOARGS | METH_STATIC, NULL},
129214
{"clear", (PyCFunction)resource_clear, METH_NOARGS | METH_STATIC, NULL},
@@ -133,4 +218,10 @@ static PyMethodDef gatCommandsplResourceI[] =
133218
{NULL, NULL, 0, NULL}
134219
};
135220

136-
PL_NEW_PYTHON_API(plResourceI)
221+
PL_NEW_PYTHON_API(plResourceI)
222+
plPythonIntConstantPair gatResourceIntPairs[] = {
223+
PL_ADD_INT_CONSTANT(PL_RESOURCE_LOAD_FLAG_NONE),
224+
PL_ADD_INT_CONSTANT(PL_RESOURCE_LOAD_FLAG_RETAIN_FILE_DATA),
225+
PL_ADD_INT_CONSTANT(PL_RESOURCE_LOAD_FLAG_BLOCK_COMPRESSED),
226+
PL_ADD_INT_CONSTANT(PL_RESOURCE_LOAD_FLAG_NO_CACHING),
227+
};

0 commit comments

Comments
 (0)