以下是事实:我在Ubuntu上使用代码块.我已经安装了SDL和SDL_ttf并已成功包含和链接它们.我想将文本渲染到字体的屏幕FreeSerif.
这是问题:当程序到达行TTF_OpenFont("FreeSerif.ttf,20")时,它返回NULL,如果传递给TTF_RenderText_Solid函数,则会导致段错误.我已将字体文件添加到项目中,但仍然无效.
这是代码:
TTF_Init();
TTF_Font *font = TTF_OpenFont("FreeSerif.ttf",20); //This returns NULL
if(!font){printf("Unable to open font");exit(1);} //The program exits here
Run Code Online (Sandbox Code Playgroud) 我最近一直在使用 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) 我有同样的问题,TTF_OpenFont返回NULL并出错.
TTF_OpenFont("/absolute/path/to/SourceSansPro-Black.ttf", 25);
std::cout << TTF_GetError() << std::endl;
Run Code Online (Sandbox Code Playgroud)
我得到" 无法加载字体文件 "
我没有想法.
我一直noodling周围的SDL和OpenGL(在C++),并决定得到一些文字到我的比赛.
我已经遵循了一些教程,但我总是得到同样的错误:"找不到.ttf"我确定之前已经问到了,但是你应该把字体放在哪里,你应该在TTF_OpenFont中首先写一下参数?这是迄今为止的TTF部分.
if (TTF_Init() != 0)
{
cerr << "TTF_Init() Failed: " << TTF_GetError() << endl;
SDL_Quit();
exit(1);
}
TTF_Font *font;
font = TTF_OpenFont("FreeSans.ttf", 24);
if (font == NULL)
{
cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl; // <-- This the error report
TTF_Quit();
SDL_Quit();
exit(1);
}
SDL_Surface *text;
SDL_Color text_color = {255, 255, 255};
text = TTF_RenderText_Solid(font, "BLAH, BLAH, BLAH!", text_color);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用SDL_ttf创建字体字典,就像我用SDL_image制作字典一样.由于字体存储时pnt_size我创建了一个包含此信息的结构:
struct fontinfo
{
string assetname;
int size;
};
Run Code Online (Sandbox Code Playgroud)
其次是两个词典:
map<string, SDL_Surface*> imageDictionary;
map<fontinfo*, TTF_Font*> fontDictionary;
Run Code Online (Sandbox Code Playgroud)
两者之间的区别在于字体字典不仅需要包含文件的字符串,还需要包含字体的大小.
然后,当对象请求图像或字体时,它会为其调用get函数.现在getSprite工作正常:
SDL_Surface* ResourceManager::getSprite(string assetname)
{
if (assetname == "")
return NULL;
map<string, SDL_Surface*>::iterator it = imageDictionary.find(assetname);
if (it != imageDictionary.end())
return it->second;
else
{
SDL_Surface* image = Load_Image(assetname);
if (image != NULL)
imageDictionary.insert(make_pair(assetname, image));
return image;
}
}
Run Code Online (Sandbox Code Playgroud)
该getFont方法几乎完全相同,除了它使用a fontinfo而不是string:
TTF_Font* ResourceManager::getFont(string assetname, int size)
{
if (assetname == "" || size …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 SDL2-ttf 库编写 SDL2 程序,并希望在 CMakeLists.txt 中添加一个检查。我怎么做?
我正在使用 CMake 3.1。
我已经研究了几种可能的解决方案来尝试解决此问题,但是在使用SDL_ttf和SDL2绘制文本时仍然遇到以下问题。
julian@julian-linux:~/Documents/SDL/Font Demo/pt2$ make
g++ texttest.cpp -w -lSDL2 -lSDL2_ttf -LSDL2_image -o texttest
/usr/bin/ld: cannot find -lSDL2_ttf
collect2: error: ld returned 1 exit status
Makefile:23: recipe for target 'all' failed
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)
我的程序如下:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <cmath>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#define down 0
#define left 1
#define up 2
#define right 3
//Screen dimension constants
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT …Run Code Online (Sandbox Code Playgroud) 所以我试着在SDL上制作一些东西,但是在第一个程序中我有内存学习(idk泄漏与否)所以有一些代码:
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#define SCREENSIZEX 180
#define SCREENSIZEY 300
SDL_Window* mainwind = NULL;
SDL_Renderer* rend = NULL;
TTF_Font* Usefont = NULL;
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
Uint32 windowflags;
windowflags = SDL_WINDOW_SHOWN;
mainwind = SDL_CreateWindow("FooBar",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREENSIZEX,
SCREENSIZEY,
windowflags);
rend = SDL_CreateRenderer(mainwind, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);
int imgFlags = IMG_INIT_PNG;
IMG_Init(imgFlags);
TTF_Init();
Usefont = TTF_OpenFont("DOTMBold.TTF",90);
SDL_Surface* TextSurf = NULL;
SDL_Texture* TextTexture = NULL;
SDL_Color UsingColor;
UsingColor.r=0;
UsingColor.g=255;
UsingColor.b=255;
UsingColor.a=100; …Run Code Online (Sandbox Code Playgroud) 下面是一个例子:
TTF_Font *Sans = TTF_OpenFont("resources/fonts/lato/Lato-Semibold.ttf", 36);
if( Sans == NULL )
{
std::cout << "Failed to load font! SDL_ttf Error: " << TTF_GetError() << std::endl;
}
else
{
SDL_Color White = {255, 255, 255};
SDL_Surface *surfaceMessage = TTF_RenderText_Blended(Sans, "GAME OVER", White);
SDL_Texture *Message = SDL_CreateTextureFromSurface(renderer_, surfaceMessage);
SDL_Rect Message_rect;
Message_rect.x = 100;
Message_rect.y = 100;
Message_rect.w = 500;
Message_rect.h = 500;
SDL_RenderCopy(renderer_, Message, NULL, &Message_rect);
SDL_RenderPresent(renderer_);
Run Code Online (Sandbox Code Playgroud)
}
(是的,我稍后释放表面)
所以问题是,我有一个函数可以让你在渲染器中显示文本,我在一个项目中实现了它,后来我想测试这个项目以防止可能的内存泄漏和宾果游戏!
经过一段时间的研究,它来自这个著名的显示文本的功能......
所以我做了一个小例子:
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
void renderText(SDL_Renderer* renderer, const char* text, int const size, const SDL_Rect text_rect, const SDL_Color text_color)
{
#ifdef __linux__
const char* font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf";
#elif _WIN32
const char* font_path = "C:\\Windows\\Fonts\\Arial.ttf";
#endif
TTF_Font* font = TTF_OpenFont(font_path, size);
if (!font) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_ttf - %s", TTF_GetError()); return; }
SDL_Surface* text_surface = TTF_RenderText_Blended(font, text, text_color);
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
if (text_color.a!=255) SDL_SetTextureAlphaMod(text_texture, text_color.a);
SDL_RenderCopy(renderer, text_texture, NULL, &text_rect);
SDL_DestroyTexture(text_texture);
SDL_FreeSurface(text_surface);
TTF_CloseFont(font);
}
int main(int argc, char** …Run Code Online (Sandbox Code Playgroud) 我正在构建一个 6502 模拟器,我希望以可视方式表示 cpu 和内存状态。
我正在使用 SDL2 来实现此目的。当 6502 cpu 或内存改变状态时,我必须在 SDL 窗口上渲染文本。
即我希望以文本和数字的形式显示整个内存内容、当前正在执行的指令、先前的CPU状态、当前的CPU状态。
这是我尝试使用 Linux 系统中已有的字体来呈现文本。后来我希望渲染动态文本和数字而不是静态字符串。
#include<SDL2/SDL.h>
#include<SDL2/SDL_ttf.h>
#define SCREEN_HEIGHT 640
#define SCREEN_WIDTH 480
int quit=false;
SDL_Window *window;
SDL_Renderer *renderer;
int initializeDrawing(int argc,char** argv){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
window = SDL_CreateWindow("6502 cpu display!", 100, 100, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_WINDOW_SHOWN);
if (window == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
renderer = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 C++20 范围和函数式编程加载目录中的所有 true-type 字体。但是,由于字体是一种资源,因此我在范围接口内分配内存。我想这就是 valgrind 认为我有泄漏的原因。我有一些std::views新分配的原始指针,最终会被丢弃 - 然而,这些原始指针被转换并复制到唯一指针的向量中。
有问题的代码:
// free a font resource
struct font_deleter {
void operator()(TTF_Font * font) { TTF_CloseFont(font); }
};
// aliases
using unique_font = std::unique_ptr<TTF_Font, font_deleter>;
using font_table = std::unordered_map<std::string, TTF_Font *>;
template<typename expected_t>
using result = tl::expected<expected_t, std::string>;
// determine if a path is a valid font file
auto _is_font_fxn(std::error_code & ec) {
return [&ec](fs::path const & path) {
return fs::is_regular_file(path, ec) and path.extension() == ".ttf";
};
} …Run Code Online (Sandbox Code Playgroud) 这是我的.cpp文件:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
bool runningOnEmpty = false;
const char* title;
int xpos, ypos, width, height;
bool fullscreen = false;
SDL_Window *window;
SDL_Renderer *renderer;
void init(){
int flags = 0;
if(fullscreen){
flags = SDL_WINDOW_FULLSCREEN;
}
if(!SDL_Init(SDL_INIT_EVERYTHING)){
std::cout << "Sdl initialised!!\n";
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if(window){
std::cout << "window created!!\n";
}
renderer = SDL_CreateRenderer(window, -1, 0);
if(renderer){
std::cout << "renderer created!!\n";
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}
runningOnEmpty = true;
} else {
runningOnEmpty …Run Code Online (Sandbox Code Playgroud)