找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 44|回复: 0

[Raylib项目设计/] 昨日(2025.4.6)Raylib实际应用源代码

[复制链接]

3

主题

12

回帖

340

积分

蓝锐

积分
340
发表于 2025-4-7 21:46:50 | 显示全部楼层 |阅读模式
本帖最后由 xinhaitianze 于 2025-4-7 21:46 编辑

效果:


演示了图片绘制、中文字体显示、键盘控制

源代码:

  1. #include <raylib.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. typedef struct {
  6.         unsigned char* fontData;
  7.         unsigned int fileSize;
  8.         Font font;
  9.         bool loaded;
  10. } FontResource;

  11. FontResource LoadFontResource(const char* fontPath) {
  12.         FontResource res = { 0 };
  13.         res.fontData = LoadFileData(fontPath, &res.fileSize);
  14.         if (res.fontData) {
  15.                 // 预加载常用ASCII字符
  16.                 const char* preloadChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{};':",./<>?!\\|`~";

  17.                 // 预加载基本汉字区的汉字
  18.                 int codepointCount = 0;
  19.                 int* codepoints = (int*)malloc(21000 * sizeof(int));  // 假设21000个汉字
  20.                 for (int i = 0x4E00; i <= 0x9FFF; i++) {
  21.                         codepoints[codepointCount++] = i;
  22.                 }

  23.                 // 合并预加载的字符和汉字
  24.                 int totalCodepointCount = codepointCount + strlen(preloadChars);
  25.                 int* totalCodepoints = (int*)malloc(totalCodepointCount * sizeof(int));
  26.                 for (int i = 0; i < codepointCount; i++) {
  27.                         totalCodepoints[i] = codepoints[i];
  28.                 }
  29.                 for (int i = 0; i < strlen(preloadChars); i++) {
  30.                         totalCodepoints[codepointCount + i] = preloadChars[i];
  31.                 }

  32.                 res.font = LoadFontFromMemory(".ttf", res.fontData, res.fileSize, 32, totalCodepoints, totalCodepointCount);
  33.                 free(codepoints);
  34.                 free(totalCodepoints);
  35.                 res.loaded = (res.font.texture.id != 0);
  36.         }
  37.         return res;
  38. }

  39. int main(void) {
  40.     // 初始化窗口
  41.     InitWindow(1300, 800, "mián");
  42.     SetWindowOpacity(0.0);

  43.     SetTargetFPS(60);

  44.     // 隐藏光标
  45.     HideCursor();

  46.     // 加载基础字体
  47.     FontResource fontRes = LoadFontResource("font/font.ttf");

  48.     int choices = 0;  // 默认选择第一个选项
  49.     Image start = LoadImage("./image/start/start.jpg");
  50.     Texture start_background = LoadTextureFromImage(start);

  51.     float opacity = 0.0f;  // 初始透明度为 0.0(完全透明)
  52.     float opacityIncrement = 0.02f;  // 每帧增加的透明度值
  53.     while (1) {
  54.         BeginDrawing();

  55.         // 逐步增加透明度
  56.         if (opacity < 1.0f) {
  57.             opacity += opacityIncrement;
  58.         }

  59.         // 设置窗口透明度
  60.         SetWindowOpacity(opacity);

  61.         ClearBackground(BLACK);
  62.         DrawTextureEx(start_background, (Vector2) { -20,0 }, 0, 2, WHITE);
  63.         //DrawTexture(start_background, 0, 0, WHITE);
  64.         // 绘制文本
  65.         DrawTextEx(fontRes.font, "SLEEP", (Vector2) { 515, 50 }, 100, 1, WHITE);
  66.         DrawTextEx(fontRes.font, "===睡觉===", (Vector2) { 495, 140 }, 55, 1, WHITE);
  67.         DrawTextEx(fontRes.font, "醒来", (Vector2) { 500, 290 }, 45, 1, (Color) { 150, 200, 100, 255 });
  68.         DrawTextEx(fontRes.font, "睡去", (Vector2) { 500, 390 }, 45, 1, (Color) { 150, 200, 100, 200 });
  69.         DrawLineV((Vector2) { 10, 775 }, (Vector2) { 1290, 775 }, WHITE);
  70.         DrawTextEx(fontRes.font, "Copyright xinhaitianze", (Vector2) { 15, 778 },20, 1, WHITE);
  71.         DrawTextEx(fontRes.font, "运行平台: Window", (Vector2) { 1000, 778 }, 20, 1, BLUE);
  72.         DrawTextEx(fontRes.font, "Ver: 1.0", (Vector2) { 1190, 778 }, 20, 1, WHITE);

  73.         if (choices == 0) {
  74.             DrawTextEx(fontRes.font, "<", (Vector2) { 750, 290 }, 45, 1, (Color) { 150, 200, 100, 255 });
  75.         }
  76.         else if (choices == 1) {
  77.             DrawTextEx(fontRes.font, "<", (Vector2) { 750, 390 }, 45, 1, (Color) { 150, 200, 100, 255 });
  78.         }

  79.         EndDrawing();
  80.         // 处理键盘输入
  81.         if (IsKeyPressed(KEY_UP)) {
  82.             choices = 0;  // 选择第一个选项
  83.             printf("KEY_UP\n");
  84.         }
  85.         else if (IsKeyPressed(KEY_DOWN)) {
  86.             choices = 1;  // 选择第二个选项
  87.             printf("KEY_DOWN\n");
  88.         }
  89.         if (IsKeyPressed(KEY_ENTER) && choices == 0) {
  90.             printf("KEY_ENTER pressed, choices is 0\n");
  91.             UnloadTexture(start_background);
  92.             break;
  93.         }
  94.         else if (IsKeyPressed(KEY_ENTER) && choices == 1) {
  95.             printf("KEY_ENTER pressed, choices is 1\n");
  96.             // 清理资源
  97.             UnloadFont(fontRes.font);
  98.             UnloadFileData(fontRes.fontData);
  99.             CloseWindow();
  100.             exit(0);
  101.         }
  102.     }
  103.    
  104.     while (1) {
  105.         BeginDrawing();
  106.         ClearBackground(BLACK);
  107.         DrawTextEx(fontRes.font, "HELLO Game start 关闭黑色命令行关闭此窗口", (Vector2) { 0, 0 }, 30, -1, WHITE);
  108.         EndDrawing();
  109.     }

  110.     return 0;
  111. }

复制代码

Make Programming Great Again

Archiver|手机版|小黑屋|CZLJ

GMT+8, 2025-5-24 15:55 , Processed in 0.052795 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表