I've been writing some rudimentary code in C++ with the SDL2 library. It was working flawlessly, but I needed to print some text on screen, and to do that, I had to download the SDL2 TTF library. I installed it just like I did with SDL2. I tried to simply print a word, but once I compile the code, Visual Studio says the following:
Unhandled exception at 0x71002A95 (SDL2_ttf.dll) in nuevo proyecto.exe:
0xC0000005: Access violation reading location 0x00000000.
And the program just doesn't work, it's frozen in a white screen (it worked with no problems before I tried to use the TTF library). What can I do? Thanks in advance. Here's my code:
#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: I do have the DLL's, fonts and other files in the Debug folder.
According to SDL_TTF Documentation:
int TTF_Init()
Initialize the truetype font API. This must be called before using other functions in this library, except TTF_WasInit. SDL does not have to be initialized before this call.
You call TTF_OpenFont
before calling TTF_Init
when declaring your font variable. Instead you should do:
TTF_Font* fuente = NULL;
int main()
{
TTF_Init();
fuente = TTF_OpenFont("arial.ttf", 20);
...
}