//
// Created by hugod on 27/11/2024.
//
#include "Affichage.h"
// Convertir une couleur hexadécimale en ALLEGRO_COLOR
ALLEGRO_COLOR hex_to_color(const char* hex) {
unsigned int r, g, b;
sscanf(hex, "#%02x%02x%02x", &r, &g, &b);
return al_map_rgb(r, g, b);
}
// Lire le contenu d'un fichier JSON
char* lire_fichier(const char* chemin) {
FILE* fichier = fopen(chemin, "r");
if (!fichier) {
perror("Erreur : fichier JSON introuvable");
return NULL;
}
fseek(fichier, 0, SEEK_END);
long taille = ftell(fichier);
rewind(fichier);
char* contenu = malloc(taille + 1);
fread(contenu, 1, taille, fichier);
contenu[taille] = '\0';
fclose(fichier);
return contenu;
}
// Afficher du texte avec retour à la ligne
void dessiner_texte_avec_retour(ALLEGRO_FONT* font, ALLEGRO_COLOR couleur, float x, float y, float largeur_max, const char* texte) {
while (*texte) {
char ligne[512] = {0};
int nb_chars = 0, dernier_espace = -1;
float largeur_ligne = 0;
// Trouver la ligne à afficher
while (texte[nb_chars] && largeur_ligne <= largeur_max) {
if (texte[nb_chars] == ' ') dernier_espace = nb_chars;
char temp = texte[nb_chars + 1];
((char*)texte)[nb_chars + 1] = '\0';
largeur_ligne = al_get_text_width(font, texte);
((char*)texte)[nb_chars + 1] = temp;
nb_chars++;
}
if (largeur_ligne > largeur_max && dernier_espace != -1)
nb_chars = dernier_espace;
strncpy(ligne, texte, nb_chars);
ligne[nb_chars] = '\0';
al_draw_text(font, couleur, x, y, 0, ligne);
texte += nb_chars + (texte[nb_chars] == ' ');
y += al_get_font_line_height(font);
}
}
// Vérifier si deux couleurs sont égales
int couleurs_egales(ALLEGRO_COLOR c1, ALLEGRO_COLOR c2) {
unsigned char r1, g1, b1, r2, g2, b2;
al_unmap_rgb(c1, &r1, &g1, &b1);
al_unmap_rgb(c2, &r2, &g2, &b2);
return (r1 == r2 && g1 == g2 && b1 == b2);
}
// Afficher les informations d'un nœud
void afficher_infos_noeud(toutAllegro * toutAllegro1, cJSON* node, ALLEGRO_COLOR couleur) {
if (!node) return;
al_draw_text(toutAllegro1->font_large, couleur, 1250, 300, 0, "Informations :");
al_draw_textf(toutAllegro1->font_small, couleur, 1250, 475, 0, "Nom : %s", cJSON_GetObjectItem(node, "name")->valuestring);
al_draw_textf(toutAllegro1->font_small, couleur, 1250, 525, 0, "Type : %s", cJSON_GetObjectItem(node, "type")->valuestring);
if(!strcmp(cJSON_GetObjectItem(node, "type")->valuestring,"Producteur primaire")){
al_draw_textf(toutAllegro1->font_small, couleur, 1250, 575, 0, "Population : %dkg", cJSON_GetObjectItem(node, "population")->valueint);
}
else
{
al_draw_textf(toutAllegro1->font_small, couleur, 1250, 575, 0, "Population : %d individus", cJSON_GetObjectItem(node, "population")->valueint);
} al_draw_text(toutAllegro1->font_small, couleur, 1250, 625, 0, "Description :");
dessiner_texte_avec_retour(toutAllegro1->font_small, couleur, 1250, 675, 400, cJSON_GetObjectItem(node, "description")->valuestring);
}
#include <allegro5/allegro.h>
#include <allegro5/allegro_color.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
typedef struct {
float x1, y1, x2, y2; // Coordonnées du bouton
int image_index; // Index de l'image associée
int action; // Action à effectuer lorsqu'on clique sur ce bouton
} Button;
int pause(toutAllegro *toutAllegro1, ALLEGRO_BITMAP *fond) {
bool pause_active = true;
ALLEGRO_COLOR pause_bg_color = al_map_rgba(0, 0, 0, 156);
// Liste des boutons
Button buttons[] = {
{745, 150, 1047, 247, menu_pause1, 0},
{745, 745, 1047, 842, menu_pause8, 7},
{570, 283, 873, 380, menu_pause2, 1},
{570, 438, 873, 536, menu_pause3, 2},
{570, 601, 873, 698, menu_pause4, 3},
{955, 283, 1258, 380, menu_pause5, 4},
{955, 438, 1258, 536, menu_pause6, 5},
{955, 601, 1258, 698, menu_pause7, 6}
};
size_t button_count = sizeof(buttons) / sizeof(buttons[0]);
// Affichage initial
al_draw_bitmap(fond, 0, 0, 0);
al_draw_filled_rectangle(0, 0, 1792, 1024, pause_bg_color);
al_draw_bitmap(toutAllegro1->tabImage[menu_pause0], 0, 0, 0);
al_flip_display();
while (pause_active) {
ALLEGRO_EVENT event;
al_wait_for_event(toutAllegro1->eventQueue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
pause_active = false;
break;
}
// Redessiner l'écran
al_draw_bitmap(fond, 0, 0, 0);
al_draw_filled_rectangle(0, 0, 1792, 1024, pause_bg_color);
al_draw_bitmap(toutAllegro1->tabImage[menu_pause0], 0, 0, 0);
if (event.type == ALLEGRO_EVENT_MOUSE_AXES || event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
float sourisX = event.mouse.x, sourisY = event.mouse.y;
bool hovered = false;
for (size_t i = 0; i < button_count; i++) {
Button btn = buttons[i];
// Vérifier si la souris est au-dessus d'un bouton
if (sourisX > btn.x1 && sourisX < btn.x2 && sourisY > btn.y1 && sourisY < btn.y2) {
al_draw_bitmap(toutAllegro1->tabImage[btn.image_index], 0, 0, 0);
hovered = true;
if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
pause_active = false;
return btn.action;
}
}
}
// Si aucun bouton n'est survolé
if (!hovered) {
al_draw_bitmap(toutAllegro1->tabImage[menu_pause0], 0, 0, 0);
}
}
al_flip_display();
}
return -1; // Retour par défaut si aucune action n'est effectuée
}
int affichage(toutAllegro * toutAllegro1, nom_fichier_extension * nomFichierExtension, cJSON * json, Graphe* graphe) {
// Charger les bitmaps
toutAllegro1->tabImage[fond_affichage] = al_load_bitmap("DATA/PNG/Menu_Affichage.png");
toutAllegro1->tabImage[graphe_total] = al_load_bitmap(nomFichierExtension->nom_fichier_png);
if (!toutAllegro1->tabImage[fond_affichage] || !toutAllegro1->tabImage[graphe_total]) {
fprintf(stderr, "Erreur : Impossible de charger les bitmaps.\n");
return -1;
}
cJSON *nodes = cJSON_GetObjectItem(json, "nodes");
cJSON *ecosystem = cJSON_GetObjectItem(json, "ecosystem");
cJSON *description = cJSON_GetObjectItem(json, "description");
cJSON *description1 = cJSON_GetObjectItem(json, "description1");
cJSON *dernier_node = NULL;
if (nodes == NULL || ecosystem == NULL) {
fprintf(stderr, "Erreur : Données JSON invalides.\n");
}
bool running = true;
int mouse_x;
int mouse_y;
int graph_x = 600 - al_get_bitmap_width(toutAllegro1->tabImage[graphe_total]) / 2;
int graph_y = 500 - al_get_bitmap_height(toutAllegro1->tabImage[graphe_total]) / 2;
ALLEGRO_COLOR couleur_sous_souris;
al_draw_bitmap(toutAllegro1->tabImage[fond_affichage], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], 600 - al_get_bitmap_width(toutAllegro1->tabImage[graphe_total]) / 2, 500 - al_get_bitmap_height(toutAllegro1->tabImage[graphe_total]) / 2, 0);
al_draw_textf(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1250, 50, 0, "%s", ecosystem->valuestring);
al_draw_textf(toutAllegro1->font_rikiki, COULEUR_PRIMAIRE, 1250, 150, 0, "%s", description->valuestring);
al_draw_textf(toutAllegro1->font_rikiki, COULEUR_PRIMAIRE, 1250, 170, 0, "%s", description1->valuestring);
afficher_infos_noeud(toutAllegro1, dernier_node, COULEUR_PRIMAIRE);
al_flip_display();
ALLEGRO_BITMAP *capture = al_create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);
if (!capture) {
fprintf(stderr, "Erreur : impossible de creer le bitmap de capture.\n");
return -1;
}
// Copier le contenu du backbuffer dans un bitmap lisible
ALLEGRO_BITMAP *backbuffer = al_get_backbuffer(toutAllegro1->display);
al_set_target_bitmap(capture);
al_draw_bitmap(backbuffer, 0, 0, 0);
al_set_target_backbuffer(toutAllegro1->display);
if (!al_save_bitmap("./IMG/vzrj.png", capture)) {
fprintf(stderr, "Erreur lors de la sauvegarde.\n");
}
while (running) {
ALLEGRO_EVENT event;
al_wait_for_event(toutAllegro1->eventQueue, &event);
switch (event.type) {
case ALLEGRO_EVENT_MOUSE_AXES:
mouse_x = event.mouse.x;
mouse_y = event.mouse.y;
/*al_copy_transform(&transform, al_get_current_transform());
al_transform_coordinates(&transform, &mouse_x, &mouse_y);
*/ ///test pour utiliser a n'importe quel echelle
// Récupérer la couleur du pixel sous la souris
if (mouse_x >= 0 && mouse_x < al_get_bitmap_width(capture) &&
mouse_y >= 0 && mouse_y < al_get_bitmap_height(capture)) {
couleur_sous_souris = al_get_pixel(toutAllegro1->tabImage[graphe_total], (int)mouse_x - graph_x, (int)mouse_y - graph_y);
// Traitez la couleur...
} else {
printf("Coordonnees en dehors des limites du bitmap.\n");
}
// Trouver le nœud correspondant
cJSON *node_selectionne = NULL;
cJSON_ArrayForEach(node_selectionne, nodes) {
const char *couleur_hex = cJSON_GetObjectItem(node_selectionne, "couleur")->valuestring;
if (couleurs_egales(couleur_sous_souris, hex_to_color(couleur_hex))) {
dernier_node = node_selectionne;
break;
}
}
break;
case ALLEGRO_EVENT_DISPLAY_CLOSE:
running = false;
break;
case ALLEGRO_EVENT_DISPLAY_RESIZE:
al_acknowledge_resize(toutAllegro1->display); // Important pour finaliser le redimensionnement
printf("Nouvelle taille : %d x %d\n", event.display.width, event.display.height);
break;
case ALLEGRO_EVENT_KEY_DOWN:
switch (event.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
backbuffer = al_get_backbuffer(toutAllegro1->display);
al_set_target_bitmap(capture);
al_draw_bitmap(backbuffer, 0, 0, 0);
al_set_target_backbuffer(toutAllegro1->display);
int sel = -1;
sel = pause(toutAllegro1, capture);
if (sel == 1)// Appel de la fonction pause
{
afficherGraphe(graphe);
}
else if (sel == 2)// Appel de la fonction pause
{
afficherAnalyseConnexite(graphe);
}
else if (sel == 3)// Appel de la fonction pause
{
RechercheProducteurPrimaire(graphe);
}
else if (sel == 4)// Appel de la fonction pause
{
afficherComplexiteReseau(graphe);
}
else if (sel == 5)// Appel de la fonction pause
{
AffichageSommetSpecifique(graphe);
AnalyseHauteurTrophique(graphe);
}
else if (sel == 6)// Appel de la fonction pause
{
RechercheSansPredateur(graphe);
}
else if (sel == 7)// Appel de la fonction pause
{
running=0;
}
break;
}
break;
}
// Redessiner l'écran
al_draw_bitmap(toutAllegro1->tabImage[fond_affichage], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], 600 - al_get_bitmap_width(toutAllegro1->tabImage[graphe_total]) / 2, 500 - al_get_bitmap_height(toutAllegro1->tabImage[graphe_total]) / 2, 0);
al_draw_textf(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1250, 50, 0, "%s", ecosystem->valuestring);
al_draw_textf(toutAllegro1->font_rikiki, COULEUR_PRIMAIRE, 1250, 150, 0, "%s", description->valuestring);
al_draw_textf(toutAllegro1->font_rikiki, COULEUR_PRIMAIRE, 1250, 170, 0, "%s", description1->valuestring);
afficher_infos_noeud(toutAllegro1, dernier_node, COULEUR_PRIMAIRE);
al_flip_display();
}
// Nettoyage
al_destroy_bitmap(capture);
return 0;
}