raylib

1.first.c

  1. 创建窗口
  2. 循环绘制窗口
  3. 窗口上绘制文本
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
#include "raylib.h"
#pragma comment(lib, "raylib.lib")
#pragma comment(lib, "winmm.lib")


int main() {
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "VS2022 Raylib Demo");
SetTargetFPS(60);

while (!WindowShouldClose()) {
// <----- UPDATE ----->
// <----- RENDER ----->
BeginDrawing();
// <--- DRAW --->
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);

EndDrawing();
}

CloseWindow();
return 0;
}

2.支持中文字体(unicode)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
#include "raylib.h"
#pragma comment(lib, "raylib.lib")
#pragma comment(lib, "winmm.lib")


int main() {
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "VS2022 Raylib Demo你好");
//读取字体文件
unsigned int fileSize;
unsigned char* fontFileData = LoadFileData("c:\\windows\\fonts\\simhei.ttf", &fileSize);
//将要输出的内容放到字符串中(必须是utf8编码)
char text[] = "世界,你好!";
// 将字符串中的字符逐一转换成Unicode码点,得到码点表
int codepointsCount;
int* codepoints = LoadCodepoints(text, &codepointsCount);
// 读取仅码点表中各字符的字体
Font font = LoadFontFromMemory(".ttf", fontFileData, fileSize, 32, codepoints, codepointsCount);
// 释放码点表
UnloadCodepoints(codepoints);

SetTargetFPS(60);

while (!WindowShouldClose()) {
// <----- UPDATE ----->
// <----- RENDER ----->
BeginDrawing();
// <--- DRAW --->
ClearBackground(RAYWHITE);
DrawText("Hello, Raylib!乱码", 10, 10, 20, DARKGRAY);
DrawTextEx(font, text, (Vector2) { 50, 50 }, 32, 5, RED);
DrawTextEx(font, text, (Vector2) { 50, 200 }, 40, 2, BLACK);

EndDrawing();
}
//释放字体
UnloadFont(font);
CloseWindow();
return 0;
}

3.Realse下去除控制台

1
2
3
4
#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#endif // DEBIG

4.c与cpp使用raylib的区别

1
2
3
4
5

//c
DrawRectangleGradientEx((Rectangle){ 300, 100, 200, 150 }, BLUE, GREEN, RED, YELLOW);
//cpp
DrawRectangleGradientEx(Rectangle{ 300, 100, 200, 150 }, BLUE, GREEN, RED, YELLOW);

5.raylib旋转动画cpp

  1. 获取程序运行的总秒数
  2. sin 函数的值在 $[-1, 1]$ 之间波动,经过计算后,xValue 会在 $[0, 1]$ 之间像波浪一样平滑地循环。
  3. 视觉效果有旋转(Rotation);颜色呼吸(Color Pulsing);万花筒感:
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

//#define _CRT_SECURE_NO_WARNINGS
//#define RAYGUI_IMPLEMENTATION
//#include "raygui.h" // 引入 GUI 扩展
#include "raylib.h"
#include <cmath> // Include this header to use std::sin
#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")

#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#endif // DEBUG

void TestDraw()
{
// <----- UPDATE ----->
float time = GetTime();
float xValue = std::sin(time) / 2.0f + 0.5f;

Color blue = BLUE;
Color skyblue = SKYBLUE;

blue.g *= xValue;
blue.b *= xValue;

skyblue.g *= xValue;
skyblue.b *= xValue;

// <--- DRAW --->
DrawPoly(Vector2{ 300, 300 }, 7, 120, 45 * xValue, skyblue);
DrawPoly(Vector2{ 300, 300 }, 7, 100, 90 * xValue, blue);
DrawPoly(Vector2{ 300, 300 }, 7, 80, 135 * xValue, skyblue);
DrawPoly(Vector2{ 300, 300 }, 7, 60, 180 * xValue, blue);
DrawPoly(Vector2{ 300, 300 }, 7, 40, 225 * xValue, skyblue);
DrawPoly(Vector2{ 300, 300 }, 7, 20, 270 * xValue, blue);
}

int main() {
const int screenWidth = 800;
const int screenHeight = 450;
// MSAA 4X FLAG
SetConfigFlags(FLAG_MSAA_4X_HINT);

InitWindow(screenWidth, screenHeight, "VS2022 Raylib Demo你好");
SetTargetFPS(60);

while (!WindowShouldClose()) {

// <----- RENDER ----->
BeginDrawing();
ClearBackground(RAYWHITE);
// <--- DRAW --->
DrawText("Hello, Raylib!乱码", 10, 10, 20, DARKGRAY);
TestDraw();

EndDrawing();
}

CloseWindow();
return 0;
}

