In C++ SDL2 How Would I Print A Random Number and Change That Number While Running

The question speaks for itself, I’m trying to look through tutorials and such on changing text or scores in SDL2 but it really does not seem to connect. All I want to do in this program is change a number on the screen to another random number and then another and then another basically just print number, refresh, print another different number.

Right now I have some code that will print a single random number and hold it their until you quit, if it completely off that mark that’s fine but I would just like to be pointed in the right direction.

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

int main(int argc, char ** argv) {

//Boolean Value: Has the exit button been pressed (default to false so the loop runs at least once)
bool exit_window_input = false;
//Creates an event, this will be used to check if a button has been pressed
SDL_Event event;

//create random number and convert it to char
char str[128];
int number = rand() % 10;
sprintf_s(str, "%8d", number);

//Initializing SDL
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();

//Initialize a window and give it a name, where it pops up on screenm and dimensions
SDL_Window * window = SDL_CreateWindow("Sorting Algorithms Visualized", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);

//This is used to create the text, making a font, a color, putting it together with some text, and then sending it to the window
TTF_Font * font = TTF_OpenFont("arial.ttf", 20);
SDL_Color color = { 255, 255, 255 };
SDL_Surface * surface = TTF_RenderText_Solid(font, str, color);
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);

int t_width = 0;
int t_height = 0;
SDL_QueryTexture(texture, NULL, NULL, &t_width, &t_height);
SDL_Rect dstrect = { 0, 0, t_width, t_height };


//Main loop for running
while (!exit_window_input) {

    SDL_WaitEvent(&event);

    switch (event.type) {

        //If the user presses the x (SDL_QUIT) the loop is broken
        case SDL_QUIT:
            exit_window_input = true;
            break;

    }

    //Render what should be on the window
    SDL_RenderCopy(renderer, texture, NULL, &dstrect);
    SDL_RenderPresent(renderer);


}

//Close/Delete/Clear objects
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
TTF_CloseFont(font);
SDL_Quit();

return 0;
}

You basically, every frame, want to create a new SDL_Texture object containing the data you want to render to the screen, in this case a string containing a random number. At the moment, at the startup of your application, you’re creating an SDL_Texture, which is then reused and rendered on the screen.

Creating a new texture every frame is of course a bit performance-heavy, but it’s easier to do that instead of having a texture that’s created at the startup of the application, which is then updated with new information every frame. Since you’re only printing 1 string (at the moment), it’s no harm in doing that.

I have added a code example below, showing how to generate a texture each frame, containing a random number, which is then rendered to the screen.

Summary
#include <SDL.h>
#include <SDL_ttf.h>

#include <string>
#include <time.h>

SDL_Window* pWindow = nullptr;
SDL_Renderer* pRenderer = nullptr;

TTF_Font* pFont = nullptr;

SDL_Event Event;

bool Running = true;

void Update(void)
{
	// Nothing to update at the moment
}

void Render(void)
{
	SDL_RenderClear(pRenderer);



	// std::to_string() is a nice function to generate an std::string from numerical data types
	const std::string	String		= std::to_string(rand() % 10);
	const SDL_Color		TextColor	= {0, 0, 0, 255};
	SDL_Surface*		pSurface	= TTF_RenderText_Solid(pFont, String.c_str(), TextColor);

	if(pSurface)
	{
		SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);

		if(pTexture)
		{
			int Width	= 0;
			int Height	= 0;

			SDL_QueryTexture(pTexture, nullptr, nullptr, &Width, &Height);

			const SDL_Rect SrcQuad = {0, 0, Width, Height};
			const SDL_Rect DstQuad = {2, 2, Width, Height};

			SDL_RenderCopy(pRenderer, pTexture, &SrcQuad, &DstQuad);

			// Must destroy the texture after it has been rendered to avoid memory leak
			SDL_DestroyTexture(pTexture);
			pTexture = nullptr;
		}

		// Must destroy the surface after it has been used to avoid memory leak
		SDL_FreeSurface(pSurface);
		pSurface = nullptr;
	}



	SDL_RenderPresent(pRenderer);
}

int main(int argc, char* argv[])
{
	// Seed the random number generator
	srand((unsigned int)time(nullptr));

	if(!InitializeSDL())	Running = false;
	if(!InitializeTTF())	Running = false;
	if(!CreateWindow())		Running = false;
	if(!CreateRenderer())	Running = false;

	if(!(pFont = CreateFont("arial.ttf", 20)))
		Running = false;

	while(Running)
	{
		while(SDL_PollEvent(&Event))
		{
			switch(Event.type)
			{
				case SDL_QUIT:
				{
					Running = false;

					break;
				}

				default:
					break;
			}
		}

		Update();
		Render();
	}

	if(pFont)
	{
		TTF_CloseFont(pFont);
		pFont = nullptr;
	}

	DestroyRenderer();
	DestroyWindow();
	DeinitializeTTF();
	DeinitializeSDL();

	return 0;
}