小编Jac*_*b H的帖子

SDL TTF - 换行和更改换行高度?

我最近一直在使用 SDL2 编写一个小型文本冒险游戏,并遇到了换行问题。我正在使用 TTF_RenderText_Blend_Wrapped() 来渲染我的字符串,这给了我一些漂亮的包裹线。但行高是一个问题,行看起来被压在一起,像“jqg”这样的字母与“tli”这样的字母重叠。

有谁知道是否有办法改变行高?TTF_RenderText_Blished_Wrapped() 甚至仍然没有出现在 SDL_ttf 的文档中。我应该编写自己的文本换行功能吗?

字体大小为 16pt,样式为 TTF_STYLE_BOLD,字体可以在这里找到。下面的代码应该会重现该错误,但几乎没有错误检查,请自行承担使用风险。这是代码的输出:

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

int main(int argc, char *argv[]) {
    SDL_Window *gui;
    SDL_Surface *screen, *text;
    SDL_Event ev;
    TTF_Font *font;
    int running = 1;

    const char *SAMPLETEXT = "This is an example of my problem, for most lines it works fine, albeit it looks a bit tight. But for any letters that \"hang\" below the line, there is a chance of overlapping with the letters …
Run Code Online (Sandbox Code Playgroud)

c sdl-ttf sdl-2

5
推荐指数
1
解决办法
2370
查看次数

malloc'd指针将自身重置为NULL?

我使用malloc获得了一些非常奇怪的行为.我从不分配超过4kB,所以这对我来说似乎特别奇怪.我的主要看起来像:

int main(int argc, char **argv)
{
    char *buf;
    char *raw = malloc(1024);
    fgets(raw, 1024, stdin);

    parse(raw, &buf); // Process raw input, get parse length

    printf("raw: 0x%p\n", raw); // Outputs 0x00000000

    if(raw != NULL)
    {  // Only prints when less than 10 characters are entered
        free(raw); 
        printf("raw is not NULL\n");
    }
    free(buf);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我输入少于10个字符时,这可以正常工作,当我输入10个字符时,我得到分段错误,当我输入10个以上时,输出显示raw为NULL.应该注意的是,raw的大小是1024 malloc'd字节,所以我应该有更多的工作空间.

解析函数是:

int parse(char *src, char **dst)
{
    int num_valid = 0, len = strlen(src), j = 0;

    // Count number of valid characters …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers segmentation-fault

0
推荐指数
1
解决办法
91
查看次数

标签 统计

c ×2

malloc ×1

pointers ×1

sdl-2 ×1

sdl-ttf ×1

segmentation-fault ×1