Tilengine - The 2D retro graphics engine forum

Full Version: How to create a framebuffer for use of external rendering
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
as title suggests, I was following the "External Rendering" guide on the documentation site, however, when following it in v 2.15.2 it doesn't seem to work, owing to the provided code snippet:

    const int hres = 400;
    const int vres = 240;
    const int pitch = hres * sizeof(uint32_t);
    void* framebuffer;

    /* init and set framebuffer */
    TLN_Init (hres, vres, 2, 80, 0);
    framebuffer = malloc (pitch * vres);
    TLN_SetRenderTarget (framebuffer, pitch);


However on my end (visual studio 2022), TLN_SetRenderTarget's first argument (framebuffer) expects a uint8_t, not a void*, and changing framebuffer to that datatype causes malloc to break, as malloc returns a void*.
so what is it you're actually meant to do?
Hi,

In plain C (not C++), void* pointer means "anything goes", it's like a placeholder that accepts any data. malloc won't break, it just returns a void* pointer, it doesn't care what are you using it for. Remember that C and C++ are unmanaged languages so memory management is up to the app developer. What's important is that you declare framebuffer with a datatype that is meaningful for the kind of access/arithmetic you're going to perform on it. Tilengine doesn't care as long as it encodes a 32-bit ARGB pixel format.

C++ is more strict with pointer matching than C, and different types must be cast explicitly. So if you're declaring framebuffer with a different datatype than uint8_t*, you must cast it:

Code:
TLN_SetRenderTarget ((uint8_t*)framebuffer, pitch);

Regards,