//****************************************************************************** ////////////////////////////////////////////////////////////////////////////////////////////////////// void handle_mouseover(BUTTON *buttonPtr, int x, int y) { if(check_click(*buttonPtr,x,y) == true) { buttonPtr->state = 1; if(sound)Mix_PlayChannel(-1,button_over_sound,0); } else { buttonPtr->state = 0; } }
t_bunny_response click(t_bunny_event_state state, t_bunny_mousebutton key, t_data *data) { (void) data; (void) state; (void) key; if (state == GO_DOWN) { check_click(data); } return (GO_ON); }
int welcome_main() { int i,j; int mx,my; int welcome = true; int option_screen = false; int score_screen = false; int first_run = true; int credits_screen = false; SDL_Rect temp; TTF_Font *MyFont; SDL_Color GREEN = {0,255,0}; SDL_Color WHITE = {255,255,255}; SDL_Surface *message = NULL; int option = -1; init_buttons(); init_images(); draw_image(backk,screen,NULL,0,0); gameLog("Welcome screen initialized..."); //draw_image(message,screen,NULL,0,0); //Mix_PlayMusic(background_music,-1); while(welcome) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEMOTION: for(i=0;i<TOTAL_NO_BUTTON;++i) { handle_mouseover(&button[i],event.motion.x,event.motion.y); } break; case SDL_MOUSEBUTTONDOWN: mx = event.button.x; my = event.button.y; for( i = 0; i < TOTAL_NO_BUTTON; ++i) { if(check_click(button[i],mx,my)==true) { if(sound)Mix_PlayChannel(-1,click_sound,4); animate_button(button[i],i); first_run = true; switch(i) { case 0: option = 1; welcome = false; break; case 1: option_screen = true; break; case 2: score_screen = true; break; case 3: credits_screen = true; break; case 4: option = -1; welcome = false; break; } } } break; case SDL_QUIT: welcome = false; option = -1; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_p: animate_button(button[0],0); welcome = false; option = 1; break; case SDLK_x: animate_button(button[4],4); welcome = false; option = -1; break; case SDLK_1: if(Mix_PlayingMusic() == 0) { Mix_PlayMusic(background_music,-1); } else { if(Mix_PausedMusic() == 1) { sound = true; Mix_ResumeMusic(); } else { sound = false; Mix_PauseMusic(); } } break; case SDLK_0: Mix_HaltMusic(); break; } } } //---=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- while(option_screen == true) { if(first_run) { draw_image(option_screen_image,screen,NULL,0,0); draw_image(back_button_image,screen,NULL,back_button.dimension.x, back_button.dimension.y); first_run = false; } draw_image(sound_icon[sound],screen,NULL,sound_button.dimension.x,sound_button.dimension.y); while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEBUTTONDOWN: if(sound)Mix_PlayChannel(-1,click_sound,0); mx = event.button.x; my = event.button.y; if(check_click(sound_button,mx,my) == true) { if(sound == 0) sound = 1; else sound =0; } if(check_click(back_button,mx,my)== true) { draw_image(backk,screen,NULL,0,0); option_screen = false; } } } SDL_Flip(screen); } //========================================================================================== while(score_screen == true) { if(first_run) { draw_image(score_screen_image,screen,NULL,0,0); draw_image(back_button_image,screen,NULL,back_button.dimension.x, back_button.dimension.y); show_scores(); first_run = false; } while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEBUTTONDOWN: if(sound)Mix_PlayChannel(-1,click_sound,0); mx = event.button.x; my = event.button.y; if(check_click(back_button,mx,my)== true) { draw_image(backk,screen,NULL,0,0); score_screen = false; } } } SDL_Flip(screen); } //========================================================================================== //========================================================================================== while(credits_screen == true) { if(first_run) { temp.x = 0; temp.y = 0; temp.w = 1100; temp.h = 600; clear_image(temp); MyFont = TTF_OpenFont("fonts\\lfont.ttf",32); message = TTF_RenderText_Blended(MyFont,"Developed By:",WHITE); draw_image(message,screen,NULL,100,100); SDL_FreeSurface(message); message = NULL; TTF_CloseFont(MyFont); MyFont = TTF_OpenFont("fonts\\sfont.ttf",20); message = TTF_RenderText_Blended(MyFont,"-> Bishruti Siku",GREEN); draw_image(message,screen,NULL,100,150); SDL_FreeSurface(message); message = NULL; message = TTF_RenderText_Blended(MyFont,"-> Nitish Dhaubhadel",GREEN); draw_image(message,screen,NULL,100,170); SDL_FreeSurface(message); message = NULL; message = TTF_RenderText_Blended(MyFont,"-> Iksha Gurung",GREEN); draw_image(message,screen,NULL,100,190); SDL_FreeSurface(message); message = NULL; message = TTF_RenderText_Blended(MyFont,"-> Pranaya Pradhananga",GREEN); draw_image(message,screen,NULL,100,210); SDL_FreeSurface(message); message = NULL; message = TTF_RenderText_Blended(MyFont,"-> Sandip Sahani",GREEN); draw_image(message,screen,NULL,100,230); SDL_FreeSurface(message); message = NULL; message = TTF_RenderText_Blended(MyFont,"Computer Engineering",WHITE); draw_image(message,screen,NULL,100,300); SDL_FreeSurface(message); message = NULL; message = TTF_RenderText_Blended(MyFont,"Kathmandu University",WHITE); draw_image(message,screen,NULL,100,320); SDL_FreeSurface(message); message = NULL; draw_image(back_button_image,screen,NULL,back_button.dimension.x, back_button.dimension.y); first_run = false; } while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEBUTTONDOWN: if(sound)Mix_PlayChannel(-1,click_sound,0); mx = event.button.x; my = event.button.y; if(check_click(back_button,mx,my)== true) { draw_image(backk,screen,NULL,0,0); credits_screen = false; } } } SDL_Flip(screen); } //========================================================================================== for(i=0;i<TOTAL_NO_BUTTON;++i) { draw_image(button_image[i][button[i].state],screen,NULL,button[i].dimension.x,button[i].dimension.y); } SDL_Flip(screen); } SDL_FreeSurface(option_screen_image); SDL_FreeSurface(score_screen_image); SDL_FreeSurface(back_button_image); SDL_FreeSurface(sound_icon[0]); SDL_FreeSurface(sound_icon[1]); SDL_FreeSurface(message); message = NULL; SDL_FreeSurface(backk); for(i=0;i<TOTAL_NO_BUTTON;++i) { for(j=0;j<2;++j) { SDL_FreeSurface(button_image[i][j]); } } //TTF_CloseFont(font); gameLog("Welcome screen closed..."); return option; }
int main(int argc, char *argv[]) { sem_t *sem_mutex; //Binary semaphore to use a mutex int *stateptr; //Shared memory global int to store active button state int hit_count = 0, rows, cols; WINDOW *mainwin = NULL; int nextch; MEVENT event; struct sigaction act; //Create a binary semaphore to use as a mutex: //(will be inerited by children) if ((sem_mutex = sem_open("/mutex", O_CREAT|O_EXCL, 0600, 1)) == SEM_FAILED) { perror("Semaphore creation failed"); return EXIT_FAILURE; } //Now unlink semaphore so it will be deleted in case of ^-C or crash: sem_unlink("/mutex"); //Setup anonymous, shared memory for global int: if ((stateptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { perror("Shared memory creation failed"); return EXIT_FAILURE; } //Initialize button state (no active buttons): *stateptr = 0; //Initialize and setup the curses window: if ((mainwin = initscr()) == NULL) { fprintf(stderr,"Failed to initialize curses window\n"); return EXIT_FAILURE; } getmaxyx(mainwin,rows,cols); mvprintw(rows/2,cols/2-18,"Click on the Corner Buttons to Score!",rows,cols); mvprintw(rows/2+2,cols/2-10,"(rows: %d cols: %d)",rows,cols); refresh(); //Setup signal handler for SIGCHLD signals: memset(&act,0,sizeof(act)); act.sa_handler = sigchld_handler; sigemptyset(&act.sa_mask); sigaction(SIGCHLD,&act,NULL); //Create children: //**************** for (int i=0; i < running; i++){//memory problem??? switch(fork()){ case -1: perror("fork failed"); sigchld_handler(EXIT_FAILURE); endwin();//put this in here since failures don't close the window apparently. exit(EXIT_FAILURE); case 0: child_func(1+i, rows, cols,sem_mutex,stateptr);//1+processcount allows for proper numbering. sigchld_handler(EXIT_SUCCESS); exit(EXIT_SUCCESS); default: break;//no break leads to error apparently. //I got it now the switch statement will determine the parent function follows the default. } } //Setup curses to get mouse events: keypad(mainwin, TRUE); mousemask(ALL_MOUSE_EVENTS, NULL); //Loop catching mouse clicks while children are running: while (running > 0) { //nextch = wgetch(mainwin); if ((nextch = getch()) == KEY_MOUSE && getmouse(&event) == OK && (event.bstate & BUTTON1_PRESSED)) { //Check if user clicked on a label: if (check_click(*stateptr, event.x, event.y, rows, cols)) { //Clicked on current label: hit_count++; mvprintw(rows/2+5,cols/2-11,"Got #%d at (%3d,%3d)",*stateptr,event.x,event.y); wrefresh(curscr); //Need this to ensure entire screen is redrawn despite child changes. } } } //Close curses window so terminal settings are restored to normal: endwin(); //Print out results: printf("\nYour hit count was: %d\n\n",hit_count); //Collect all the children: //************************* //return exit success if child successfully exited. int status; int tempcount = 0;//made a temp count since I think running will be 0 by now. while(tempcount<4){ wait(&status);// wait for children shouldn't all return status if(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS){ tempcount++;//increment temp count. }else exit(EXIT_FAILURE);//return exit failure according to lecture. } exit(EXIT_SUCCESS);//completes while loop so returns exit success. }