air86323 air86323
关注数: 23 粉丝数: 30 发帖数: 297 关注贴吧数: 44
SDL2计时。请问如何把按键改为触控? #include "SDL2/SDL.h" #include "SDL_image.h" #include "SDL2/SDL_ttf.h" #include <string> #include <sstream> const int W=640; const int H=480; SDL_Window *win=NULL; SDL_Renderer *ren=NULL; SDL_Event event; SDL_Texture *loadimage(std::string file) { SDL_Texture *tex=NULL; tex=IMG_LoadTexture(ren,file.c_str()); return tex; } SDL_Texture *rendertext(std::string message,std::string fontfile,SDL_Color color,int fontsize) { TTF_Init(); TTF_Font *font=NULL; font=TTF_OpenFont(fontfile.c_str(),fontsize); SDL_Surface *surf=TTF_RenderUTF8_Blended(font,message.c_str(),color); SDL_Texture *texture=SDL_CreateTextureFromSurface(ren,surf); SDL_FreeSurface(surf); TTF_CloseFont(font); return texture; } void draw(int x,int y,SDL_Texture *tex,SDL_Renderer *rend) { SDL_Rect pos; pos.x=x;pos.y=y; SDL_QueryTexture(tex,NULL,NULL,&pos.w,&pos.h); SDL_RenderCopy(rend,tex,NULL,&pos); } int main(int argc,char **argv) { char str[1000000]; SDL_Color color{255,0,0}; SDL_Init(SDL_INIT_EVERYTHING); win=SDL_CreateWindow("time text",0,0,W,H,SDL_WINDOW_SHOWN); ren=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC); bool quit=false; Uint32 start=0; bool running=true; SDL_Texture *notice=NULL; SDL_Texture *background=NULL; notice=rendertext("press S for start or puse","/system/fonts/DroidSansFallback.ttf",color,50); background=loadimage("/storage/sdcard0/background.png"); start=0; while (quit==false) { while (SDL_PollEvent(&event)) { if (event.type==SDL_KEYDOWN) { if (event.key.keysym.sym==SDLK_s) { if (running==true) { running==false; start=0; } else { running=true; start=SDL_GetTicks(); } } } else if (event.type==SDL_QUIT) { quit=true; } } draw(0,0,background,ren); draw(0,340,notice,ren); SDL_Texture *second=NULL; if (running==true) { sprintf(str,"%d",SDL_GetTicks()-start); } second=rendertext(str,"/system/fonts/DroidSansFallback.ttf",color,50); draw(0,60,second,ren); SDL_RenderPresent(ren); } return 0; }
1 下一页