diff --git a/display.c b/display.c index 6df03e0fa16da6dd2de964783cb6ddea0cfd49a5..3637b2e87656224493757aa599a33bd9ce888dea 100644 --- a/display.c +++ b/display.c @@ -1,9 +1,10 @@ #include "display.h" -#include "one_armed_bandit.h" +#include "game.h" // the following global variables are defined for graphical objects only: // the images size is 104x128 pixels +int object_height; // global for convenience SDL_Window* init_display(){ int one_arm_width, one_arm_height; @@ -31,7 +32,7 @@ SDL_Window* init_display(){ return window; } -display_wheel(int pixel,int offset){ +void display_wheel(int pixel,int offset){ // ------------------------- // example of board display: // ------------------------- @@ -42,7 +43,8 @@ display_wheel(int pixel,int offset){ // src_rect.y is positionned here on the 2nd object of objects.png //85 + index*104 + 1 - src_rect.y=object_height*offset; + // src_rect.y= offset; + src_rect.y= 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); @@ -52,7 +54,7 @@ display_wheel(int pixel,int offset){ void *display_func(void *param){ printf("\ndisplay_func"); - machine mas = *((machine *) param); + machine* mas = (machine *) (param); while(1){ @@ -66,20 +68,16 @@ void *display_func(void *param){ } for(int i=0; i<3; i++){ - display_wheel(85+104*i + 1,mas.wheels[i].current_px); + // pthread_mutex_lock(&mas.lock); + int current_px = mas->wheels[i].current_px; + // pthread_mutex_unlock(&mas.lock); + display_wheel(85+104*i + 1, current_px); } - + SDL_RenderPresent(renderer); wait_until(20); } return NULL; } - - - - - - - diff --git a/display.h b/display.h index dcf5342ff382711b0079e15619790ef354128c55..91c66642b6531f6d87126c43d0a3009abfaa0bbb 100644 --- a/display.h +++ b/display.h @@ -16,14 +16,13 @@ static SDL_Renderer *renderer; static SDL_Texture *one_arm_texture, *objects_texture, *coin_texture; static SDL_Rect one_arm_rect, coin_rect; static SDL_Rect object_rect; -int object_height; // global for convenience -void *display_func(void *param); -display_wheel(int pixel,int offset); SDL_Window* init_display(); +void *display_func(void *param); +void display_wheel(int pixel,int offset); -// height of one single object -extern int object_height; // global for convenience +// height of one single object +// extern int object_height; // global for convenience -#endif \ No newline at end of file +#endif diff --git a/game.c b/game.c new file mode 100644 index 0000000000000000000000000000000000000000..e7446fc4ea8641fdac0e468454d69d24965b8adc --- /dev/null +++ b/game.c @@ -0,0 +1,30 @@ +#include "game.h" + +int insert_coin(){ + + return -1; + +} + +int set_wheel_speed(int index){ + return -1; +} + +int next_wheel(){ + return -1; +} + +wheel create_wheel(int pixel,int speed){ + wheel ret; + ret.current_px = pixel; + ret.speed = speed; + // ret.lock = PTHREAD_MUTEX_INITIALIZER; + return ret; + +} + +machine new_machine(machine mas, int nb_wheels){ + for (int i = 0; i < nb_wheels; i++) { + mas.wheels[i] = create_wheel(96,nb_wheels-i); + } +} diff --git a/game.h b/game.h new file mode 100644 index 0000000000000000000000000000000000000000..20e1cf329edae0b1e53530465b2c3b26135f4de5 --- /dev/null +++ b/game.h @@ -0,0 +1,22 @@ +#ifndef GAME_H +#define GAME_H + + +#define OBJECT_NB 7 +#define WHEEL_NB 3 + +#include "wheel.h" + +typedef struct machine_a_sous{ + wheel wheels[WHEEL_NB]; + int player_wallet; + int machine_wallet; +} machine; + +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); + +#endif diff --git a/one_armed_bandit.c b/one_armed_bandit.c index cd4a07479136f19d5858c7fed9a093ec909574cd..88703990dc7b9fc90a606ce0f088d4457a3e1ebb 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -1,4 +1,7 @@ // Template of one-armed bandit lab +// KB main, thread handling + + #include <stdio.h> #include <stdbool.h> @@ -8,36 +11,15 @@ #include <semaphore.h> #include "utilities.h" #include "display.h" +#include "game.h" #include "one_armed_bandit.h" #include "threads.h" -int insert_coin(){ - - return -1; - -} - -int set_wheel_speed(int index){ - return -1; -} - -int next_wheel(){ - return -1; -} - -wheel create_wheel(int pixel,int speed){ - wheel ret; - ret.current_px = pixel; - ret.speed = speed; - return ret; - -} - int main() { - + SDL_Window* window = init_display(); pthread_t threads[4]; @@ -45,9 +27,7 @@ int main() 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); + mas = new_machine(mas, 3); for(int i = 0; i<3; i++){ run_thread(&threads[i],wheel_func,&mas.wheels[i]); diff --git a/one_armed_bandit.h b/one_armed_bandit.h index f8ea6a403645a11df2a9d1acc596e25a790be752..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- a/one_armed_bandit.h +++ b/one_armed_bandit.h @@ -1,20 +0,0 @@ -#ifndef ONE_ARM_H -#define ONE_ARM_H - - -#define OBJECT_NB 7 -#define WHEEL_NB 3 - -#include "wheel.h"; - -typedef struct machine_a_sous{ - wheel wheels[WHEEL_NB]; - int player_wallet; - int machine_wallet; -} machine; - -int insert_coin(); -int set_wheel_speed(int index); -int next_wheel(); - -#endif diff --git a/threads.h b/threads.h index 437e6efae56813c4ebe534778cd397100b1f7361..a9ded8240f9f373567f5488ddf7863b40ff93736 100644 --- a/threads.h +++ b/threads.h @@ -16,6 +16,12 @@ | |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| | */ + +// TODO: MALLOOOOC + +#ifndef THREADS_H +#define THREADS_H + #define __GNU_SOURCE // #include <stdio.h> @@ -29,3 +35,5 @@ int wait_for_thread(pthread_t t); int run_thread_and_wait (void* (*fn )(void*), void *arg); int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int t_cnt); int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt); + +#endif diff --git a/wheel.c b/wheel.c index 741fc1b0f691b49208613340f7811cc9eb01760e..f3b75a3826d777fd12c0345e3178bb855813d86b 100644 --- a/wheel.c +++ b/wheel.c @@ -1,6 +1,6 @@ -#include "display.h" +// #include "display.h" #include "utilities.h" -#include "wheel.h"; +#include "wheel.h" int set_speed(int s){ @@ -19,29 +19,35 @@ int get_current_px(){ void *wheel_func(void *param){ printf("\nwheel_func"); - wheel id = *((wheel *) 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); - wheel_spin_1_tick(&id); - wait_until(wheel_delay_ms(id)); + 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 ; + if (this->speed) { + // mutex + this->current_px = (this->current_px + 2) % 896 ; + // mutex + } } int wheel_delay_ms(wheel this){ - return this.speed * 2; + if (this.speed) { + return this.speed * 2; + } return 2; } int stop_wheel(wheel this){ - while (this.current_px % 128 != 0) { + while (this.current_px % 128 != 32) { // 32 is the offset to center the item 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 289ad08726e29b8586ca436a9655c754f92c08b3..a94f69fb0be4de8d6e8c630b76ae83fb4e9ebe53 100644 --- a/wheel.h +++ b/wheel.h @@ -1,6 +1,10 @@ +#ifndef WHEEL_H +#define WHEEL_H + typedef struct roue{ int speed; int current_px; + // pthread_mutex_t lock; } wheel; @@ -10,3 +14,6 @@ void *wheel_func(void *param); int wheel_spin_1_tick(wheel* this); int wheel_delay_ms(wheel this); int stop_wheel(wheel this); + + +#endif