[Solved] Trouble about SDL Image and TTF location

Hi,

I’am going on SDL2 developement and I had some trouble when I load a picture or external ressource like a TTF font.

When I launch the app by Terminal it work like a charm, but when I launch by the Tracker it don"t load the picture.

#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <string.h>

char *Concat(char* chaine1, char* chaine2);

int main(int argc, char ** argv)
{
	int texW=100;
	int texH=100;
	const char *text ="Hello les gens";
	char cwd[255]={0};
	char buffer[PATH_MAX+16];
	char newpath[PATH_MAX+16]="/data/image.png";
	int quit = 0;
	if (getcwd(cwd, sizeof(cwd)) != NULL) {
       printf("Current working dir:%s\n", cwd);
   } else {
       perror("getcwd() error");
       return 1;
   }
   	strcpy(buffer,cwd);
   	Concat(cwd,newpath);
	const char *finalpath=cwd;
	printf("finalpath:%s\n",finalpath);
	
	SDL_Event event;
	SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS);
	TTF_Init();
	IMG_Init(IMG_INIT_PNG);
	SDL_Window * window = SDL_CreateWindow("SDL Displaying Image",
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640,480,0);
		
	SDL_Renderer * renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
	SDL_Surface * surface = IMG_Load(finalpath);
	if(!surface)
	{
		printf("Impossible de charger l'image:%s",SDL_GetError());
	}
	SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);
	SDL_RenderClear(renderer);
	SDL_RenderCopy(renderer, texture, NULL, NULL);
	SDL_Color color = {255,255,255};
	TTF_Font *font_arial=NULL;
	font_arial=TTF_OpenFont("/boot/system/data/fonts/ttfonts/NotoSans-Regular.ttf", 14);
	if(!font_arial)
	{
		fprintf(stdout,"Erreur de chargement de la font\n");
	}
	//On récupèree la taille du texte
	if(TTF_SizeText(font_arial,text,&texW, &texH))
	{
		printf("Error loading text");
	}
	SDL_FreeSurface(surface);
	surface=TTF_RenderText_Solid(font_arial,text,color);
	if(!surface)
	{
		printf("Erreur de rendu Text: %s\n",TTF_GetError());
	}
	//On positionne le texte
	SDL_Rect dstrect = { 10, 400, texW, texH };
	texture = SDL_CreateTextureFromSurface(renderer, surface);
	//SDL_RenderCopy(renderer, texture,NULL,NULL);
	SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
	SDL_RenderCopy(renderer, texture, NULL, &dstrect);
	SDL_RenderPresent(renderer);
	while(!quit)
	{
		SDL_WaitEvent(&event);
		
		switch(event.type)
		{
			case SDL_QUIT:
				quit=1;
				break;
		}
	}
	
	
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);
	TTF_CloseFont(font_arial);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	
	IMG_Quit();
	TTF_Quit();
	SDL_Quit();
	
	return 0;
}

char* Concat(char *chaine1, char *chaine2)
{
int i = 0, k = 0;
for(i=0,k=0;chaine2[k]!='\0';i++)
{
	if(chaine1[i]== '\0'){
		for(; chaine2[k] != '\0' ; i++, k++)
			chaine1[i] = chaine2[k];
	}
}
	return chaine1;
}

screenshot3

Known issue, here is one example on how it has been fixed in other places:

1 Like

That work! thanks a lot :slight_smile:

by adding this:

+#ifdef __HAIKU__
+#include <libgen.h>
+#include <unistd.h>
+#endif
+
int main (int argc, char *argv[])
 {
+#ifdef __HAIKU__
+	char* binpath = realpath(argv[0], NULL);
+	if (binpath != NULL) {
+		chdir(dirname(binpath));
+		free(binpath);
+	}
+#endif
1 Like