<commit_before>//**************************************************************
//*				OpenGLide - Glide->OpenGL Wrapper
//*				  Linear Frame Buffer Functions
//*					   Made by Glorfindel
//**************************************************************

#include "GlOgl.h"
#include "Glextensions.h"
#include "GLRender.h"

#define BLUE_SCREEN (0x7ff)

inline void Convert565to8888( WORD *Buffer1, DWORD *Buffer2, DWORD Pixels )
{
	while(Pixels)
	{
		*Buffer2++ = ((*Buffer1) ? 0xFF000000 : 0) |	// A
			((*Buffer1)		& 0x001F) << 19 |			// B
			((*Buffer1)		& 0x07E0) << 5  |			// G
			((*Buffer1++)	& 0xF800) >> 8;				// R
		Pixels--;
	}
}

inline void Convert565to5551( WORD *Buffer1, WORD *Buffer2, DWORD Pixels )
{
	while(Pixels)
	{
		*Buffer2++ = ((*Buffer1) & 0xFFC0) |
					(((*Buffer1) & 0x001F) << 1) |
					( (*Buffer1++) ? 0x0001 : 0x0000);
		Pixels--;
	}
}

//----------------------------------------------------------------
DLLEXPORT FxBool __stdcall
grLfbLock(GrLock_t dwType, 
          GrBuffer_t dwBuffer, 
          GrLfbWriteMode_t dwWriteMode,
          GrOriginLocation_t dwOrigin, 
          FxBool bPixelPipeline, 
          GrLfbInfo_t *lfbInfo )
{ 
#ifdef CRITICAL
	GlideMsg("grLfbLock( %d, %d, %d, %d, %d, --- )\n", dwType, dwBuffer, dwWriteMode, dwOrigin, bPixelPipeline); 
#endif
    int width = Glide.WindowWidth;
    int height = Glide.WindowHeight;

	RenderDrawTriangles();


    if((dwType & 1) == 0)
    {
        FxU32 *buf = new FxU32[width * height];
        int i, j;

        glReadBuffer(dwBuffer == GR_BUFFER_BACKBUFFER
            ? GL_BACK : GL_FRONT);

        glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (void *) buf);
        
        for(j = 0; j < height; j++)
        {
            WORD *line = Glide.SrcBuffer.Address + (height - 1 - j) * width;
            FxU32 *bufl = buf + j * width;

            for(i = 0; i < width; i++)
            {
                line[i] = (WORD)
                    (((bufl[i] & 0xf8) << 8)
                    | ((bufl[i] & 0xfc00) >> 5)
                    | ((bufl[i] & 0xf80000) >> 19)
                    );
            }
        }
        
        delete [] buf;

	    Glide.SrcBuffer.Lock = true;
	    Glide.SrcBuffer.Type = dwType;
	    Glide.SrcBuffer.Buffer = dwBuffer;
	    Glide.SrcBuffer.WriteMode = dwWriteMode;

	    lfbInfo->lfbPtr = Glide.SrcBuffer.Address;
    }
    else
    {
        int i;

        for(i = 0; i < width * height; i++)
            Glide.DstBuffer.Address[i] = BLUE_SCREEN;

	    Glide.DstBuffer.Lock = true;
	    Glide.DstBuffer.Type = dwType;
	    Glide.DstBuffer.Buffer = dwBuffer;
	    Glide.DstBuffer.WriteMode = dwWriteMode;

	    lfbInfo->lfbPtr = Glide.DstBuffer.Address;
    }

	lfbInfo->strideInBytes = Glide.WindowWidth * 2;

	lfbInfo->writeMode = GR_LFBWRITEMODE_565;

	return FXTRUE;
}
//typedef struct {
//    int                size;
//    void               *lfbPtr;
//    FxU32              strideInBytes;        
//    GrLfbWriteMode_t   writeMode;
//    GrOriginLocation_t origin;
//} GrLfbInfo_t;

