raylib

1.windows上使用vcpkg下载raylib动态库与静态库

1.raylib动态库

1
vcpkg install raylib:x64-windows

2.raylib静态库

1
vcpkg install raylib:x64-windows-static

3.raygui库

1
vcpkg install raygui:x64-windows-static

4.安装最新版而非稳定版

1
2
3
4
5
6
7
8
9
git -C /c/D/test/CC++/cc++lib/vcpkg/vcpkg pull
# 1. 先卸载旧版本(防止链接冲突)
./vcpkg remove raylib:x64-windows

# 2. 安装最新的开发版 --head 参数会尝试下载最新的 master 分支
./vcpkg install raylib:x64-windows --head

# 3. 重新集成(可选,通常不需要再次执行,但执行一下更稳)
./vcpkg integrate install

5.下载安装git bash环境

1.cmake使用vcpkg管理的raylib库

1.项目结构

1
2
3
4
5
6
7
8
9
10
11
rayuse
│ build.sh
│ CMakeLists.txt

├─bin
│ rayuse.exe

├─build
├─include
└─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
179
180
181
182
//#define _CRT_SECURE_NO_WARNINGS
//#define RAYGUI_IMPLEMENTATION
//#include "raygui.h" // 引入 GUI 扩展

#include "raylib.h"
#pragma comment(lib, "raylib.lib")
#pragma comment(lib, "winmm.lib")
// #pragma comment(lib, "opengl32.lib")
// #pragma comment(lib, "gdi32.lib")
// #pragma comment(lib, "shell32.lib")
// #pragma comment(lib, "user32.lib")

/*******************************************************************************************
*
* 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
27
28
29
30
31
32
33
34
35
36
37
cmake_minimum_required(VERSION 3.15)
project(rayuse C)

# 1. 强制为 Release 模式,彻底摆脱带 'D' 的调试库
set(CMAKE_BUILD_TYPE Release)

# 2. 静态链接 C 运行时库 (CRT) - 关键步骤
if(MSVC)
# 对于 MSVC 编译器
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
else()
# 对于 MinGW / Clang 编译器
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()

set(VCPKG_TARGET_TRIPLET "x64-windows-static")

find_package(raylib CONFIG REQUIRED)
find_package(glfw3 CONFIG REQUIRED)

add_executable(rayuse src/main.c)

# 输出到项目下的 bin 文件夹
set_target_properties(rayuse PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")

target_link_libraries(rayuse PRIVATE
raylib
glfw
opengl32 gdi32 winmm shell32 user32
)

target_include_directories(rayuse PRIVATE "include")

if(WIN32)
target_link_options(rayuse PRIVATE "-mwindows")
endif()

4.build.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
# 请确保此文件保存为 UTF-8 (No BOM) 格式

PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
VCPKG_PATH="C:/D/test/CC++/cc++lib/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake"

rm -rf build
mkdir build
cd build

# 明确指定 Release 模式
cmake .. -G "MinGW Makefiles" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE="$VCPKG_PATH" \
-DVCPKG_TARGET_TRIPLET=x64-windows-static

cmake --build . --config Release

echo "Build complete. Check the 'bin' folder."

使用git bash环境运行build.sh