raylib

Личный сайт Go-разработчика из Казани

raylib is a cross-platform easy-to-use graphics library, built around OpenGL 1.1, 2.1, 3.3 and OpenGL ES 2.0. Even though it is written in C it has bindings to over 50 different languages. This tutorial will use C, more specifically C99.

1#include <raylib.h> 2 3int main(void) 4{ 5 const int screenWidth = 800; 6 const int screenHeight = 450; 7 8 // Before initialising raylib we can set configuration flags 9 SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT); 10 11 // raylib doesn't require us to store any instance structures 12 // At the moment raylib can handle only one window at a time 13 InitWindow(screenWidth, screenHeight, "MyWindow"); 14 15 // Set our game to run at 60 frames-per-second 16 SetTargetFPS(60); 17 18 // Set a key that closes the window 19 // Could be 0 for no key 20 SetExitKey(KEY_DELETE); 21 22 // raylib defines two types of cameras: Camera3D and Camera2D 23 // Camera is a typedef for Camera3D 24 Camera camera = { 25 .position = {0.0f, 0.0f, 0.0f}, 26 .target = {0.0f, 0.0f, 1.0f}, 27 .up = {0.0f, 1.0f, 0.0f}, 28 .fovy = 70.0f, 29 .projection = CAMERA_PERSPECTIVE 30 }; 31 32 // raylib supports loading of models, animations, images and sounds 33 // from various different file formats 34 Model myModel = LoadModel("my_model.obj"); 35 Font someFont = LoadFont("some_font.ttf"); 36 37 // Creates a 100x100 render texture 38 RenderTexture renderTexture = LoadRenderTexture(100, 100); 39 40 // WindowShouldClose checks if the user is closing the window 41 // This might happen using a shortcut, window controls 42 // or the key we set earlier 43 while (!WindowShouldClose()) 44 { 45 46 // BeginDrawing needs to be called before any draw call 47 BeginDrawing(); 48 { 49 50 // Sets the background to a certain color 51 ClearBackground(BLACK); 52 53 if (IsKeyDown(KEY_SPACE)) 54 DrawCircle(400, 400, 30, GREEN); 55 56 // Simple draw text 57 DrawText("Congrats! You created your first window!", 58 190, // x 59 200, // y 60 20, // font size 61 LIGHTGRAY 62 ); 63 64 // For most functions there are several versions 65 // These are usually postfixed with Ex, Pro, V 66 // or sometimes Rec, Wires (only for 3D), Lines (only for 2D) 67 DrawTextEx(someFont, 68 "Text in another font", 69 (Vector2) {10, 10}, 70 20, // font size 71 2, // spacing 72 LIGHTGRAY); 73 74 // Required for drawing 3D, has 2D equivalent 75 BeginMode3D(camera); 76 { 77 78 DrawCube((Vector3) {0.0f, 0.0f, 3.0f}, 79 1.0f, 1.0f, 1.0f, RED); 80 81 // White tint when drawing will keep the original color 82 DrawModel(myModel, (Vector3) {0.0f, 0.0f, 3.0f}, 83 1.0f, //Scale 84 WHITE); 85 86 } 87 // End 3D mode so we can draw normally again 88 EndMode3D(); 89 90 // Start drawing onto render texture 91 BeginTextureMode(renderTexture); 92 { 93 94 // It behaves the same as if we just called `BeginDrawing()` 95 96 ClearBackground(RAYWHITE); 97 98 BeginMode3D(camera); 99 { 100 101 DrawGrid(10, // Slices 102 1.0f // Spacing 103 ); 104 105 } 106 EndMode3D(); 107 108 } 109 EndTextureMode(); 110 111 // render textures have a Texture2D field 112 DrawTexture(renderTexture.texture, 40, 378, BLUE); 113 114 } 115 EndDrawing(); 116 } 117 118 // Unloading loaded objects 119 UnloadFont(someFont); 120 UnloadModel(myModel); 121 122 // Close window and OpenGL context 123 CloseWindow(); 124 125 return 0; 126}

Further reading

raylib has some great examples If you don’t like C check out the raylib bindings