diff --git a/display.c b/display.c index e3da824f6c40fa2f868edeca241cda6d4055a6b2..319df2e181c7ce4b5f511d68e101f88149c48cc6 100644 --- a/display.c +++ b/display.c @@ -1,9 +1,8 @@ +// In charge of all the display using SDL2 + #include "display.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 @@ -44,7 +43,6 @@ void 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= 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 @@ -55,7 +53,7 @@ void display_wheel(int pixel,int offset){ void *display_func(void *param){ SDL_Window* window = init_display(); - printf("\ndisplay_func"); + // printf("\ndisplay_func"); machine* mas = (machine *) (param); while(1){ diff --git a/display.h b/display.h index 91c66642b6531f6d87126c43d0a3009abfaa0bbb..7e42c48a5c2af04a1b4f6d763f903a3e2eeb8884 100644 --- a/display.h +++ b/display.h @@ -12,17 +12,19 @@ #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> +// the following global variables are defined for graphical objects only: +// the images size is 104x128 pixels + 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; SDL_Window* init_display(); + // graphic initialization void *display_func(void *param); + // run by the display thread, displays the wallets, the wheels void display_wheel(int pixel,int offset); - -// height of one single object -// extern int object_height; // global for convenience - + // displays each wheel #endif diff --git a/game.c b/game.c index a5fa54b14662a000c9bf3decc4560e8285f13b6a..98a085817c9e6b6a6be62ee9fb469729dd7a860a 100644 --- a/game.c +++ b/game.c @@ -1,27 +1,6 @@ -#include "game.h" - -int insert_coin(){ - - return -1; - -} - -int set_wheel_speed(int index){ - return -1; -} - -int next_wheel(){ - return -1; -} +// abstraction layer between display and wheels (cf game.h) -wheel create_wheel(int pixel,int speed){ - wheel ret; - ret.current_px = pixel; - ret.speed = speed; - pthread_mutex_init(&ret.lock,NULL); - return ret; - -} +#include "game.h" void new_machine(machine* mas, int nb_wheels,int player, int machine){ for (int i = 0; i < nb_wheels; i++) { diff --git a/game.h b/game.h index a61cec6d4362c9a5ea776672d6c70e32618898dc..17f4ed92cc1b570acc7a8260b05b470161531213 100644 --- a/game.h +++ b/game.h @@ -6,10 +6,6 @@ #define WHEEL_NB 3 #include "wheel.h" -#include <stdlib.h> -#include <SDL2/SDL.h> -#include <SDL2/SDL_image.h> - typedef struct machine_a_sous{ wheel wheels[WHEEL_NB]; @@ -17,10 +13,6 @@ typedef struct machine_a_sous{ int machine_wallet; } machine; -int insert_coin(); -int set_wheel_speed(int index); -int next_wheel(); -wheel create_wheel(int pixel,int speed); void new_machine(machine* mas, int nb_wheels,int player, int machine); #endif diff --git a/one_armed_bandit.c b/one_armed_bandit.c index d24a9bc7b0695e01cd7a40a0a0412db97f386f8e..1df2646208da3c1999dec89444c73e1c10d5e86b 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -1,7 +1,10 @@ -// Template of one-armed bandit lab -// KB main, thread handling - +/** +* One Armed Bandit +* 24.05.21 +* Authors : Sitbon benjamin, Beck Léonard +**/ +// Keyboard thread and game initialization #include <stdio.h> #include <stdbool.h> @@ -11,9 +14,8 @@ #include <semaphore.h> #include "utilities.h" #include "display.h" -#include "game.h" -#include "one_armed_bandit.h" #include "threads.h" +#include "one_armed_bandit.h" @@ -37,14 +39,14 @@ int main() machine mas; new_machine(&mas, 3,10,30); - + for(int i = 0; i<3; i++){ run_thread(&threads[i],wheel_func,&mas.wheels[i]); } - + run_thread(&threads[3],display_func,&mas); - + SDL_Event event; bool quit=false; diff --git a/one_armed_bandit.h b/one_armed_bandit.h index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..13e36f0ec75c0c83ad83fa2d09798797c80e15fe 100644 --- a/one_armed_bandit.h +++ b/one_armed_bandit.h @@ -0,0 +1,23 @@ +#include "game.h" +/* + _____ ___ _ ______ _ _ _ + | _ | / _ \ | | | ___ \ | (_) | + | | | |_ __ ___ / /_\ \_ __ _ __ ___ ___ __| | | |_/ / __ _ _ __ __| |_| |_ + | | | | '_ \ / _ \ | _ | '__| '_ ` _ \ / _ \/ _` | | ___ \/ _` | '_ \ / _` | | __| + \ \_/ / | | | __/ | | | | | | | | | | | __/ (_| | | |_/ / (_| | | | | (_| | | |_ + \___/|_| |_|\___| \_| |_/_| |_| |_| |_|\___|\__,_| \____/ \__,_|_| |_|\__,_|_|\__| + + + + /\/\ + | | + (____|_____|____) + | o o | + |__^__| + \#####/ + \###/ + \#/ + +*/ +int how_many_points(machine mas); +// returns the score based on the positionning of the wheels diff --git a/threads.c b/threads.c index 3aa85332ed70b52d4874675d3bcdeeff6fe36842..68f86c4f8ea2bee45b112f2a489874583b735726 100644 --- a/threads.c +++ b/threads.c @@ -1,3 +1,7 @@ +// Small library to ease the use of pthread + +// TODO: MALLOOOOC + #define __GNU_SOURCE #include <stdio.h> diff --git a/threads.h b/threads.h index a9ded8240f9f373567f5488ddf7863b40ff93736..88a646f88bcff5ed15f7bfc4869f56602563323f 100644 --- a/threads.h +++ b/threads.h @@ -1,31 +1,8 @@ -/** -* Authors : Sitbon Benjamin, Beck Léonard -* Small library to ease the use of pthread -*/ - - -/* - ___ _ - / _ )___ _________(_)__ ____ - / _ / _ `/ __/ __/ / -_) __/ - /____/\_,_/_/ /_/ /_/\__/_/ - - |^| |^| |^| |^| - | |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| | - | |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| | - | |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| | - -*/ - -// TODO: MALLOOOOC - #ifndef THREADS_H #define THREADS_H #define __GNU_SOURCE -// #include <stdio.h> -// #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <sys/types.h> diff --git a/wheel.c b/wheel.c index 1055717aed9b899ef0db9dc96745c2b2cb5e30ae..e995d2d4d317dd99898f0f572963b3288c363f18 100644 --- a/wheel.c +++ b/wheel.c @@ -1,20 +1,20 @@ -// #include "display.h" +// in charge of each of the game's "wheel" independantly (cf. wheel.h) + #include "utilities.h" #include "wheel.h" - -int set_speed(int s){ - return -1; -} - -int get_current_px(){ - return -1; +wheel create_wheel(int pixel,int speed){ + wheel ret; + ret.current_px = pixel; + ret.speed = speed; + pthread_mutex_init(&ret.lock,NULL); + return ret; } void *wheel_func(void *param){ - printf("\nwheel_func"); + // printf("\nwheel_func"); wheel* id = (wheel *) (param); while(1){ @@ -34,12 +34,11 @@ int wheel_spin_1_tick(wheel* this){ int wheel_delay_ms(wheel this){ if (this.speed) { return this.speed * 2; - } + } } int stop_wheel(wheel* this){ - while (this->current_px % 128 != 96) { // 32 is the offset to center the item - //wheel_spin_1_tick(this); + while (this->current_px % 128 != 96) { // 96 is the offset to center the item wait_until(wheel_delay_ms(*this)); } this->speed = 0; diff --git a/wheel.h b/wheel.h index f66b13ec1b462e534989f97e8359dfae8ccc7a15..467bf2ed15a2c5ffaa84e6adbd332887a1124588 100644 --- a/wheel.h +++ b/wheel.h @@ -5,17 +5,27 @@ typedef struct roue{ int speed; + // speed is either 1 (the fastest), 2, 3 (the slowest) or 0 (stopped) int current_px; + // stores the position of the wheel, used to determine what item is visible pthread_mutex_t lock; + // used to synchronize cache with memory so display is always up to date } wheel; -int set_speed(int s); -int get_current_px(); +wheel create_wheel(int pixel,int speed); + void *wheel_func(void *param); + // function run by each wheel thread, turns the wheel + int wheel_spin_1_tick(wheel* this); + // spins the wheel + int wheel_delay_ms(wheel this); + // calculates the delay based on the speed + int stop_wheel(wheel* this); + // stops the wheel on the right pixel to have a centered item #endif