|
本帖最后由 xinhaitianze 于 2025-4-7 21:46 编辑
效果:

演示了图片绘制、中文字体显示、键盘控制
源代码:
- #include <raylib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- unsigned char* fontData;
- unsigned int fileSize;
- Font font;
- bool loaded;
- } FontResource;
- FontResource LoadFontResource(const char* fontPath) {
- FontResource res = { 0 };
- res.fontData = LoadFileData(fontPath, &res.fileSize);
- if (res.fontData) {
- // 预加载常用ASCII字符
- const char* preloadChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{};':",./<>?!\\|`~";
- // 预加载基本汉字区的汉字
- int codepointCount = 0;
- int* codepoints = (int*)malloc(21000 * sizeof(int)); // 假设21000个汉字
- for (int i = 0x4E00; i <= 0x9FFF; i++) {
- codepoints[codepointCount++] = i;
- }
- // 合并预加载的字符和汉字
- int totalCodepointCount = codepointCount + strlen(preloadChars);
- int* totalCodepoints = (int*)malloc(totalCodepointCount * sizeof(int));
- for (int i = 0; i < codepointCount; i++) {
- totalCodepoints[i] = codepoints[i];
- }
- for (int i = 0; i < strlen(preloadChars); i++) {
- totalCodepoints[codepointCount + i] = preloadChars[i];
- }
- res.font = LoadFontFromMemory(".ttf", res.fontData, res.fileSize, 32, totalCodepoints, totalCodepointCount);
- free(codepoints);
- free(totalCodepoints);
- res.loaded = (res.font.texture.id != 0);
- }
- return res;
- }
- int main(void) {
- // 初始化窗口
- InitWindow(1300, 800, "mián");
- SetWindowOpacity(0.0);
- SetTargetFPS(60);
- // 隐藏光标
- HideCursor();
- // 加载基础字体
- FontResource fontRes = LoadFontResource("font/font.ttf");
- int choices = 0; // 默认选择第一个选项
- Image start = LoadImage("./image/start/start.jpg");
- Texture start_background = LoadTextureFromImage(start);
- float opacity = 0.0f; // 初始透明度为 0.0(完全透明)
- float opacityIncrement = 0.02f; // 每帧增加的透明度值
- while (1) {
- BeginDrawing();
- // 逐步增加透明度
- if (opacity < 1.0f) {
- opacity += opacityIncrement;
- }
- // 设置窗口透明度
- SetWindowOpacity(opacity);
- ClearBackground(BLACK);
- DrawTextureEx(start_background, (Vector2) { -20,0 }, 0, 2, WHITE);
- //DrawTexture(start_background, 0, 0, WHITE);
- // 绘制文本
- DrawTextEx(fontRes.font, "SLEEP", (Vector2) { 515, 50 }, 100, 1, WHITE);
- DrawTextEx(fontRes.font, "===睡觉===", (Vector2) { 495, 140 }, 55, 1, WHITE);
- DrawTextEx(fontRes.font, "醒来", (Vector2) { 500, 290 }, 45, 1, (Color) { 150, 200, 100, 255 });
- DrawTextEx(fontRes.font, "睡去", (Vector2) { 500, 390 }, 45, 1, (Color) { 150, 200, 100, 200 });
- DrawLineV((Vector2) { 10, 775 }, (Vector2) { 1290, 775 }, WHITE);
- DrawTextEx(fontRes.font, "Copyright xinhaitianze", (Vector2) { 15, 778 },20, 1, WHITE);
- DrawTextEx(fontRes.font, "运行平台: Window", (Vector2) { 1000, 778 }, 20, 1, BLUE);
- DrawTextEx(fontRes.font, "Ver: 1.0", (Vector2) { 1190, 778 }, 20, 1, WHITE);
- if (choices == 0) {
- DrawTextEx(fontRes.font, "<", (Vector2) { 750, 290 }, 45, 1, (Color) { 150, 200, 100, 255 });
- }
- else if (choices == 1) {
- DrawTextEx(fontRes.font, "<", (Vector2) { 750, 390 }, 45, 1, (Color) { 150, 200, 100, 255 });
- }
- EndDrawing();
- // 处理键盘输入
- if (IsKeyPressed(KEY_UP)) {
- choices = 0; // 选择第一个选项
- printf("KEY_UP\n");
- }
- else if (IsKeyPressed(KEY_DOWN)) {
- choices = 1; // 选择第二个选项
- printf("KEY_DOWN\n");
- }
- if (IsKeyPressed(KEY_ENTER) && choices == 0) {
- printf("KEY_ENTER pressed, choices is 0\n");
- UnloadTexture(start_background);
- break;
- }
- else if (IsKeyPressed(KEY_ENTER) && choices == 1) {
- printf("KEY_ENTER pressed, choices is 1\n");
- // 清理资源
- UnloadFont(fontRes.font);
- UnloadFileData(fontRes.fontData);
- CloseWindow();
- exit(0);
- }
- }
-
- while (1) {
- BeginDrawing();
- ClearBackground(BLACK);
- DrawTextEx(fontRes.font, "HELLO Game start 关闭黑色命令行关闭此窗口", (Vector2) { 0, 0 }, 30, -1, WHITE);
- EndDrawing();
- }
- return 0;
- }
复制代码
|
|