Tilengine - The 2D retro graphics engine forum

Full Version: Direct Pixel Manipulation (looking for an example of TLN_GetBitmapPtr)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey everyone,

I've just started to learn how to use Tilengine, and I was checking ou how to do direct pixel manipulation with the TLN_GetBitmapPtr function, but when changed the value of the pointer the program segfaulted (obviously). And since I could not find any examples of that function (well, not easily at least) I decided to ask some one over here to see if I can get some help.

This is my noob code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include "Tilengine.h"

int main (int argc, char* argv[])
{
   TLN_Init(400, 240, 1, 0, 0);

   TLN_SetBGColor(0, 36, 96);

   TLN_CreateWindow(NULL, 0);

   TLN_Bitmap bitmap = TLN_CreateBitmap(400, 240, 8);

   TLN_SetLayerBitmap(0, bitmap);

   int frame = 0;
   while(TLN_ProcessWindow())
   {
       TLN_DrawFrame(frame);

       for (int x = 0; x < 400; x++)
       {
           for (int y = 0; y < 240; y++)
           {
               uint8_t *pixel = TLN_GetBitmapPtr(bitmap, x, y);
               *pixel = (uint8_t)0xff;
           }
       }

       frame++;
   }

   TLN_DeleteBitmap(bitmap);
   TLN_DeleteWindow();
   TLN_Deinit();
   return 0;
}

Already thanking the support,

Alan.
Hi Alan,

Welcome here and thanks for your interest!

There isn't any problem with your direct pixel manipulation code. The problem is that any layer requires having a palette assigned. When you load a bitmap with TLN_LoadBitmap() and assign it with TLN_SetLayerBitmap(), it's own palette gets loaded and attached. But you're creating an empty bitmap without a palette. Of course the renderer shouldn't crash, just refuse to draw it. I have to fix this behavior.

To fix your code, you have to manually create and assign a palette with TLN_CreatePalette() and TLN_SetBitmapPalette():


Code:
    TLN_Bitmap bitmap = TLN_CreateBitmap(400, 240, 8);
    TLN_Palette palette = TLN_CreatePalette(256);
    TLN_SetBitmapPalette(bitmap, palette);
    TLN_SetLayerBitmap(0, bitmap);
    
You should get a black window because the default palette is black. To modify it, you can use TLN_SetPaletteColor().

Let me know if it works!
Thanks, marc, it worked (well, at least it didn't segfaulted...),

However, I tried to use TLN_SetPaletteColor, and the background is still black. My doubt here is that if I can draw something in runtime in the bitmap layer, or I actually need to use a tileset so I can manipulate that with the bitmap layer I've created.

My current code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include "Tilengine.h"

int main (int argc, char* argv[])
{
    TLN_Init(400, 240, 1, 0, 0);
    TLN_CreateWindow(NULL, CWF_VSYNC);

    TLN_Bitmap bitmap = TLN_CreateBitmap(400, 240, 8);
    TLN_Palette palette = TLN_CreatePalette(256);
    TLN_SetBitmapPalette(bitmap, palette);
    TLN_SetLayerBitmap(0, bitmap);
    TLN_SetLayerPalette(0, palette);

    for (int i = 0; i < 256; i++)
        TLN_SetPaletteColor(palette, i, 0, 36, 96);

    int frame = 0;
    int color = 0;
    while(TLN_ProcessWindow())
    {
        TLN_DrawFrame(frame);

        TLN_SetPaletteColor(palette, color, color, color, color);
        if (color < 256)
            color++;
        else color = 0;

        frame++;
    }

    TLN_DeleteBitmap(bitmap);
    // TLN_DeletePalette(palette);
    TLN_DeleteWindow();
    TLN_Deinit();
    return 0;
}
Hi again!

You can -in fact you must- draw something to the bitmap you're using in order to see anything. Just setting pixels or color entries won't give any result, you must set both. Be aware that a pixel value of 0 means fully transparent.

You don't have to use tilesets: bitmap layers and tiled layers are totally independent, which one to use depends on your game needs.

In your example, your call to TLN_SetLayerPalette(0, palette); is redundant: as you've already attached the palette to the bitmap with TLN_SetBitmapPalette(), calling TLN_SetLayerBitmap() automatically sets the palette too. You can set bitmap pixels and/or palette colors at any time, the effect will be visible on the next call to TLN_DrawFrame().

Keep going!