//----------------------------------------------------------------
DLLEXPORT FxBool __stdcall
grLfbUnlock(GrLock_t dwType, GrBuffer_t dwBuffer )
{ 
#ifdef CRITICAL
	GlideMsg("grLfbUnlock( %d, %d )\n", dwType, dwBuffer ); 
#endif
	
    int width = Glide.WindowWidth;
    int height = Glide.WindowHeight;

    if((dwType & 1) == 0)
    {
        if ( !Glide.SrcBuffer.Lock )
            return FXFALSE;
        
        Glide.SrcBuffer.Lock = false;
        
        return FXTRUE; 
    }
    else
    {
        int x,           y;
        int maxx = 0,    maxy = 0;
        int minx = 2048, miny = 2048;

        if( !Glide.DstBuffer.Lock )
            return FXFALSE;

        for(y = 0; y < height; y++)
        {
            for(x = 0; x < width; x++)
            {
                if(Glide.DstBuffer.Address[y*width + x] != BLUE_SCREEN)
                {
                    if(x > maxx) maxx = x;
                    if(y > maxy) maxy = y;
                    if(x < minx) minx = x;
                    if(y < miny) miny = y;
                }
            }
        }

        if(maxx >= minx)
        {
            int xsize = maxx + 1 - minx;
            int ysize = maxy + 1 - miny;
            FxU32 *buf = new FxU32[xsize*ysize];

            glReadBuffer(Glide.DstBuffer.Buffer == GR_BUFFER_BACKBUFFER
                                                 ? GL_BACK : GL_FRONT);

            glReadPixels(minx, height-miny-ysize, xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, (void *) buf);

            for(y = 0; y < ysize; y++)
            {
                WORD *line = Glide.DstBuffer.Address + (miny + ysize - 1 - y) * width + minx;
                FxU32 *bufl = buf + y * xsize;

                for(x = 0; x < xsize; x++)
                {
                    if(line[x] != BLUE_SCREEN)
                    {
                        bufl[x] = (((line[x] & 0x1f) << 19)
                                  | ((line[x] & 0x7e0) << 5)
                                  | ((line[x] & 0xf100) >> 8)
                                  | 0xff000000);
                    }
                }
            }

            glDrawBuffer(Glide.DstBuffer.Buffer == GR_BUFFER_BACKBUFFER
                                                 ? GL_BACK : GL_FRONT);

            glRasterPos2i(minx,  miny+ysize);

            glDrawPixels(xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, (void *) buf);

            glDrawBuffer(OpenGL.RenderBuffer);

            delete [] buf;
        }

        Glide.DstBuffer.Lock = false;

        return FXTRUE;
    }
}

//----------------------------------------------------------------
DLLEXPORT FxBool __stdcall
grLfbReadRegion( GrBuffer_t src_buffer,
                 FxU32 src_x, FxU32 src_y,
                 FxU32 src_width, FxU32 src_height,
                 FxU32 dst_stride, void *dst_data )
{<commit_msg>Turn off blending and texturing while doing DrawPixels. Get Chat working and lightens the map in RB.<commit_after>//**************************************************************
//*				OpenGLide - Glide->OpenGL Wrapper
//*				  Linear Frame Buffer Functions
//*					   Made by Glorfindel
//**************************************************************

#include "GlOgl.h"
#include "Glextensions.h"
#include "GLRender.h"

#define BLUE_SCREEN (0x7ff)

inline void Convert565to8888( WORD *Buffer1, DWORD *Buffer2, DWORD Pixels )
{
	while(Pixels)
	{
		*Buffer2++ = ((*Buffer1) ? 0xFF000000 : 0) |	// A
			((*Buffer1)		& 0x001F) << 19 |			// B
			((*Buffer1)		& 0x07E0) << 5  |			// G
			((*Buffer1++)	& 0xF800) >> 8;				// R
		Pixels--;
	}
}

inline void Convert565to5551( WORD *Buffer1, WORD *Buffer2, DWORD Pixels )
{
	while(Pixels)
	{
		*Buffer2++ = ((*Buffer1) & 0xFFC0) |
					(((*Buffer1) & 0x001F) << 1) |
					( (*Buffer1++) ? 0x0001 : 0x0000);
		Pixels--;
	}
}

//----------------------------------------------------------------
DLLEXPORT FxBool __stdcall
grLfbLock(GrLock_t dwType, 
          GrBuffer_t dwBuffer, 
          GrLfbWriteMode_t dwWriteMode,
          GrOriginLocation_t dwOrigin, 
          FxBool bPixelPipeline, 
          GrLfbInfo_t *lfbInfo )
{ 
#ifdef CRITICAL
	GlideMsg("grLfbLock( %d, %d, %d, %d, %d, --- )\n", dwType, dwBuffer, dwWriteMode, dwOrigin, bPixelPipeline); 
#endif
    int width = Glide.WindowWidth;
    int height = Glide.WindowHeight;

	RenderDrawTriangles();


    if((dwType & 1) == 0)
    {
        FxU32 *buf = new FxU32[width * height];
        int i, j;

        glReadBuffer(dwBuffer == GR_BUFFER_BACKBUFFER
            ? GL_BACK : GL_FRONT);

        glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (void *) buf);
        
        for(j = 0; j < height; j++)
        {
            WORD *line = Glide.SrcBuffer.Address + (height - 1 - j) * width;
            FxU32 *bufl = buf + j * width;

            for(i = 0; i < width; i++)
            {
                line[i] = (WORD)
                    (((bufl[i] & 0xf8) << 8)
                    | ((bufl[i] & 0xfc00) >> 5)
                    | ((bufl[i] & 0xf80000) >> 19)
                    );
            }
        }
        
        delete [] buf;

	    Glide.SrcBuffer.Lock = true;
	    Glide.SrcBuffer.Type = dwType;
	    Glide.SrcBuffer.Buffer = dwBuffer;
	    Glide.SrcBuffer.WriteMode = dwWriteMode;

	    lfbInfo->lfbPtr = Glide.SrcBuffer.Address;
    }
    else
    {
        int i;

        for(i = 0; i < width * height; i++)
            Glide.DstBuffer.Address[i] = BLUE_SCREEN;

	    Glide.DstBuffer.Lock = true;
	    Glide.DstBuffer.Type = dwType;
	    Glide.DstBuffer.Buffer = dwBuffer;
	    Glide.DstBuffer.WriteMode = dwWriteMode;

	    lfbInfo->lfbPtr = Glide.DstBuffer.Address;
    }

	lfbInfo->strideInBytes = Glide.WindowWidth * 2;

	lfbInfo->writeMode = GR_LFBWRITEMODE_565;

	return FXTRUE;
}
//typedef struct {
//    int                size;
//    void               *lfbPtr;
//    FxU32              strideInBytes;        
//    GrLfbWriteMode_t   writeMode;
//    GrOriginLocation_t origin;
//} GrLfbInfo_t;

