2015-03-19 33 views
-1

我一直在使用SDL2庫在C++中編寫一些基本代碼。它的工作完美無瑕,但我需要在屏幕上打印一些文本,爲此,我必須下載SDL2 TTF庫。我像安裝SDL2一樣安裝它。我想簡單地打印一個字,但一旦我編譯代碼時,Visual Studio說以下內容:未處理的異常在0x71002A95(SDL2_ttf.dll)

Unhandled exception at 0x71002A95 (SDL2_ttf.dll) in nuevo proyecto.exe: 
0xC0000005: Access violation reading location 0x00000000. 

而且該程序是行不通的,它是在一個白色的屏幕凍結(它沒有問題之前工作我試圖使用TTF庫)。我能做什麼?提前致謝。這裏是我的代碼:

#include "stdafx.h" 
#include <SDL.h> 
#include <SDL_ttf.h> 
#include <string> 

SDL_Window * ventana; 
SDL_Surface * superficie; 
SDL_Surface * alpha; 
SDL_Surface * renderizar; 
SDL_Surface * texto; 
bool inicia = false, cierra=false; 
SDL_Point mouse; 
TTF_Font *fuente = TTF_OpenFont("arial.ttf", 20); 
char* palabras="hola"; 
SDL_Color color = { 0, 0, 0, 0 }; 

int controles(){ 
    SDL_GetMouseState(&mouse.x,&mouse.y); 
    return 0; 
} 

int graficos(char *archivo){ //la primera vez inicia ventana 
    //el argumento es el nombre del bmp a renderizar 
    if (inicia == false){ ventana = SDL_CreateWindow("ventana", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); } //abre ventana solo una vez 
    inicia = true; // no permite que se abra mas de una vez la ventana 
    superficie = SDL_GetWindowSurface(ventana); 
    alpha = SDL_LoadBMP("alpha.bmp"); 
    texto = TTF_RenderText_Solid(fuente, palabras, color); 
    renderizar = SDL_LoadBMP(archivo); 
    SDL_Rect rectangulo = { 0, 0, 640, 480 }; 
    SDL_Rect rrenderizar = { mouse.x, mouse.y, 4, 4 }; 
    SDL_Rect rtexto = { 0, 0, 60, 60 }; 
    SDL_BlitSurface(alpha, NULL, superficie, &rectangulo); 
    SDL_BlitSurface(renderizar, NULL, superficie, &rrenderizar); 
    SDL_BlitSurface(texto, NULL, superficie, &rtexto); 
    SDL_UpdateWindowSurface(ventana); 
    return 0; 
} 

int main(int argc, char **argv) 
{ 
    TTF_Init(); 
    SDL_Init(SDL_INIT_VIDEO); 
    SDL_Event evento; 
    while (!cierra){ 
     SDL_PollEvent(&evento); 
     switch (evento.type){ 
     case SDL_QUIT:cierra = true; break; 
     } 
     //programa aqui abajo 
     controles(); 
     graficos("hw.bmp"); 
    } 
    TTF_Quit(); 
    SDL_Quit(); 
    return 0; 
} 

PS:我在Debug文件夾中有DLL,字體和其他文件。

回答

1

根據SDL_TTF Documentation

int TTF_Init() 

初始化TrueType字體API。 在使用此庫中的其他函數之前,必須將其稱爲 ,但TTF_WasInit除外。在此調用之前,SDL 不必進行初始化。

在致電TTF_Init時,請在致電TTF_OpenFont時聲明字體變量。相反,你應該這樣做:

TTF_Font* fuente = NULL;  
int main() 
{ 
    TTF_Init(); 
    fuente = TTF_OpenFont("arial.ttf", 20); 
    ... 
} 
+0

謝謝!工作就像一個魅力:) – gregalerna 2015-03-19 01:20:05