Skip to content
Snippets Groups Projects
Commit c5f2460f authored by benjamin.sitbon's avatar benjamin.sitbon
Browse files

ça marche pas mais ça compile

parent 2908038c
No related branches found
No related tags found
No related merge requests found
#include "display.h" #include "display.h"
#include "one_armed_bandit.h"
// the following global variables are defined for graphical objects only: // the following global variables are defined for graphical objects only:
// the images size is 104x128 pixels // the images size is 104x128 pixels
SDL_Window* init_display(){
void *display_func(void *param){
int one_arm_width, one_arm_height; int one_arm_width, one_arm_height;
SDL_Window *window; SDL_Window *window;
...@@ -28,8 +28,34 @@ void *display_func(void *param){ ...@@ -28,8 +28,34 @@ void *display_func(void *param){
&object_rect))!=NULL); &object_rect))!=NULL);
object_height=object_rect.h/9; object_height=object_rect.h/9;
return window;
}
display_wheel(int pixel,int offset){
// -------------------------
// 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);
}
void *display_func(void *param){
printf("\ndisplay_func");
machine mas = *((machine *) param);
while(1){
SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect); SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect);
SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h}; SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h};
...@@ -39,21 +65,15 @@ void *display_func(void *param){ ...@@ -39,21 +65,15 @@ void *display_func(void *param){
SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos);
} }
for(int i=0; i<3; i++){
display_wheel(85+104*i + 1,mas.wheels[i].current_px);
}
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
wait_until(20); wait_until(20);
// -------------------------
// managing events:
// -------------------------
}
SDL_DestroyTexture(objects_texture);
SDL_DestroyTexture(one_arm_texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return NULL; return NULL;
} }
......
...@@ -19,7 +19,8 @@ static SDL_Rect object_rect; ...@@ -19,7 +19,8 @@ static SDL_Rect object_rect;
int object_height; // global for convenience int object_height; // global for convenience
void *display_func(void *param); void *display_func(void *param);
int wheel_spin(double offset,int pixel,int time); display_wheel(int pixel,int offset);
SDL_Window* init_display();
// height of one single object // height of one single object
extern int object_height; // global for convenience extern int object_height; // global for convenience
......
...@@ -7,7 +7,10 @@ ...@@ -7,7 +7,10 @@
#include <pthread.h> #include <pthread.h>
#include <semaphore.h> #include <semaphore.h>
#include "utilities.h" #include "utilities.h"
#include "display.h"
#include "one_armed_bandit.h" #include "one_armed_bandit.h"
#include "threads.h"
int insert_coin(){ int insert_coin(){
...@@ -34,10 +37,29 @@ wheel create_wheel(int pixel,int speed){ ...@@ -34,10 +37,29 @@ wheel create_wheel(int pixel,int speed){
int main() int main()
{ {
pthread_t threads[5];
SDL_Window* window = init_display();
pthread_t threads[4];
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);
for(int i = 0; i<3; i++){
run_thread(&threads[i],wheel_func,&mas.wheels[i]);
}
display_func(&mas);
display_func(NULL); SDL_DestroyTexture(objects_texture);
SDL_DestroyTexture(one_arm_texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
SDL_Event event; SDL_Event event;
bool quit=false; bool quit=false;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "wheel.h"; #include "wheel.h";
typedef struct machine_a_sous{ typedef struct machine_a_sous{
wheel* wheels; wheel wheels[WHEEL_NB];
int player_wallet; int player_wallet;
int machine_wallet; int machine_wallet;
} machine; } machine;
......
...@@ -11,41 +11,37 @@ int get_current_px(){ ...@@ -11,41 +11,37 @@ int get_current_px(){
return -1; 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){ void *wheel_func(void *param){
printf("\nwheel_func");
wheel id = *((wheel *) param); wheel id = *((wheel *) param);
while(1){ while(1){
for(double a = 0; a<8.0; a +=0.1){ // for(double a = 0; a<8.0; a +=0.1){
wheel_spin(a,id.current_px,id.speed);
// wheel_spin(a,id.current_px,id.speed);
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 ;
}
int wheel_delay_ms(wheel this){
return this.speed * 2;
}
int stop_wheel(wheel this){
while (this.current_px % 128 != 0) {
wheel_spin_1_tick(&this);
wait_until(wheel_delay_ms(this));
}
this.speed = 0;
} }
\ No newline at end of file
...@@ -4,6 +4,9 @@ typedef struct roue{ ...@@ -4,6 +4,9 @@ typedef struct roue{
} wheel; } wheel;
int wheel_spin(double offset, int pixel,int time);
int set_speed(int s); int set_speed(int s);
int get_current_px(); int get_current_px();
void *wheel_func(void *param);
int wheel_spin_1_tick(wheel* this);
int wheel_delay_ms(wheel this);
int stop_wheel(wheel this);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment