From c5f2460fc82ca95170fc1568a5097f4f2dda4c90 Mon Sep 17 00:00:00 2001 From: "benjamin.sitbon" <benjamin.sibon@etu.hesge.ch> Date: Sun, 23 May 2021 17:35:12 +0200 Subject: [PATCH] =?UTF-8?q?=C3=A7a=20marche=20pas=20mais=20=C3=A7a=20compi?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- display.c | 64 ++++++++++++++++++++++++++++++---------------- display.h | 3 ++- one_armed_bandit.c | 26 +++++++++++++++++-- one_armed_bandit.h | 2 +- wheel.c | 52 +++++++++++++++++-------------------- wheel.h | 5 +++- 6 files changed, 97 insertions(+), 55 deletions(-) diff --git a/display.c b/display.c index b3d63fb..6df03e0 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 e782d76..dcf5342 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 1bb0672..cd4a074 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 8175743..f8ea6a4 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 0fa41ad..741fc1b 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 1ff5f12..289ad08 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); -- GitLab