Sign in to follow this  

Couldn't open lazy.ttf

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Description
I have program, which will create small window and write into some text:


#include <string>
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <fstream>

#define SIRKA 240
#define VYSKA 320

using namespace std;

/***************************************/
/* FUNKCE PRO PRACI S SDL A KNIHOVNAMI */
/***************************************/

// Nacte vsechny komponenty SDL
bool K_LoadSDL() {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
cout << "Nezdarilo se nacist SDL: " << SDL_GetError() << endl;
return false;
SDL_Quit();
}
return true;
}

// Nacte SDL_TTF
bool K_LoadTTF() {
if (TTF_Init() == -1) {
cout << "Nezdarilo se nacist TTF: " << TTF_GetError() << endl;
return false;
SDL_Quit();
TTF_Quit();
}
return true;
}


/**********************************/
/* FUNKCE PRO PRACI S OBRAZOVKAMI */
/**********************************/

// Nacte obrazek na obrazovku
bool K_AddImageToScreen(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
SDL_Rect umisteni;

umisteni.x = x;
umisteni.y = y;

if (SDL_BlitSurface(source, NULL, destination, &umisteni) == -1){
cout << "Nezdarilo se nacist obrazek na obrazovku: " << SDL_GetError() << endl;
return false;
}
return true;
}

bool K_ReloadScreen(SDL_Surface* screenName) {
if (SDL_Flip(screenName) == -1) {
cout << "Nezdarilo se obnovit obrazovku: " << SDL_GetError() << endl;
return false;
}
return true;
}

// Funkce pro nacteni textu na obrazovku
// bool AddTextToScreen(int r, int g, int b, string font, string text, int size)

/**************************************************/
/* FUNKCE ZPRACOVAVAJICI AKCE V PROGRAMU (EVENTS) */
/**************************************************/

// Kontrola akci v programu
bool K_Process() {
SDL_Event event;
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_ESCAPE:
return false;
break;
default: break;
}
break;
case SDL_QUIT:
return false;
break;
default: break;
}
}
return true;
}

/****************************/
/* JEDNOTLIVA OKNA PROGRAMU */
/****************************/

// Hlavni okno programu
bool K_MainWindow() {
SDL_Surface* screen = NULL; // Obrazovka
SDL_Surface* message = NULL; // Text

bool done = false;
while(!done){
done = !K_Process();

// Nacteni SDL
K_LoadSDL();

// Nacteni TTF
K_LoadTTF();

// Nacteni fontu
TTF_Font *font = NULL; // Font
font = TTF_OpenFont("lazy.ttf", 28);
if (!font) {
cout << "Nezdarilo se nacit font: " << TTF_GetError() << endl;
return false;
SDL_Quit();
TTF_Quit();
}
SDL_Color color = {255, 255, 255};
message = TTF_RenderText_Solid( font, "POKUS", color);

// Nacteni hlavni obrazovky + kontrola
screen = SDL_SetVideoMode(SIRKA, VYSKA, 32, SDL_SWSURFACE);

if (screen == NULL){
cout << "Nepodarilo se vytvorit okno: " << SDL_GetError() << endl;
return false;
SDL_Quit();
}

// Nastaveni titulku okna
SDL_WM_SetCaption("Kubatura", NULL);

K_AddImageToScreen(20,20, message, screen);

// Obnoveni obrazovky
K_ReloadScreen( screen );
}
SDL_FreeSurface(message);
return true;
}

/***************/
/* FUNKCE MAIN */
/***************/

int main() {
K_MainWindow();
SDL_Quit();
return 0;
}


Compilation
# g++ -o kubatura.run kubatura.cpp -lSDL_ttf `sdl-config --cflags --libs`

Compilation is without any problem.

Problem
When I was run this program, it showed window with text (there is all ok). But after 5-10 seconds it wrote this:
Nezdarilo se nacit font: Couldn't open lazy.ttf

And program went off.

I think, the problem is somewhere in TTF/SDL_Close, but i don't know where.

Thanks for your help. L.

Share this post


Link to post
Share on other sites
Are you sure you want to be initialising SDL and loading the font every time you go through that loop? Any reason you don't just do those things once, outside the loop?

Share this post


Link to post
Share on other sites

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this