raylib

1.准备环境

1.从github上下载安装raylib库

1. 安装构建依赖

1
2
3
4
sudo apt update
sudo apt install build-essential cmake git \
libasound2-dev libx11-dev libxrandr-dev libxi-dev \
libgl1-mesa-dev libglu1-mesa-dev libxcursor-dev libxinerama-dev

2. 克隆并编译 Raylib

1
2
3
4
5
6
git clone --depth 1 https://github.com/raysan5/raylib.git
cd raylib
mkdir build && cd build
cmake .. -DBUILD_EXAMPLES=OFF
make
sudo make install

这会将库安装到 /usr/local/lib,头文件放入 /usr/local/include

2.将raygui.h下载到项目中

1
2
3
# 回到你的项目目录
mkdir -p include
wget https://raw.githubusercontent.com/raysan5/raygui/master/src/raygui.h -P ./include

2.第一个cmake管理的raylib项目

1.文件结构

1
2
3
4
5
6
7
8
9
10
first
├── bin
│ └── rayuse
├── build
├── build.sh
├── CMakeLists.txt
├── include
│ └── raygui.h
└── src
└── main.c

2.main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// #define _CRT_SECURE_NO_WARNINGS

// #define RAYGUI_IMPLEMENTATION
// #include "raygui.h" // 引入 GUI 扩展

#include "raylib.h"


