c++ SDL(按钮)文本在一段时间后变化(实际上多次执行)

vddsk6oq  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(136)

如前所述,我的按钮文本丢失。它发生在多次渲染迭代(大约1399次)之后。由于我调用了相同的类函数。因此函数不可能是culpurit。我在文本变亮之前和消失之后检查了它的值。我注意到SDL_Surface* buttonTextSurface(数据成员)中的差异,当文本消失时,它指向NULL

之前

之后

#include"include/SDL.h"
#include<iostream>
#include<Windows.h>
#include"include/SDL_ttf.h"
#include<conio.h>
// Create a window
SDL_Window* window = SDL_CreateWindow("Button Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
// Create a renderer for the window
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
TTF_Font* font;
using namespace std;
class Button {
    SDL_Rect Letter_Button;
    char text_for_button;
    bool Button_pushed;
    bool Button_hovered;
    COORD Position;
    int Shadow_offset;
    int Button_Size;
    SDL_Rect buttonShadow;
    SDL_Surface* buttonTextSurface = TTF_RenderText_Solid(font, &text_for_button, { 0, 0, 255 }); //text Color
    SDL_Texture* buttonTextTexture = SDL_CreateTextureFromSurface(renderer, buttonTextSurface);
public:
    Button() :
        Button_pushed(0), Button_hovered(0), Position({ 0,0 }), Shadow_offset(5), Button_Size(0) {}
    Button(short x_axis, short y_axis) :
        Button_pushed(0), Button_hovered(0), Position({ x_axis, y_axis }) {}
    void Set_Button(char Alpahbet, COORD Position, int Button_Size) {
        cout << "enterd\n";
        text_for_button = 'B';
        this->Position = Position;
        this->Button_Size = Button_Size;
        Letter_Button = { Position.X, Position.Y, Button_Size, Button_Size };
        buttonShadow = { Position.X + Shadow_offset, Position.Y + Shadow_offset, Button_Size, Button_Size };
        buttonTextSurface = TTF_RenderText_Solid(font, &text_for_button, { 0, 0, 255 }); //text Color
    }
    void Diplay_Shadow() {
        SDL_SetRenderDrawColor(renderer, 75, 75, 75, 50);
        SDL_RenderFillRect(renderer, &buttonShadow);
    }
    void Display_Button() {
        Diplay_Shadow();
        char test[] = "x";
        buttonTextSurface = TTF_RenderText_Solid(font, &text_for_button, { 0, 0, 255 }); //text Color
        buttonTextTexture = SDL_CreateTextureFromSurface(renderer, buttonTextSurface);
        SDL_SetRenderDrawColor(renderer, 255, 65, 65, 50);
        SDL_RenderFillRect(renderer, &Letter_Button);
        SDL_RenderCopy(renderer, buttonTextTexture, NULL, &Letter_Button);
        //if(Button_pushed)
            //change color
        //if(Button_hover)
            //change color
        //SDL_RenderPresent(renderer);
    }
};
Button Normal_Letters[26];

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();
    font = TTF_OpenFont("arial.ttf", 1000);//16  //max : 7332
    // Load a font to use for the button text
    // Create a surface for the button text
    SDL_Surface* buttonTextSurface = TTF_RenderText_Solid(font, "A", { 0, 0, 255 }); //text Color
    int x;
    // Create a texture from the surface
    SDL_Texture* buttonTextTexture = SDL_CreateTextureFromSurface(renderer, buttonTextSurface);
    // Create a rectangle for the button position and dimensions
    SDL_Rect buttonRect{ 10, 10, 100, 100 };
    int Shadow_offset = 5;
    SDL_Rect button_Shadow{ 10 + Shadow_offset, 10 + Shadow_offset, 100, 100 };
    bool buttonHovered = false, Button_Pushed = 0;
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
    //cout << test << "\n";
    while (true) {
        SDL_Event event;
        if (_kbhit()) {
            x = _getch() - '0';
            cout << "df\n";
        }
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE) {
                // Close the window and quit the game
                SDL_DestroyTexture(buttonTextTexture);
                SDL_FreeSurface(buttonTextSurface);
                TTF_CloseFont(font);
                SDL_DestroyRenderer(renderer);
                SDL_DestroyWindow(window);
                SDL_Quit();
                exit(0);
                return 0;
            }
            if (event.type == SDL_MOUSEMOTION) {
                // Check if the mouse is hovering over the button rectangle
                int x, y;
                SDL_GetMouseState(&x, &y);
                buttonHovered = (x >= buttonRect.x && x < buttonRect.x + buttonRect.w && y >= buttonRect.y && y < buttonRect.y + buttonRect.h);
            }
            if (event.type == SDL_MOUSEBUTTONUP) {
                // Check if the mouse click was within the button rectangle
                int x, y;
                SDL_GetMouseState(&x, &y);
                if (x >= buttonRect.x && x < buttonRect.x + buttonRect.w && y >= buttonRect.y && y < buttonRect.y + buttonRect.h) {
                    // The mouse click was within the button, so do something
                    std::cout << "Button clicked!\n";
                    Button_Pushed = !Button_Pushed;
                    Shadow_offset = Shadow_offset * -1;
                    if (Button_Pushed) {
                        buttonRect = { 15, 15, 95, 95 };
                        button_Shadow.x = 10;
                        button_Shadow.y = 10;
                    }
                    else
                    {
                        buttonRect = { 10, 10, 100, 100 };
                        button_Shadow.x = 10 + Shadow_offset;
                        button_Shadow.y = 10 + Shadow_offset;
                    }
                }
            }
        }
        // Render the button
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        SDL_RenderClear(renderer);
        //for (size_t i = 0; i < 26; i++)
        //  Normal_Letters[i].text_for_button = char(i + 65);
        Normal_Letters[1].Set_Button('B', { 200, 200 }, 100);
        Normal_Letters[1].Display_Button();
        if (_kbhit())
            x = _getch() - '0';
        if (Button_Pushed || 1)//if not Pressed then show shadow
        {
            SDL_SetRenderDrawColor(renderer, 75, 75, 75, 200);
            SDL_RenderFillRect(renderer, &button_Shadow);
        }
        if (buttonHovered) {
            // Use cyan and yellow for the button color when it is hovered
            SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
            SDL_RenderFillRect(renderer, &buttonRect);
            //SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
            //SDL_RenderDrawRect(renderer, &buttonRect);
        }
        else {
            // Use white for the button color when it is not hovered
            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        }
        SDL_RenderFillRect(renderer, &buttonRect);
        SDL_RenderCopy(renderer, buttonTextTexture, NULL, &buttonRect);
        SDL_RenderPresent(renderer);
    }
}
6tdlim6h

6tdlim6h1#

找到问题了!不需要重新初始化表面和纹理。

void Set_Button(char Alphabet, COORD Position, int Button_Size) {
    cout << "enterd\n";
    text_for_button = Alphabet;
    this->Position = Position;
    this->Button_Size = Button_Size;
    Letter_Button = { Position.X, Position.Y, Button_Size, Button_Size };
    buttonShadow = { Position.X + Shadow_offset, Position.Y + Shadow_offset, Button_Size, Button_Size };
    buttonTextSurface = TTF_RenderText_Solid(font, &text_for_button, { 0, 0, 255 }); //text Color
    buttonTextTexture1 = SDL_CreateTextureFromSurface(renderer, buttonTextSurface);
}
void Display_Button() {
    Diplay_Shadow();
    SDL_SetRenderDrawColor(renderer, 255, 65, 65, 250);
    SDL_RenderFillRect(renderer, &Letter_Button);
    SDL_RenderCopy(renderer, buttonTextTexture1, NULL, &Letter_Button);
    //if(Button_pushed)         //change color
    //if(Button_hover)          //change color
}

相关问题