Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing primitives with antialiasing and multisampling

Credits go to Edgar Reynaldo, Elias, and Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
Drawing primitives with antialiasing and multisampling
Sarfaraz Sheikh
Member #12,739
April 2011
avatar

hi everybody, I want to make a game using primitives only. for that, I want to enable multisampling and antialiase diagonal lines and circles. I've searched on the forum for this and found this thread thread/603894 . below is the code in which I've tried to enable multisampling but I cannot see any effect of it.

please guide me what should I do to get multisampling enabled .
I just want smooth antialiased primitives drawn on the screen .

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_color.h> 5 6int main(int argc, char **argv) 7{ 8 ALLEGRO_DISPLAY *display = NULL; 9 10 //allegro initialization 11 if(!al_init()) { 12 fprintf(stderr, "failed to initialize allegro!\n"); 13 return -1; 14 } 15 //allegro primitive addon initialization 16 al_init_primitives_addon(); 17 18 //creating display 19 display = al_create_display(640, 480); 20 21 //checking if display has been created 22 if(!display) { 23 fprintf(stderr, "failed to create display!\n"); 24 return -1; 25 } 26 27 28 29 30 //creating color variables of ALLEGRO_COLOR data type 31 ALLEGRO_COLOR yellow = al_map_rgb(200,200,0); 32 ALLEGRO_COLOR black = al_map_rgb(0,0,0); 33 34 //clear the display buffer with given color 35 al_clear_to_color(black); 36 37 //draw circle on buffer with given center,radius,color,line-width 38 al_draw_circle(210.0,210.0,110.0,yellow,6); 39 40 //turning multisampling on 41 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); 42 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_REQUIRE); 43 44 //display the current buffer on screen and other buffer will be editable 45 al_flip_display(); 46 47 48 al_rest(5.0); 49 50 //using variable yellow and black of ALLEGRO_COLOR data type 51 al_clear_to_color(yellow); 52 al_draw_circle(210.0,210.0,50.0,black,9); 53 54 al_flip_display(); 55 56 al_rest(5.0); 57 58 //using ALLEGRO_COLOR al_color_name(char const *name) 59 al_clear_to_color(al_color_name("blue")); 60 al_draw_circle(210.0,210.0,50.0,al_color_name("gold"),9); 61 62 al_flip_display(); 63 64 al_rest(5.0); 65 66 al_shutdown_primitives_addon(); 67 al_destroy_display(display); 68 69 return 0; 70}

Matthew Leverton
Supreme Loser
January 1999
avatar

Call al_set_new_display_option before creating the display.

Sarfaraz Sheikh
Member #12,739
April 2011
avatar

Is it possible to create transparent or translucent primitives using allegro 5. I want to create Translucent rectangles so that if one rectangle go behind another, both can be visible .

Edgar Reynaldo
Member #8,592
May 2007
avatar

Yes, it is possible. Pass a premultiplied alpha color as the color to the drawing routines :

float r = 1.0f;
float g = 0.75f;
float b = 0.5f;
float a = 0.5f;

al_draw_line(x,y,x2,y2,al_map_rgba_f(r*a , g*a , b*a , a),25.0);
al_flip_display();

Sarfaraz Sheikh
Member #12,739
April 2011
avatar

@Matthew Leverton : as shown below now I am calling al_set_new_display_option before creating display but still I can't see any difference. I am also checking if the option is set or not with al_get_display_option and it is returning 0. please help me out !! :(

#SelectExpand
1 ALLEGRO_DISPLAY *display = NULL; 2 3 //turning multisampling on 4 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_REQUIRE); 5 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_REQUIRE); 6 7 //creating display 8 display = al_create_display(640, 480); 9 10 printf("%d",al_get_display_option(display,ALLEGRO_SAMPLE_BUFFERS));

Elias
Member #358
May 2000

Use ALLEGRO_SUGGEST instead of ALLEGRO_REQUIRE, in case the exact value of 8 samples is not supported by your hardware (it might only support 4 or 10 or 16 or something). With suggest it will use the closest available one.

--
"Either help out or stop whining" - Evert

Sarfaraz Sheikh
Member #12,739
April 2011
avatar

the problem is solved many thanks to you all .. :)

First i had to set the options before creating the display.Second, I had to set the importance to ALLEGRO_SUGGEST so that if requested sampling value is not available allegro will set the closest available.

here is how the final code looks like

#SelectExpand
1 //enabling multisampling 2 // option on/off importance 3 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); 4 // option value importance 5 al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); 6 7 //creating display 8 display = al_create_display(640, 480); 9 10 //checking if display options are set with new display. 11 printf("%d",al_get_display_option(display,ALLEGRO_SAMPLE_BUFFERS));

Go to: