1 #include "sav.h" 2 #include "drw.h" 3 #include "sort.h" 4 #include "util.h" 5 #include "sdl_extra.h" 6 7 #include <stdio.h> 8 #include <pthread.h> 9 10 #define SHOW_FPS false 11 #define WELCOME_MSG_TIME 3 12 13 void check_events(Drw *, SAV *); 14 15 /* TODO: fix Drw and SAV names and methods? */ 16 /* TODO: add sav methods */ 17 /* TODO: Support command line arguments */ 18 /* TODO: Support sound */ 19 /* TODO: More sorting algorithms */ 20 21 int main (void) 22 { 23 SAV *sav = NULL; 24 Drw *drw = NULL; 25 time_t tic, toc; 26 unsigned int ti, tf, dt, time_per_frame; 27 pthread_t p1 = 0; 28 status_t st; 29 30 if((st = SAV_create(&sav)) != OK) goto end; 31 if((st = drw_create(&drw)) != OK) goto end; 32 33 tic = time(NULL); 34 time_per_frame = 16; /* miliseconds */ 35 sav->arr->shuffle(sav->arr); 36 37 #if SHOW_FPS 38 short fps = 0; 39 #endif 40 41 /* main loop */ 42 dt = ti = tf = 0; 43 while(sav->status != STOP) { 44 if(!ti) ti = SDL_GetTicks(); 45 else dt = tf - ti; /* how many ms for a frame */ 46 47 check_events(drw, sav); 48 drw_update_frame(drw, sav); 49 50 if((sav->status == START) || (sav->status == WELCOME)) { 51 /* Print welcome message during the first WELCOME_MSG_TIME seconds */ 52 if(((toc = time(NULL)) - tic) > WELCOME_MSG_TIME) 53 sav->status = START; 54 55 if(sav->sort_status == RUN) { 56 sav->status = RUN; 57 58 /* start sorting thread */ 59 pthread_create(&p1, NULL, &start_sorting, (void *)sav); 60 } 61 } 62 63 if(sav->status == RESTART) { 64 /* if sorting thread is running stop it */ 65 sav->sort_status = STOP; 66 pthread_join(p1, NULL); 67 68 sort_reset_stats(sav); 69 sav->arr->shuffle(sav->arr); 70 71 sav->status = START; 72 sav->sort_status = PAUSE; 73 } 74 75 if(sav->sort_status == SORTED) { 76 pthread_join(p1, NULL); 77 sav->sel = sav->cmp = ARR_LEN + 1; 78 } 79 80 /* if less than `time_per_frame`, delay */ 81 if(dt <= time_per_frame) 82 SDL_Delay(time_per_frame - dt); 83 84 #if SHOW_FPS 85 if(dt > time_per_frame) 86 fps = 1000 / dt; 87 88 printf("FPS is: %i\n", fps); 89 #endif 90 91 ti = tf; 92 tf = SDL_GetTicks(); 93 } 94 95 end: 96 /* check if p1 has been initialized */ 97 if(p1 != 0) pthread_join(p1, NULL); 98 99 SAV_destroy(sav); 100 101 drw_destroy(drw); 102 return 0; 103 } 104 105 void check_events(Drw *drw, SAV *sav) 106 { 107 SDL_Event event; 108 109 while (SDL_PollEvent(&event)) { 110 switch(event.type) { 111 case SDL_QUIT: 112 sav->status = sav->sort_status = STOP; 113 break; 114 case SDL_KEYDOWN: 115 switch(event.key.keysym.scancode) { 116 case SDL_SCANCODE_R: 117 if(sav->status == RUN) sav->status = RESTART; 118 break; 119 case SDL_SCANCODE_SPACE: 120 if(sav->sort_status == PAUSE) 121 sav->sort_status = RUN; 122 else if(sav->sort_status == RUN) 123 sav->sort_status = PAUSE; 124 else if(sav->sort_status == SORTED) { 125 sort_reset_stats(sav); 126 arr_restore_from_bk(sav->arr); 127 sav->status = START; 128 sav->sort_status = RUN; 129 } 130 break; 131 case SDL_SCANCODE_Q: 132 sav->status = sav->sort_status = STOP; 133 break; 134 case SDL_SCANCODE_EQUALS: 135 set_sort_speed(sav, sav->sort_delay - 1); 136 break; 137 case SDL_SCANCODE_MINUS: 138 set_sort_speed(sav, sav->sort_delay + 1); 139 break; 140 case SDL_SCANCODE_TAB: 141 sort_selector(sav); 142 break; 143 case SDL_SCANCODE_S: 144 arr_shuffle_next(sav->arr); 145 if(sav->sort_status == PAUSE || sav->sort_status == SORTED) { 146 if(sav->status == RUN) 147 sav->status = RESTART; 148 else arr_shuffle(sav->arr); 149 } 150 else if (sav->sort_status == RUN) { 151 sav->status = RESTART; 152 arr_shuffle(sav->arr); 153 } 154 break; 155 default: break; 156 } 157 break; 158 case SDL_WINDOWEVENT: 159 switch(event.window.event) { 160 case SDL_WINDOWEVENT_RESIZED: 161 /* SDL_Log("Window resized to %dx%d", event.window.data1, event.window.data2); */ 162 drw->w = event.window.data1; 163 drw->h = event.window.data2; 164 165 /* set new window borders */ 166 drw->x_border = (drw->w / 2) - ((sav->arr->len * RECT_WIDTH) / 2); 167 drw->y_border = (drw->h / 2) - (ARR_MAX / 2); 168 break; 169 default: break; 170 } 171 break; 172 default: break; 173 } 174 } 175 }