diff --git a/display.c b/display.c index 421b31ecf5f3e160660d96dbe03ca5b7da9e5439..b3d63fbbea3e942201b9ad8caf713aeb1e133580 100644 --- a/display.c +++ b/display.c @@ -1,24 +1,11 @@ -#include <stdio.h> -#include <stdbool.h> -#include <assert.h> -#include <unistd.h> -#include <pthread.h> -#include "utilities.h" #include "display.h" -#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; -int object_height; // global for convenience -void *display_func(void *param) -{ + +void *display_func(void *param){ int one_arm_width, one_arm_height; SDL_Window *window; @@ -42,19 +29,23 @@ void *display_func(void *param) object_height=object_rect.h/9; - while(1){ - for(double a = 0; a<8.0; a +=0.1){ - - wheel_func(a); + + 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); } - + + SDL_RenderPresent(renderer); + wait_until(20); // ------------------------- - // managing events: + // managing events: // ------------------------- @@ -68,37 +59,7 @@ void *display_func(void *param) -int wheel_func(double index){ - - - // ------------------------- - // example of board display: - // ------------------------- - - 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); - } - 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; - for(int i=0; i<3; i++){ - // src_rect.y is positionned here on the 2nd object of objects.png - src_rect.y=object_height*index; - dst_rect.x=85 + i*104 + 1; - 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_RenderPresent(renderer); - sleep(0.1); - return 0; -} diff --git a/display.h b/display.h index 8f08a0d9c8e4cf84c0300f90228cfe4fdac434ac..e782d764cc7088fd722f6214ff688d0336b63ae0 100644 --- a/display.h +++ b/display.h @@ -2,9 +2,24 @@ #define DISPLAY_H #include <time.h> +#include <stdio.h> +#include <stdbool.h> +#include <assert.h> +#include <unistd.h> +#include <pthread.h> +#include "utilities.h" + +#include <SDL2/SDL.h> +#include <SDL2/SDL_image.h> + +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); -int wheel_func(double index); +int wheel_spin(double offset,int pixel,int time); // height of one single object extern int object_height; // global for convenience diff --git a/display.o b/display.o deleted file mode 100644 index c70f3a8922462cb97750d7361c5d1b6518643b02..0000000000000000000000000000000000000000 Binary files a/display.o and /dev/null differ diff --git a/one_armed_bandit b/one_armed_bandit deleted file mode 100755 index 5f449a5a9e3c854950b6e70bde110117ea52a8e5..0000000000000000000000000000000000000000 Binary files a/one_armed_bandit and /dev/null differ diff --git a/one_armed_bandit.c b/one_armed_bandit.c index 96bd75b5278f68dae0807e0e1fa4508d5bb1c62b..1bb0672e01a49dff5c62a0f23856457df07498d9 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -7,56 +7,68 @@ #include <pthread.h> #include <semaphore.h> #include "utilities.h" -#include "display.h" #include "one_armed_bandit.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() { - display_func(NULL); - - SDL_Event event; - bool quit=false; - do { - SDL_WaitEvent(&event); // passive waiting on an event - switch (event.type) - { - case SDL_QUIT: // if SDL_Quit() called - quit=true; - break; - case SDL_WINDOWEVENT: - if (event.window.event==SDL_WINDOWEVENT_CLOSE) // if windows closed by user + pthread_t threads[5]; + + + display_func(NULL); + + SDL_Event event; + bool quit=false; + do { + SDL_WaitEvent(&event); // passive waiting on an event + switch (event.type) + { + case SDL_QUIT: // if SDL_Quit() called + quit=true; + break; + case SDL_WINDOWEVENT: + if (event.window.event==SDL_WINDOWEVENT_CLOSE) // if windows closed by user + { + quit=true; + } + break; + case SDL_KEYDOWN: // if key pressed + switch(event.key.keysym.sym) { - quit=true; - } - break; - case SDL_KEYDOWN: // if key pressed - switch(event.key.keysym.sym) - { - case SDLK_ESCAPE: - quit=true; - break; - case SDLK_SPACE: - wait_key_release(); - break; - case SDLK_s: - wait_key_release(); - break; - } - } - } while(!quit); - - return 0; + case SDLK_ESCAPE: + quit=true; + break; + case SDLK_SPACE: + wait_key_release(); + break; + case SDLK_s: + wait_key_release(); + break; + } + } + } while(!quit); + + return 0; } diff --git a/one_armed_bandit.h b/one_armed_bandit.h index 13d21dbf7a4d5245a3afcf5aaef04a769e944f76..817574348ddbc3144eeec728f63dbd0e5279658d 100644 --- a/one_armed_bandit.h +++ b/one_armed_bandit.h @@ -5,6 +5,8 @@ #define OBJECT_NB 7 #define WHEEL_NB 3 +#include "wheel.h"; + typedef struct machine_a_sous{ wheel* wheels; int player_wallet; diff --git a/one_armed_bandit.o b/one_armed_bandit.o deleted file mode 100644 index 3befc5d830bdc07e6e9109c566c5e2ca22f21591..0000000000000000000000000000000000000000 Binary files a/one_armed_bandit.o and /dev/null differ diff --git a/utilities.c b/utilities.c index aff6f7ffe4b75d1fea0c7204f99f66c8546c80a9..091520bd6c4b3066ce7ba082fb85519a276e419e 100644 --- a/utilities.c +++ b/utilities.c @@ -75,8 +75,11 @@ void wait_key_release() /* return updated value: time_start + offset_ms. wait until current time reaches the time value returned. If delta time is negative, do not wait. */ -struct timespec wait_until(struct timespec time_start, unsigned long offset_ms) -{ +struct timespec wait_until( unsigned long offset_ms) +{ + struct timespec time_start; + clock_gettime(CLOCK_MONOTONIC, &time_start); + struct timespec current_time, ret_time; LL end_ns, delay_ns; diff --git a/utilities.h b/utilities.h index f5993c94a25e59bd13e420539117ee1dcb2f5966..ae1c83b7fcc83482802017467c485eedddd8d22b 100644 --- a/utilities.h +++ b/utilities.h @@ -26,7 +26,7 @@ void wait_key_release(); * Parameters: time_start: start timestamp. Can be get at first with clock_gettime(CLOCK_MONOTONIC, &time_start) offset_ms: duration of the passive waiting [ms] */ -struct timespec wait_until(struct timespec time_start, unsigned long offset_ms); +struct timespec wait_until(unsigned long offset_ms); /* Description: calculate the difference of time between a timestamp and present time * Parameters: time_start: past start timestamp. Can be get at first with clock_gettime(CLOCK_MONOTONIC, &time_start) @@ -34,4 +34,4 @@ struct timespec wait_until(struct timespec time_start, unsigned long offset_ms); */ long timespec_diff_ms(struct timespec time_start); -#endif +#endif diff --git a/utilities.o b/utilities.o deleted file mode 100644 index 2428ff3883394c0af87942959a341e1fe4796001..0000000000000000000000000000000000000000 Binary files a/utilities.o and /dev/null differ diff --git a/wheel.c b/wheel.c index 716c7910d46e889a6a986771672a770bfad4e71d..0fa41ad22568cf3df55809ba24cb0db22a4b968b 100644 --- a/wheel.c +++ b/wheel.c @@ -1,7 +1,51 @@ -int set_speed(int s){ +#include "display.h" +#include "utilities.h" +#include "wheel.h"; + +int set_speed(int s){ + return -1; } 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); + + } + } + + + +} \ No newline at end of file diff --git a/wheel.h b/wheel.h index 2ec8c28cd0789626e9f9076585395a9bb71e48ad..1ff5f12383d7e874265ca37f180527a80aa142a5 100644 --- a/wheel.h +++ b/wheel.h @@ -3,5 +3,7 @@ typedef struct roue{ int current_px; } wheel; + +int wheel_spin(double offset, int pixel,int time); int set_speed(int s); int get_current_px();