From 25f95f1302b10b316719b31167445ae730ada71b Mon Sep 17 00:00:00 2001 From: "benjamin.sitbon" <benjamin.sibon@etu.hesge.ch> Date: Sun, 23 May 2021 20:49:54 +0200 Subject: [PATCH] wheels spinning well --- display.c | 9 +++++---- game.c | 6 +++--- game.h | 4 +++- one_armed_bandit.c | 8 ++++---- wheel.c | 7 ++++--- wheel.h | 2 +- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/display.c b/display.c index 4c48214..9f70ded 100644 --- a/display.c +++ b/display.c @@ -6,6 +6,7 @@ int object_height; // global for convenience + SDL_Window* init_display(){ int one_arm_width, one_arm_height; SDL_Window *window; @@ -53,8 +54,8 @@ void display_wheel(int pixel,int offset){ void *display_func(void *param){ - printf("\ndisplay_func"); SDL_Window* window = init_display(); + printf("\ndisplay_func"); machine* mas = (machine *) (param); while(1){ @@ -69,10 +70,10 @@ void *display_func(void *param){ } for(int i=0; i<3; i++){ - //pthread_mutex_lock(&mas->wheels[i].lock); + pthread_mutex_lock(&mas->wheels[i].lock); int current_px = mas->wheels[i].current_px; - //pthread_mutex_unlock(&mas->wheels[i].lock); - display_wheel(85+104*i + 1, current_px); + pthread_mutex_unlock(&mas->wheels[i].lock); + display_wheel(85+(104*i + 1), current_px); } diff --git a/game.c b/game.c index 99c2c81..acc79dd 100644 --- a/game.c +++ b/game.c @@ -18,13 +18,13 @@ wheel create_wheel(int pixel,int speed){ wheel ret; ret.current_px = pixel; ret.speed = speed; - //pthread_mutex_init(&ret.lock,NULL); + pthread_mutex_init(&ret.lock,NULL); return ret; } -machine new_machine(machine mas, int nb_wheels){ +void new_machine(machine* mas, int nb_wheels){ for (int i = 0; i < nb_wheels; i++) { - mas.wheels[i] = create_wheel(96,nb_wheels-i); + mas->wheels[i] = create_wheel(96,nb_wheels-i); } } diff --git a/game.h b/game.h index 46f4c60..95b2ba6 100644 --- a/game.h +++ b/game.h @@ -7,6 +7,8 @@ #include "wheel.h" #include <stdlib.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_image.h> typedef struct machine_a_sous{ @@ -19,6 +21,6 @@ int insert_coin(); int set_wheel_speed(int index); int next_wheel(); wheel create_wheel(int pixel,int speed); -machine new_machine(machine mas, int nb_wheels); +void new_machine(machine* mas, int nb_wheels); #endif diff --git a/one_armed_bandit.c b/one_armed_bandit.c index 4c81105..07155b8 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -19,17 +19,17 @@ int main() { - + pthread_t threads[4]; machine mas; - mas = new_machine(mas, 3); - run_thread(&threads[3],display_func,&mas); + new_machine(&mas, 3); + for(int i = 0; i<3; i++){ run_thread(&threads[i],wheel_func,&mas.wheels[i]); } - + run_thread(&threads[3],display_func,&mas); diff --git a/wheel.c b/wheel.c index 5ba0f59..465fce0 100644 --- a/wheel.c +++ b/wheel.c @@ -3,6 +3,7 @@ #include "wheel.h" + int set_speed(int s){ return -1; } @@ -26,15 +27,15 @@ void *wheel_func(void *param){ // wheel_spin(a,id.current_px,id.speed); wheel_spin_1_tick(id); - //wait_until(wheel_delay_ms(*id)); + wait_until(wheel_delay_ms(*id)); } } int wheel_spin_1_tick(wheel* this){ if (this->speed) { - // mutex + pthread_mutex_lock(&this->lock); this->current_px = (this->current_px + 2) % 896 ; - // mutex + pthread_mutex_unlock(&this->lock); } } diff --git a/wheel.h b/wheel.h index a6d0dc9..bfa751e 100644 --- a/wheel.h +++ b/wheel.h @@ -6,7 +6,7 @@ typedef struct roue{ int speed; int current_px; - //pthread_mutex_t lock; + pthread_mutex_t lock; } wheel; -- GitLab