example of code for al_draw_ribbon, al_calculate_spline/ribbon
Fred58

hello,

i'm looking for examples of code for functions because i don't understand the
int points_stride and int num_segments parameters :

void al_draw_ribbon(const float *points, int points_stride, ALLEGRO_COLOR color,
float thickness, int num_segments)

void al_calculate_ribbon(float* dest, int dest_stride, const float *points,
int points_stride, float thickness, int num_segments)

void al_calculate_spline(float* dest, int stride, float points[8],
float thickness, int num_segments)

Thank very much
F

SiegeLord

Here's a quick example. I note that num_segments is a wrong name for that parameter: it should be num_corners or num_points perhaps. The stride parameters allow you to use structs that are not just pairs of floats when the functions ask for points.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3 4int main() 5{ 6 al_init(); 7 al_init_primitives_addon(); 8 auto disp = al_create_display(800, 600); 9 auto queue = al_create_event_queue(); 10 al_register_event_source(queue, al_get_display_event_source(disp)); 11 al_clear_to_color(al_map_rgb_f(0, 0, 0)); 12 13 struct MyPoint 14 { 15 float x, y; 16 int something_else; 17 }; 18 19 const size_t num_points1 = 5; 20 MyPoint points1[5] = 21 { 22 {100, 100, 0}, 23 {100, 150, 0}, 24 {200, 200, 0}, 25 {200, 300, 0}, 26 {100, 500, 0} 27 }; 28 29 al_draw_ribbon((float*)points1, sizeof(MyPoint), al_map_rgb_f(1, 0, 0.5), 10, num_points1); 30 31 const size_t num_points2 = num_points1 * 2; 32 auto points2 = new MyPoint[num_points2]; 33 34 al_calculate_ribbon((float*)points2, sizeof(MyPoint), (float*)points1, sizeof(MyPoint), 30, num_points1); 35 36 for(size_t ii = 0; ii < num_points2; ii++) 37 al_draw_filled_circle(points2[ii].x, points2[ii].y, 5, al_map_rgb_f(1, 0.5, 0)); 38 39 float spline_control_points[8] = 40 { 41 400, 100, 42 500, 200, 43 300, 400, 44 400, 500 45 }; 46 47 al_calculate_spline((float*)points2, sizeof(MyPoint), spline_control_points, 30, num_points1); 48 49 for(size_t ii = 0; ii < num_points2; ii++) 50 al_draw_filled_circle(points2[ii].x, points2[ii].y, 5, al_map_rgb_f(0.5, 1, 0)); 51 52 al_flip_display(); 53 ALLEGRO_EVENT ev; 54 al_wait_for_event(queue, &ev); 55 56 delete[] points2; 57}

Fred58

Thank you very much for this example of code.
Very usefull for understanding !

Thread #610638. Printed from Allegro.cc