From 3f8fea2ed645ffe73e5765afb24b31726d8d8c97 Mon Sep 17 00:00:00 2001 From: "leonard.beck" <leonard.beck@etu.hesge.ch> Date: Mon, 24 May 2021 16:44:58 +0200 Subject: [PATCH] comments and cleanup --- display.c | 8 +++----- display.h | 10 ++++++---- game.c | 25 ++----------------------- game.h | 8 -------- one_armed_bandit.c | 18 ++++++++++-------- one_armed_bandit.h | 23 +++++++++++++++++++++++ threads.c | 4 ++++ threads.h | 23 ----------------------- wheel.c | 23 +++++++++++------------ wheel.h | 14 ++++++++++++-- 10 files changed, 71 insertions(+), 85 deletions(-) diff --git a/display.c b/display.c index e3da824..319df2e 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 91c6664..7e42c48 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 a5fa54b..98a0858 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 a61cec6..17f4ed9 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 d24a9bc..1df2646 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 e69de29..13e36f0 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 3aa8533..68f86c4 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 a9ded82..88a646f 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 1055717..e995d2d 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 f66b13e..467bf2e 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 -- GitLab