#include <stdio.h>
#include <SDL2/SDL.h>#define FF_QUIT_EVENT (SDL_USEREVENT +2)// 用戶自定義事件intmain(){SDL_Window *window = NULL;// Declare a pointerSDL_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 titleSDL_WINDOWPOS_UNDEFINED,// initial x positionSDL_WINDOWPOS_UNDEFINED,// initial y position640,// width, in pixels480,// height, in pixelsSDL_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;}
效果
4. SDL Thread
1. 函數
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
2. 代碼示例
代碼
#include <stdio.h>
#include <SDL2/SDL.h>
#include <unistd.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); // 用來測試獲取鎖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;}#undef main
intmain(){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;}