6.raylib渐变色cpp

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

//#define _CRT_SECURE_NO_WARNINGS
//#define RAYGUI_IMPLEMENTATION
//#include "raygui.h" // 引入 GUI 扩展
#include "raylib.h"
#include <cmath> // Include this header to use std::sin
#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")

#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#endif // DEBIG

void TestDraw()
{
// Rectangle object.
Rectangle rec{ 225, 150, 150, 300 };
// <----- UPDATE ----->
float time = GetTime();
float xValuePos = std::sin(time) / 2.0f + 0.5f;
float yValuePos = std::cos(time) / 2.0f + 0.5f;
// <--- DRAW --->
DrawRectangleGradientEx(
rec,
Fade(RED, xValuePos),
Fade(PINK, yValuePos),
Fade(SKYBLUE, yValuePos),
Fade(ORANGE, xValuePos)
);

}

int main() {
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "VS2022 Raylib Demo你好");
SetTargetFPS(60);

while (!WindowShouldClose()) {

// <----- RENDER ----->
BeginDrawing();
ClearBackground(RAYWHITE);
// <--- DRAW --->
DrawText("Hello, Raylib!乱码", 10, 10, 20, DARKGRAY);
TestDraw();

EndDrawing();
}

CloseWindow();
return 0;
}

raylib与raygui配合

1. 控件与事件

1.鼠标点击控件打印消息

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
#define _CRT_SECURE_NO_WARNINGS
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "raylib.h"
#include <stdio.h> // printf 需要

#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#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")
#endif // DEBIG

int main() {
// Initialization
InitWindow(400, 300, "Raygui Event Demo");
SetTargetFPS(60);

int clickCount = 0;
while (!WindowShouldClose()) {
// --- DRAWING ---
BeginDrawing();
ClearBackground(RAYWHITE);

// If GuiButton returns true, it means it was clicked in THIS frame
if (GuiButton((Rectangle) { 40, 100, 120, 40 }, "CLICK ME"))
{
clickCount++;
// Print message to console
printf("Button clicked! Total: %d\n", clickCount);
}

EndDrawing();
}

CloseWindow();
return 0;
}

2.点击键盘printf打印消息

  1. 检测空格键
  2. 检测任意键
  3. 点击任意键盘打印输出
  4. 点击space键后打印次数并在窗口中计数
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
#define _CRT_SECURE_NO_WARNINGS
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "raylib.h"
#include <stdio.h> // printf 需要

#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#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")
#endif // DEBIG

int main() {
InitWindow(400, 300, "Keyboard & GUI Event Demo");
SetTargetFPS(60);

int count = 0;

while (!WindowShouldClose()) {

// --- 1. KEYBOARD INPUT ---
// 检测空格键按下 (只在按下的那一帧返回 true)
if (IsKeyPressed(KEY_SPACE)) {
count++;
printf("KEY [SPACE] pressed! Total: %d\n", count);
}

// 检测任意按键按下并获取键值
int key = GetKeyPressed();
if (key > 0) {
printf("Detected KeyCode: %d\n", key);
}

// --- 2. DRAWING ---
BeginDrawing();
ClearBackground(RAYWHITE);

// Visual hints
DrawText("Press SPACE on keyboard", 40, 160, 20, DARKGRAY);
DrawText(TextFormat("Total Actions: %d", count), 40, 200, 20, BLUE);

EndDrawing();
}

CloseWindow();
return 0;
}

3.点击窗口printf打印消息

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
#define _CRT_SECURE_NO_WARNINGS
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "raylib.h"
#include <stdio.h>

#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#pragma comment(lib, "raylib.lib")
#pragma comment(lib, "winmm.lib")
#endif

int main() {
InitWindow(400, 300, "Mouse Click Demo");
SetTargetFPS(60);

// 定义按钮区域,方便后续做点击判定
Rectangle btnRec = { 40, 100, 120, 40 };

while (!WindowShouldClose()) {

// --- 1. MOUSE INPUT LOGIC ---
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
Vector2 mousePos = GetMousePosition();

// 逻辑:如果鼠标点击的位置不在按钮范围内,才打印窗口点击消息
if (!CheckCollisionPointRec(mousePos, btnRec)) {
printf("Window clicked at: (%.0f, %.0f)\n", mousePos.x, mousePos.y);
}
}

// --- 2. DRAWING ---
BeginDrawing();
ClearBackground(RAYWHITE);

// Raygui Button (点击时会返回 true)
if (GuiButton(btnRec, "GUI BUTTON"))
{
printf("GUI Button clicked!\n");
}

DrawText("Click anywhere in the window", 40, 160, 20, DARKGRAY);

EndDrawing();
}

