<commit_before>#include "SDL_gpu.h"

#define SDL_GPU_USE_INTERNAL_MATRICES
#include "SDL_gpu_GL_matrix.h"
#include <math.h>
#include <string.h>


#ifndef M_PI
#define M_PI 3.1415926f
#endif

static int _gpu_matrix_mode = GPU_MODELVIEW;

#ifndef GPU_MATRIX_STACK_MAX
#define GPU_MATRIX_STACK_MAX 5
#endif

// Can be used up to two times per line evaluation...
const char* GPU_GetMatrixString(float* A)
{
    static char buffer[512];
    static char buffer2[512];
    static char flip = 0;
    
    char* b = (flip? buffer : buffer2);
    flip = !flip;
    
    snprintf(b, 512, "%.1f %.1f %.1f %.1f\n"
                          "%.1f %.1f %.1f %.1f\n"
                          "%.1f %.1f %.1f %.1f\n"
                          "%.1f %.1f %.1f %.1f", 
                          A[0], A[1], A[2], A[3], 
                          A[4], A[5], A[6], A[7], 
                          A[8], A[9], A[10], A[11], 
                          A[12], A[13], A[14], A[15]);
    return b;
}

typedef struct GPU_MatrixStack
{
    unsigned int size;
    float matrix[GPU_MATRIX_STACK_MAX][16];
} GPU_MatrixStack;

static GPU_MatrixStack projection_matrix = {0};

static GPU_MatrixStack modelview_matrix = {0};

float* _GPU_GetModelView(void)
{
    if(modelview_matrix.size == 0)
        return NULL;
    return modelview_matrix.matrix[modelview_matrix.size-1];
}

float* _GPU_GetProjection(void)
{
    if(projection_matrix.size == 0)
        return NULL;
    return projection_matrix.matrix[projection_matrix.size-1];
}

float* _GPU_GetCurrentMatrix(void)
{
    if(_gpu_matrix_mode == GPU_MODELVIEW)
    {
        if(modelview_matrix.size == 0)
            return NULL;
        return modelview_matrix.matrix[modelview_matrix.size-1];
    }
    else
    {
        if(projection_matrix.size == 0)
            return NULL;
        return projection_matrix.matrix[projection_matrix.size-1];
    }
}

// Implementations based on <NAME>'s (wcochran) matrix.c

// Column-major
#define INDEX(row,col) ((col)*4 + (row))

void _GPU_MatrixCopy(float* result, const float* A)
{
    memcpy(result, A, 16*sizeof(float));
}

void _GPU_MatrixIdentity(float* result)
{
    memset(result, 0, 16*sizeof(float));
    result[0] = result[5] = result[10] = result[15] = 1;
}

// Matrix multiply: result = A * B
void _GPU_Multiply4x4(float* result, float* A, float* B)
{
    int r, c, i;
    float s;
    for(r = 0; r < 4; r++)
    {
        for(c = 0; c < 4; c++)
        {
            s = 0;
            for(i = 0; i < 4; i++)
                s += A[INDEX(r,i)]*B[INDEX(i,c)];

            result[INDEX(r,c)] = s;
        }
    }
}

void _GPU_MultiplyAndAssign(float* result, float* A)
{
    float temp[16];
    _GPU_Multiply4x4(temp, result, A);
    _GPU_MatrixCopy(result, temp);
}


// Public API

void _GPU_Dummy(void) {}

void _GPU_InitMatrix(void)
{
    projection_matrix.size = 1;
    _GPU_MatrixIdentity(projection_matrix.matrix[0]);
    
    modelview_matrix.size = 1;
    _GPU_MatrixIdentity(modelview_matrix.matrix[0]);
    
    _gpu_matrix_mode = GPU_MODELVIEW;
}

void _GPU_MatrixMode(int matrix_mode)
{
    _gpu_matrix_mode = matrix_mode;
}

void _GPU_PushMatrix()
{
    GPU_MatrixStack* stack = (_gpu_matrix_mode == GPU_MODELVIEW? &modelview_matrix : &projection_matrix);
    if(stack->size + 1 >= GPU_MATRIX_STACK_MAX)
    {
        GPU_LogError("GPU_PushMatrix() failed to push to a full stack!\n");
        return;
    }
    GPU_LogError("Pushing stack of size %u\n", stack->size);
    _GPU_MatrixCopy(stack->matrix[stack->size], stack->matrix[stack->size-1]);
    stack->size++;
}

void _GPU_PopMatrix()
{
    GPU_MatrixStack* stack = (_gpu_matrix_mode == GPU_MODELVIEW? &modelview_matrix : &projection_matrix);
    if(stack->size == 0)
    {
        GPU_LogError("GPU_PopMatrix() failed to pop an empty stack!\n");
        return;
    }
    GPU_LogError("Popping stack of size %u\n", stack->size);
    stack->size--;
}

