|
|
|
03-19-2018, 03:30 AM
|
#1
|
Human being with feelings
Join Date: Nov 2006
Location: Finland
Posts: 1,528
|
HOWTO: Newbie tips on blit & external buffers!
After searching, trying, fighting and failing for past a dozen hours, I FINALLY got it to work!
I'd imagine other newbies would fight the exact same issues.
Idea is to output stuff to a hidden graphics buffer that can then be fetched partially or in whole.
Often this is used to save resources, not needing to calculate something 30 times per second
just to show the same thing. But as is known, JSFX documentation is often not that helpful
in learning how to actually do things.
Code:
// 1) Initialize buffer:
gfx_dest = 127; // Change destination to buffer #127
gfx_setimgdim(127 , -1 , -1); // Reset the buffer #127
gfx_setimgdim(127 , gfx_w , gfx_h); // Initialize the buffer #127
// 2) Then draw anything, they will all now go to the external buffer:
gfx_x = gfx_w /2 ; gfx_y = gfx_h /2 ; // Move cursor to the center
gfx_set(0.9 , 0.8 , 0.1 , 1); // Set color (r, g, b, a)
gfx_drawstr("Hello world"); // Write an original piece of literature
// 3) Then fetch the buffered image to the main drawing buffer:
gfx_dest = -1; // Change destination back to the default drawing buffer
gfx_x = gfx_y = 0; // Reset cursor
gfx_blit(127, 1, 0); // Blit the whole buffer #127
// Alternatively you can extend the Blit command with source and
// destination x,y,w,h, and rotation offset x,y, from center:
gfx_blit(127, 1, 0, 0,0,gfx_w,gfx_h, 0,0,gfx_w,gfx_h, 0,0);
// That will fetch the whole image from buffer #127, with scaling=1
// (no scaling), and rotation=0 (rads).
And for saving resources we can do the writing part for example once per second, and retrieve only
the bitmap image which is a lot lighter process.
Code:
counter += 1; // Add 1 to "counter"
counter > 30 ? ( // When "counter" passes 30 (1 second)
// Here you can do the 1) initialization (, calculations)
// and 2) drawing from the earlier code
counter = 0; // Reset "counter"
);
// And then the 3) fetching part from the earlier code.
To only update the buffer when a slider is changed,
__________________
______ Announcing__mrelwood plugins______
.. MacBook Pro 16" Late '19 .. Scarlett 6i6, Saffire Pro 24 DSP (+ADA8000) .. FCA610 .. EVE SC207 .. Focal: Shape 65, Alpha 65, CMS 40, Listen Pro ..
Last edited by mrelwood; 10-22-2018 at 07:12 PM.
|
|
|
03-20-2018, 10:04 AM
|
#2
|
Human being with feelings
Join Date: Jul 2010
Location: Slovakia
Posts: 2,588
|
Thanks for this!
|
|
|
03-20-2018, 11:50 AM
|
#3
|
Human being with feelings
Join Date: Apr 2014
Posts: 4,171
|
I would add - only redraw the graphics backbuffer when you need to. Add a flag into your code that signals to redraw the buffer, if flag is not set when the drawing routine is called - just display the back buffer - if it is set - perform the redraw and then display the buffer.
Last edited by lb0; 03-20-2018 at 05:39 PM.
|
|
|
03-23-2018, 02:57 AM
|
#4
|
Human being with feelings
Join Date: Nov 2006
Location: Finland
Posts: 1,528
|
lb0, you are absolutely right.
One example:
Code:
slider1_old != slider1 ? update = 1;
// If slider1 changes, change ”update” to 1.
slider1_old = slider1;
// If slider1 changes, it will do so after the code, so between now and the row above.
update == 1 ? (
update = 0; // Reset the update flag.
// 1) Initialize buffer:
gfx_dest = 127; // Change destination to buffer #127
gfx_setimgdim(127 , -1 , -1); // Reset the buffer #127
gfx_setimgdim(127 , gfx_w , gfx_h); // Initialize the buffer #127
// 2) Then draw anything, they will all now go to the external buffer:
gfx_x = gfx_w /2 ; gfx_y = gfx_h /2 ; // Move cursor to the center
gfx_set(0.9 , 0.8 , 0.1 , 1); // Set color (r, g, b, a)
gfx_drawstr("Hello world"); // Write an original piece of literature
);
// 3) Then fetch the buffered image to the main drawing buffer,
// updated or not:
gfx_dest = -1; // Change destination back to the default drawing buffer
gfx_x = gfx_y = 0; // Reset cursor
gfx_blit(127, 1, 0); // Blit the whole buffer #127
__________________
______ Announcing__mrelwood plugins______
.. MacBook Pro 16" Late '19 .. Scarlett 6i6, Saffire Pro 24 DSP (+ADA8000) .. FCA610 .. EVE SC207 .. Focal: Shape 65, Alpha 65, CMS 40, Listen Pro ..
Last edited by mrelwood; 10-22-2018 at 07:20 PM.
|
|
|
03-23-2018, 03:00 PM
|
#5
|
Human being with feelings
Join Date: Mar 2013
Posts: 515
|
Thanks for this one. Bookmarked!
__________________
"After silence, that which comes nearest to expressing the inexpressible is music", Aldous Huxley
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 02:34 PM.
|