-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtextures_from_memory.js
More file actions
68 lines (52 loc) · 2.83 KB
/
textures_from_memory.js
File metadata and controls
68 lines (52 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************************
*
* raylib [textures] example - Image loading and texture creation from memory
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 3.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
********************************************************************************************/
const r = require('raylib')
const { resolve } = require('path')
const { readFileSync } = require('fs')
// Initialization
// --------------------------------------------------------------------------------------
const screenWidth = 800
const screenHeight = 450
// Load the image using fs module into a `Buffer` instance.
// NOTE: this `Buffer` could come from *anywhere* (statically encoded in your source code, as a
// partial read from a custom resource packer file, etc.)
const raw = readFileSync(resolve(__dirname, 'resources', 'raylib_logo.png'))
r.InitWindow(screenWidth, screenHeight, 'raylib [textures] example - image loading from memory')
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
const image = r.LoadImageFromMemory('.png', raw, raw.length) // Loaded in CPU memory (RAM)
if (!image) {
console.error('image failed to load!')
process.exit(1)
}
// TODO: Fix thrown exception
// INFO: [/home/rob/Documents/node-raylib/examples/textures/resources/raylib_logo.png] Image loaded successfully (256x256)
// terminate called after throwing an instance of 'char const*'
// [1] 26364 abort (core dumped) bin/node-raylib examples/textures/textures_image_loading.js
const texture = r.LoadTextureFromImage(image) // Image converted to texture, GPU memory (VRAM)
r.UnloadImage(image) // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
// ---------------------------------------------------------------------------------------
// Main game loop
while (!r.WindowShouldClose()) { // Detect window close button or ESC key
// Update
// Draw
// ----------------------------------------------------------------------------------
r.BeginDrawing()
r.ClearBackground(r.RAYWHITE)
r.DrawTexture(texture, screenWidth / 2 - texture.width / 2, screenHeight / 2 - texture.height / 2, r.WHITE)
r.DrawText('this IS a texture loaded from memory!', 300, 370, 10, r.GRAY)
r.EndDrawing()
// ----------------------------------------------------------------------------------
}
// De-Initialization
// --------------------------------------------------------------------------------------
r.UnloadTexture(texture) // Texture unloading
r.CloseWindow() // Close window and OpenGL context
// --------------------------------------------------------------------------------------