В C ++ SDL2 Как распечатать случайное число и изменить его во время работы - PullRequest
0 голосов
/ 26 апреля 2020

Вопрос говорит сам за себя, я пытаюсь просмотреть учебные пособия и тому подобное об изменении текста или оценок в SDL2, но, похоже, он действительно не соединяется. Все, что я хочу сделать в этой программе, это изменить число на экране на другое случайное число, а затем другое, а затем еще одно, просто напечатать число, refre sh, напечатать другое другое число.

Сейчас у меня есть некоторый код, который будет печатать одно случайное число и удерживать его до тех пор, пока вы не выйдете, если он полностью не соответствует этой отметке, это нормально, но я просто хотел бы, чтобы указали в правильном направлении.

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;

}

...