I am using the following command to compile
g++ s19_demo2.cpp -o s19_ttf $(pkg-config --cflags --libs sdl2) -lSDL2_ttf
& its giving an error
Failed to load font : Couldn't open arial .ttf
does it requires any path for the font ??
I am using the following command to compile
g++ s19_demo2.cpp -o s19_ttf $(pkg-config --cflags --libs sdl2) -lSDL2_ttf
& its giving an error
Failed to load font : Couldn't open arial .ttf
does it requires any path for the font ??
I am compiling the following code–
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
using namespace std;
using std::cout;
using std::endl;
int main(int argc, char *argv[])
{
SDL_Window* window1;
SDL_Renderer* renderer1;
SDL_Surface* surface1;
SDL_Texture* image_texture;
SDL_Texture* text_texture;
TTF_Font* myfont;
SDL_Color colred = {255, 0, 0};
SDL_Rect text_rect;
text_rect.x = 0;
text_rect.y = 0;
text_rect.w = 100;
text_rect.h = 100;
window1 = SDL_CreateWindow("my_example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 580, 0);
renderer1 = SDL_CreateRenderer(window1, -1, SDL_RENDERER_ACCELERATED);
surface1 = SDL_LoadBMP("/home/odroid/Downloads/t2.bmp");
image_texture = SDL_CreateTextureFromSurface(renderer1, surface1);
SDL_FreeSurface(surface1);
SDL_Rect SrcR;
SDL_Rect DestR;
/*SrcR.x = 0;
SrcR.y = 0;
SrcR.w = 640;
SrcR.h = 580; */
DestR.x = 0;
DestR.y = 0;
DestR.w = 640;
DestR.h = 580;
//-----------------------------------------------------------------------------------------------------
if ( TTF_Init() == -1 )
cout << " Failed to initialize TTF : " << SDL_GetError() << endl;
myfont = TTF_OpenFont("Sans.ttf", 12);
if ( myfont == nullptr )
cout << " Failed to load font : " << SDL_GetError() << endl;
surface1 = TTF_RenderText_Solid(myfont, "hello", colred);
text_texture = SDL_CreateTextureFromSurface(renderer1, surface1);
SDL_FreeSurface(surface1);
//------------------------------------------------------------------------------------------------------
SDL_RenderCopy(renderer1, image_texture, NULL, &DestR);
SDL_RenderCopy(renderer1, text_texture, NULL, &text_rect);
SDL_RenderPresent(renderer1);
SDL_Delay(5000);
SDL_DestroyTexture(image_texture);
SDL_DestroyTexture(text_texture);
SDL_DestroyRenderer(renderer1);
SDL_DestroyWindow(window1);
TTF_Quit();
SDL_Quit();
return 0;
}
And I don’t know why it is giving this error—
odroid@odroid:~/Desktop/is2/@_opencl/cl_sdl$ ./s19_ttf
Failed to load font : Couldn’t open Sans.ttf
Segmentation fault
also during executing the code, screen is also going blank for a second.
My initial guess is that it cannot find “Sans.ttf” is not the correct path.
But I cannot be sure as I don’t know how your folders and files are structured.
Do a quick test
Put a copy of the font in the same directory and as the textures,
myfont = TTF_OpenFont("/home/odroid/Downloads/Sans.ttf", 12);
This will let you know if its a path issue.
Thank you, it worked.
I copied sans.ttf into my working directory and it gave me the desired output.