#include<SDL.h>#include<stdio.h>#defineFF_QUIT_EVENT(SDL_USEREVENT +2)// 用户自定义事件#undefmainintmain(int argc,char* argv[]){
SDL_Window *window =NULL;// Declare a pointer
SDL_Renderer *renderer =NULL;SDL_Init(SDL_INIT_VIDEO);// Initialize SDL2// Create an application window with the following settings:
window =SDL_CreateWindow("An SDL2 window",// window title
SDL_WINDOWPOS_UNDEFINED,// initial x position
SDL_WINDOWPOS_UNDEFINED,// initial y position640,// width, in pixels480,// height, in pixels
SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS// flags - see below);// Check that the window was successfully createdif(window ==NULL){// In the case that the window could not be made...printf("Could not create window: %s\n",SDL_GetError());return1;}/* We must call SDL_CreateRenderer in order for draw calls to affect this window. */
renderer =SDL_CreateRenderer(window,-1,0);/* Select the color for drawing. It is set to red here. */SDL_SetRenderDrawColor(renderer,255,0,0,255);/* Clear the entire screen to our selected color. */SDL_RenderClear(renderer);/* Up until now everything was drawn behind the scenes.
This will show the new, red contents of the window. */SDL_RenderPresent(renderer);
SDL_Event event;int b_exit =0;for(;;){SDL_WaitEvent(&event);switch(event.type){case SDL_KEYDOWN:/* 键盘事件 */switch(event.key.keysym.sym){case SDLK_a:printf("key down a\n");break;case SDLK_s:printf("key down s\n");break;case SDLK_d:printf("key down d\n");break;case SDLK_q:printf("key down q and push quit event\n");
SDL_Event event_q;
event_q.type = FF_QUIT_EVENT;SDL_PushEvent(&event_q);break;default:printf("key down 0x%x\n", event.key.keysym.sym);break;}break;case SDL_MOUSEBUTTONDOWN:/* 鼠标按下事件 */if(event.button.button == SDL_BUTTON_LEFT){printf("mouse down left\n");}elseif(event.button.button == SDL_BUTTON_RIGHT){printf("mouse down right\n");}else{printf("mouse down %d\n", event.button.button);}break;case SDL_MOUSEMOTION:/* 鼠标移动事件 */printf("mouse movie (%d,%d)\n", event.button.x, event.button.y);break;case FF_QUIT_EVENT:printf("receive quit event\n");
b_exit =1;break;}if(b_exit)break;}//destory rendererif(renderer)SDL_DestroyRenderer(renderer);// Close and destroy the windowif(window)SDL_DestroyWindow(window);// Clean upSDL_Quit();return0;}
SDL多线程
SDL线程创建
SDL_CreateThread
SDL线程等待
SDL_WaitThead
SDL互斥锁
SDL_CreateMutex/SDL_DestroyMutex
SDL锁定互斥
SDL_LockMutex/SDL_UnlockMutex
SDL条件变量(信号量)
SDL_CreateCond/SDL_DestoryCond
SDL条件变量(信号量)等待/通知
SDL_CondWait/SDL_CondSingal
示例:
#include<SDL.h>#include<stdio.h>
SDL_mutex *s_lock =NULL;
SDL_cond *s_cond =NULL;intthread_work(void*arg){SDL_LockMutex(s_lock);printf(" <============thread_work sleep\n");sleep(10);// 用来测试获取锁printf(" <============thread_work wait\n");// 释放s_lock资源,并等待signal。之所以释放s_lock是让别的线程能够获取到s_lockSDL_CondWait(s_cond, s_lock);//另一个线程(1)发送signal和(2)释放lock后,这个函数退出printf(" <===========thread_work receive signal, continue to do ~_~!!!\n");printf(" <===========thread_work end\n");SDL_UnlockMutex(s_lock);return0;}#undefmainintmain(){
s_lock =SDL_CreateMutex();
s_cond =SDL_CreateCond();
SDL_Thread * t =SDL_CreateThread(thread_work,"thread_work",NULL);if(!t){printf(" %s",SDL_GetError);return-1;}for(int i =0;i<2;i++){sleep(2);printf("main execute =====>\n");}printf("main SDL_LockMutex(s_lock) before ====================>\n");SDL_LockMutex(s_lock);// 获取锁,但是子线程还拿着锁printf("main ready send signal====================>\n");printf("main SDL_CondSignal(s_cond) before ====================>\n");SDL_CondSignal(s_cond);// 发送信号,唤醒等待的线程printf("main SDL_CondSignal(s_cond) after ====================>\n");sleep(10);SDL_UnlockMutex(s_lock);// 释放锁,让其他线程可以拿到锁printf("main SDL_UnlockMutex(s_lock) after ====================>\n");SDL_WaitThread(t,NULL);SDL_DestroyMutex(s_lock);SDL_DestroyCond(s_cond);return0;}