1
Я рассмотрел несколько возможных решений, чтобы попытаться исправить это, но у меня все еще возникает проблема с рисованием текста с SDL_ttf и SDL2.Ошибка SDL2_TTF на Ubuntu
[email protected]:~/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
Моя программа выглядит следующим образом:
#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 = 600;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
TTF_Font *font;
SDL_Surface *sText;
//Loads individual image as texture
SDL_Texture* loadTexture(std::string path);
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
SDL_Surface * image = NULL;
SDL_Texture * texture = NULL;
SDL_Surface * tile_image = NULL;
SDL_Texture * tile_texture = NULL;
bool init(){
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")){
printf("Warning: Linear texture filtering not enabled!");
}
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(gWindow == NULL){
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer == NULL){
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
} else {
//Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if(!(IMG_Init(imgFlags) & imgFlags)){
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = false;
}
}
}
TTF_Init();
}
return success;
}
bool loadMedia(){
//Loading success flag
bool success = true;
image = IMG_Load("assets/player_sprites.png");
texture = SDL_CreateTextureFromSurface(gRenderer, image);
tile_image = IMG_Load("assets/tiles.png");
tile_texture = SDL_CreateTextureFromSurface(gRenderer, tile_image);
font = TTF_OpenFont("fonts/staravenue.ttf", 12);
std::cout << font << std::endl;
if(font == NULL) {
std::cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << "\n";
success = false;
}
//Nothing to load
return success;
}
void close(){
//Destroy window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyTexture(texture);
SDL_DestroyTexture(tile_texture);
SDL_FreeSurface(image);
SDL_FreeSurface(tile_image);
SDL_DestroyWindow(gWindow);
TTF_CloseFont(font);
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
TTF_Quit();
}
int main(int argc, char* args[]){
bool quit = false;
srand (time(NULL));
if(!init()){
printf("Failed to initialize!\n");
bool run = false;
} else if(!loadMedia()){
printf("Failed to load media!\n");
}
std::cout << font << std::endl;
//Event handler
SDL_Event e;
while(!quit){
while(SDL_PollEvent(&e) != 0) //event handling
{
if(e.type == SDL_QUIT){
quit = true;
}
}
/*
//Clear screen
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
*/
/* ...(We'll do something here later.) */
SDL_Color clrFg = {0,0,255,0}; // Blue ("Fg" is foreground)
sText = TTF_RenderText_Solid(font, "", clrFg);
SDL_Rect rcDest = {0,0,0,0};
SDL_FreeSurface(sText);
//Update screen
SDL_RenderPresent(gRenderer);
SDL_Delay(16);
}
//Free resources and close SDL
close();
return 0;
}
Шрифт находится в соответствующем каталоге.
Мой Makefile выглядит следующим образом:
OBJS = texttest.cpp
#CC specifies which compiler we're using
CC = g++
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -w
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_ttf -LSDL2_image
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = texttest
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
Заранее спасибо за любую помощь.
'не может найти -lSDL2_ttf' означает, что вы не имеете' libSDL2_ttf.so' или ' libSDL2_ttf.a' в ваших путях библиотек компоновщиков. Либо вы не установили пакет разработки для указанной библиотеки (но тогда как у вас есть заголовки?), Сломали его или у вас получилась неправильная архитектура. Также использование флага '-w' компилятора - наихудшая идея, хотя и не связанная с вопросом. – keltar