diff options
| author | Amir Rajan <[email protected]> | 2020-11-13 01:29:16 -0600 |
|---|---|---|
| committer | Amir Rajan <[email protected]> | 2020-11-13 01:29:16 -0600 |
| commit | 128fa1d90cea6289605a49daf56a0cbb72e2dd28 (patch) | |
| tree | 5cfdb499d275e2b43075e4d6a076365fc58ff0f7 /samples/12_c_extensions/03_native_pixel_arrays/app | |
| parent | 05cbef7fb8224332795e5685be499d81d20e7d93 (diff) | |
| download | dragonruby-game-toolkit-contrib-128fa1d90cea6289605a49daf56a0cbb72e2dd28.tar.gz dragonruby-game-toolkit-contrib-128fa1d90cea6289605a49daf56a0cbb72e2dd28.zip | |
synced from DRGTK 1.27
Diffstat (limited to 'samples/12_c_extensions/03_native_pixel_arrays/app')
| -rw-r--r-- | samples/12_c_extensions/03_native_pixel_arrays/app/ext.c | 54 | ||||
| -rw-r--r-- | samples/12_c_extensions/03_native_pixel_arrays/app/main.rb | 22 |
2 files changed, 76 insertions, 0 deletions
diff --git a/samples/12_c_extensions/03_native_pixel_arrays/app/ext.c b/samples/12_c_extensions/03_native_pixel_arrays/app/ext.c new file mode 100644 index 0000000..49d0e40 --- /dev/null +++ b/samples/12_c_extensions/03_native_pixel_arrays/app/ext.c @@ -0,0 +1,54 @@ +#ifndef NULL +#define NULL 0 +#endif +typedef unsigned int Uint32; + +extern void *(*drb_symbol_lookup)(const char *sym); +typedef void (*drb_upload_pixel_array_fn)(const char *name, const int w, const int h, const Uint32 *pixels); + +void update_scanner_texture(void) +{ + #define dimension 10 + + static drb_upload_pixel_array_fn drb_upload_pixel_array = NULL; + static int pos = 0; + static int posinc = 1; + + if (!drb_upload_pixel_array) { + drb_upload_pixel_array = drb_symbol_lookup("drb_upload_pixel_array"); + if (!drb_upload_pixel_array) { + return; // oh well. + } + } + + + // Set up our "scanner" pixel array and fill it with black pixels. + + // You could make this faster by making this array static (which will + // initialize it all to zero at startup), and then blanking the previous + // line and drawing the next, and not touching the rest. + Uint32 pixels[dimension * dimension]; + for (int i = 0; i < (dimension * dimension); i++) { + pixels[i] = 0xFF000000; // full alpha, full black + } + + // Draw a green line that bounces up and down the sprite. + Uint32 *line = pixels + (pos * dimension); + for (int i = 0; i < dimension; i++) { + line[i] = 0xFF00FF00; // full alpha, full green + } + + // Adjust position for next frame. + pos += posinc; + if ((posinc > 0) && (pos >= dimension)) { + posinc = -1; + pos = dimension - 1; + } else if ((posinc < 0) && (pos < 0)) { + posinc = 1; + pos = 1; + } + + // Send it to the renderer to create/update a sprite. + drb_upload_pixel_array("scanner", dimension, dimension, pixels); +} + diff --git a/samples/12_c_extensions/03_native_pixel_arrays/app/main.rb b/samples/12_c_extensions/03_native_pixel_arrays/app/main.rb new file mode 100644 index 0000000..1dbc716 --- /dev/null +++ b/samples/12_c_extensions/03_native_pixel_arrays/app/main.rb @@ -0,0 +1,22 @@ +$gtk.ffi_misc.gtk_dlopen("ext") +include FFI::CExt + +def tick args + args.state.rotation ||= 0 + + update_scanner_texture # this calls into a C extension! + + # New/changed pixel arrays get uploaded to the GPU before we render + # anything. At that point, they can be scaled, rotated, and otherwise + # used like any other sprite. + w = 100 + h = 100 + x = (1280 - w) / 2 + y = (720 - h) / 2 + args.outputs.background_color = [64, 0, 128] + args.outputs.primitives << [x, y, w, h, :scanner, args.state.rotation].sprite + args.state.rotation += 1 + + args.outputs.primitives << args.gtk.current_framerate_primitives +end + |
