Created
November 5, 2018 11:17
-
-
Save blewert/4666dfc1121a9f1f89b9822a3175709f to your computer and use it in GitHub Desktop.
A file to test if SDL_ttf is leaking memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
//-- | |
#include "SDL.h" | |
#include "SDL_ttf.h" | |
//-- | |
#include <windows.h> | |
#include <psapi.h> | |
inline ULONG_PTR WINAPI getMemoryUsage(void) | |
{ | |
//make pmc | |
PROCESS_MEMORY_COUNTERS_EX procMem = { }; | |
//copy process memory info struct into pmc, use an awful cast | |
//to convert to PROCESS_MEMORY_COUNTERS* because this func only | |
//takes PROCESS_MEMORY_COUNTERS, not the _EX struct | |
GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PPROCESS_MEMORY_COUNTERS>(&procMem), sizeof procMem); | |
//and return the amount of bytes used within this process | |
return procMem.PrivateUsage; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
//make stuff look nicer with antialias | |
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); | |
//Call ttf init | |
TTF_Init(); | |
//The colour of the text | |
SDL_Color color = { 255, 255, 255 }; | |
//Open font, make a window & renderer | |
TTF_Font* font = TTF_OpenFont("roboto.ttf", 48); | |
TTF_SetFontHinting(font, TTF_HINTING_LIGHT); | |
//Create a window, renderer & empty event | |
SDL_Window* window = SDL_CreateWindow("a window", 500, 200, 800, 600, SDL_WINDOW_SHOWN); | |
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | |
SDL_Event event; | |
while(true) | |
{ | |
//if they've pressed quit then break out | |
if (SDL_PollEvent(&event) && event.type == SDL_QUIT) | |
break; | |
//counter for this infinite loop | |
static int i = 0; | |
//get memory usage using the awful psapi.h | |
auto mem = getMemoryUsage(); | |
//copy frame number + kb memory usage | |
char str[128]; | |
sprintf_s(str, "#%d, %.2f kb usage", ++i, mem/1024.0f); | |
//Get font size for rect | |
int w, h; | |
TTF_SizeText(font, str, &w, &h); | |
//render the text (blended, make it look nice and smooth), convert it to a texture | |
SDL_Surface* surface = TTF_RenderText_Blended(font, str, color); | |
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); | |
//clear the window | |
SDL_RenderClear(renderer); | |
//copy the create text texture to the renderer and swap buffers | |
SDL_Rect rect = { 800/2 - w/2, 600/2 - h/2, w, h }; | |
SDL_RenderCopy(renderer, texture, NULL, &rect); | |
SDL_RenderPresent(renderer); | |
//free surface and texture -- this should work | |
SDL_FreeSurface(surface); | |
SDL_DestroyTexture(texture); | |
} | |
//close the font, quit ttf | |
TTF_CloseFont(font); | |
TTF_Quit(); | |
//and return | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment