惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

lzw-723's blog

Lojban教程 - 逻辑语学习中常见专有名词列表 | lzw-723's blog Lojban基础教程 - 字母 | lzw-723's blog 扩充巴科斯范式(ABNF)初识 | lzw-723's blog 【译】5分钟入门Nim编程语言 | lzw-723's blog Cakewalk安装无反应的解决办法 | lzw-723's blog C语言教程 - while循环 | lzw-723's blog C++ FAQ | lzw-723's blog 《Moe Era》 - 时代潮流中不值一提的我 | lzw-723's blog C语言教程 - for循环 | lzw-723's blog
raylib基础学习 | lzw-723's blog
2022-03-06 · via lzw-723's blog

raylib 是一个跨平台、易用的图形库,围绕OpenGL 1.1、2.1、3.3和OpenGL ES 2.0构建。

尽管它是用C语言编写的,却有超过50种不同语言的绑定。本教程将使用C语言。
更确切地说,是C99。

#include <raylib.h>

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

    // 在初始化raylib之前,可以设置配置标志
    SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);

    // raylib并不要求我们存储任何实例结构
    // 目前raylib一次只能处理一个窗口
    InitWindow(screenWidth, screenHeight, "MyWindow");

    // 设置我们的游戏以每秒60帧的速度运行
    SetTargetFPS(60);

    // 设置一个关闭窗口的键。
    //可以是0,表示没有键
    SetExitKey(KEY_DELETE);

    // raylib定义了两种类型的相机。Camera3D和Camera2D
    // Camera是Camera3D的一个类型化定义
    Camera camera = {
            .position = {0.0f, 0.0f, 0.0f},
            .target   = {0.0f, 0.0f, 1.0f},
            .up       = {0.0f, 1.0f, 0.0f},
            .fovy     = 70.0f,
            .type     = CAMERA_PERSPECTIVE
    };


    // raylib支持加载各种不同的文件格式的模型、动画、图像和声音。
    Model myModel = LoadModel("my_model.obj");
    Font someFont = LoadFont("some_font.ttf");

    // 创建一个100x100的渲染纹理
    RenderTexture renderTexture = LoadRenderTexture(100, 100);

    // WindowShouldClose方法检查用户是否正在关闭窗口。
    // 可能用的是快捷方式、窗口控制或之前设置的关闭窗口键
    while (!WindowShouldClose())
    {

        // BeginDrawing方法要在任何绘图操作之前被调用。
        BeginDrawing();
        {

            // 为背景设定某种颜色
            ClearBackground(BLACK);

            if (IsKeyDown(KEY_SPACE))
                DrawCircle(400, 400, 30, GREEN);

            // 简单地绘制文本
            DrawText("Congrats! You created your first window!",
                     190, // x
                     200, // y
                     20,  // 字体大小
                     LIGHTGRAY
            );

            // 大多数函数都有几个版本
            // 通常后缀为Ex, Pro, V
            // 或者是Rec、Wires(仅适用于3D)、Lines(仅适用于2D)。
            DrawTextEx(someFont,
                       "Text in another font",
                       (Vector2) {10, 10},
                       20, // 字体大小
                       2,  // 间距
                       LIGHTGRAY);

            // 绘制3D时需要,有2D的等价方法
            BeginMode3D(camera);
            {

                DrawCube((Vector3) {0.0f, 0.0f, 3.0f},
                         1.0f, 1.0f, 1.0f, RED);

                // 绘图时的白色色调将保持原来的颜色
                DrawModel(myModel, (Vector3) {0.0f, 0.0f, 3.0f},
                          1.0f, // 缩放
                          WHITE);

            }
            // 结束3D模式,这样就可以再次普通绘图
            EndMode3D();

            // 开始在渲染纹理上绘图
            BeginTextureMode(renderTexture);
            {

                // 它的行为与刚才调用的`BeginDrawing()`方法相同

                ClearBackground(RAYWHITE);

                BeginMode3D(camera);
                {

                    DrawGrid(10, // Slices
                             1.0f // 间距
                    );

                }
                EndMode3D();

            }
            EndTextureMode();

            // 渲染有Texture2D字段的纹理
            DrawTexture(renderTexture.texture, 40, 378, BLUE);

        }
        EndDrawing();
    }

    // 卸载已载入的对象
    UnloadFont(someFont);
    UnloadModel(myModel);

    // 关闭窗口和OpenGL上下文
    CloseWindow();

    return 0;
}

延伸阅读

raylib有一些不错的例子
如果你不喜欢C语言你也可以看看raylib的其他语言绑定

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=mlz8qfx2vlil

    • 延伸阅读