void Init_Window(unsigned int Height, unsigned Width, std::string Title) { Window_Info.Height = Height; Window_Info.Width = Width; Window_Info.Title = Title; Window_Info.Ren = nullptr; Window_Info.Win = nullptr; Window_Info.Sur = nullptr; if (SDL_Init(SDL_INIT_VIDEO) < 0) throw std::runtime_error("SDL Init Failed!!"); #ifdef SDL_EX_TTF if (TTF_Init() < 0) throw std::runtime_error("TTF Init Failed!!"); #endif if (SDL_CreateWindowAndRenderer(Window_Info.Width, Window_Info.Height, SDL_WINDOW_SHOWN, &Window_Info.Win, &Window_Info.Ren) < 0) { std::cout << "F**k!!" << SDL_GetError() << std::endl; } std::cout << "F**k!!" << SDL_GetError() << std::endl; SDL_SetWindowTitle(Window_Info.Win, Title.c_str()); Window_Info.Sur = SDL_GetWindowSurface(Window_Info.Win); SetSDLWindowIcon(Window_Info.Win); if (Window_Info.Sur == nullptr) throw std::runtime_error("Failed to Get Window Surface!!"); }
int main() { _log("\n"); if(SDL_Init(SDL_INIT_VIDEO) < 0){ _log("SDL init error.\n"); return -1; } //Create window gWindow = SDL_CreateWindow( "SDL Tutorial", POSITION_LEFT, POSITION_TOP, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); if( gWindow == NULL ) { printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); return -1; } //set window icon char* bmp_path = "icon.bmp"; SetSDLWindowIcon(gWindow, bmp_path); //Get window surface gScreenSurface = SDL_GetWindowSurface( gWindow ); //设定监听的鼠标焦点位置 //下面所涉及的所有的坐标的计算都是基于坐标的原点在SDL窗口的左上角,而不是显示器屏幕的右上角 prepare_button_rect(); int oper_state = -1; for(;;){ SDL_Event event; double x; SDL_WaitEvent(&event); _log("===event.type=[%d]\n", event.type); switch(event.type) { case SDL_KEYDOWN: //监听按键事件,重绘进度条,刷新屏幕 { printf("=========SDL_KEYDOWN==========="); break; } case SDL_MOUSEBUTTONDOWN: //监听鼠标点击事件,重绘button,刷新屏幕 { x= event.button.x; //若监测到在矩形框的范围内,则高亮显示矩形框,并且刷新屏幕surface if(((x >= fast_rewind.x && x <= (fast_rewind.x + fast_rewind.w)) && (y >= fast_rewind.y && y <= (fast_rewind.y + fast_rewind.h)))){ oper_state = FAST_REWIND; } else if(((x >= fast_forward.x && x <= (fast_forward.x + fast_forward.w)) && (y >= fast_forward.y && y <= (fast_forward.y + fast_forward.h)))){ oper_state = FAST_FORWARD; } else if(((x >= pause.x && x <= (pause.x + pause.w)) && (y >= pause.y && y <= (pause.y + pause.h)))){ oper_state = PAUSE; } else if(((x >= progress_bar.x && x <= (progress_bar.x + progress_bar.w)) && (y >= progress_bar.y && y <= (progress_bar.y + progress_bar.h)))){ oper_state = PROG_BAR; } if(oper_state <= PAUSE){ //重绘button,刷新屏幕 button_highlight_show(oper_state); SDL_Flip(gBarSurface); } else if(oper_state == PROG_BAR){ //计算显示进度,刷新屏幕 show_progress(x); SDL_Flip(gBarSurface); } break; } case SDL_MOUSEMOTION: //监听鼠标移动事件,重绘button,刷新屏幕 { x = event.motion.x; if((event.motion.y >= (SCREEN_HEIGHT - 60) && event.motion.y =< SCREEN_HEIGHT) && (event.motion.x >= 0 && event.motion.x <= SCREEN_WIDTH)) { SDL_Rect bar_rect; bar_rect.x = 0; bar_rect.y = SCREEN_HEIGHT - 60; bar_rect.w = SCREEN_WIDTH; bar_rect.h = 60; SDL_FillRect(gBarSurface, &bar_rect, SDL_MapRGB(screen->format,255,255,0)); //填充颜色 SDL_SetAlpha(gBarSurface, SDL_SRCALPHA|SDL_RLEACCEL, 128);//半透明显示图片 ;128为设置透明度 show_barnner(x); SDL_Flip(gBarSurface); } else { break; } if(event.motion.state == SDL_PRESSED){ //重绘进度条,刷新屏幕 if(((x >= progress_bar.x && x <= (progress_bar.x + progress_bar.w)) && (y >= progress_bar.y && y <= (progress_bar.y + progress_bar.h)))){ oper_state = PROG_BAR; show_progress(x); SDL_Flip(gBarSurface); } break; } else { if(((x >= progress_bar.x && x <= (progress_bar.x + progress_bar.w)) && (y >= progress_bar.y && y <= (progress_bar.y + progress_bar.h)))){ oper_state = PROG_BAR; show_progress(x); show_playtime(x); SDL_Flip(gBarSurface); } break; } break; } case SDL_VIDEORESIZE: //更新窗口大小 { printf("=====SDL_VIDEORESIZE=====\n"); break; } default: { printf("======default=====\n"); break; } } }