我正在学习法语教程,以学习C编程,现在我正在面对制作计时器的练习,该计时器每100毫秒更新一次。由于本教程适用于SDL,并且我正在使用SDL2,因此我将其与Internet上的一些知识相结合。 如果这里有人有时间并且了解一些SDL2 / SDL_TTF,您可以尝试解决此问题吗? 函数nulos()是一种找到初始化的方法。 要恢复,我的两个问题是:窗口在大约2秒后关闭,并且我无法单击关闭选项,第二个是我的文本不显示。
祝你有美好的一天!
int compteur()
{
SDL_Window *pWindow = NULL;
SDL_Renderer *pRenderer = NULL;
SDL_Texture *pTexture = NULL;
SDL_Surface *pSurface = NULL;
SDL_Surface *pTexte = NULL;
SDL_Event event;
TTF_Font *pFont = NULL;
SDL_Color black = {0, 0, 0};
SDL_Color white = {255, 255, 255};
SDL_Rect position = {200, 200, 0, 0};
int bPlay = 1;
int tempsActuel = 0;
int tempsPrecedent = 0;
int compteur = 0;
char temps[20];
temps[0] = '\0';
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
{
fprintf(stderr, "Erreur SDL_Init : %s ", SDL_GetError());
exit(EXIT_FAILURE);
}
TTF_Init();
pWindow = SDL_CreateWindow("COMPTEUR.C", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
pSurface = SDL_GetWindowSurface(pWindow);
if(nulos(pWindow, pRenderer, pSurface) != 0)
exit(EXIT_FAILURE);
pFont = TTF_OpenFont("images/Gabriola.ttf", 40);
if(pFont == NULL)
{
fprintf(stderr, "Error TTF_OpenFont : %s ", TTF_GetError());
exit(EXIT_FAILURE);
}
TTF_SetFontStyle(pFont, TTF_STYLE_ITALIC | TTF_STYLE_UNDERLINE);
tempsActuel = SDL_GetTicks();
sprintf(temps, "Temps : %d", compteur);
pTexte = TTF_RenderText_Shaded(pFont, temps, black, white);
while(bPlay != 0)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
bPlay = 0;
break;
}
SDL_FillRect(pSurface, NULL, SDL_MapRGB(pSurface->format, black.r, black.g, black.b));
SDL_RenderClear(pRenderer);
tempsActuel = SDL_GetTicks();
if(tempsActuel - tempsPrecedent >= 100)
{
compteur += 100;
sprintf(temps, "Temps : %d", compteur);
pTexte = TTF_RenderText_Shaded(pFont, temps, black, white);
tempsPrecedent = tempsActuel;
}
SDL_BlitSurface(pTexte, NULL, pSurface, &position);
SDL_FreeSurface(pTexte);
pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
SDL_RenderCopy(pRenderer, pTexture, NULL, NULL);
SDL_RenderClear(pRenderer);
}
SDL_DestroyRenderer(pRenderer);
SDL_FreeSurface(pSurface);
SDL_DestroyRenderer(pRenderer);
SDL_DestroyWindow(pWindow);
TTF_Quit();
SDL_Quit();
return EXIT_SUCCESS;
}
int nulos(SDL_Window *w, SDL_Renderer *r, SDL_Surface *s)
{
if(w == NULL)
{
fprintf(stderr, "Erreur SDL_CreateWindow : %s ", SDL_GetError());
return -1;
}
else if(r == NULL)
{
fprintf(stderr, "Erreur SDL_CreateRenderer : %s ", SDL_GetError());
return -1;
}
else if(s == NULL)
{
fprintf(stderr, "Erreur SDL_GetWindowSurface : %s ", SDL_GetError());
return -1;
}
return 0;
}
答案 0 :(得分:0)
好的,我已经解决了第一个问题,那只是一个粗心的错误。应该是:
SDL_RenderPresent(pRenderer);
代替
SDL_RenderClear(pRenderer);
但是窗口仍然会自行关闭...
答案 1 :(得分:0)
好吧,我做到了!对于任何想知道的人,这里都是“好的”编程。我将SDL_PollEvent并将pTexte移到其他地方:
#include "main.h"
int compteur()
{
SDL_Window *pWindow = NULL;
SDL_Renderer *pRenderer = NULL;
SDL_Texture *pTexture = NULL;
SDL_Surface *pSurface = NULL;
SDL_Surface *pTexte = NULL;
SDL_Event event;
TTF_Font *pFont = NULL;
SDL_Color black = {0, 0, 0};
SDL_Color white = {255, 255, 255};
SDL_Rect position;
SDL_bool bQuit = SDL_FALSE;
int tempsActuel = 0;
int tempsPrecedent = 0;
int compteur = 0;
char temps[20];
temps[0] = '\0';
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "Erreur SDL_Init : %s ", SDL_GetError());
exit(EXIT_FAILURE);
}
TTF_Init();
pWindow = SDL_CreateWindow("COMPTEUR.C", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
SDL_RenderClear(pRenderer);
pSurface = SDL_GetWindowSurface(pWindow);
if(pWindow == NULL || pRenderer == NULL || pSurface == NULL)
{
fprintf(stderr, "Erreur : %s ", SDL_GetError());
exit(EXIT_FAILURE);
}
pFont = TTF_OpenFont("images/Gabriola.ttf", 40);
if(pFont == NULL)
{
fprintf(stderr, "Error TTF_OpenFont : %s ", TTF_GetError());
exit(EXIT_FAILURE);
}
TTF_SetFontStyle(pFont, TTF_STYLE_ITALIC | TTF_STYLE_UNDERLINE);
tempsActuel = SDL_GetTicks();
sprintf(temps, "Temps : %d", compteur);
pTexte = TTF_RenderText_Shaded(pFont, temps, black, white);
if(pTexte == NULL)
{
fprintf(stderr, "Error TTF_RenderText_Shaded : %s ", TTF_GetError());
exit(EXIT_FAILURE);
}
while(!bQuit) //SDL_FALSE == 0
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
bQuit = SDL_TRUE;
break;
}
}
tempsActuel = SDL_GetTicks();
if(tempsActuel - tempsPrecedent >= 100)
{
compteur += 100;
sprintf(temps, "Temps : %d", compteur);
tempsPrecedent = tempsActuel;
}
position.x = 180;
position.y = 210;
pTexte = TTF_RenderText_Shaded(pFont, temps, white, black);
SDL_FillRect(pSurface, NULL, SDL_MapRGB(pSurface->format, black.r, black.g, black.b));
SDL_BlitSurface(pTexte, NULL, pSurface, &position);
SDL_FreeSurface(pTexte);
pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
if(pTexture == NULL)
{
fprintf(stderr, "Error SDL_CreateTextureFromSurface : %s ", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_RenderCopy(pRenderer, pTexture, NULL, NULL);
SDL_RenderPresent(pRenderer);
}
SDL_Delay(20);
SDL_FreeSurface(pSurface);
if(pTexture != NULL)
SDL_DestroyTexture(pTexture);
if(pRenderer != NULL)
SDL_DestroyRenderer(pRenderer);
if(pWindow != NULL)
SDL_DestroyWindow(pWindow);
TTF_Quit();
SDL_Quit();
return EXIT_SUCCESS;
}