//----------------------------------------------------------------
DLLEXPORT FxBool __stdcall
grLfbUnlock(GrLock_t dwType, GrBuffer_t dwBuffer )
{ 
#ifdef CRITICAL
	GlideMsg("grLfbUnlock( %d, %d )\n", dwType, dwBuffer ); 
#endif
	
    int width = Glide.WindowWidth;
    int height = Glide.WindowHeight;

    if((dwType & 1) == 0)
    {
        if ( !Glide.SrcBuffer.Lock )
            return FXFALSE;
        
        Glide.SrcBuffer.Lock = false;
        
        return FXTRUE; 
    }
    else
    {
        int x,           y;
        int maxx = 0,    maxy = 0;
        int minx = 2048, miny = 2048;

        if( !Glide.DstBuffer.Lock )
            return FXFALSE;

        for(y = 0; y < height; y++)
        {
            for(x = 0; x < width; x++)
            {
                if(Glide.DstBuffer.Address[y*width + x] != BLUE_SCREEN)
                {
                    if(x > maxx) maxx = x;
                    if(y > maxy) maxy = y;
                    if(x < minx) minx = x;
                    if(y < miny) miny = y;
                }
            }
        }

        if(maxx >= minx)
        {
            int xsize = maxx + 1 - minx;
            int ysize = maxy + 1 - miny;
            FxU32 *buf = new FxU32[xsize*ysize];

            glReadBuffer(Glide.DstBuffer.Buffer == GR_BUFFER_BACKBUFFER
                                                 ? GL_BACK : GL_FRONT);

            glReadPixels(minx, height-miny-ysize, xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, (void *) buf);

            for(y = 0; y < ysize; y++)
            {
                WORD *line = Glide.DstBuffer.Address + (miny + ysize - 1 - y) * width + minx;
                FxU32 *bufl = buf + y * xsize;

                for(x = 0; x < xsize; x++)
                {
                    if(line[x] != BLUE_SCREEN)
                    {
                        bufl[x] = (((line[x] & 0x1f) << 19)
                                  | ((line[x] & 0x7e0) << 5)
                                  | ((line[x] & 0xf100) >> 8)
                                  | 0xff000000);
                    }
                }
            }

            glDisable(GL_BLEND);

            glDisable(GL_TEXTURE_2D);

            glDrawBuffer(Glide.DstBuffer.Buffer == GR_BUFFER_BACKBUFFER
                                                 ? GL_BACK : GL_FRONT);

            glRasterPos2i(minx,  miny+ysize);

            glDrawPixels(xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, (void *) buf);

            glDrawBuffer(OpenGL.RenderBuffer);

            if(OpenGL.Blend)
                glEnable(GL_BLEND);

            if(Glide.DstBuffer.Buffer != GR_BUFFER_BACKBUFFER)
                glFlush();

            delete [] buf;
        }

        Glide.DstBuffer.Lock = false;

        return FXTRUE;
    }
}

//----------------------------------------------------------------
DLLEXPORT FxBool __stdcall
grLfbReadRegion( GrBuffer_t src_buffer,
                 FxU32 src_x, FxU32 src_y,
                 FxU32 src_width, FxU32 src_height,
                 FxU32 dst_stride, void *dst_data )
{
{'fraction_non_alphanumeric': 0.16902691511387163, 'fraction_numerical': 0.026749482401656314, 'mean_word_length': 1.4248995983935744, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1006072', 'n_tokens_mistral': 4439, 'n_tokens_neox': 3983, 'n_words': 815}