SDL_FontCache icon indicating copy to clipboard operation
SDL_FontCache copied to clipboard

BUG: Hard limit on rendering more than 999 characters

Open alexlnkp opened this issue 4 months ago • 0 comments

I was trying to make a "pseudo-terminal" SDL2 window, when I encountered this. I thought I was going insane at first, but there ACTUALLY seems to be some kind of a weird limit on number of characters that can be rendered in a single draw call, which is equal to 999. Code that reproduces the issue:

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>

#include "lib/SDL_FontCache.h"

#define GRID_X_RES 41
#define GRID_Y_RES 41

void render_grid(char* grid) {
    Uint32 x = 0, y = 0, idx = 0;
    for (y = 0; y < GRID_Y_RES; ++y) {
        for (x = 0; x < GRID_X_RES; ++x) {
            idx = x + y * GRID_X_RES;
            grid[idx] = '.' - ('$' * ((idx+1) % GRID_X_RES == 0));
        }
    }
    grid[GRID_X_RES * GRID_Y_RES + GRID_Y_RES] = '\0';
}

int main(int argc, char* argv[]) {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    }

    SDL_Window* window = SDL_CreateWindow(
        "SDL2 Pseudo-Terminal",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        800, 600,
        SDL_WINDOW_SHOWN
    );

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    FC_Font* font = FC_CreateFont();
    FC_LoadFont(font, renderer, "res/PressStart2P-Regular.ttf", 20, FC_MakeColor(255, 255, 255, 255), TTF_STYLE_NORMAL);  

    int running = 1;

    /* malloc gives same results as simple `char grid[((GRID_X_RES * GRID_Y_RES + GRID_Y_RES) + 1)]` */
    char* grid = malloc(((GRID_X_RES * GRID_Y_RES + GRID_Y_RES) + 1) * sizeof(char));

    SDL_Event event;
    while (running) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = 0;
            }
        }

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        render_grid(grid);
        FC_Draw(font, renderer, 0, 0, "%s", grid); 

        SDL_RenderPresent(renderer);
    }

    FC_FreeFont(font);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

Screenshot image

Changing GRID_Y_RES to any number higher than what is currently set doesn't change a thing in rendering, however printing the constructed grid to regular terminal does what is expected.

........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................

I'm not exactly sure if this is an SDL2 quirk or this library's issue, just thought I'd report this nonetheless

alexlnkp avatar Oct 04 '24 12:10 alexlnkp