参考
介绍(官网+百度翻译)
raylib is a simple and easy-to-use library to enjoy videogames programming.
raylib是一个简单易用的库,用于欣赏视频游戏编程。
raylib is highly inspired by Borland BGI graphics lib and by XNA framework. Do you want to see all functions available in raylib? Check raylib cheatsheet
raylib受到Borland BGI图形库和XNA框架的高度启发。是否要查看raylib中可用的所有函数?检查raylib备忘单
函数原型
RLAPI void InitWindow(int width, int height, const char *title); // Initialize window and OpenGL context
RLAPI bool WindowShouldClose(void); // Check if KEY_ESCAPE pressed or Close icon pressed
RLAPI void BeginDrawing(void); // Setup canvas (framebuffer) to start drawing
RLAPI void EndDrawing(void); // End canvas drawing and swap buffers (double buffering)
RLAPI void ClearBackground(Color color); // Set background color (framebuffer clear color)
RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
代码
//包含
#include <iostream>
#include <raylib.h>
int main()
{
InitWindow(640,480,"");//创建窗口
while(!WindowShouldClose())//主循环,如果没有会自己结束
{
BeginDrawing();//开始绘图
{//可有可无,只是突出强调
ClearBackground(BLACK);//清空背景
DrawText("Hello World!",0,0,50,RAYWHITE);//绘制文字
}
EndDrawing();//结束绘图
}
return 0;
}
编译
简单的运行脚本
@echo off
echo ----------Input File----------
set /p input=
echo %input%.exe
echo ----------Compling----------
g++ %input%.cpp -o %input%.exe -lraylib -lopengl32 -lgdi32 -lwinmm
echo ----------Running----------
%input%.exe
echo ----------End----------
pause
运行时务必加上运行参数
-lraylib -lopengl32 -lgdi32 -lwinmm
否则会报错
运行
运行日志应该大同小异
INFO: Initializing raylib 4.0
INFO: DISPLAY: Device initialized successfully
INFO: > Display size: 1280 x 1024
INFO: > Screen size: 640 x 480
INFO: > Render size: 640 x 480
INFO: > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 243
INFO: GL: OpenGL device information:
INFO: > Vendor: Intel
INFO: > Renderer: Intel® UHD Graphics 630
INFO: > Version: 3.3.0 - Build 30.0.101.1340
INFO: > GLSL: 3.30 - Build 30.0.101.1340
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
可以来琢磨琢磨
给出了屏幕和窗口尺寸,OpenGL拓展,GL版本貌似是3.3的,然后加载默认着色器
最后的窗口应该长这样
文本原点是左上角,这字体…好方…