Advertisement

SDL_TTF crashes c++ program?

Started by April 16, 2017 06:41 PM
14 comments, last by rip-off 7 years, 7 months ago
Also when you detect an error (e.g. window == null), try printing SDL_GetError() to the console before quitting. If you get an error with SDL_TTF, use TTF_GetError().

If you "return false" from main(), technically you are reporting your program ran successfully. Main returns an "Int", not a boolean (although a boolean will convert to an Int). So when an error occurs, use " return 1" instead, or you can use EXIT_FAILURE (http://en.cppreference.com/w/cpp/utility/program/EXIT_status).

This code would crash if no "Sans.ttf" file could be found in app's current working directory. It doesn't crash otherwise for me. Check what's result from `TTF_OpenFont()`, is it null or a valid pointer.

How can I check this? I think you are right in that i dont have Sans.ttf in the current working directory, but how can i find out what that directory is?

Advertisement

Working directory could be anything, and probably you don't need to know that at all.

What you need to know is where .exe file located, so resources could be read by the path relative to .exe location. See SDL_GetBasePath() function: https://wiki.libsdl.org/SDL_GetBasePath

Place your assets near exe file, maybe in subdirectories. Then open it by concatenated name - base path and asset file name.

Working directory could be anything, and probably you don't need to know that at all.

What you need to know is where .exe file located, so resources could be read by the path relative to .exe location. See SDL_GetBasePath() function: https://wiki.libsdl.org/SDL_GetBasePath

Place your assets near exe file, maybe in subdirectories. Then open it by concatenated name - base path and asset file name.

Alright you and everyone else has been super helpful. This fixed the crash but now when I run the program and update the screen, the text isn't there. My code is this:



#include <iostream>
#include <fstream>
#include <SDL.h>
#include "SDL_ttf.h"
#include "Input.h"
#include "Screen.h"


using namespace std;
Screen screen;
Input input;
SDL_Renderer *renderer;
int main(int argc, char* argv[]) {
	 screen.init();

	 screen.test_text();

	 screen.update();


	 while (true) {
	     string event = input.check_event();
	     if(event == "1"){
	    	 break;
	     }
	 }

	TTF_Quit();
	SDL_Quit();
    return 0;
}


 

screen.init(); does this:


bool Screen::init() {
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		return false;
	}
	if(TTF_Init()==-1) {
		     printf("TTF_Init: %s\n", TTF_GetError());
		     exit(2);
	 }

	window = SDL_CreateWindow("Particle Simulation", SDL_WINDOWPOS_UNDEFINED,
	SDL_WINDOWPOS_UNDEFINED, WIDTH, HIGHT, SDL_WINDOW_SHOWN);
	if (window == NULL) {
		return false;
	}
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
	if (renderer == NULL) {
		return false;
		SDL_DestroyWindow(window);
		SDL_Quit();
	}
	texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
			SDL_TEXTUREACCESS_STATIC, WIDTH, HIGHT);
	if (texture == NULL) {
		return false;
		SDL_DestroyWindow(window);
		SDL_DestroyRenderer(renderer);
		SDL_Quit();
	}
	buffer = new Uint32[WIDTH * HIGHT];

	return true;
} 

screen.test_text(); does this:


void Screen::test_text(){
	TTF_Font* font = TTF_OpenFont("C:\\Users\\KP7\\Desktop\\C++2\\Hero\\Debug\\block.ttf", 24); 

	SDL_Color color = {255, 255, 255};  

	SDL_Surface* surfaceMessage = TTF_RenderText_Solid(font, "Hello World!", color); 

	SDL_Texture* message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); 

	SDL_Rect message_rect; 
	message_rect.x = 0;  
	message_rect.y = 0; 
	message_rect.w = 100; 
	message_rect.h = 100; 

	

	SDL_RenderCopy(renderer, message, NULL, &message_rect); 
} 

and screen.update(); does this:


void Screen::update() {
	SDL_UpdateTexture(texture, NULL, buffer, WIDTH * sizeof(Uint32));
	SDL_RenderClear(renderer);
	SDL_RenderCopy(renderer, texture, NULL, NULL);
	SDL_RenderPresent(renderer);
} 

This just displays a black screen for me. Does anyone know why?

Screen::update does a SDL_RenderClear, which essentially deletes everything you tried to render in test_text.

Additionally, you have a SDL_RenderCopy there as well, which I assume would overwrite anything you have in text_text.

You should reshuffle your code a bit:

1. Load everything you need (fonts, etc.).

2. While loop, lasting until some condition:

3a. Clear.

3b. Draw stuff (background, then text). Could also be just text, to make things slightly less complicated.

3c. Present.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement