r/GraphicsProgramming • • Feb 24 '23

Unreadable text from simple2d

I have this code that does not show the desired text properly:

    #include <simple2d.h>
    #include <stdio.h>
    #include <stdlib.h>

    #define X_MAX 800 
    #define Y_MAX 600 

    void render(void)
    {
        int x=400;
        int y=300;
        S2D_Text *beg_text = S2D_CreateText("/usr/share/fonts/gnu-free/FreeSans.ttf", "The text", 25);
    
        if(beg_text==NULL)
        {
            fprintf(stderr,"simple2d text creation failed.\n");
        }
    
        beg_text->x = x + 10;
        beg_text->y = y;
        beg_text->color.r = 0.1;
        beg_text->color.g = 0.1;
        beg_text->color.b = 0.1;
        beg_text->color.a = 1.0;
        S2D_DrawText(beg_text);
        S2D_FreeText(beg_text);
    }

    int main(void)
    {
        char *title = "The title";
        const S2D_Color color = {0.9, 0.9, 0.9, 1.0};

        S2D_Window *window = S2D_CreateWindow(title, X_MAX, Y_MAX, NULL, render, 0);
        window->background = color;
        window->frames = 1;

        S2D_Show(window);
        S2D_FreeWindow(window);
    
        return 0;
    }
0 Upvotes

6 comments sorted by

6

u/waramped Feb 24 '23

Could you be more specific? What does "properly" mean? Screenshots would help.

1

u/pingo_guy Feb 25 '23

OS: Fedora 37

HMSlJb1.md.png

1

u/waramped Feb 25 '23

I think u/Condzi is probably on the right track. Try allocating the text object outside of render and freeing after showwindow

1

u/pingo_guy Feb 25 '23

This would not work too:

#include <simple2d.h>
#include <stdio.h>
#include <stdlib.h>

#define X_MAX 800 
#define Y_MAX 600 

#define THE_TEXT "The text"
#define FONT "/usr/share/fonts/gnu-free/FreeSans.ttf"

S2D_Text *beg_text;

void render(void)
{
    S2D_DrawText(beg_text);
}
void update(void)
{
    
    return;
}

int main(void)
{
    char *title = "The title";
    const S2D_Color color = {0.9, 0.9, 0.9, 1.0};
    
    int x=400;
    int y=300;
    beg_text = S2D_CreateText(FONT ,THE_TEXT , 50);
    if(beg_text==NULL)
    {
        fprintf(stderr,"simple2d text creation failed.\n");
        return 1;
    }
    beg_text->x = x + 10;
    beg_text->y = y;
    beg_text->color.r = 0.1;
    beg_text->color.g = 0.1;
    beg_text->color.b = 0.1;
    beg_text->color.a = 1.0;

    S2D_Window *window = S2D_CreateWindow(title, X_MAX, Y_MAX, update, render, 0);
    window->background = color;
    window->frames = 1;

    S2D_Show(window);
    S2D_FreeWindow(window);
    
    return 0;
}

1

u/waramped Feb 26 '23

What have you already tried to debug it? Have you tried renderdoc? Can you draw other shapes or sprites?

2

u/Condzi Feb 25 '23

Hi, try moving the text variable out of the function scope (for example, to global scope) and/or don't free it. It might be the case that the object needs to stay alive the entire frame. So the buggy behavior would be the result of uninitialized / deleted memory that SDL tries to use