void _GPU_LoadIdentity(void)
{
    float* result = _GPU_GetCurrentMatrix();<commit_msg>Removed stray logging from matrix stack commands.<commit_after>#include "SDL_gpu.h"

#define SDL_GPU_USE_INTERNAL_MATRICES
#include "SDL_gpu_GL_matrix.h"
#include <math.h>
#include <string.h>


#ifndef M_PI
#define M_PI 3.1415926f
#endif

static int _gpu_matrix_mode = GPU_MODELVIEW;

#ifndef GPU_MATRIX_STACK_MAX
#define GPU_MATRIX_STACK_MAX 5
#endif

// Can be used up to two times per line evaluation...
const char* GPU_GetMatrixString(float* A)
{
    static char buffer[512];
    static char buffer2[512];
    static char flip = 0;
    
    char* b = (flip? buffer : buffer2);
    flip = !flip;
    
    snprintf(b, 512, "%.1f %.1f %.1f %.1f\n"
                          "%.1f %.1f %.1f %.1f\n"
                          "%.1f %.1f %.1f %.1f\n"
                          "%.1f %.1f %.1f %.1f", 
                          A[0], A[1], A[2], A[3], 
                          A[4], A[5], A[6], A[7], 
                          A[8], A[9], A[10], A[11], 
                          A[12], A[13], A[14], A[15]);
    return b;
}

typedef struct GPU_MatrixStack
{
    unsigned int size;
    float matrix[GPU_MATRIX_STACK_MAX][16];
} GPU_MatrixStack;

static GPU_MatrixStack projection_matrix = {0};

static GPU_MatrixStack modelview_matrix = {0};

float* _GPU_GetModelView(void)
{
    if(modelview_matrix.size == 0)
        return NULL;
    return modelview_matrix.matrix[modelview_matrix.size-1];
}

float* _GPU_GetProjection(void)
{
    if(projection_matrix.size == 0)
        return NULL;
    return projection_matrix.matrix[projection_matrix.size-1];
}

float* _GPU_GetCurrentMatrix(void)
{
    if(_gpu_matrix_mode == GPU_MODELVIEW)
    {
        if(modelview_matrix.size == 0)
            return NULL;
        return modelview_matrix.matrix[modelview_matrix.size-1];
    }
    else
    {
        if(projection_matrix.size == 0)
            return NULL;
        return projection_matrix.matrix[projection_matrix.size-1];
    }
}

// Implementations based on Wayne Cochran's (wcochran) matrix.c

// Column-major
#define INDEX(row,col) ((col)*4 + (row))

void _GPU_MatrixCopy(float* result, const float* A)
{
    memcpy(result, A, 16*sizeof(float));
}

void _GPU_MatrixIdentity(float* result)
{
    memset(result, 0, 16*sizeof(float));
    result[0] = result[5] = result[10] = result[15] = 1;
}

// Matrix multiply: result = A * B
void _GPU_Multiply4x4(float* result, float* A, float* B)
{
    int r, c, i;
    float s;
    for(r = 0; r < 4; r++)
    {
        for(c = 0; c < 4; c++)
        {
            s = 0;
            for(i = 0; i < 4; i++)
                s += A[INDEX(r,i)]*B[INDEX(i,c)];

            result[INDEX(r,c)] = s;
        }
    }
}

void _GPU_MultiplyAndAssign(float* result, float* A)
{
    float temp[16];
    _GPU_Multiply4x4(temp, result, A);
    _GPU_MatrixCopy(result, temp);
}


// Public API

void _GPU_Dummy(void) {}

void _GPU_InitMatrix(void)
{
    projection_matrix.size = 1;
    _GPU_MatrixIdentity(projection_matrix.matrix[0]);
    
    modelview_matrix.size = 1;
    _GPU_MatrixIdentity(modelview_matrix.matrix[0]);
    
    _gpu_matrix_mode = GPU_MODELVIEW;
}

void _GPU_MatrixMode(int matrix_mode)
{
    _gpu_matrix_mode = matrix_mode;
}

void _GPU_PushMatrix()
{
    GPU_MatrixStack* stack = (_gpu_matrix_mode == GPU_MODELVIEW? &modelview_matrix : &projection_matrix);
    if(stack->size + 1 >= GPU_MATRIX_STACK_MAX)
    {
        GPU_LogError("GPU_PushMatrix() failed to push to a full stack!\n");
        return;
    }
    _GPU_MatrixCopy(stack->matrix[stack->size], stack->matrix[stack->size-1]);
    stack->size++;
}

void _GPU_PopMatrix()
{
    GPU_MatrixStack* stack = (_gpu_matrix_mode == GPU_MODELVIEW? &modelview_matrix : &projection_matrix);
    if(stack->size == 0)
    {
        GPU_LogError("GPU_PopMatrix() failed to pop an empty stack!\n");
        return;
    }
    stack->size--;
}

void _GPU_LoadIdentity(void)
{
    float* result = _GPU_GetCurrentMatrix();
{'fraction_non_alphanumeric': 0.13732438931780788, 'fraction_numerical': 0.026325781546639667, 'mean_word_length': 2.6907986921999067, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7232541', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2856, 'n_words': 600}