Advertisement
KeithS

A5_Deg_Trig_Test.cpp

Jun 22nd, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.51 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_primitives.h>
  3. #include <allegro5/allegro_font.h>
  4. #include <allegro5/allegro_ttf.h>
  5. #include <cstdio>
  6. #include <string>
  7. #include "deg_trig.h"
  8.  
  9. using namespace std;
  10.  
  11. const int SCREEN_X = 800;
  12. const int SCREEN_Y = 600;
  13. const float fps = 60.0f;
  14.  
  15. struct posits
  16. {
  17.     float x_pos;
  18.     float y_pos;
  19. };
  20.  
  21. bool allegro5_setup(        ALLEGRO_DISPLAY **disp,
  22.                             ALLEGRO_EVENT_QUEUE **event_q,
  23.                             ALLEGRO_TIMER **f_timer,
  24.                             ALLEGRO_FONT **fnt_1,
  25.                             ALLEGRO_FONT **fnt_2)
  26. {
  27.     if(al_init() == false)
  28.     {
  29.         printf("Allegro did not initialize.\n");
  30.         return false;
  31.     }
  32.     if(al_install_mouse() == false)
  33.     {
  34.         printf("Mouse did not install.\n");
  35.         return false;
  36.     }
  37.     al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
  38.     *disp = al_create_display(SCREEN_X, SCREEN_Y);
  39.     if(*disp == NULL)
  40.     {
  41.         printf("Display not created.\n");
  42.         return false;
  43.     }
  44.     *event_q = al_create_event_queue();
  45.     if(*event_q == NULL)
  46.     {
  47.         printf("Event Queue not created.\n");
  48.         al_destroy_display(*disp);
  49.         return false;
  50.     }
  51.     *f_timer = al_create_timer(1.0f / fps);
  52.     if(*f_timer == NULL)
  53.     {
  54.         printf("Timer not created.\n");
  55.         al_destroy_display(*disp);
  56.         al_destroy_event_queue(*event_q);
  57.         return false;
  58.     }
  59.  
  60.     al_init_font_addon();
  61.     if(al_init_ttf_addon() == false)
  62.     {
  63.         printf("TTF addon not initialized.\n");
  64.         al_destroy_display(*disp);
  65.         al_destroy_event_queue(*event_q);
  66.         al_destroy_timer(*f_timer);
  67.         return false;
  68.     }
  69.     *fnt_1 = al_load_ttf_font("./fonts/Verdana.ttf", -11, NULL);
  70.     *fnt_2 = al_load_ttf_font("./fonts/Verdana.ttf", -19, NULL);
  71.     if((*fnt_1 == NULL || *fnt_2 == NULL) || (*fnt_1 == NULL && *fnt_2 == NULL))
  72.     {
  73.         printf("TTF Font did not load. Check that it exists in /font.\n");
  74.         al_destroy_display(*disp);
  75.         al_destroy_event_queue(*event_q);
  76.         al_destroy_timer(*f_timer);
  77.         return false;
  78.     }
  79.     if(al_init_primitives_addon() == false)
  80.     {
  81.         printf("Primitives did not initialize.\n");
  82.         al_destroy_display(*disp);
  83.         al_destroy_event_queue(*event_q);
  84.         al_destroy_timer(*f_timer);
  85.         return false;
  86.     }
  87.     al_register_event_source(*event_q, al_get_display_event_source(*disp));
  88.     al_register_event_source(*event_q, al_get_mouse_event_source());
  89.     al_register_event_source(*event_q, al_get_timer_event_source(*f_timer));
  90.     return true;
  91. }
  92.  
  93. //---------------------------------------------------------------
  94.  
  95. void allegro5_terminate(        ALLEGRO_DISPLAY **disp,
  96.                                 ALLEGRO_EVENT_QUEUE **event_q,
  97.                                 ALLEGRO_TIMER **f_timer)
  98. {
  99.     al_destroy_display(*disp);
  100.     al_destroy_event_queue(*event_q);
  101.     al_destroy_timer(*f_timer);
  102. }
  103.  
  104. //---------------------------------------------------------------
  105.  
  106. //---------------------------------------------------------------
  107.  
  108. int main(int argc, char *argv[])
  109. {
  110.     ALLEGRO_DISPLAY *display = NULL;
  111.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  112.     ALLEGRO_TIMER *timer = NULL;
  113.     ALLEGRO_FONT *font_1 = NULL;
  114.     ALLEGRO_FONT *font_2 = NULL;
  115.  
  116.     bool draw = false;
  117.     bool loop = true;
  118.     bool mouseb = false;
  119.     char buffer[100];
  120.  
  121.     //int mx = 0;
  122.     //int my = 0;
  123.     float x_dist = 0.0f;
  124.     float y_dist = 0.0f;
  125.     float r_bearing = 0.0f;
  126.     float full_r_bearing = 0.0f;
  127.     float d_bearing = 0.0f;
  128.     float deg_cos = 0.0f;
  129.     float deg_sin = 0.0f;
  130.     float rad_cos = 0.0f;
  131.     float rad_sin = 0.0f;
  132.     float deg_tan = 0.0f;
  133.     float rad_tan = 0.0f;
  134.  
  135.     float nod_d_bearing = 0.0f;
  136.     float nod_deg_cos = 0.0f;
  137.     float nod_deg_sin = 0.0f;
  138.  
  139.     posits act_ac_pos;
  140.     posits dest_ac_pos;
  141.  
  142.     act_ac_pos.x_pos = 400.0f;
  143.     act_ac_pos.y_pos = 300.0f;
  144.     dest_ac_pos.x_pos = 0.0f;
  145.     dest_ac_pos.y_pos = 0.0f;
  146.  
  147.     if(allegro5_setup(&display, &event_queue, &timer, &font_1, &font_2) == false)
  148.         return -1;
  149.  
  150.     al_start_timer(timer);
  151.  
  152.  
  153.     while(loop == true)
  154.     {
  155.         ALLEGRO_EVENT event;
  156.         al_wait_for_event(event_queue, &event);
  157.  
  158.         switch(event.type)
  159.         {
  160.             case ALLEGRO_EVENT_DISPLAY_CLOSE:
  161.                 loop = false;
  162.                 break;
  163.             case ALLEGRO_EVENT_TIMER:
  164.                 draw = true;
  165.                 break;
  166.             case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
  167.                 mouseb = true;
  168.                 dest_ac_pos.x_pos = (float)event.mouse.x;
  169.                 dest_ac_pos.y_pos = (float)event.mouse.y;
  170.                 break;
  171.             case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
  172.                 mouseb = false;
  173.                 break;
  174.             case ALLEGRO_EVENT_MOUSE_AXES:
  175.                 if(mouseb == true)
  176.                 /*{
  177.                     dest_ac_pos.x_pos = event.mouse.x;
  178.                     dest_ac_pos.y_pos = event.mouse.y;
  179.                 }*/
  180.                 break;
  181.             default:
  182.                 break;
  183.         }
  184.         if(al_event_queue_is_empty(event_queue) && draw == true)
  185.         {
  186.             if(mouseb == true)
  187.             {
  188.                 x_dist = dest_ac_pos.x_pos - act_ac_pos.x_pos;
  189.                 y_dist = dest_ac_pos.y_pos - act_ac_pos.y_pos;
  190.                 r_bearing = atan2f(y_dist, x_dist);
  191.                 full_r_bearing = my_atan2f_PIx2_rads(y_dist, x_dist);
  192.                 d_bearing = my_atan2f_deg(y_dist, x_dist);
  193.                 deg_cos = my_cosf_deg(d_bearing);
  194.                 deg_sin = my_sinf_deg(d_bearing);
  195.                 rad_cos = cosf(r_bearing);
  196.                 rad_sin = sinf(r_bearing);
  197.                 deg_tan = my_tanf_deg(d_bearing);
  198.                 rad_tan = tanf(r_bearing);
  199.                 nod_d_bearing = my_nod_atan2f_deg(y_dist, x_dist);
  200.                 nod_deg_cos = my_nod_cosf_deg(nod_d_bearing);
  201.                 nod_deg_sin = my_nod_sinf_deg(nod_d_bearing);
  202.             }
  203.             al_clear_to_color(al_map_rgb(0,0,30));
  204.             al_draw_circle(act_ac_pos.x_pos, act_ac_pos.y_pos, 3.0f, al_map_rgb(200,200,200), 0);
  205.  
  206.             sprintf(buffer, "Bearing radians %.4f", r_bearing);
  207.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 10.0f, ALLEGRO_ALIGN_LEFT, buffer);
  208.  
  209.             sprintf(buffer, "Bearing full radians %.4f", full_r_bearing);
  210.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 20.0f, ALLEGRO_ALIGN_LEFT, buffer);
  211.  
  212.             sprintf(buffer, "Bearing degrees %.2f", d_bearing);
  213.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 30.0f, ALLEGRO_ALIGN_LEFT, buffer);
  214.  
  215.             sprintf(buffer, "Cosine Value from degrees %.4f", deg_cos);
  216.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 45.0f, ALLEGRO_ALIGN_LEFT, buffer);
  217.  
  218.             sprintf(buffer, "Sine Value from degrees %.4f", deg_sin);
  219.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 55.0f, ALLEGRO_ALIGN_LEFT, buffer);
  220.  
  221.             sprintf(buffer, "Cosine Value from radians %.4f", rad_cos);
  222.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 70.0f, ALLEGRO_ALIGN_LEFT, buffer);
  223.  
  224.             sprintf(buffer, "Sine Value from radians %.4f", rad_sin);
  225.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 80.0f, ALLEGRO_ALIGN_LEFT, buffer);
  226.  
  227.             sprintf(buffer, "Tangent Value from radians %.4f", rad_tan);
  228.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 95.0f, ALLEGRO_ALIGN_LEFT, buffer);
  229.  
  230.             sprintf(buffer, "Tangent Value from degrees %.4f", deg_tan);
  231.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 105.0f, ALLEGRO_ALIGN_LEFT, buffer);
  232.  
  233.             sprintf(buffer, "North Orientated Atan Degrees %.2f", nod_d_bearing);
  234.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 120.0f, ALLEGRO_ALIGN_LEFT, buffer);
  235.  
  236.             sprintf(buffer, "North Orientated Cosine form Degrees %.4f", nod_deg_cos);
  237.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 130.0f, ALLEGRO_ALIGN_LEFT, buffer);
  238.  
  239.             sprintf(buffer, "North Orientated Cosine form Degrees %.4f", nod_deg_sin);
  240.             al_draw_text(font_1, al_map_rgb(200,200,200), 10.0f, 140.0f, ALLEGRO_ALIGN_LEFT, buffer);
  241.  
  242.             al_flip_display();
  243.             draw = false;
  244.         }
  245.     }
  246.  
  247.     al_stop_timer(timer);
  248.     allegro5_terminate(&display, &event_queue, &timer);
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement