gpt4 book ai didi

c++ - 将鼠标悬停在 SDL2 上时更改文本颜色

转载 作者:行者123 更新时间:2023-11-30 05:43:22 25 4
gpt4 key购买 nike

我试图在悬停时更改文本的颜色。我不明白为什么它不起作用。

我尝试了很多不同的方法,但似乎无法找到解决我的问题的方法。

也许我需要使用其他东西然后 SDL_FreeSurface

代码如下:

int MainGame::mainMenu(){
TTF_Font *font;
TTF_Init();
font = TTF_OpenFont("Myfont.TTF", 30);

int x, y;
const int NUMMENU = 2;
const char* labels[NUMMENU] = { "Play", "Exit" };
SDL_Surface* menus[NUMMENU];
bool selected[NUMMENU] = { 0, 0 };
SDL_Color color[2] = { { 255, 255, 255 }, { 255, 0, 0 } };

SDL_Window* window = NULL;

window = SDL_CreateWindow(
"Main menu",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1024,
768,
SDL_WINDOW_SHOWN
);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);


menus[0] = TTF_RenderText_Solid(font, labels[0], color[0]);
menus[1] = TTF_RenderText_Solid(font, labels[1], color[0]);

SDL_Texture* Play = SDL_CreateTextureFromSurface(renderer, menus[0]);
SDL_Texture* Exit = SDL_CreateTextureFromSurface(renderer, menus[1]);


SDL_Rect pos[NUMMENU];
pos[0].x = 330;
pos[0].y = 250;
pos[0].w = 200;
pos[0].h = 100;
pos[1].x = 330;
pos[1].y = 380;
pos[1].w = 200;
pos[1].h = 100;

SDL_Event event;


while (1)
{
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, Play, NULL, &pos[0]);
SDL_RenderCopy(renderer, Exit, NULL, &pos[1]);
SDL_RenderPresent(renderer);
t = SDL_GetTicks();

while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
SDL_FreeSurface(menus[0]);
SDL_FreeSurface(menus[1]);
return 1;
case SDL_MOUSEMOTION:
x = event.motion.x;
y = event.motion.y;

for (int i = 0; i < NUMMENU; i += 1) {
if (x >= pos[i].x && x <= pos[i].x + pos[i].w && y >= pos[i].y && y <= pos[i].y + pos[i].h)
{
if (!selected[i]) // this part
{
selected[i] = 1;
SDL_FreeSurface(menus[i]);
menus[i] = TTF_RenderText_Solid(font, labels[i], color[1]);
}
}
else
{
if (selected[i])
{
selected[i] = 0;
SDL_FreeSurface(menus[i]);
menus[i] = TTF_RenderText_Solid(font, labels[i], color[0]);
}
}
}
break;
case SDL_MOUSEBUTTONDOWN:
x = event.button.x;
y = event.button.y;
for (int i = 0; i < NUMMENU; i += 1) {
if (x >= pos[i].x && x <= pos[i].x + pos[i].w && y >= pos[i].y && y <= pos[i].y + pos[i].h)
{
SDL_FreeSurface(menus[0]);
SDL_FreeSurface(menus[1]);
return i;
}
}
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
SDL_FreeSurface(menus[0]);
SDL_FreeSurface(menus[1]);
return 0;
}
}
}
}

SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}

最佳答案

你的问题确实在这里:

if (!selected[i]) // this part
{
selected[i] = 1;
SDL_FreeSurface(menus[i]);
menus[i] = TTF_RenderText_Solid(font, labels[i], color[1]);
}

此处您正在更新 menus[i] SDL_Surface。但是在你的渲染代码中你这样做:

SDL_RenderCopy(renderer, Play, NULL, &pos[0]);
SDL_RenderCopy(renderer, Exit, NULL, &pos[1]);

您永远不会更新PlayExit


你宁愿在你的 SDL_Texture 中犯错误,而只是临时创建和销毁 SDL_Surface :

初始化:

SDL_Textue* menus[NUMMENU];

for (int i = 0; i < NUMMENU; i += 1)
{
SDL_Surface* temp = TTF_RenderText_Solid(font, labels[i], color[i]);
menus[0] = SDL_CreateTextureFromSurface(renderer, temp);
}

更新`SDL_Texture`:

if (!selected[i]) // this part
{
selected[i] = 1;

SDL_Surface* temp = TTF_RenderText_Solid(font, labels[i], color[1]);
menus[i] = SDL_CreateTextureFromSurface(renderer, temp);
SDL_FreeSurface(temp);
}

渲染:

for (int i = 0; i < NUMMENU; i += 1)
SDL_RenderCopy(renderer, menu[i], NULL, &pos[i]);

最后一点建议:创建一个人们无需修改代码即可运行的示例。并使用标准字体。否则,您的font 指针将为null 并且应用程序将segfault

关于c++ - 将鼠标悬停在 SDL2 上时更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30239427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com