Xcode调用raylib图形库
操作环境:
-
MacBook Pro (16-inch, 2019)
-
macOS Big Sur 11.3
-
Xcode 12.5 (12E262)
具体操作步骤:
1. raylib图形库的下载与准备
-
从GitHub中下载raylib:raylib-master.zip,其中包含所有必需的文件:源代码、示例、模板、游戏…
-
解压
raylib-master.zip
(如果使用Safari浏览器,将自动解压缩)。 -
在终端中访问
raylib-master/src
目录:cd /.../raylib-master/src
-
使用终端的以下命令编译raylib库:
make PLATFORM=PLATFORM_DESKTOP
如果以上步骤一切正常,
raylib-master/src
目录中应有libraylib.a
文件被创建。
2. 将生成的raylib库添加到Xcode项目中
-
创建一个
Command Line Tool
项目,选择语言为C。 -
在项目中添加所需框架和库:
- Cocoa.framework
- CoreVideo.framework
- IOKit.framework
- libraylib.a(从
raylib-master/src
中直接拖入即可) - OpenGL.framework
-
添加
raylib.h
头文件及libraylib.a
库的搜索路径/.../raylib-master/src
:
3. 测试
-
附上raylib官网的贪吃蛇游戏测试代码(只需将代码复制到
main.c
文件中即可):/******************************************************************************************* * * raylib - sample game: snake * * Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria * * This game has been created using raylib v1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * * Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ #include "raylib.h" #if defined(PLATFORM_WEB) #include <emscripten/emscripten.h> #endif //---------------------------------------------------------------------------------- // Some Defines //---------------------------------------------------------------------------------- #define SNAKE_LENGTH 256 #define SQUARE_SIZE 31 //---------------------------------------------------------------------------------- // Types and Structures Definition //---------------------------------------------------------------------------------- typedef struct Snake { Vector2 position; Vector2 size; Vector2 speed; Color color; } Snake; typedef struct Food { Vector2 position; Vector2 size; bool active; Color color; } Food; //------------------------------------------------------------------------------------ // Global Variables Declaration //------------------------------------------------------------------------------------ static const int screenWidth = 800; static const int screenHeight = 450; static int framesCounter = 0; static bool gameOver = false; static bool pause = false; static Food fruit = { 0 }; static Snake snake[SNAKE_LENGTH] = { 0 }; static Vector2 snakePosition[SNAKE_LENGTH] = { 0 }; static bool allowMove = false; static Vector2 offset = { 0 }; static int counterTail = 0; //------------------------------------------------------------------------------------ // Module Functions Declaration (local) //------------------------------------------------------------------------------------ static void InitGame(void); // Initialize game static void UpdateGame(void); // Update game (one frame) static void DrawGame(void); // Draw game (one frame) static void UnloadGame(void); // Unload game static void UpdateDrawFrame(void); // Update and Draw (one frame) //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ int main(void) { // Initialization (Note windowTitle is unused on Android) //--------------------------------------------------------- InitWindow(screenWidth, screenHeight, "sample game: snake"); InitGame(); #if defined(PLATFORM_WEB) emscripten_set_main_loop