diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-04-11 21:22:10 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-04-11 21:22:10 +0200 |
| commit | 03dec3f9ade59cad2951ca9dd682df6afb12388d (patch) | |
| tree | 2cb0fb834719521a2fb642cb53c3d6163416955c /content | |
| parent | a6bc7a7697ac4745d9f724811cecfafb98b9f209 (diff) | |
| download | mitjafelicijan.com-03dec3f9ade59cad2951ca9dd682df6afb12388d.tar.gz | |
Update
Diffstat (limited to 'content')
| -rw-r--r-- | content/posts/2025-04-08-embedding-game-assets-within-your-binary.md | 108 |
1 files changed, 60 insertions, 48 deletions
diff --git a/content/posts/2025-04-08-embedding-game-assets-within-your-binary.md b/content/posts/2025-04-08-embedding-game-assets-within-your-binary.md index c3aa8bf..491a53b 100644 --- a/content/posts/2025-04-08-embedding-game-assets-within-your-binary.md +++ b/content/posts/2025-04-08-embedding-game-assets-within-your-binary.md | |||
| @@ -50,6 +50,18 @@ We first need to setup main game loop and get the game running. | |||
| 50 | #include "raylib.h" | 50 | #include "raylib.h" |
| 51 | 51 | ||
| 52 | int main(void) { | 52 | int main(void) { |
| 53 | SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI); | ||
| 54 | InitWindow(900, 400, "Embedding assets"); | ||
| 55 | SetTargetFPS(60); | ||
| 56 | |||
| 57 | while (!WindowShouldClose()) { | ||
| 58 | BeginDrawing(); | ||
| 59 | ClearBackground(BLACK); | ||
| 60 | EndDrawing(); | ||
| 61 | } | ||
| 62 | |||
| 63 | CloseWindow(); | ||
| 64 | return 0; | ||
| 53 | } | 65 | } |
| 54 | ``` | 66 | ``` |
| 55 | 67 | ||
| @@ -104,24 +116,24 @@ An example of such a file (in our case armor.png) looks like this. | |||
| 104 | ```c | 116 | ```c |
| 105 | // data/armor.h | 117 | // data/armor.h |
| 106 | unsigned char data_armor_png[] = { | 118 | unsigned char data_armor_png[] = { |
| 107 | 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | 119 | 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, |
| 108 | 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x01, 0x40, | 120 | 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x01, 0x40, |
| 109 | 0x08, 0x06, 0x00, 0x00, 0x00, 0xcd, 0x90, 0xa5, 0xaa, 0x00, 0x00, 0x00, | 121 | 0x08, 0x06, 0x00, 0x00, 0x00, 0xcd, 0x90, 0xa5, 0xaa, 0x00, 0x00, 0x00, |
| 110 | 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, | 122 | 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, |
| 111 | 0x00, 0x06, 0x62, 0x4b, 0x47, 0x44, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, | 123 | 0x00, 0x06, 0x62, 0x4b, 0x47, 0x44, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, |
| 112 | 0xa0, 0xbd, 0xa7, 0x93, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, | 124 | 0xa0, 0xbd, 0xa7, 0x93, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, |
| 113 | 0x00, 0x00, 0x17, 0x12, 0x00, 0x00, 0x17, 0x12, 0x01, 0x67, 0x9f, 0xd2, | 125 | 0x00, 0x00, 0x17, 0x12, 0x00, 0x00, 0x17, 0x12, 0x01, 0x67, 0x9f, 0xd2, |
| 114 | 0x52, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xdc, 0x0c, | 126 | 0x52, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xdc, 0x0c, |
| 115 | 0x1b, 0x07, 0x30, 0x17, 0xb2, 0x1a, 0xee, 0xda, 0x00, 0x00, 0x20, 0x00, | 127 | 0x1b, 0x07, 0x30, 0x17, 0xb2, 0x1a, 0xee, 0xda, 0x00, 0x00, 0x20, 0x00, |
| 116 | ... | 128 | ... |
| 117 | 0x81, 0x98, 0xa6, 0xb9, 0x2d, 0x37, 0xac, 0x6d, 0x57, 0xb0, 0xed, 0xea, | 129 | 0x81, 0x98, 0xa6, 0xb9, 0x2d, 0x37, 0xac, 0x6d, 0x57, 0xb0, 0xed, 0xea, |
| 118 | 0x86, 0xac, 0xa1, 0xa6, 0x6b, 0xf8, 0x54, 0x1f, 0x8e, 0xe3, 0x90, 0xcb, | 130 | 0x86, 0xac, 0xa1, 0xa6, 0x6b, 0xf8, 0x54, 0x1f, 0x8e, 0xe3, 0x90, 0xcb, |
| 119 | 0xe6, 0x36, 0x7d, 0x4e, 0x6b, 0xab, 0xcb, 0x78, 0xbd, 0x5e, 0x6c, 0xc7, | 131 | 0xe6, 0x36, 0x7d, 0x4e, 0x6b, 0xab, 0xcb, 0x78, 0xbd, 0x5e, 0x6c, 0xc7, |
| 120 | 0x01, 0x8f, 0xa7, 0x5e, 0x46, 0x22, 0x37, 0x17, 0x76, 0x13, 0x4d, 0x6c, | 132 | 0x01, 0x8f, 0xa7, 0x5e, 0x46, 0x22, 0x37, 0x17, 0x76, 0x13, 0x4d, 0x6c, |
| 121 | 0xab, 0x0c, 0xb0, 0x89, 0x26, 0x9a, 0x68, 0xe2, 0x45, 0xa3, 0x99, 0x2b, | 133 | 0xab, 0x0c, 0xb0, 0x89, 0x26, 0x9a, 0x68, 0xe2, 0x45, 0xa3, 0x99, 0x2b, |
| 122 | 0x34, 0xd1, 0x44, 0x13, 0xff, 0xb7, 0xf8, 0x27, 0x9b, 0x96, 0x5c, 0x0d, | 134 | 0x34, 0xd1, 0x44, 0x13, 0xff, 0xb7, 0xf8, 0x27, 0x9b, 0x96, 0x5c, 0x0d, |
| 123 | 0x8b, 0xbb, 0x21, 0xa6, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, | 135 | 0x8b, 0xbb, 0x21, 0xa6, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, |
| 124 | 0xae, 0x42, 0x60, 0x82 | 136 | 0xae, 0x42, 0x60, 0x82 |
| 125 | }; | 137 | }; |
| 126 | unsigned int data_armor_png_len = 118324; | 138 | unsigned int data_armor_png_len = 118324; |
| 127 | ``` | 139 | ``` |
| @@ -141,36 +153,36 @@ API fits perfectly with this style of converting binary files. | |||
| 141 | #define FONT_SIZE 24 | 153 | #define FONT_SIZE 24 |
| 142 | 154 | ||
| 143 | int main(void) { | 155 | int main(void) { |
| 144 | SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI); | 156 | SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI); |
| 145 | InitWindow(900, 400, "Embedding assets"); | 157 | InitWindow(900, 400, "Embedding assets"); |
| 146 | SetTargetFPS(60); | 158 | SetTargetFPS(60); |
| 147 | 159 | ||
| 148 | // Load font from memory. | 160 | // Load font from memory. |
| 149 | Font font = LoadFontFromMemory(".ttf", data_dejavusans_mono_ttf, data_dejavusans_mono_ttf_len, FONT_SIZE, NULL, 0); | 161 | Font font = LoadFontFromMemory(".ttf", data_dejavusans_mono_ttf, data_dejavusans_mono_ttf_len, FONT_SIZE, NULL, 0); |
| 150 | SetTextureFilter(font.texture, TEXTURE_FILTER_TRILINEAR); // Font antialising. | 162 | SetTextureFilter(font.texture, TEXTURE_FILTER_TRILINEAR); // Font antialising. |
| 151 | 163 | ||
| 152 | // Load image from memory and create texture from it. | 164 | // Load image from memory and create texture from it. |
| 153 | Image armor = LoadImageFromMemory(".png", data_armor_png, data_armor_png_len); | 165 | Image armor = LoadImageFromMemory(".png", data_armor_png, data_armor_png_len); |
| 154 | Texture2D armor_texture = LoadTextureFromImage(armor); | 166 | Texture2D armor_texture = LoadTextureFromImage(armor); |
| 155 | UnloadImage(armor); | 167 | UnloadImage(armor); |
| 156 | 168 | ||
| 157 | while (!WindowShouldClose()) { | 169 | while (!WindowShouldClose()) { |
| 158 | BeginDrawing(); | 170 | BeginDrawing(); |
| 159 | ClearBackground(BLACK); | 171 | ClearBackground(BLACK); |
| 160 | 172 | ||
| 161 | // Draw the armor texture. | 173 | // Draw the armor texture. |
| 162 | DrawTexture(armor_texture, 20, 20, WHITE); | 174 | DrawTexture(armor_texture, 20, 20, WHITE); |
| 163 | 175 | ||
| 164 | // Draw some text on the screen. | 176 | // Draw some text on the screen. |
| 165 | DrawTextEx(font, "Hello embedded assets.", (Vector2){ 400, 20 }, FONT_SIZE, 0, WHITE); | 177 | DrawTextEx(font, "Hello embedded assets.", (Vector2){ 400, 20 }, FONT_SIZE, 0, WHITE); |
| 166 | DrawTextEx(font, "This is example how we can use embedded fonts.", (Vector2){ 400, 50 }, FONT_SIZE - 4, 0, WHITE); | 178 | DrawTextEx(font, "This is example how we can use embedded fonts.", (Vector2){ 400, 50 }, FONT_SIZE - 4, 0, WHITE); |
| 167 | 179 | ||
| 168 | EndDrawing(); | 180 | EndDrawing(); |
| 169 | } | 181 | } |
| 170 | 182 | ||
| 171 | UnloadTexture(armor_texture); | 183 | UnloadTexture(armor_texture); |
| 172 | CloseWindow(); | 184 | CloseWindow(); |
| 173 | return 0; | 185 | return 0; |
| 174 | } | 186 | } |
| 175 | ``` | 187 | ``` |
| 176 | 188 | ||
