Mac: ‘SDL2/SDL_events.h‘ file not found解決方案及demo示例
1 安裝SDL2
brew install sdl2
2 建立專案
mkdir myproject
cd myproject
touch sdl_color.c
touch Makefile
mkdir include
mkdir lib
sdl_color.c的原始碼為:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <SDL2/SDL.h>
//Utility macros
#define CHECK_ERROR(test, message) \
do{ \
if((test)) { \
fprintf(stderr, "%s\n", (message));\
exit(1);\
}\
}while(0)
//Get a random number from 0 to 255
int randInt(int rmin, int rmax){
return rand() % rmax + rmin;
}
//Window dimensions
static const int width = 800;
static const int height = 600;
int main(int argc, char **argv){
//Initialize the random number generator
srand((unsigned int) time(NULL));
//Initialize SDL
CHECK_ERROR(SDL_Init(SDL_INIT_VIDEO) != 0, SDL_GetError());
//Create an SDL window
SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
CHECK_ERROR(window == NULL, SDL_GetError());
//Create a renderer (accelerated and in sync with the display refresh rate)
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
CHECK_ERROR(renderer == NULL, SDL_GetError());
//Initialize renderer color
SDL_SetRenderDrawColor(renderer, 255,0,0,255);
bool running = true;
SDL_Event event;
while(running){
//Process events
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
running = false;
}else if(event.type == SDL_KEYDOWN){
const char *key = SDL_GetKeyName(event.key.keysym.sym);
if(strcmp(key, "C") == 0){
SDL_SetRenderDrawColor(renderer, randInt(0,255), randInt(0,255), randInt(0,255), 255);
}
}
}
//Clear screen
SDL_RenderClear(renderer);
//Draw
//Show what was drawn
SDL_RenderPresent(renderer);
}
//Release resources
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
3 將SDL檔案複製到當前專案中
cp -r /usr/local/Cellar/sdl2/2.0.12_1/include myproject/include
cp -r /usr/local/Cellar/sdl2/2.0.12_1/lib myproject/lib
4 建立Makefile檔案
game:
gcc sdl_color.c -o play -I include -L lib -l SDL2-2.0.0
其中:
-I
(i as in include) tells it additional include directories you want to add
-L
tells it additional library directories you want to add
-l
(lowercase l as in lib) tells it specific library binaries you want to add
5 編譯
make game
6 執行
./play
結果截圖(按’C’隨機改變顏色):
參考部落格
[1] Set up SDL2 on your Mac without Xcode
[2] Install SDL2 on macOS Catalina
[3] Lazy Foo’ Productions
相關文章
- 'libxml/HTMLparser.h' file not found in ASIHTTPRequest 解決方法XMLHTMLHTTP
- mysqld: File './binlog.~rec~' not found 問題解決處理MySql
- Mac終端出現 brew command not found 解決Mac
- Liquid Exception: Included file '_includes/customizer-variables.html' not found in assets/bootstrap/docs/customize.html 解決方案UIExceptionHTMLboot
- MySQL錯誤Incorrect file format解決方案薦MySqlORM
- CentOS 7 命令 lsb_release: command not found 解決方案CentOS
- IMP-00013: only a DBA can import a file exported by another DBA 問題及解決方案ImportExport
- /system/bin/sh: screenrecord: inaccessible or not found 報錯解決方案
- Error 15: File not found for RedhatErrorRedhat
- Java限流及常用解決方案Java
- mac下/usr/local/bin No such file or directory問題解決Mac
- mac php8安裝swoole 'pcre2.h' file not foundMacPHP
- Mac brew 特別慢 解決方案Mac
- 快取穿透詳解及解決方案快取穿透
- No input file specified 出現的原因及解決方法
- This dependency was not found: * !!vue-style-loader!css-loader? 解決方案VueCSS
- 解決 command not found: expressExpress
- AIX dtterm not found的解決AI
- Android技術分享| Android 中部分記憶體洩漏示例及解決方案Android記憶體
- mysql RSA private key file not foundMySql
- file not found WFMLRSVCApp.earAPP
- java.lang.OutOfMemoryError及解決方案JavaError
- 跨域問題及解決方案跨域
- 粘包拆包及解決方案
- Bash漏洞檢測及解決方案
- iOS中常見 Crash 及解決方案iOS
- 常見問題及解決方案
- mac php環境終極解決方案MacPHP
- 智慧社群發展前景及解決方案
- 造成黏包的原因,及解決方案
- 梯度消失和梯度爆炸及解決方案梯度
- WordPress:常見問題及解決方案
- Newline required at end of file but not foundUI
- MySQL server PID file could not be found!MySqlServer
- libcudart.so.8.0: cannot open shared object file解決方案DartObject
- remount of /system failed: Read-only file system原因及解決REMAI
- sed 命令詳解及示例
- 解決Vue中”This dependency was not found”的方法Vue