/*******************************************************************************************
*
* PROJECT: BLOCKS GAME
* LESSON 01: raylib intro
* DESCRIPTION: Introduction to raylib and the basic videogames life cycle
*
* COMPILATION (Windows - MinGW):
* gcc -o $(NAME_PART).exe $(FILE_NAME) -lraylib -lopengl32 -lgdi32 -lwinmm
-Wall -std=c99
*
* COMPILATION (Linux - GCC):
* gcc -o $(NAME_PART).exe $(FILE_NAME) -lraylib -lGL -lm -lpthread -ldl
-lrt -lX11
*
* Example originally created with raylib 2.0, last time updated with
raylib 4.2

* Example licensed under an unmodified zlib/libpng license, which is an
OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2017-2022 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------

// LESSON 01: Window initialization and screens management
typedef enum GameScreen { LOGO, TITLE, GAMEPLAY, ENDING } GameScreen;

// TODO: Define required structs

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main() {
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;

// LESSON 01: Window initialization and screens management
InitWindow(screenWidth, screenHeight, "PROJECT: BLOCKS GAME");

// NOTE: Load resources (textures, fonts, audio) after Window initialization

// Game required variables
GameScreen screen = LOGO; // Current game screen state

int framesCounter = 0; // General pourpose frames counter
int gameResult = -1; // Game result: 0 - Loose, 1 - Win, -1 - Not defined
bool gamePaused = false; // Game paused state toggle

// TODO: Define and Initialize game variables

SetTargetFPS(60); // Set desired framerate (frames per second)
//--------------------------------------------------------------------------------------

// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
switch (screen) {
case LOGO: {
// Update LOGO screen data here!

framesCounter++;

if (framesCounter > 180) {
screen = TITLE; // Change to TITLE screen after 3 seconds
framesCounter = 0;
}

} break;
case TITLE: {
// Update TITLE screen data here!

framesCounter++;

// LESSON 03: Inputs management (keyboard, mouse)
if (IsKeyPressed(KEY_ENTER))
screen = GAMEPLAY;

} break;
case GAMEPLAY: {
// Update GAMEPLAY screen data here!

if (!gamePaused) {
// TODO: Gameplay logic
}

if (IsKeyPressed(KEY_ENTER))
screen = ENDING;

} break;
case ENDING: {
// Update END screen data here!

framesCounter++;

// LESSON 03: Inputs management (keyboard, mouse)
if (IsKeyPressed(KEY_ENTER))
screen = TITLE;

} break;
default:
break;
}
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(RAYWHITE);

switch (screen) {
case LOGO: {
// TODO: Draw LOGO screen here!
DrawText("LOGO SCREEN", 20, 20, 40, LIGHTGRAY);
DrawText("WAIT for 3 SECONDS...", 290, 220, 20, GRAY);

} break;
case TITLE: {
// TODO: Draw TITLE screen here!
DrawRectangle(0, 0, screenWidth, screenHeight, GREEN);
DrawText("TITLE SCREEN", 20, 20, 40, DARKGREEN);
DrawText("PRESS ENTER or TAP to JUMP to GAMEPLAY SCREEN", 120, 220, 20,
DARKGREEN);

} break;
case GAMEPLAY: {
// TODO: Draw GAMEPLAY screen here!
DrawRectangle(0, 0, screenWidth, screenHeight, PURPLE);
DrawText("GAMEPLAY SCREEN", 20, 20, 40, MAROON);
DrawText("PRESS ENTER or TAP to JUMP to ENDING SCREEN", 130, 220, 20,
MAROON);

} break;
case ENDING: {
// TODO: Draw ENDING screen here!
DrawRectangle(0, 0, screenWidth, screenHeight, BLUE);
DrawText("ENDING SCREEN", 20, 20, 40, DARKBLUE);
DrawText("PRESS ENTER or TAP to RETURN to TITLE SCREEN", 120, 220, 20,
DARKBLUE);

} break;
default:
break;
}

EndDrawing();
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------

// NOTE: Unload any loaded resources (texture, fonts, audio)

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}

3.CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cmake_minimum_required(VERSION 3.10)
project(rayuse C)

# 1. 设置可执行文件输出到项目根目录下的 bin 文件夹
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# 2. 寻找 raylib 库
# 如果你是通过 sudo make install 安装的,它能自动找到
find_package(raylib REQUIRED)

# 3. 添加执行程序
add_executable(rayuse src/main.c)

# 4. 指定头文件搜索路径 (针对 raygui.h)
target_include_directories(rayuse PRIVATE ${CMAKE_SOURCE_DIR}/include)

# 5. 链接库
# 在 Debian 下,除了 raylib,通常还需要链接这些系统库
target_link_libraries(rayuse PRIVATE
raylib
m # 数学库
pthread # 线程库
dl # 动态链接库
rt # 实时库
X11 # 窗口系统库
)

4.build.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash

# --- 1. 准备目录 ---
# 建议进行“外部构建”(Out-of-source build),保持源码干净
if [ ! -d "build" ]; then
mkdir build
fi

# --- 2. 配置项目 (生成 Makefile) ---
# -S . 指定源码目录
# -B build 指定构建中间文件目录
echo "正在配置 CMake..."
cmake -S . -B build

# --- 3. 执行编译 ---
# --build 会自动调用底层的 make 命令
echo "正在编译项目..."
cmake --build build

# --- 4. 运行 ---
if [ $? -eq 0 ]; then
echo "---------------------------------------"
echo "编译成功!程序位置:./bin/rayuse"
echo "---------------------------------------"

else
echo "编译失败,请检查 CMake 报错信息。"
exit 1
fi

运行build.sh编译

附录

使用Makefile编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# --- 项目配置 ---
TARGET = rayuse
SRC_DIR = src
INC_DIR = include
BIN_DIR = bin

# 编译器
CC = gcc
# CFLAGS: -O2 开启优化, -Wall 显示所有警告
CFLAGS = -O2 -Wall -std=c99 -D_DEFAULT_SOURCE -I$(INC_DIR)

# 链接库 (桌面环境通常通过 X11 和 OpenGL 直接显示)
LDFLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11

# 搜索源码
SRCS = $(wildcard $(SRC_DIR)/*.c)
OBJS = $(patsubst $(SRC_DIR)/%.c, $(BIN_DIR)/%.o, $(SRCS))

# 默认编译目标
all: $(BIN_DIR)/$(TARGET)

# 链接
$(BIN_DIR)/$(TARGET): $(OBJS)
@mkdir -p $(BIN_DIR)
$(CC) $(OBJS) -o $@ $(LDFLAGS)
@echo "--------------------------------------"
@echo "编译成功!生成路径: $@"
@echo "--------------------------------------"

# 编译中间件
$(BIN_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) -c $< -o $@

# 运行 (直接启动,利用 GPU)
run: all
./$(BIN_DIR)/$(TARGET)

# 清理
clean:
rm -rf $(BIN_DIR)
@echo "已清理 bin 目录"

.PHONY: all clean run