Advertisement

display text in sdl

Started by October 03, 2024 09:19 PM
14 comments, last by pbivens67 2 months, 1 week ago

“Doesn't work” is not a valid bug report. You should provide more details what is not working.

Don't do calls to library functions and blindly assume they work. Usually these functions have a way to return “I failed” that you can check for. With a bit of luck it can even indicate what it found wrong.

By checking returned data from calls everywhere, it's not “somewhere in this heap of code something is wrong”, but “this particular function fails”. In that way you get more focus what the problem is, and can ponder about what happens at that point and how to find the cause.

For example, for the TTF_OpenFont call, read https://wiki.libsdl.org/SDL2_ttf/TTF_OpenFont . Note it may return an error value if it fails. Do that for all function calls. Check its documentation, and verify that your code correctly uses the functions so they don't give errors.

Advertisement

I stubbed out my code into a separate file. It compiles and runs but does output any text.

#include <iostream>
#include "SDL_ttf.h"
#include "SDL.h"

SDL_Surface* screen;
SDL_Surface* fontSurface;
SDL_Color fColor;
SDL_Rect fontRect;

SDL_Event Event;

TTF_Font* font;

void fontInit()
{
	TTF_Init();
	font = TTF_OpenFont("C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\Arial.ttf", 12);
	fColor.r = 255;
	fColor.g = 255;
	fColor.b = 255;
}

void printF(const char* c, int x, int y)
{
	fontSurface = TTF_RenderText_Solid(font, c, fColor);
	fontRect.x = x;
	fontRect.y = y;
	SDL_BlitSurface(fontSurface, NULL, screen, &fontRect);
	int SDL_Flip(SDL_Surface* screen);
}

	int main(int argc, char* args[])
	{
		fontInit();
		printF("Hello", 512, 384);


		//The window we'll be rendering to
		SDL_Window* window = NULL;

		//The surface contained by the window
		SDL_Surface* screenSurface = NULL;

		//Initialize SDL
		if (SDL_Init(SDL_INIT_VIDEO) < 0)
		{
			printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
		}
		else
		{
			//Create window
			window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, SDL_WINDOW_SHOWN);
			if (window == NULL)
			{
				printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
			}
			else
			{
				//Get window surface
				screenSurface = SDL_GetWindowSurface(window);

				//Fill the surface white
				SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00));

				//Update the surface
				SDL_UpdateWindowSurface(window);

				//Wait two seconds
				SDL_Delay(10000);
			}
		}

		//Destroy window
		SDL_DestroyWindow(window);

		//Quit SDL subsystems
		SDL_Quit();

		return 0;
	}

I have made some progress. I have got text displayed in a window but not in my main window. here is some code. The code outside the get_text_and_rect function is in the main function.

	SDL_Event event;
	SDL_Rect rect1, rect2;
	SDL_Renderer* renderer;
	SDL_Texture* texture1, * texture2;
	SDL_Window* window;
	const char* font_path;
	int quit;

	if (argc == 1) {
		font_path = "C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\Arial.ttf";
	}
	else if (argc == 2) {
		font_path = argv[1];
	}
	else {
		fprintf(stderr, "error: too many arguments\n");
		exit(EXIT_FAILURE);
	}

	/* Inint TTF. */
	SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
	SDL_CreateWindowAndRenderer(300, 300, 0, &window, &renderer);
	TTF_Init();
	TTF_Font* font = TTF_OpenFont(font_path, 24);
	if (font == NULL) {
		fprintf(stderr, "error: font not found\n");
		exit(EXIT_FAILURE);
	}
	get_text_and_rect(renderer, 0, 0, "hello", font, &texture1, &rect1);
	get_text_and_rect(renderer, 0, rect1.y + rect1.h, "world", font, &texture2, &rect2);

	quit = 0;
	while (!quit) {
		while (SDL_PollEvent(&event) == 1) {
			if (event.type == SDL_QUIT) {
				quit = 1;
			}
		}
		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
		SDL_RenderClear(renderer);

		/* Use TTF textures. */
		SDL_RenderCopy(renderer, texture1, NULL, &rect1);
		SDL_RenderCopy(renderer, texture2, NULL, &rect2);

		SDL_RenderPresent(renderer);
	}

void get_text_and_rect(SDL_Renderer* renderer, int x, int y, const char* text, TTF_Font* font, SDL_Texture** texture, SDL_Rect* rect)
{
	int text_width;
	int text_height;
	SDL_Surface* surface;
	SDL_Color textColor = { 255, 255, 255, 0 };

	surface = TTF_RenderText_Solid(font, text, textColor);
	*texture = SDL_CreateTextureFromSurface(renderer, surface);
	text_width = surface->w;
	text_height = surface->h;
	SDL_FreeSurface(surface);
	rect->x = x;
	rect->y = y;
	rect->w = text_width;
	rect->h = text_height;
}

I finally figured out how to display text to the screen. Thanks for all the help. Here is my codew so far.

			SDL_Rect score_one;
			score_one.x = 800;
			score_one.y = 600;

			int score = 0;
			char buffer[50];
			sprintf_s(buffer, "%d", score);
			SDL_Surface* screen=NULL;
			SDL_Surface* surface=NULL;
			SDL_Color textColor = { 255, 255, 255, 0 };

			surface = TTF_RenderText_Solid(font, buffer, textColor);
			SDL_BlitSurface(surface, NULL, gScreenSurface, &score_one);

This topic is closed to new replies.

Advertisement