CloseWindow();
return 0;
}

4.鼠标点击控件打印消息与改变图形大小

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
#define _CRT_SECURE_NO_WARNINGS
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "raylib.h"
#include <stdio.h> // printf 需要



#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#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")
#endif // DEBIG

int main() {
// 1. Initialization
InitWindow(400, 300, "Raygui Event Demo");
SetTargetFPS(60);

// 2. Control Variables
int clickCount = 0;

while (!WindowShouldClose()) {
// --- DRAWING ---
BeginDrawing();
ClearBackground(RAYWHITE);

// A. Raylib Drawing (Non-interactive)
DrawCircle(300, 150, 40 + (clickCount * 5), MAROON);
DrawText("Raylib is drawing this circle", 200, 210, 10, GRAY);

// B. Raygui Interaction
// If GuiButton returns true, it means it was clicked in THIS frame
if (GuiButton((Rectangle) { 40, 100, 120, 40 }, "CLICK ME")) {
clickCount++;

// Print message to console
printf("Button clicked! Total: %d\n", clickCount);
}

if (GuiButton((Rectangle) { 40, 160, 120, 40 }, "EXIT")) {
break; // Manually break the loop
}

EndDrawing();
}

CloseWindow();
return 0;
}

5.鼠标右键改变窗口文本颜色

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
#define _CRT_SECURE_NO_WARNINGS  
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "raylib.h"
#include <stdio.h> // printf 需要

#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#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")
#endif // DEBIG




int main() {
// Initialization
InitWindow(400, 300, "Raygui Event Demo");
SetTargetFPS(60);
Color baseColor = BLACK;
while (!WindowShouldClose()) {
// --- DRAWING ---
BeginDrawing();
ClearBackground(RAYWHITE);

// 1. 简单的右键点击(像左键一样打印)
if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) {
printf("Right Click! Maybe show a menu here?\n");
}

// 2. 右键长按(改变背景颜色,手放开就变回来)
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) {
ClearBackground(DARKGRAY); // 按住右键背景变深
}
else {
ClearBackground(RAYWHITE); // 否则背景是白色
}

DrawTextEx(GetFontDefault(), "Hold right mouse button to change background color", (Vector2) { 10, 10 }, 10, 1, baseColor);
// 3. 结合之前的 TestDraw,右键点击重置矩形颜色
if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) {
baseColor = BLUE; // 假设 baseColor 是你之前定义的全局变量
}
EndDrawing();
}

CloseWindow();
return 0;
}

6.窗口中右键菜单出现选项

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
#define _CRT_SECURE_NO_WARNINGS
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "raylib.h"
#include <stdio.h> // printf 需要



#ifndef _DEBUG
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
#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")
#endif // DEBIG

int main() {
InitWindow(500, 400, "Fixed Right Click Menu");
SetTargetFPS(60);

bool showMenu = false;
Vector2 menuPos = { 0, 0 };

while (!WindowShouldClose()) {
// --- 1. 逻辑更新 ---

// 右键按下:打开菜单
if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) {
menuPos = GetMousePosition();
showMenu = true;
}

// 左键按下:
// 只有当菜单已经显示,且点击位置不在菜单范围内时,才关闭
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && showMenu) {
Rectangle menuRect = { menuPos.x, menuPos.y, 120, 60 }; // 菜单的大致范围
if (!CheckCollisionPointRec(GetMousePosition(), menuRect)) {
showMenu = false;
}
}

// --- 2. 绘制渲染 ---
BeginDrawing();
ClearBackground(RAYWHITE);

// 状态提示:方便调试
DrawText(showMenu ? "Status: MENU OPEN" : "Status: WAITING FOR RIGHT CLICK", 10, 370, 15, GRAY);
DrawText("Right click anywhere!", 10, 10, 20, DARKGRAY);

// 菜单绘制必须放在最后,确保它在最上层
if (showMenu) {
// 画一个深色的边框背景,确保肯定能看见
DrawRectangle(menuPos.x - 2, menuPos.y - 2, 124, 64, DARKGRAY);

// Raygui 按钮
if (GuiButton((Rectangle) { menuPos.x, menuPos.y, 120, 30 }, "Option A")) {
TraceLog(LOG_INFO, "Option A clicked");
showMenu = false;
}
if (GuiButton((Rectangle) { menuPos.x, menuPos.y + 30, 120, 30 }, "Option B")) {
TraceLog(LOG_INFO, "Option B clicked");
showMenu = false;
}
}
EndDrawing();
}
CloseWindow();
return 0;
}