#include <SDL2/SDL.h>
#include <stdio.h>
#include <SDL2/SDL_ttf.h>
int W, H;
const int b = 1000/60;
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *Fonts;
SDL_Surface *title_surface;
SDL_Texture *texture_title;
SDL_Rect rect, rect_title;
SDL_Color color_title ={0x1f, 0xff, 0x1f, 0xff};
char str[lbk]32[rbk];
int a=0,time1,time2;
char touch(SDL_Event e)
{
if(SDL_WaitEvent(&e))
if (e.type == SDL_FINGERDOWN)
{
return true;
}
return false;
}
int Init()// 初始化
{
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
window = SDL_CreateWindow("window", 0,0,
W,H, 0);
// 创建window
SDL_GetWindowSize(window, &W, &H);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 0x1f, 0xff, 0xff, 0xff);
SDL_RenderClear(renderer);
}
void menu()//显示
{
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
rect ={W/5.5, H/11, 675, 300};
}
SDL_Texture *Text(int fontsize, SDL_Color color)
{
snprintf(str, sizeof(str), "%d", a);// 输出变量
Fonts = TTF_OpenFont("./resources/05.ttf", fontsize);// 字体
title_surface = TTF_RenderText_Blended(Fonts, str, color);// 文字
rect_title ={(W - title_surface->w) / 2, H/9};// 文本矩形位置
texture_title = SDL_CreateTextureFromSurface(renderer, title_surface);
SDL_QueryTexture(texture_title, NULL, NULL, &rect_title.w, &rect_title.h);
SDL_RenderCopy(renderer, texture_title, NULL, &rect_title);
SDL_FreeSurface(title_surface);
SDL_DestroyTexture(texture_title);
}
int Destory()
{
SDL_DestroyWindow(window);// 销毁
SDL_DestroyRenderer(renderer);
TTF_CloseFont(Fonts);
SDL_FreeSurface(title_surface);
SDL_DestroyTexture(texture_title);
}
int main(int argc, char *args[lbk][rbk])
{
SDL_Event e;
Init();
menu();
while (true)
{
if (touch(e))
a++;
time1=SDL_GetTicks();
SDL_RenderFillRect(renderer, &rect);
Text(100, color_title);
SDL_RenderPresent(renderer);
time2=SDL_GetTicks();
if(time2-time1<b)
SDL_Delay(b-(time2-time1));
}
Destory();
return 0;
}
#include <stdio.h>
#include <SDL2/SDL_ttf.h>
int W, H;
const int b = 1000/60;
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *Fonts;
SDL_Surface *title_surface;
SDL_Texture *texture_title;
SDL_Rect rect, rect_title;
SDL_Color color_title ={0x1f, 0xff, 0x1f, 0xff};
char str[lbk]32[rbk];
int a=0,time1,time2;
char touch(SDL_Event e)
{
if(SDL_WaitEvent(&e))
if (e.type == SDL_FINGERDOWN)
{
return true;
}
return false;
}
int Init()// 初始化
{
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
window = SDL_CreateWindow("window", 0,0,
W,H, 0);
// 创建window
SDL_GetWindowSize(window, &W, &H);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 0x1f, 0xff, 0xff, 0xff);
SDL_RenderClear(renderer);
}
void menu()//显示
{
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
rect ={W/5.5, H/11, 675, 300};
}
SDL_Texture *Text(int fontsize, SDL_Color color)
{
snprintf(str, sizeof(str), "%d", a);// 输出变量
Fonts = TTF_OpenFont("./resources/05.ttf", fontsize);// 字体
title_surface = TTF_RenderText_Blended(Fonts, str, color);// 文字
rect_title ={(W - title_surface->w) / 2, H/9};// 文本矩形位置
texture_title = SDL_CreateTextureFromSurface(renderer, title_surface);
SDL_QueryTexture(texture_title, NULL, NULL, &rect_title.w, &rect_title.h);
SDL_RenderCopy(renderer, texture_title, NULL, &rect_title);
SDL_FreeSurface(title_surface);
SDL_DestroyTexture(texture_title);
}
int Destory()
{
SDL_DestroyWindow(window);// 销毁
SDL_DestroyRenderer(renderer);
TTF_CloseFont(Fonts);
SDL_FreeSurface(title_surface);
SDL_DestroyTexture(texture_title);
}
int main(int argc, char *args[lbk][rbk])
{
SDL_Event e;
Init();
menu();
while (true)
{
if (touch(e))
a++;
time1=SDL_GetTicks();
SDL_RenderFillRect(renderer, &rect);
Text(100, color_title);
SDL_RenderPresent(renderer);
time2=SDL_GetTicks();
if(time2-time1<b)
SDL_Delay(b-(time2-time1));
}
Destory();
return 0;
}