samedi 7 janvier 2023

C++ number pointer in a loop

I'm not experienced in C++ and I'm trying to loop a number pointer that contains RGB data so I store it as a JPG, it represents a game texture, the problem I have is that there is some assignation issue either in the pointer or when assigning m_pRGBData that it leaves it broken (couldn't access to memory exception). There is something wrong in these for loops but I can't figure out what, What I need is basically iterate this m_pRGBData that contains the texture data and save it in a JPG.

I use this to store this as a JPG image, it does store it but the texture in game game goes full white and it may crash afterwards with couldn't assign memory exception, so these fors are breaking the pointer or I don't know what's going on...

_ulong is unsigned int; _ubyte is unsigned char;

_bool L_DynamicTexture::SaveRGBDataAsJPG(const L_String* pOverRideFileName) const
{
    //-- If we don't have RGBData, kick out.
    if (!m_pRGBData)
        return FALSE;

    const _ushort ImageSize = static_cast<_ushort>(m_Size);

    auto    pStream    = new _ubyte[m_Size * m_Size * 3];
    _ubyte* pCurStream = pStream;
    _ulong* pCur       = m_pRGBData;

    _ubyte r, g, b;

    for (_ulong y = 0; y < ImageSize; y++)
    {
        for (_ulong x = 0; x < ImageSize; x)
        {
            //-- 24 or 32 bits data
            GETB_RGBA(b, *pCur);
            GETG_RGBA(g, *pCur);
            GETR_RGBA(r, *pCur);

            *pCurStream++ = b;
            *pCurStream++ = g;
            *pCurStream++ = r;

            pCur++;
            x++;
        }
    }

    //-- Write image data
    const _bool Result = stbi_write_jpg(pOverRideFileName->As_Ansi(), ImageSize, ImageSize, 3, &(pStream[0]), 100);
    //const _bool Result = stbi_write_png(pOverRideFileName->As_Ansi(), ImageSize, ImageSize, 3, &(pStream[0]), 3 * ImageSize);

    L_Free(pStream);

    return Result;
}

I've tried cloning the pointer, deep clone, heap, everything but it always ends in texture being broken or having a crash after that texture is cleaned or used again by the game engine..

Aucun commentaire:

Enregistrer un commentaire