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;
}