Okay bear with me, this requires some context and is likely my bumbling around with C for the first time in 12 years.
I'm working on a 3D rasterizing setup, and as part of that I'm laying down the scaffolding of Vector3
, Tri
, and Mesh
. I want everything to point back to the original Vector3
in space, so it's pointers all the way down.
struct definitions
struct Vector3
{
float x, y, z;
};
struct Tri
{
struct Vector3* p[3];
};
struct Mesh
{
int length;
struct Tri** f;
};
I have a set of functions Vector3_make
, Tri_make
, and a test Cube_make
for a Mesh. Inspecting the results of all of these in Visual Studio's step-through inspector indicates that everything is being set up correctly. I've attached the source for those methods, symbols.c
.
symbols.zip (628 Bytes)
Here's where it gets weird. I initially tried instantiating cube
in eventHandler
on kEventInit
. cube
looked great in the inspector, but by the time it got to update
the pointer targets were completely different, seemingly random memory.
I initialized it in update instead as follows:
struct Mesh cube;
static int update(void* userdata)
{
if (frame_count == 0) // Init
{
Cube_make(100, &cube);
}
PlaydateAPI* pd = userdata;
pd->graphics->clear(kColorWhite);
...
And from the inspector's point of view, the pointer targets are as expected until pd->graphics->clear is called.
Before:
After:
Vector3D
and Tri
objects don't seem to be affected. I've been able to reference those across multiple updates. That tells me it's probably something to do with how I've set up the Mesh
struct.
Any guidance is very welcome. Let me know if you need to see any other parts of the code.