//
// Created by hugod on 27/11/2024.
//
#include "Affichage.h"
#include "simulation1.h"
cJSON * creerJson(char * nomJson){
// Charger et analyser le fichier JSON
char *contenu_json = lire_fichier(nomJson);
cJSON *json = cJSON_Parse(contenu_json);
free(contenu_json);
if (!json) {
fprintf(stderr, "Erreur : Impossible de parser le fichier JSON.\n");
}
return json;
}
// Vérifie si la souris est dans une zone
static inline bool is_mouse_in_zone(ButtonZone zone, int x, int y) {
return x >= zone.x1 && x <= zone.x2 && y >= zone.y1 && y <= zone.y2;
}
// Fonction pour charger une image et vérifier son succès
static inline ALLEGRO_BITMAP* load_bitmap_or_exit(const char* path) {
ALLEGRO_BITMAP* bmp = al_load_bitmap(path);
if (!bmp) {
fprintf(stderr, "Erreur : Impossible de charger l'image %s.\n", path);
exit(EXIT_FAILURE);
}
return bmp;
}
int interface_graphique(nom_fichier_extension * nomFichierExtension) {
toutAllegro toutAllegro1;
initAllegro(&toutAllegro1);
ButtonZone buttons[] = {
{542, 374, 860, 470}, // "Affichage"
{548, 570, 850, 665}, // "Simulation"
{545, 790, 850, 881} // "Quitter"
};
ALLEGRO_BITMAP* current_menu = toutAllegro1.tabImage[0];
int mouse_x;
int mouse_y;
bool running = true;
al_draw_bitmap(current_menu, 0, 0, 0);
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);
while (running) {
ALLEGRO_EVENT event;
al_wait_for_event(toutAllegro1.eventQueue, &event);
switch (event.type) {
case ALLEGRO_EVENT_DISPLAY_CLOSE:
running = false;
break;
case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
mouse_x = event.mouse.x;
mouse_y = event.mouse.y;
for (int i = 0; i < NB_IMAGE_menu; ++i) {
if (is_mouse_in_zone(buttons[i], mouse_x, mouse_y)) {
if (i == 0) {
convertion_json_mmd(nomFichierExtension, &toutAllegro1); /// cree un fichier .mmd en fonction du reseau demande puis renvoi son chemin d'acces,
/// et modifie la varibale nom_fichier_json pour donner le chemin du .json selectionner par l'utilisateur
cJSON *json = creerJson(nomFichierExtension->nom_fichier_json);
Graphe *graphe = creerGrapheAvecEspeces(json);
affichage(&toutAllegro1, nomFichierExtension, json, graphe);
cJSON_Delete(json);
current_menu = toutAllegro1.tabImage[0];
}
else if (i == 1) {
convertion_json_mmd(nomFichierExtension, &toutAllegro1);
/// passser tbleau d espece en para. et contenu json pour eviter de le lire trop de fois, voir inclusion car toutAllegro et nom_fichier_extension ne sont pas reconnue dans le simulation1.h
cJSON *json = creerJson(nomFichierExtension->nom_fichier_json);
Graphe *graphe = creerGrapheAvecEspeces(json);
saisirPopulation(&toutAllegro1, nomFichierExtension, json, graphe);
simulationPopulation(&toutAllegro1, nomFichierExtension, graphe, json);
cJSON_Delete(json);
}
else if (i == 2) {
running = false;
}
}
}
break;
case ALLEGRO_EVENT_MOUSE_AXES:
mouse_x = event.mouse.x;
mouse_y = event.mouse.y;
for (int i = 0; i < NB_IMAGE_menu; ++i) {
if (is_mouse_in_zone(buttons[i], mouse_x, mouse_y)) {
current_menu = toutAllegro1.tabImage[i + 1]; // Retour au menu après affichage
break;
}
else{
current_menu = toutAllegro1.tabImage[0];
}
}
break;
}
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_bitmap(current_menu, 0, 0, 0);
al_flip_display();
}
// Libération des ressources
destroyAllegro(&toutAllegro1);
return EXIT_SUCCESS;
}
void initAllegro(toutAllegro * toutAllegro1){
if (!al_init() || !al_init_image_addon() || !al_install_mouse() || !al_install_keyboard()) {
fprintf(stderr, "Erreur d'initialisation Allegro ou de ses modules.\n");
}
// Initialiser les addons de police
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
toutAllegro1->display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
if (!toutAllegro1->display) {
fprintf(stderr, "Erreur : Impossible de creer la fenêtre.\n");
}
al_set_window_title(toutAllegro1->display, "Trophic Networks");
for (int i = 0; i < NB_IMAGE; i++) {
toutAllegro1->tabImage[i] = NULL;
}
toutAllegro1->tabImage[menu] = load_bitmap_or_exit("DATA/PNG/Menu.png");
toutAllegro1->tabImage[afficher] = load_bitmap_or_exit("DATA/PNG/Menu1.png");
toutAllegro1->tabImage[simulation] = load_bitmap_or_exit("DATA/PNG/Menu2.png");
toutAllegro1->tabImage[quitter] = load_bitmap_or_exit("DATA/PNG/Menu3.png");
toutAllegro1->tabImage[chargement] = load_bitmap_or_exit("DATA/PNG/Chargement.png");
// Menu_Pause
toutAllegro1->tabImage[menu_pause0] = load_bitmap_or_exit("DATA/PNG/Menu_Pause/Menu_Pause0.png");
toutAllegro1->tabImage[menu_pause1] = load_bitmap_or_exit("DATA/PNG/Menu_Pause/Menu_Pause1.png");
toutAllegro1->tabImage[menu_pause2] = load_bitmap_or_exit("DATA/PNG/Menu_Pause/Menu_Pause2.png");
toutAllegro1->tabImage[menu_pause3] = al_load_bitmap("DATA/PNG/Menu_Pause/Menu_Pause3.png");
toutAllegro1->tabImage[menu_pause4] = al_load_bitmap("DATA/PNG/Menu_Pause/Menu_Pause4.png");
toutAllegro1->tabImage[menu_pause5] = al_load_bitmap("DATA/PNG/Menu_Pause/Menu_Pause5.png");
toutAllegro1->tabImage[menu_pause6] = al_load_bitmap("DATA/PNG/Menu_Pause/Menu_Pause6.png");
toutAllegro1->tabImage[menu_pause7] = al_load_bitmap("DATA/PNG/Menu_Pause/Menu_Pause7.png");
toutAllegro1->tabImage[menu_pause8] = al_load_bitmap("DATA/PNG/Menu_Pause/Menu_Pause8.png");
toutAllegro1->timer = al_create_timer(2);
// Charger les polices
toutAllegro1->font_mid = al_load_ttf_font("DATA/OTF/ClashGrotesk-Bold.otf", 55, 0);
toutAllegro1->font_large = al_load_ttf_font("DATA/OTF/ClashGrotesk-Bold.otf", 70, 0);
toutAllegro1->font_small = al_load_ttf_font("DATA/OTF/ClashGrotesk-Bold.otf", 30, 0);
toutAllegro1->font_rikiki = al_load_ttf_font("DATA/OTF/ClashGrotesk-Bold.otf", 20, 0);
if (!toutAllegro1->font_small || !toutAllegro1->font_large || !toutAllegro1->font_mid) {
fprintf(stderr, "Erreur : Impossible de charger les polices.\n");
}
toutAllegro1->eventQueue = al_create_event_queue();
al_register_event_source(toutAllegro1->eventQueue, al_get_display_event_source(toutAllegro1->display));
al_register_event_source(toutAllegro1->eventQueue, al_get_mouse_event_source());
al_register_event_source(toutAllegro1->eventQueue, al_get_keyboard_event_source());
al_register_event_source(toutAllegro1->eventQueue, al_get_timer_event_source(toutAllegro1->timer));
al_register_event_source(toutAllegro1->eventQueue, al_get_display_event_source(toutAllegro1->display));
};
void destroyAllegro(toutAllegro* toutAllegro1){
for (int i = 0; i < NB_IMAGE; i++) {
if (toutAllegro1->tabImage[i]){
al_destroy_bitmap(toutAllegro1->tabImage[i]);
}
}
if (toutAllegro1->font_small) al_destroy_font(toutAllegro1->font_small);
if (toutAllegro1->font_large) al_destroy_font(toutAllegro1->font_large);
if (toutAllegro1->font_mid) al_destroy_font(toutAllegro1->font_mid);
if (toutAllegro1->eventQueue) al_destroy_event_queue(toutAllegro1->eventQueue);
al_destroy_display(toutAllegro1->display);
al_destroy_timer(toutAllegro1->timer);
}