diff --git a/display.c b/display.c index b3d63fbbea3e942201b9ad8caf713aeb1e133580..6df03e0fa16da6dd2de964783cb6ddea0cfd49a5 100644 --- a/display.c +++ b/display.c @@ -1,11 +1,11 @@ #include "display.h" +#include "one_armed_bandit.h" // the following global variables are defined for graphical objects only: // the images size is 104x128 pixels - -void *display_func(void *param){ +SDL_Window* init_display(){ int one_arm_width, one_arm_height; SDL_Window *window; @@ -28,32 +28,52 @@ void *display_func(void *param){ &object_rect))!=NULL); object_height=object_rect.h/9; + return window; +} + +display_wheel(int pixel,int offset){ + // ------------------------- + // example of board display: + // ------------------------- + SDL_Rect src_rect, dst_rect=object_rect; + dst_rect.h=object_height*1.5; // display 1.5 object on screen for a wheel + src_rect=dst_rect; + src_rect.x=0; + + // src_rect.y is positionned here on the 2nd object of objects.png + //85 + index*104 + 1 + src_rect.y=object_height*offset; + dst_rect.x= pixel; + dst_rect.y=410-object_height/2; // setup the coord. of the icon in the global renderer + SDL_RenderCopy(renderer, objects_texture, &src_rect, &dst_rect); +} - - SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect); - SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h}; - SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); - for (int i=0; i<4; i++){ - SDL_Rect coin_rect_pos={700, 400-10*i, coin_rect.w, coin_rect.h}; - SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); - } +void *display_func(void *param){ + printf("\ndisplay_func"); + machine mas = *((machine *) param); + while(1){ - - SDL_RenderPresent(renderer); - wait_until(20); - // ------------------------- - // managing events: - // ------------------------- - + SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect); + + SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h}; + SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); + for (int i=0; i<4; i++){ + SDL_Rect coin_rect_pos={700, 400-10*i, coin_rect.w, coin_rect.h}; + SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); + } + + for(int i=0; i<3; i++){ + display_wheel(85+104*i + 1,mas.wheels[i].current_px); + } - SDL_DestroyTexture(objects_texture); - SDL_DestroyTexture(one_arm_texture); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); + + SDL_RenderPresent(renderer); + wait_until(20); + + } return NULL; } diff --git a/display.h b/display.h index e782d764cc7088fd722f6214ff688d0336b63ae0..dcf5342ff382711b0079e15619790ef354128c55 100644 --- a/display.h +++ b/display.h @@ -19,7 +19,8 @@ static SDL_Rect object_rect; int object_height; // global for convenience void *display_func(void *param); -int wheel_spin(double offset,int pixel,int time); +display_wheel(int pixel,int offset); +SDL_Window* init_display(); // height of one single object extern int object_height; // global for convenience diff --git a/one_armed_bandit.c b/one_armed_bandit.c index 1bb0672e01a49dff5c62a0f23856457df07498d9..cd4a07479136f19d5858c7fed9a093ec909574cd 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -7,7 +7,10 @@ #include <pthread.h> #include <semaphore.h> #include "utilities.h" +#include "display.h" #include "one_armed_bandit.h" +#include "threads.h" + int insert_coin(){ @@ -34,10 +37,29 @@ wheel create_wheel(int pixel,int speed){ int main() { - pthread_t threads[5]; + SDL_Window* window = init_display(); + + pthread_t threads[4]; + + + + machine mas; + mas.wheels[0] = create_wheel(85,6); + mas.wheels[1] = create_wheel(85+105,4); + mas.wheels[2] = create_wheel(85+104*2+1,2); + + for(int i = 0; i<3; i++){ + run_thread(&threads[i],wheel_func,&mas.wheels[i]); + } + + display_func(&mas); - display_func(NULL); + SDL_DestroyTexture(objects_texture); + SDL_DestroyTexture(one_arm_texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); SDL_Event event; bool quit=false; diff --git a/one_armed_bandit.h b/one_armed_bandit.h index 817574348ddbc3144eeec728f63dbd0e5279658d..f8ea6a403645a11df2a9d1acc596e25a790be752 100644 --- a/one_armed_bandit.h +++ b/one_armed_bandit.h @@ -8,7 +8,7 @@ #include "wheel.h"; typedef struct machine_a_sous{ - wheel* wheels; + wheel wheels[WHEEL_NB]; int player_wallet; int machine_wallet; } machine; diff --git a/wheel.c b/wheel.c index 0fa41ad22568cf3df55809ba24cb0db22a4b968b..741fc1b0f691b49208613340f7811cc9eb01760e 100644 --- a/wheel.c +++ b/wheel.c @@ -11,41 +11,37 @@ int get_current_px(){ return -1; } -int wheel_spin(double offset, int pixel,int time){ - - // ------------------------- - // example of board display: - // ------------------------- - SDL_Rect src_rect, dst_rect=object_rect; - dst_rect.h=object_height*1.5; // display 1.5 object on screen for a wheel - src_rect=dst_rect; - src_rect.x=0; - - // src_rect.y is positionned here on the 2nd object of objects.png - //85 + index*104 + 1 - src_rect.y=object_height*offset; - dst_rect.x= pixel; - dst_rect.y=410-object_height/2; // setup the coord. of the icon in the global renderer - SDL_RenderCopy(renderer, objects_texture, &src_rect, &dst_rect); - - wait_until(time); - - return 0; -} -void *wheel_func(void *param){ - wheel id = *((wheel *) param); - while(1){ - for(double a = 0; a<8.0; a +=0.1){ - wheel_spin(a,id.current_px,id.speed); - } - } +void *wheel_func(void *param){ + printf("\nwheel_func"); + wheel id = *((wheel *) param); + + while(1){ + // for(double a = 0; a<8.0; a +=0.1){ + + // wheel_spin(a,id.current_px,id.speed); + wheel_spin_1_tick(&id); + wait_until(wheel_delay_ms(id)); + } +} +int wheel_spin_1_tick(wheel* this){ + this->current_px = (this->current_px + 2) % 896 ; +} +int wheel_delay_ms(wheel this){ + return this.speed * 2; +} +int stop_wheel(wheel this){ + while (this.current_px % 128 != 0) { + wheel_spin_1_tick(&this); + wait_until(wheel_delay_ms(this)); + } + this.speed = 0; } \ No newline at end of file diff --git a/wheel.h b/wheel.h index 1ff5f12383d7e874265ca37f180527a80aa142a5..289ad08726e29b8586ca436a9655c754f92c08b3 100644 --- a/wheel.h +++ b/wheel.h @@ -4,6 +4,9 @@ typedef struct roue{ } wheel; -int wheel_spin(double offset, int pixel,int time); int set_speed(int s); int get_current_px(); +void *wheel_func(void *param); +int wheel_spin_1_tick(wheel* this); +int wheel_delay_ms(wheel this); +int stop_wheel(wheel this);