1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
| #include <stdlib.h>
#include <stdarg.h>
// string.h nécessaire pour éviter warning implicit declaration of function strcmp
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <gtk/gtk.h>
#include <goocanvas.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#ifndef M_PI
#define M_PI 3.141592653
#endif
#define scale_ini 1.0
GtkWidget *window;
GtkWidget *canvas;
GooCanvasItem *root;
GooCanvasItem *text_item;
// ratio: entre CFA et MEN : 71Nm = 225px (on multiplie par plane.ratio pour passer de Nm en pixels)
double plane_ratio = 225/71;
// mise a jour plot radar en secondes
double refreshTime = 4;
// gamespeed : 1000 ms
int gameSpeed = 1000;
int timer = 0;
double canvas_width = 924;
double canvas_height = 734;
static double cw = 924;
static double ch = 734;
static double scale = scale_ini;
static double scale_saved;
static double cw_saved;
static double ch_saved;
typedef struct {
char wp_nom[6];
double wp_x;
double wp_y;
GooCanvasItem *wp_cercle;
GooCanvasItem *wp_label;
} s_waypoint;
s_waypoint *waypoints;
static int nb_wp;
void import_wp();
void affiche_wp();
typedef struct {
double x;
double y;
GooCanvasItem *plot_comete;
} s_comete;
typedef struct {
double x;
double y;
} s_coord;
typedef struct
{
char callsign[8];
char actype[5];
char departure[5];
char arrival[5];
double rfl;
double pfl;
double xfl;
double afl;
double cfl;
// tableau de x chaines dont la longueur fait 5 caractères (x étant inconnu au départ) : char (*route)[6]
s_waypoint *route;
double x;
double y;
double tx;
double ty;
double dtaBeforeX;
double dtaBeforeY;
double dtaAfterX;
double dtaAfterY;
double inclinaisonStd;
int currentSegment;
double turnDirection;
_Bool turn;
_Bool onHdg;
char *hdg;
double heading;
double targetHdg;
double groundSpd;
s_comete comete[25];
GooCanvasItem *plot;
GooCanvasItem *label;
GooCanvasItem *label_callsign;
GooCanvasItem *connection;
int handler_id;
double labelx;
double labely;
gdouble drag_x;
gdouble drag_y;
} s_plane;
static s_plane fleet[30];
s_coord coord;
double degToRad(double angleDegre);
double radToDeg(double angleRad);
double normeVecteur(double x, double y);
double angleEntreRoute(double xA, double yA, double xB, double yB, double xC, double yC);
double trueRoute(double xA, double yA, double xB, double yB);
void draw_background();
void draw_plane(s_plane *plane);
void canvas_connect_item(s_plane *plane);
static gboolean on_button_press_event_cb (GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, gpointer user_data);
static gboolean on_button_release_event_cb (GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, gpointer user_data);
static gboolean on_motion_notify_event_cb (GooCanvasItem *item, GooCanvasItem *target_item, GdkEventMotion *event, gpointer user_data);
void scaleup(GtkWidget *button, gpointer data);
void scaledown(GtkWidget *button, gpointer data);
void scaleinit(GtkWidget *button, gpointer data);
void scalesave(GtkWidget *button, gpointer data);
void scalerestore(GtkWidget *button, gpointer data);
void stop_timer(GtkWidget *button, gpointer data);
void start_timer(GtkWidget *button, gpointer data);
void speed_plus(GtkWidget *button, gpointer data);
void speed_plus(GtkWidget *button, gpointer data);
void checkSpeed(s_plane *plane);
double rayon_virage();
double dta(s_plane *plane);
void dtaCoord(s_plane *plane);
void checkTurn(s_plane *plane);
_Bool doroute();
/* -------------------------------------------------------
import waypoints
-------------------------------------------------------*/
void import_wp() {
xmlDoc *doc = xmlReadFile("basic.xml", NULL, 0);
if (doc == NULL)
{
g_print("Le fichier xml n'existe pas.\n");
} else
{
xmlNode *xmlroot = xmlDocGetRootElement(doc)->children;
xmlNode *current = NULL;
int i = 0;
for (current=xmlroot; current != NULL; current=current->next) {
if (strcmp((char*)current->name, "targetpoint") == 0)
{
nb_wp++;
}
}
waypoints = malloc(nb_wp*sizeof(s_waypoint));
for (current=xmlroot; current != NULL; current=current->next) {
if (strcmp((char*)current->name, "targetpoint") == 0) {
strcpy(waypoints[i].wp_nom, (const char*) xmlGetProp(current, (const xmlChar*)"label"));
char *tmpx= (char *)xmlGetProp(current, (const xmlChar*)"x");
if (tmpx != NULL) {
waypoints[i].wp_x = atoi(tmpx);
free(tmpx);
} else {
waypoints[i].wp_x = 0;
}
char *tmpy= (char *)xmlGetProp(current, (const xmlChar*)"y");
if (tmpy != NULL) {
waypoints[i].wp_y = atoi(tmpy);
free(tmpy);
} else {
waypoints[i].wp_y = 0;
}
i++;
}
}
g_print("nb_wp: %d", nb_wp);
for(i=0; i<nb_wp; i++) {
waypoints[i].wp_cercle = goo_canvas_ellipse_new (root, waypoints[i].wp_x, waypoints[i].wp_y, 1.0, 1.0,
"stroke-color", "red",
"line-width", 1.0,
"fill-color", "red",
NULL);
double lx = waypoints[i].wp_x + 10.0;
double ly = waypoints[i].wp_y + 10.0;
waypoints[i].wp_label = goo_canvas_text_new (root, waypoints[i].wp_nom, lx, ly, 50.0, GOO_CANVAS_ANCHOR_SW,
"fill-color", "white", "font", "Sans 10px",
NULL);
}
xmlFreeDoc(doc);
xmlCleanupParser();
}
}
/* -------------------------------------------------------
fonctions utilitaires
-------------------------------------------------------*/
double degToRad(double angleDegre) {
return angleDegre*M_PI/180;
}
double radToDeg(double angleRad) {
return angleRad*180/M_PI;
}
double normeVecteur(double x, double y) {
return sqrt(x*x+y*y);
}
// angle entre 2 routes (en Rad)
double angleEntreRoute(double xA, double yA, double xB, double yB, double xC, double yC) {
double curHdg, nextHdg, diff;
curHdg = trueRoute(xA, yA, xB, yB);
nextHdg = trueRoute(xB, yB, xC, yC);
diff = nextHdg - curHdg;
if (diff < 0 && fabs(diff) > M_PI) diff = diff + 2*M_PI;
if (diff > M_PI) diff = diff - M_PI*2;
return fmod(diff, 2*M_PI);
}
// calcul route vraie d'une route en Rad
double trueRoute(double xA, double yA, double xB, double yB) {
double res;
res = atan((xB-xA)/(yB-yA));
if ((xB > xA) && (yB < yA)) { res = fabs(res); }
if ((xB > xA) && (yB > yA)) { res = M_PI - res; }
if ((xB < xA) && (yB < yA)) { res = 2*M_PI - res; }
if ((xB < xA) && (yB > yA)) { res = fabs(res) + M_PI; }
//g_print("true route: %f deg \n", radToDeg(res));
return res;
}
/* --------------------------------------------
Drag Label
-------------------------------------------- */
GooCanvasItem *drag_item = NULL;
gdouble drag_x = 0.0;
gdouble drag_y = 0.0;
gdouble item_x = 0.0;
gdouble item_y = 0.0;
static gboolean on_button_press_event_cb (GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, gpointer data) {
g_print("Clicked \n");
if (event->button == 1) {
drag_item = item;
drag_x = event->x;
drag_y = event->y;
g_object_get (item, "x", &item_x, "y", &item_y, NULL);
goo_canvas_pointer_grab (GOO_CANVAS (canvas), item, GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_RELEASE_MASK, NULL, event->time);
return TRUE;
}
return FALSE;
}
static gboolean on_button_release_event_cb (GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, gpointer data) {
if (drag_item == item && drag_item != NULL)
{
goo_canvas_pointer_ungrab (GOO_CANVAS (canvas), drag_item, event->time);
drag_item = NULL;
return TRUE;
}
return FALSE;
}
static gboolean on_motion_notify_event_cb (GooCanvasItem *item, GooCanvasItem *target_item, GdkEventMotion *event, gpointer plane) {
if (drag_item == item && drag_item != NULL)
{
gdouble rel_x = event->x - drag_x;
gdouble rel_y = event->y - drag_y;
g_object_set (item, "x", item_x + rel_x, "y", item_y + rel_y, NULL);
canvas_connect_item(plane);
return TRUE;
}
return FALSE;
}
/* -------------------------------------------------------
plane
-------------------------------------------------------*/
void plane_constructor(s_plane *plane, int nb_fixes, ...) {
/* ----construction route --- nb paramètres inconnus = nb_fixes = 5 ici ----*/
(*plane).route = malloc(nb_fixes*sizeof(*(*plane).route));
int i=0, j=0;
va_list ap;
va_start(ap, nb_fixes);
for( i=0 ; i<nb_fixes ; i++) {
strcpy((*plane).route[i].wp_nom, va_arg(ap, char *));
for (j=0; j<nb_wp; j++) {
if (strcmp((*plane).route[i].wp_nom, waypoints[j].wp_nom) == 0) {
(*plane).route[i].wp_x = waypoints[j].wp_x;
(*plane).route[i].wp_y = waypoints[j].wp_y;
(*plane).route[i].wp_cercle = NULL;
(*plane).route[i].wp_label = NULL;
break;
}
}
}
va_end(ap);
(*plane).x = (*plane).route[0].wp_x;
(*plane).y = (*plane).route[0].wp_y;
(*plane).inclinaisonStd = 30.0;
(*plane).currentSegment = 0;
(*plane).turnDirection = 1;
(*plane).turn = FALSE;
(*plane).onHdg = FALSE;
(*plane).hdg = (*plane).route[0].wp_nom;
(*plane).heading = trueRoute((*plane).x, (*plane).y, (*plane).route[1].wp_x, (*plane).route[1].wp_y);
(*plane).plot = goo_canvas_ellipse_new (root, (*plane).x, (*plane).y, 3.0, 3.0,
"stroke-color", "white",
"line-width", 2.0,
"fill-color", "white",
NULL);
for(i=0; i<25; i++) {
(*plane).comete[i].x = (*plane).route[0].wp_x;
(*plane).comete[i].y = (*plane).route[0].wp_y;
//g_print("comete %d: x:%f y:%f \n", i, (*plane).comete[i].x, (*plane).comete[i].y);
}
(*plane).comete[3].plot_comete = goo_canvas_ellipse_new (root, (*plane).x, (*plane).y, 1.0, 1.0,
"stroke-color", "white",
"line-width", 1.0,
"fill-color", "white",
NULL);
(*plane).comete[6].plot_comete = goo_canvas_ellipse_new (root, (*plane).x, (*plane).y, 1.0, 1.0,
"stroke-color", "white",
"line-width", 1.0,
"fill-color", "white",
NULL);
(*plane).comete[9].plot_comete = goo_canvas_ellipse_new (root, (*plane).x, (*plane).y, 1.0, 1.0,
"stroke-color", "white",
"line-width", 1.0,
"fill-color", "white",
NULL);
(*plane).comete[12].plot_comete = goo_canvas_ellipse_new (root, (*plane).x, (*plane).y, 1.0, 1.0,
"stroke-color", "white",
"line-width", 1.0,
"fill-color", "white",
NULL);
(*plane).comete[15].plot_comete = goo_canvas_ellipse_new (root, (*plane).x, (*plane).y, 1.0, 1.0,
"stroke-color", "white",
"line-width", 1.0,
"fill-color", "white",
NULL);
(*plane).drag_x = 0.0;
(*plane).drag_y = 0.0;
(*plane).labelx = (*plane).x + 20.0;
(*plane).labely = (*plane).y + 20.0;
plane->label = goo_canvas_group_new(root, NULL);
(*plane).label_callsign = goo_canvas_text_new (plane->label, (*plane).callsign, (*plane).labelx, (*plane).labely, 100.0, GOO_CANVAS_ANCHOR_SW,
"fill-color", "white", "font", "Arial 12px",
NULL);
coord.x = (*plane).x;
coord.y = (*plane).y;
g_signal_connect (G_OBJECT ((*plane).label), "button-press-event", G_CALLBACK (on_button_press_event_cb), NULL);
g_signal_connect (G_OBJECT ((*plane).label), "button-release-event", G_CALLBACK (on_button_release_event_cb), NULL);
(*plane).handler_id = g_signal_connect (G_OBJECT ((*plane).label), "motion-notify-event", G_CALLBACK (on_motion_notify_event_cb), plane);
GooCanvasBounds label_bounds;
goo_canvas_item_get_bounds((*plane).label, &label_bounds);
(*plane).connection = goo_canvas_polyline_new_line(root, (*plane).x, (*plane).y, label_bounds.x1, label_bounds.y1,
"line-width", 1.0, "stroke-color", "white", NULL);
}
void checkSpeed(s_plane *plane) {
(*plane).groundSpd = 480;
g_print("Speed: %f \n", (*plane).groundSpd);
}
// Rayon de virage en Nm : ne dépend que de la Vp et l'angle d' bank angle
double rayon_virage(s_plane *plane) {
// Turn (in the formula below, Vp in m/s)
double VpInMeterPerSecond = (*plane).groundSpd * 1852 / 3600;
// inclination angle in rad
double inclinaisonStdInRad = degToRad((*plane).inclinaisonStd);
return VpInMeterPerSecond * VpInMeterPerSecond / (tan(inclinaisonStdInRad) * 9.81 * 1852);
}
// Distance to Turn Anticipation en Nm, si angle est faible (<20°) dta = 2Nm
double dta(s_plane *plane) {
return 2.0;
}
// coordonnées du prochain dta
void dtaCoord(s_plane *plane) {
// on attend la fin du virage
if ((*plane).turn == FALSE && (strcmp((*plane).route[(*plane).currentSegment+2].wp_nom, "") != 0 )) {
double dtaNm = dta(plane);
double dtapx = dtaNm*plane_ratio;
double xA = (*plane).x;
double yA = (*plane).y;
double xB = (*plane).route[(*plane).currentSegment+1].wp_x;
double yB = (*plane).route[(*plane).currentSegment+1].wp_y;
double xC = (*plane).route[(*plane).currentSegment+2].wp_x;
double yC = (*plane).route[(*plane).currentSegment+2].wp_y;
double trueRouteRad, deltax, deltay;
trueRouteRad = trueRoute(xA, yA, xB, yB);
deltax = -dtapx*sin(trueRouteRad);
deltay = -dtapx*cos(trueRouteRad);
if (yB > yA) { if (deltay>0) deltay = -deltay; }
if (yB < yA) { if (deltay<0) deltay = -deltay; }
if (xB > xA) { if (deltax>0) deltax = -deltax; }
if (xB < xA) { if (deltax<0) deltax = -deltax; }
(*plane).dtaBeforeX = (xB + deltax);
(*plane).dtaBeforeY = (yB + deltay);
trueRouteRad = trueRoute(xB, yB, xC, yC);
deltax = -dtapx*sin(trueRouteRad);
deltay = -dtapx*cos(trueRouteRad);
if (yC > yB) { if (deltay<0) deltay = -deltay; }
if (yC < yB) { if (deltay>0) deltay = -deltay; }
if (xC > xB) { if (deltax<0) deltax = -deltax; }
if (xC < xB) { if (deltax>0) deltax = -deltax; }
(*plane).dtaAfterX = (xB + deltax);
(*plane).dtaAfterY = (yB + deltay);
//g_print("dtaBx: %f dtaBy: %f dtaAx: %f dtaAy: %f \n", (*plane).dtaBeforeX, (*plane).dtaBeforeY, (*plane).dtaAfterX, (*plane).dtaAfterY);
}
}
void checkTurn(s_plane *plane) {
dtaCoord(plane);
// Début du virage : dta atteint, on tourne
if ((fabs((*plane).x - (*plane).dtaBeforeX) < 3.0) && (fabs((*plane).y - (*plane).dtaBeforeY) < 3.0) && (*plane).onHdg == FALSE) {
if ((*plane).turn == FALSE) {
(*plane).targetHdg = trueRoute((*plane).route[(*plane).currentSegment+1].wp_x, (*plane).route[(*plane).currentSegment+1].wp_y, (*plane).route[(*plane).currentSegment+2].wp_x, (*plane).route[(*plane).currentSegment+2].wp_y);
(*plane).currentSegment += 1;
(*plane).hdg = (*plane).route[(*plane).currentSegment+1].wp_nom;
}
(*plane).turn = TRUE;
}
// fin du virage
// si retour sur la route standard
if ((fabs((*plane).x - (*plane).dtaAfterX) < 4.0) && (fabs((*plane).y - (*plane).dtaAfterY) < 4.0)) {
(*plane).turn = FALSE;
(*plane).heading = trueRoute((*plane).x, (*plane).y, (*plane).route[(*plane).currentSegment+1].wp_x, (*plane).route[(*plane).currentSegment+1].wp_y);
} else {
// si fin de virage pour joindre le cap de la directe
if (fabs(radToDeg((*plane).heading) - radToDeg((*plane).targetHdg)) < 5.0) {
if ((*plane).turn == TRUE) {
(*plane).turn = FALSE;
if ((*plane).onHdg == FALSE) {
(*plane).heading = trueRoute((*plane).x, (*plane).y, (*plane).route[(*plane).currentSegment+1].wp_x, (*plane).route[(*plane).currentSegment+1].wp_y);
} else {
(*plane).heading = (*plane).targetHdg;
}
}
}
}
}
_Bool doroute(s_plane *plane) {
/* C'est là où je suis obligé de déconnecter le signal pour le reconnecter sinon plane->x reprend sa valeur ini quand je drag le plane->label */
if (plane->handler_id > 0) { g_signal_handler_disconnect(plane->label, plane->handler_id); }
plane->handler_id = g_signal_connect (G_OBJECT (plane->label), "motion-notify-event", G_CALLBACK (on_motion_notify_event_cb), plane);
checkSpeed(plane);
dtaCoord(plane);
checkTurn(plane);
double dist;
dist = (*plane).groundSpd*refreshTime/3600;
if ((*plane).turn == TRUE) {
double beta = dist/rayon_virage(plane);
(*plane).heading = fmod((*plane).heading + (*plane).turnDirection*beta, M_PI*2);
if (radToDeg((*plane).heading) < 0) (*plane).heading += 2*M_PI;
}
(*plane).tx = dist*sin(M_PI-(*plane).heading)*plane_ratio;
(*plane).ty = dist*cos(M_PI-(*plane).heading)*plane_ratio;
(*plane).x += (*plane).tx;
(*plane).y += (*plane).ty;
g_print("heading: %f deg x: %f y: %f\n", radToDeg((*plane).heading), (*plane).x, (*plane).y);
int i;
for(i=24;i>=1;i--) {
(*plane).comete[i].x = (*plane).comete[i-1].x;
(*plane).comete[i].y = (*plane).comete[i-1].y;
}
(*plane).comete[0].x = (*plane).x;
(*plane).comete[0].y = (*plane).y;
draw_plane(plane);
return TRUE;
}
/* -------------------------------------------------------
boutons
-------------------------------------------------------*/
void show_wp() {
int i;
for(i=0; i<nb_wp; i++) {
g_object_set(waypoints[i].wp_label, "visibility", GOO_CANVAS_ITEM_VISIBLE, NULL);
}
};
void hide_wp() {
int i;
for(i=0; i<nb_wp; i++) {
g_object_set(waypoints[i].wp_label, "visibility", GOO_CANVAS_ITEM_INVISIBLE, NULL);
}
};
void scaleup(GtkWidget *button, gpointer data) {
scale *= 1.1;
cw *= 1.1;
ch *= 1.1;
double sx, sy;
sx = (canvas_width - cw)/2;
sy = (canvas_height - ch)/2;
goo_canvas_item_set_simple_transform(root, sx, sy, scale, 0);
}
void scaledown(GtkWidget *button, gpointer data) {
double sc = scale / 1.1;
if (sc < 0.9) {
} else {
scale /= 1.1;
cw /= 1.1;
ch /= 1.1;
double sx, sy;
sx = (canvas_width - cw)/2;
sy = (canvas_height - ch)/2;
goo_canvas_item_set_simple_transform(root, sx, sy, scale, 0);
}
}
void scaleinit(GtkWidget *button, gpointer data) {
scale = 1.0;
cw = canvas_width;
ch = canvas_height;
goo_canvas_item_set_simple_transform(root, 0, 0, scale, 0);
}
void scalesave(GtkWidget *button, gpointer data) {
scale_saved = scale;
cw_saved = cw;
ch_saved = ch;
}
void scalerestore(GtkWidget *button, gpointer data) {
scale = scale_saved;
cw = cw_saved;
ch = ch_saved;
double sx, sy;
sx = (canvas_width - cw)/2;
sy = (canvas_height - ch)/2;
goo_canvas_item_set_simple_transform(root, sx, sy, scale, 0);
}
void stop_timer(GtkWidget *button, gpointer data) {
if (timer > 0) g_source_remove(timer);
timer = 0;
}
void start_timer(GtkWidget *button, gpointer data) {
if (timer == 0) {
timer = g_timeout_add(gameSpeed, (GSourceFunc) doroute, (gpointer) &fleet[0]);
} else {
g_print("Un timer est deja en route");
}
g_print("timer id: %d", timer);
}
void speed_moins(GtkWidget *button, gpointer data) {
if (gameSpeed < 500) {
gameSpeed += 100;
} else {
gameSpeed += 500;
}
stop_timer(NULL, NULL);
start_timer(NULL, NULL);
}
void speed_plus(GtkWidget *button, gpointer data) {
if (gameSpeed > 100) {
if (gameSpeed < 600) gameSpeed -= 100;
if (gameSpeed > 900) gameSpeed -= 500;
}
stop_timer(NULL, NULL);
start_timer(NULL, NULL);
}
/* -------------------------------------------------------
canvas
-------------------------------------------------------*/
void draw_background(){
GooCanvasLineDash *dash;
GooCanvasItem *secto, *sectoadj;
GooCanvasItem *path1, *path2, *path3, *path4, *path5, *path6, *path7, *path8, *path9;
//GooCanvasItem *rect_item, *text_item;
GooCanvasItem *root;
root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
dash = goo_canvas_line_dash_new (2, 8.0, 4.0);
secto = goo_canvas_path_new (root, "M 234,325 l 250,-189 12,13 -7,9 -3,10 1,8 4,8 6,7 6,1 33,-8 60,69 49,85 37,43 -124,150 -16,37 -76,-37 -223,-1 z", "stroke-color", "#000000", "fill-color", "#777", "line-dash", dash, "line-width", 1.0, NULL);
sectoadj = goo_canvas_path_new (root, "M 234,325 0,248 M 243,530 231,735 M 467,532 496,677 541,734 M 655,0 839,128 925,298 M 633,317 839,129 M 501,0 484,89 484,136 M 475,0 214,88 0,204 M 214,88 177,1 M 683,382 699,396 734,507 781,511 785,535 873,601 925,617 M 873,601 725,735", "line-dash", dash, "line-width", 1.0, NULL);
// DIRMO - CFA - MEN - ETORI
path1 = goo_canvas_path_new (root, "M 29,0 174,246 179,471 159,734", "stroke-color", "#B0B0B0", "line-dash", 0, "line-width", 1.0, NULL);
// NEV - CFA - MTL - JAMBI
path2 = goo_canvas_path_new (root, "M 131,0 174,246 398,470 862,734");
// DIRMO - GAI
path3 = goo_canvas_path_new (root, "M 36,0 2,600 361,736");
// GAI - MEN - LSE - PAS
path4 = goo_canvas_path_new (root, "M 720,1 426,238 175,477 0,599");
// GAI - JUVEN
path5 = goo_canvas_path_new (root, "M 0,599 924,298");
// BOJOL - LSE - LTP - GRENA - SANTO - JAMBI
path6 = goo_canvas_path_new (root, "M 286,0 426,241 473,288 746,734");
// SANTO - SAMOS
path7 = goo_canvas_path_new (root, "M 613,518 924,645");
// SEVET - JUVEN
path8 = goo_canvas_path_new (root, "M 565,0 926,571");
// MAJOR - MTL - LTP - SEVET
path9 = goo_canvas_path_new (root, "M 414,735 398,470 473,288 834,0");
}
void draw_plane(s_plane *plane) {
goo_canvas_item_translate((*plane).plot, (*plane).tx, (*plane).ty);
goo_canvas_item_translate((*plane).label, (*plane).tx, (*plane).ty);
canvas_connect_item(plane);
goo_canvas_item_translate((*plane).comete[3].plot_comete, (*plane).comete[2].x-(*plane).comete[3].x, (*plane).comete[2].y-(*plane).comete[3].y);
goo_canvas_item_translate((*plane).comete[6].plot_comete, (*plane).comete[5].x-(*plane).comete[6].x, (*plane).comete[5].y-(*plane).comete[6].y);
goo_canvas_item_translate((*plane).comete[9].plot_comete, (*plane).comete[8].x-(*plane).comete[9].x, (*plane).comete[8].y-(*plane).comete[9].y);
goo_canvas_item_translate((*plane).comete[12].plot_comete, (*plane).comete[11].x-(*plane).comete[12].x, (*plane).comete[11].y-(*plane).comete[12].y);
goo_canvas_item_translate((*plane).comete[15].plot_comete, (*plane).comete[14].x-(*plane).comete[15].x, (*plane).comete[14].y-(*plane).comete[15].y);
}
void canvas_connect_item(s_plane *plane) {
goo_canvas_item_remove((*plane).connection);
GooCanvasBounds label_bounds;
goo_canvas_item_get_bounds((*plane).label, &label_bounds);
double tab[4];
double cx, cy;
tab[0] = normeVecteur(label_bounds.x1-(*plane).x, label_bounds.y1-(*plane).y);
tab[1] = normeVecteur(label_bounds.x2-(*plane).x, label_bounds.y1-(*plane).y);
tab[2] = normeVecteur(label_bounds.x2-(*plane).x, label_bounds.y2-(*plane).y);
tab[3] = normeVecteur(label_bounds.x1-(*plane).x, label_bounds.y2-(*plane).y);
int i, index_min, mint;
index_min = 0;
mint=tab[0];
for(i=1;i<4;i++) {
if (tab[i]<mint) {
mint=tab[i];
index_min = i;
}
}
//g_print("resultat: %d \n\n", index_min);
switch (index_min) {
case 0:
cx = label_bounds.x1;
cy = label_bounds.y1;
break;
case 1:
cx = label_bounds.x2;
cy = label_bounds.y1;
break;
case 2:
cx = label_bounds.x2;
cy = label_bounds.y2;
break;
case 3:
cx = label_bounds.x1;
cy = label_bounds.y2;
break;
}
(*plane).connection = goo_canvas_polyline_new_line(root, (*plane).x, (*plane).y, cx, cy,
"line-width", 1.0, "stroke-color", "white", NULL);
}
/* -------------------------------------------------------
GTK Main
-------------------------------------------------------*/
int
main (int argc, char *argv[])
{
GtkWidget *grid, *main_grid, *bottom_grid;
GtkWidget *b_scale_up, *b_scale_down, *b_scale_init, *b_scale_save, *b_scale_restore;
GtkWidget *b_hide_wp, *b_show_wp;
GtkWidget *b_stop_timer, *b_start_timer;
GtkWidget *b_speed_plus, *b_speed_moins;
/*-- CSS ------------------*/
GtkCssProvider *provider;
GdkDisplay *display;
GdkScreen *screen;
/*---------------------------*/
/* Initialize GTK+. */
gtk_init (&argc, &argv);
/* Create the window and widgets. */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 1200, 760);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
canvas = goo_canvas_new ();
gtk_widget_set_size_request (canvas, canvas_width, canvas_height);
goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, canvas_width*2, canvas_height*2);
g_object_set(G_OBJECT(canvas),"background-color","#999",NULL);
root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
b_scale_down = gtk_button_new_with_label("Scale -");
g_signal_connect(G_OBJECT(b_scale_down), "clicked", G_CALLBACK (scaledown), NULL);
b_scale_up = gtk_button_new_with_label("Scale +");
g_signal_connect(G_OBJECT(b_scale_up), "clicked", G_CALLBACK (scaleup), NULL);
b_scale_init = gtk_button_new_with_label("Scale Init");
g_signal_connect(G_OBJECT(b_scale_init), "clicked", G_CALLBACK (scaleinit), NULL);
b_scale_save = gtk_button_new_with_label("Scale Save");
g_signal_connect(G_OBJECT(b_scale_save), "clicked", G_CALLBACK (scalesave), NULL);
b_scale_restore = gtk_button_new_with_label("Scale Restore");
g_signal_connect(G_OBJECT(b_scale_restore), "clicked", G_CALLBACK (scalerestore), NULL);
b_hide_wp = gtk_button_new_with_label("Hide wp");
g_signal_connect(G_OBJECT(b_hide_wp), "clicked", G_CALLBACK (hide_wp), NULL);
b_show_wp = gtk_button_new_with_label("Show wp");
g_signal_connect(G_OBJECT(b_show_wp), "clicked", G_CALLBACK (show_wp), NULL);
b_stop_timer = gtk_button_new_with_label("Stop Timer");
g_signal_connect(G_OBJECT(b_stop_timer), "clicked", G_CALLBACK (stop_timer), NULL);
b_start_timer = gtk_button_new_with_label("Start Timer");
g_signal_connect(G_OBJECT(b_start_timer), "clicked", G_CALLBACK (start_timer), NULL);
b_speed_plus = gtk_button_new_with_label("Speed Plus");
g_signal_connect(G_OBJECT(b_speed_plus), "clicked", G_CALLBACK (speed_plus), NULL);
b_speed_moins = gtk_button_new_with_label("Speed Moins");
g_signal_connect(G_OBJECT(b_speed_moins), "clicked", G_CALLBACK (speed_moins), NULL);
grid = gtk_grid_new();
main_grid = gtk_grid_new();
bottom_grid = gtk_grid_new();
gtk_grid_attach(GTK_GRID (main_grid), canvas, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_scale_down, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_scale_up, 1, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_scale_init, 2, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_scale_save, 3, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_scale_restore, 4, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_hide_wp, 5, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_show_wp, 6, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_stop_timer, 7, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_start_timer, 8, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_speed_plus, 9, 0, 1, 1);
gtk_grid_attach(GTK_GRID (bottom_grid), b_speed_moins, 10, 0, 1, 1);
gtk_grid_attach(GTK_GRID (grid), main_grid, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID (grid), bottom_grid, 0, 1, 1, 1);
gtk_container_add (GTK_CONTAINER (window), grid);
/*---------------- CSS ----------------------------------------------------------------------------------------------------*/
provider = gtk_css_provider_new ();
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gsize bytes_written, bytes_read;
const gchar* home = "atc.css"; // E:/c++/atc/bin/Debug/ your path, for instance: /home/zerohour/Documents/programming/cssexternal.css
GError *error = 0;
gtk_css_provider_load_from_path (provider, g_filename_to_utf8(home, strlen(home), &bytes_read, &bytes_written, &error), NULL);
g_object_unref (provider);
/*-------------------------------------------------------------------------------------------------------------------------*/
draw_background();
import_wp();
static s_plane avion1 = { "SWR422", "B777", "LSGG", "SKBO", 360, 360, 360, 360, 360};
plane_constructor(&avion1, 5, "MTL2", "MTL", "GRENA", "LTP", "RAPID");
fleet[0] = avion1;
gtk_widget_show_all(window);
gtk_main ();
return 0;
} |
Partager