Advertisement
pouar

al_draw_star

Mar 8th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************al_draw_star.h*********************************/
  2. #include <allegro5/allegro_primitives.h>
  3. #include <math.h>
  4. #define PI 3.14159265358979323846264338327950288419716939937510582
  5. int al_draw_star(float x, float y, float r, int s, ALLEGRO_COLOR color, float thickness);
  6. /*********************************al_draw_star.c*********************************/
  7. #include "al_draw_star.h"
  8.  
  9. int al_draw_star(float x, float y, float r, int s, ALLEGRO_COLOR color, float thickness)
  10. {
  11.     int i = 0;
  12.     do
  13.     {
  14.         float y1=y-(r*cos(i*(PI/180)));
  15.         float x1=x+(r*sin(i*(PI/180)));
  16.         float y2=y-((r/2)*cos((i+360/s/2)*(PI/180)));
  17.         float x2=x+((r/2)*sin((i+360/s/2)*(PI/180)));
  18.         al_draw_line(x1, y1, x2, y2, color, thickness);
  19.         y1=y-((r/2)*cos((i+360/s/2)*(PI/180)));
  20.         x1=x+((r/2)*sin((i+360/s/2)*(PI/180)));
  21.         y2=y-(r*cos((i+360/s)*(PI/180)));
  22.         x2=x+(r*sin((i+360/s)*(PI/180)));
  23.         al_draw_line(x1, y1, x2, y2, color, thickness);
  24.         i+=360/s;
  25.     } while(i<360);
  26. }
  27. /*********************************example.c*********************************/
  28. #include <stdio.h>
  29. #include <allegro5/allegro.h>
  30. #include <stdlib.h>
  31. #include "al_draw_star.h"
  32. const float FPS = 60;
  33. const int SCREEN_W = 640;
  34. const int SCREEN_H = 480;
  35. int main()
  36. {
  37.     ALLEGRO_DISPLAY *display = NULL;
  38.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  39.     ALLEGRO_TIMER *timer = NULL;
  40.     bool redraw = true;
  41.     al_init();
  42.     al_init_primitives_addon();
  43.     al_install_mouse();
  44.     timer = al_create_timer(1.0 / FPS);
  45.     display = al_create_display(SCREEN_W, SCREEN_H);
  46.     al_clear_to_color(al_map_rgb(255, 0, 255));
  47.     al_set_target_bitmap(al_get_backbuffer(display));
  48.     event_queue = al_create_event_queue();
  49.     al_register_event_source(event_queue, al_get_display_event_source(display));
  50.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  51.     al_register_event_source(event_queue, al_get_mouse_event_source());
  52.     al_clear_to_color(al_map_rgb(0,0,0));
  53.     al_flip_display();
  54.     al_start_timer(timer);
  55.     while(1)
  56.     {
  57.         ALLEGRO_EVENT ev;
  58.         al_wait_for_event(event_queue, &ev);
  59.         if(ev.type == ALLEGRO_EVENT_TIMER)
  60.         {
  61.             redraw = true;
  62.         }
  63.         else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  64.         {
  65.             break;
  66.         }
  67.         if(redraw && al_is_event_queue_empty(event_queue))
  68.         {
  69.             redraw = false;
  70.             al_clear_to_color(al_map_rgb(0,0,0));
  71.             al_draw_star(300, 300, 100, 5,al_map_rgb(255,255,255),3);
  72.             al_flip_display();
  73.         }
  74.     }
  75.     al_destroy_timer(timer);
  76.     al_destroy_display(display);
  77.     al_destroy_event_queue(event_queue);
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement