I want to write some text on the screen. For this purpose I'm using this code:
TTF_Font* font = TTF_OpenFont("arial.ttf", 15);
SDL_Color text_color = { 255, 255, 255 };
SDL_Surface* text_surface = TTF_RenderText_Blended_Wrapped(font, "inventory", text_color, 100);
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(rend, text_surface);
I have some other stuff below this but that doesn't matter. Point is this works. Now instead of it saying "inventory" all the time I want to have a string to control that. This is what I put before the above code:
char inv_string[] = "";
char items[15][10] = { "air", "cloud", "water", "wood ", "leaves ", "in_cave", "grass ", "dirt ", "stone ", "coal ", "iron ", "gold ", "diamond", "player" };
if (item_count <= 0) strcpy(inv_string, "your inventory is empty\n");
if (item_count > 0) sprintf(inv_string, "> %s %d <", items[inventory->item], inventory->amount);
if (item_count > 1) sprintf(inv_string + strlen(inv_string), " %s %d\n", items[inventory->next->item], inventory->next->amount);
if (item_count > 2) sprintf(inv_string + strlen(inv_string), " %s %d\n", items[inventory->next->next->item], inventory->next->next->amount);
if (item_count > 3) sprintf(inv_string + strlen(inv_string), " %s %d\n", items[inventory->next->next->next->item], inventory->next->next->next->amount);
if (item_count > 4) sprintf(inv_string + strlen(inv_string), " %s %d\n", items[inventory->next->next->next->next->item], inventory->next->next->next->next->amount);
for (unsigned int i = 5 - item_count; i > 0; i--) sprintf(inv_string + strlen(inv_string), " empty\n");
And the string contains exactly what I need. But now even without replacing "inventory"
with inv_string
, I get an access violation error at SDL_CreateTextureFromSurface. Can someone tell me why is this happening and how to fix it?
Thanks in advance and have a nice day!