Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gtk/gtk.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/types.h>
- //----SEMAFORY----
- int clientReadySID, serverReadySID;
- struct sembuf clientReady;
- struct sembuf serverReady;
- //----STRUKTURY----
- struct prepareClient {
- long mtype;
- int CSQueueNumber;
- int SCQueueNumber;
- };
- struct clientStatus {
- long mtype;
- int surowce;
- int lekka_piechota;
- int ciezka_piechota;
- int jazda;
- int robotnicy;
- int punkty;
- };
- struct message {
- long mtype;
- int value;
- };
- //----AZER_STAF-----
- char playerNumber = 0;
- int client_server_QID;
- int server_client_QID;
- GtkWidget *progressBar;
- GtkWidget *waitingLabel;
- GtkWidget *lekkaPiechotaLabel;
- GtkWidget *ciezkaPiechotaLabel;
- GtkWidget *jazdaLabel;
- GtkWidget *robotnicyLabel;
- GtkWidget *surowceLabel;
- GtkWidget *punktyLabel;
- bool isInteger(const char *str) {
- if (!*str || (int)*str == 48)
- return false;
- while (*str)
- {
- if (!isdigit(*str))
- return false;
- else
- ++str;
- }
- return true;
- }
- void closeClient(GtkWidget *widget, gpointer data) {
- clientReady.sem_op = 1;
- semop(clientReadySID, &clientReady, 1);
- gtk_main_quit();
- }
- //================--OBSŁUGA-BUTTONÓW--====================
- void lekka_piechota_click(GtkWidget *widget, gpointer data) {
- GtkEntry* entry = (GtkEntry*)data;
- char *dane;
- dane = (char*)gtk_entry_get_text(entry);
- if(isInteger(dane)) {
- struct message recruit;
- int recruit_ID = msgget(client_server_QID, 0606 | IPC_CREAT);
- recruit.mtype = 3;
- recruit.value = atoi(dane);
- msgsnd(recruit_ID, &recruit, 20, 0);
- }
- }
- void ciezka_piechota_click(GtkWidget *widget, gpointer data) {
- GtkEntry* entry = (GtkEntry*)data;
- char *dane;
- dane = (char*)gtk_entry_get_text(entry);
- if(isInteger(dane)) {
- struct message recruit;
- int recruit_ID = msgget(client_server_QID, 0606 | IPC_CREAT);
- recruit.mtype = 4;
- recruit.value = atoi(dane);
- msgsnd(recruit_ID, &recruit, 20, 0);
- }
- }
- void jazda_click(GtkWidget *widget, gpointer data) {
- GtkEntry* entry = (GtkEntry*)data;
- char *dane;
- dane = (char*)gtk_entry_get_text(entry);
- if(isInteger(dane)) {
- struct message recruit;
- int recruit_ID = msgget(client_server_QID, 0606 | IPC_CREAT);
- recruit.mtype = 5;
- recruit.value = atoi(dane);
- msgsnd(recruit_ID, &recruit, 20, 0);
- }
- }
- void robotnicy_click(GtkWidget *widget, gpointer data) {
- GtkEntry* entry = (GtkEntry*)data;
- char *dane;
- dane = (char*)gtk_entry_get_text(entry);
- if(isInteger(dane)) {
- struct message recruit;
- int recruit_ID = msgget(client_server_QID, 0606 | IPC_CREAT);
- recruit.mtype = 6;
- recruit.value = atoi(dane);
- msgsnd(recruit_ID, &recruit, 20, 0);
- }
- }
- void walka_click(GtkWidget *widget, gpointer data) {
- }
- static gpointer serverIsReady(gpointer data) {
- struct prepareClient pC;
- int pC_ID = msgget(1000, 0606 | IPC_CREAT);
- msgrcv(pC_ID, &pC, 10, 0, 0);
- client_server_QID = pC.CSQueueNumber;
- server_client_QID = pC.SCQueueNumber;
- serverReady.sem_num = 0;
- serverReady.sem_flg = 0;
- serverReady.sem_op = -1;
- semop(serverReadySID, &serverReady, 1);
- GtkWidget* window = (GtkWidget*)data;
- gdk_threads_enter();
- gtk_widget_show_all(window);
- gtk_widget_hide(progressBar);
- gtk_widget_hide(waitingLabel);
- gdk_threads_leave();
- if(fork()) {
- while (1) {
- struct clientStatus cS;
- int cS_ID = msgget(server_client_QID, 0606 | IPC_CREAT);
- msgrcv(cS_ID, &cS, 50, 1, 0);
- char LP_Status[10], CP_Status[10], J_Status[10], R_Status[10], S_Status[10], P_Status[10];
- sprintf(LP_Status, "%d", cS.lekka_piechota);
- sprintf(CP_Status, "%d", cS.ciezka_piechota);
- sprintf(J_Status, "%d", cS.jazda);
- sprintf(R_Status, "%d", cS.robotnicy);
- sprintf(S_Status, "%d", cS.surowce);
- sprintf(P_Status, "%d", cS.punkty);
- gdk_threads_enter();
- gtk_label_set_text((GtkLabel*)lekkaPiechotaLabel, LP_Status);
- gtk_label_set_text((GtkLabel*)ciezkaPiechotaLabel, CP_Status);
- gtk_label_set_text((GtkLabel*)jazdaLabel, J_Status);
- gtk_label_set_text((GtkLabel*)robotnicyLabel, R_Status);
- gtk_label_set_text((GtkLabel*)surowceLabel, S_Status);
- gtk_label_set_text((GtkLabel*)punktyLabel, P_Status);
- gdk_threads_leave();
- }
- }
- }
- gboolean pulse(gpointer data) {
- gtk_progress_bar_pulse(GTK_PROGRESS_BAR(data));
- return TRUE;
- }
- /**
- Funkcja odpowiadająca za inicjalizacje całego GUI
- @param window wskaźnik na okno
- @param container wskaźnik na kontener przechowujący widgety
- @param lekka_piechota wskaźnik na button dla lekkiej piechoty
- @param ciezka_piechota wskaznik na button dla ciezkiej piechoty
- @param jazda wskaźnik na button dla jazdy
- @param robotnicy wskaźnik na button dla robotników
- @param walka wskaźnik na button dla walki
- */
- void GTK_INIT(int argc, char* argv[], GtkWidget *window, GtkWidget *container, GtkWidget *lekka_piechota, GtkWidget *ciezka_piechota, GtkWidget *jazda, GtkWidget *robotnicy, GtkWidget *walka) {
- GtkWidget *frame;
- GtkWidget *lekka_piechota_label;
- GtkWidget *lekka_piechota_entry;
- GtkWidget *ciezka_piechota_entry;
- GtkWidget *jazda_entry;
- GtkWidget *robotnicy_entry;
- GtkWidget *waitingContainer;
- GThread *waitForServer;
- GError *error = NULL;
- if(!g_thread_supported())
- g_thread_init(NULL);
- gdk_threads_init();
- gdk_threads_enter();
- gtk_init(&argc, &argv);
- //---------------TWORZENIE OBIEKTÓW----------------
- window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- container = gtk_fixed_new();
- lekka_piechota = gtk_button_new_with_label("Lekka Piechota");
- ciezka_piechota = gtk_button_new_with_label("Ciężka Piechota");
- jazda = gtk_button_new_with_label("Jazda");
- robotnicy = gtk_button_new_with_label("Robotnicy");
- walka = gtk_button_new_with_label("Walka");
- frame = gtk_frame_new(" GAME INFO ");
- lekka_piechota_label = gtk_label_new("Lekka Piechota: \n\nCiezka Piechota: \n\nJazda: \n\nRobotnicy: \n\nSurowce: \n\nPunkty: ");
- lekka_piechota_entry = gtk_entry_new();
- ciezka_piechota_entry = gtk_entry_new();
- jazda_entry = gtk_entry_new();
- robotnicy_entry = gtk_entry_new();
- progressBar = gtk_progress_bar_new();
- waitingLabel = gtk_label_new("Waiting for second player.");
- waitingContainer = gtk_fixed_new();
- lekkaPiechotaLabel = gtk_label_new("0");
- ciezkaPiechotaLabel = gtk_label_new("0");
- jazdaLabel = gtk_label_new("0");
- robotnicyLabel = gtk_label_new("0");
- surowceLabel = gtk_label_new("0");
- punktyLabel = gtk_label_new("0");
- //--------------INICJALIZACJA OBIEKTÓW--------------
- gtk_container_add(GTK_CONTAINER(window), container);
- gtk_window_set_default_size (GTK_WINDOW(window), 480, 350);
- gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_title (GTK_WINDOW(window), "Gra");
- //-------------DODANIE BUTTONÓW------------
- gtk_fixed_put(GTK_FIXED(container), lekka_piechota, 5, 305);
- gtk_widget_set_size_request(lekka_piechota, 120, 35);
- gtk_fixed_put(GTK_FIXED(container), ciezka_piechota, 126, 305);
- gtk_widget_set_size_request(ciezka_piechota, 122, 35);
- gtk_fixed_put(GTK_FIXED(container), jazda, 248, 305);
- gtk_widget_set_size_request(jazda, 57, 35);
- gtk_fixed_put(GTK_FIXED(container), robotnicy, 305, 305);
- gtk_widget_set_size_request(robotnicy, 83, 35);
- gtk_fixed_put(GTK_FIXED(container), walka, 405, 305);
- gtk_widget_set_size_request(walka, 70, 35);
- gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
- gtk_fixed_put(GTK_FIXED(container), frame, 10, 10);
- gtk_widget_set_size_request(frame, 460, 250);
- gtk_fixed_put(GTK_FIXED(container), lekka_piechota_label, 130, 50);
- //----------DODANIE PÓL TYPU ENTRY-----------
- gtk_fixed_put(GTK_FIXED(container), lekka_piechota_entry, 5, 270);
- gtk_widget_set_size_request(lekka_piechota_entry, 120, 30);
- gtk_fixed_put(GTK_FIXED(container), ciezka_piechota_entry, 126, 270);
- gtk_widget_set_size_request(ciezka_piechota_entry, 122, 30);
- gtk_fixed_put(GTK_FIXED(container), jazda_entry, 248, 270);
- gtk_widget_set_size_request(jazda_entry, 57, 30);
- gtk_fixed_put(GTK_FIXED(container), robotnicy_entry, 305, 270);
- gtk_widget_set_size_request(robotnicy_entry, 83, 30);
- gtk_fixed_put(GTK_FIXED(container), progressBar, 162, 175);
- g_timeout_add(90, ( GSourceFunc ) pulse, progressBar);
- gtk_fixed_put(GTK_FIXED(container), waitingLabel, 148, 151);
- gtk_fixed_put(GTK_FIXED(container), lekkaPiechotaLabel, 250, 51);
- gtk_fixed_put(GTK_FIXED(container), ciezkaPiechotaLabel, 250, 85);
- gtk_fixed_put(GTK_FIXED(container), jazdaLabel, 250, 119);
- gtk_fixed_put(GTK_FIXED(container), robotnicyLabel, 250, 153);
- gtk_fixed_put(GTK_FIXED(container), surowceLabel, 250, 187);
- gtk_fixed_put(GTK_FIXED(container), punktyLabel, 250, 221);
- //------------DEKLARACJE FINALIZUJĄCE--------------
- g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(closeClient), NULL);
- g_signal_connect(lekka_piechota, "pressed", G_CALLBACK(lekka_piechota_click), (gpointer)lekka_piechota_entry);
- g_signal_connect(ciezka_piechota, "pressed", G_CALLBACK(ciezka_piechota_click), (gpointer)ciezka_piechota_entry);
- g_signal_connect(jazda, "pressed", G_CALLBACK(jazda_click), (gpointer)jazda_entry);
- g_signal_connect(robotnicy, "pressed", G_CALLBACK(robotnicy_click), (gpointer)robotnicy_entry);
- g_signal_connect(walka, "pressed", G_CALLBACK(walka_click), NULL);
- gtk_widget_show(window);
- gtk_widget_show(container);
- gtk_widget_show(progressBar);
- gtk_widget_show(waitingLabel);
- waitForServer = g_thread_create(serverIsReady, (gpointer)window, FALSE, &error);
- gtk_main();
- gdk_threads_leave();
- }
- int main (int argc, char* argv[]) {
- GtkWidget *window;
- GtkWidget *container;
- GtkWidget *lekka_piechota;
- GtkWidget *ciezka_piechota;
- GtkWidget *jazda;
- GtkWidget *robotnicy;
- GtkWidget *walka;
- clientReadySID = semget(7001, 1, 0600 | IPC_CREAT);
- serverReadySID = semget(7002, 1, 0600 | IPC_CREAT);
- if(clientReadySID == -1 || serverReadySID == -1) {
- exit(1);
- }
- clientReady.sem_num = 0;
- clientReady.sem_flg = 0;
- clientReady.sem_op = 1;
- semop(clientReadySID, &clientReady, 1);
- if(fork())
- GTK_INIT(argc, argv, window, container, lekka_piechota, ciezka_piechota, jazda, robotnicy, walka);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement