#define SDL_MAIN_HANDLED #include #include #include #include #include #include int main() { SDL_Init(SDL_INIT_EVERYTHING); IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); Mix_Init(MIX_INIT_MP3); TTF_Init(); Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048); //创建窗口和与之关联渲染器 SDL_Window* window = SDL_CreateWindow(u8"你好,世界 ", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN); SDL_Renderer* renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED); //加载图像并创建纹理 SDL_Surface* suf_img = IMG_Load("avatar.jpg"); SDL_Texture* tex_img = SDL_CreateTextureFromSurface(renderer,suf_img); TTF_Font* font = TTF_OpenFont("ipix.ttf", 32); SDL_Color color = { 255,255,255,255 }; SDL_Surface* suf_text = TTF_RenderUTF8_Blended(font, u8"你好,世界", color); SDL_Texture* tex_text = SDL_CreateTextureFromSurface(renderer, suf_text); Mix_Music* music = Mix_LoadMUS("music.mp3"); Mix_FadeInMusic(music, -1, 1500); int fps = 60; bool is_quit = false; SDL_Event event; SDL_Point pos_cursor = { 0,0 }; SDL_Rect rect_img,rect_text; Uint64 last_counter = SDL_GetPerformanceCounter(); Uint64 counter_freq = SDL_GetPerformanceFrequency(); rect_img.w = suf_img->w; rect_img.h = suf_img->h; rect_text.w = suf_text->w; rect_text.h = suf_text->h; while (!is_quit) { while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { is_quit = true; } else if (event.type == SDL_MOUSEMOTION) { pos_cursor.x = event.motion.x; pos_cursor.y = event.motion.y; } } Uint64 current_counter = SDL_GetPerformanceCounter(); double delta = (double)(current_counter - last_counter) / counter_freq; last_counter = current_counter; if (delta * 1000 < 1000.0 / 60) SDL_Delay((Uint32)1000.0 / 60 - delta * 1000); //处理数据 rect_img.x = pos_cursor.x; rect_img.y = pos_cursor.y; rect_text.x = pos_cursor.x; rect_text.y = pos_cursor.y; SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer);//清除渲染器 //渲染绘图 SDL_RenderCopy(renderer, tex_img, nullptr, &rect_img);//复制纹理到渲染器 filledCircleRGBA(renderer, pos_cursor.x, pos_cursor.y, 50, 255, 0, 0, 125); SDL_RenderCopy(renderer, tex_text, nullptr, &rect_text); SDL_RenderPresent(renderer);//更新屏幕显示 } return 0; } /**SDL_FreeSurface:释放不再需要的 SDL_Surface。 SDL_DestroyTexture、SDL_DestroyRenderer 和 SDL_DestroyWindow:销毁纹理、渲染器和窗口。**/