SDL学习总结

上传人:xy****7 文档编号:45537052 上传时间:2018-06-17 格式:DOC 页数:58 大小:174KB
返回 下载 相关 举报
SDL学习总结_第1页
第1页 / 共58页
SDL学习总结_第2页
第2页 / 共58页
SDL学习总结_第3页
第3页 / 共58页
SDL学习总结_第4页
第4页 / 共58页
SDL学习总结_第5页
第5页 / 共58页
点击查看更多>>
资源描述

《SDL学习总结》由会员分享,可在线阅读,更多相关《SDL学习总结(58页珍藏版)》请在金锄头文库上搜索。

1、SDL 学习总结 等待事件 调用 SDL_WaitEvent()等待事件 例程: SDL_Event event;SDL_WaitEvent(switch (event.type) case SDL_KEYDOWN:printf(“键 %s 按下了!n“,SDL_GetKeyName(event.key.keysym.sym);break;case SDL_QUIT:exit(0); 事件轮询 事件轮询调用 SDL_PollEvent()函数 提示: 如果只要查看(peek)事件,而不需要从事件队列中移走,你可以用 SDL_PEEKEVENT 作为参数调用 SDL_PeepEvents()。

2、例程: SDL_Event event;while ( SDL_PollEvent(break;case SDL_MOUSEBUTTONDOWN:printf(“鼠标 %d 点击在 (%d,%d)n“,event.button.button, event.button.x, event.button.y);break;case SDL_QUIT:exit(0); 事件状态轮询 除了直接处理事件,每种类型的事件都有一个相应的函数可以用作检查事件的 状态。如果你需要独占使用,应该调用 SDL_EventState() 忽略所有的事件,并 且定期的调用SDL_PumpEvents()更新程序的事件状

3、态。 例程: SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); void CheckMouseHover(void) int mouse_x, mouse_y;SDL_PumpEvents();SDL_GetMouseState(if ( (mouse_x format, 0 , 0 , 255 );Uint32 colorGreen = SDL_MapRGB(screen - format, 0 , 255 , 0 );Uint32 colorRed = SDL_MapRGB(screen - format, 255 , 0 , 0 );Uint32

4、colorBlack = SDL_MapRGB(screen - format, 0 , 0 , 0 );SDL_Event event ;while ( ! quit)if (SDL_PollEvent( Uint16 y = event .motion.y;if (x 0 /* Initialize defaults, Video and Audio subsystems */if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)=-1) printf(“Could not initialize SDL: %s.n“, SDL_GetError();exit(

5、-1);printf(“SDL initialized.n“);printf(“Quitting SDL.n“);/* Shutdown all subsystems */SDL_Quit();printf(“Quitting.n“);exit(0); Compiling the applicationThe program can be compiled by:In linux:gcc -o sdl-config -cflags -libs= = Programming FAQThis differs from the main SDL FAQ, which is about SDL its

6、elf and getting it working, as this is about the actual programming and problems you may come up with.I made a game loop, but it uses 100% CPUAnd so it should! As soon as it finishes one run of the loop itll start again straight away, so its bound to use all the CPU. You usually dont need the loop t

7、o go as fast as it possibly can, which is where the SDL_Delay function comes in. This function basically tells the Operating System that the program is going to go to sleep for a specified amount of milliseconds, so the OS knows it can use the time for other things, or nothing, depending on what you

8、ve got going on.Check out the time-based loop in Time_Examples. Youll notice it doesnt just delay the same time every time, it uses SDL_GetTicks to work out how much time has passed and then decides how much longer to sleep for. This is to try and maintain a constant rate, because the work your prog

9、ram does in the loop could change depending on what your program is doing.I used SDL_INIT_EVENTTHREAD on Win32 and SDL crashes.Well dont ask me why but initializing the event thread leads to SDL to crash on windows. The problem is that when you leave it away on any X11 system it may, depending on th

10、e version, not produce any events. The only real solution is to use conditional compiling. At least there is the WIN32 macro defined.#ifdef WIN32 SDL_Init(SDL_INIT_VIDEO); #else SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTTHREAD); #endifOn the other hand you could hope that the system running your program

11、 has a newer SDL version, which works fine without the SDL_INIT_EVENTTHREAD flag, but can you rely on this? = = SDL_Delay functionSDL_Delay - Waits a specified number of milliseconds before returning.Syntax切换行号显示void SDL_Delay(Uint32 ms);DescriptionThis function waits a specified number of milliseco

12、nds before returning. It waits at least the specified time, but possible longer due to OS scheduling. The delay granularity is at least 10 ms. Some platforms have shorter clock ticks but this is the most common.See alsoSDL_AddTimerRequirementsHeaderSDL.hVersion1.2.13Shared objectlibSDL.soDLLSDL.dll

13、= = SDL_AddTimer functionSDL_AddTimer - Adds a timer which will call a callback after the specified number of milliseconds has elapsed.Syntax切换行号显示SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void* param);Callback切换行号显示/* type definition for the “new“ timer callback funct

14、ion */ typedef Uint32 (*SDL_NewTimerCallback)(Uint32 interval, void* param);DescriptionAdds a callback function to be run after the specified number of milliseconds has elapsed. The callback function is passed the current timer interval and the user supplied parameter from the SDL_AddTimer call and

15、returns the next timer interval. If the returned value from the callback is the same as the one passed in, the timer continues at the same rate. If the returned value from the callback is 0, the timer is cancelled.Another way to cancel a currently-running timer is by calling SDL_RemoveTimer with the timers ID (which was returned from SDL_AddTimer).The timer callback function may run in a different thread than your main program, and so shouldnt call a

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号