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

wheels spinning well

parent cd501d38
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@
int object_height; // global for convenience
SDL_Window* init_display(){
int one_arm_width, one_arm_height;
SDL_Window *window;
......@@ -53,8 +54,8 @@ void display_wheel(int pixel,int offset){
void *display_func(void *param){
printf("\ndisplay_func");
SDL_Window* window = init_display();
printf("\ndisplay_func");
machine* mas = (machine *) (param);
while(1){
......@@ -69,10 +70,10 @@ void *display_func(void *param){
}
for(int i=0; i<3; i++){
//pthread_mutex_lock(&mas->wheels[i].lock);
pthread_mutex_lock(&mas->wheels[i].lock);
int current_px = mas->wheels[i].current_px;
//pthread_mutex_unlock(&mas->wheels[i].lock);
display_wheel(85+104*i + 1, current_px);
pthread_mutex_unlock(&mas->wheels[i].lock);
display_wheel(85+(104*i + 1), current_px);
}
......
......@@ -18,13 +18,13 @@ wheel create_wheel(int pixel,int speed){
wheel ret;
ret.current_px = pixel;
ret.speed = speed;
//pthread_mutex_init(&ret.lock,NULL);
pthread_mutex_init(&ret.lock,NULL);
return ret;
}
machine new_machine(machine mas, int nb_wheels){
void new_machine(machine* mas, int nb_wheels){
for (int i = 0; i < nb_wheels; i++) {
mas.wheels[i] = create_wheel(96,nb_wheels-i);
mas->wheels[i] = create_wheel(96,nb_wheels-i);
}
}
......@@ -7,6 +7,8 @@
#include "wheel.h"
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
typedef struct machine_a_sous{
......@@ -19,6 +21,6 @@ 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);
void new_machine(machine* mas, int nb_wheels);
#endif
......@@ -23,13 +23,13 @@ int main()
pthread_t threads[4];
machine mas;
mas = new_machine(mas, 3);
run_thread(&threads[3],display_func,&mas);
new_machine(&mas, 3);
for(int i = 0; i<3; i++){
run_thread(&threads[i],wheel_func,&mas.wheels[i]);
}
run_thread(&threads[3],display_func,&mas);
......
......@@ -3,6 +3,7 @@
#include "wheel.h"
int set_speed(int s){
return -1;
}
......@@ -26,15 +27,15 @@ void *wheel_func(void *param){
// wheel_spin(a,id.current_px,id.speed);
wheel_spin_1_tick(id);
//wait_until(wheel_delay_ms(*id));
wait_until(wheel_delay_ms(*id));
}
}
int wheel_spin_1_tick(wheel* this){
if (this->speed) {
// mutex
pthread_mutex_lock(&this->lock);
this->current_px = (this->current_px + 2) % 896 ;
// mutex
pthread_mutex_unlock(&this->lock);
}
}
......
......@@ -6,7 +6,7 @@
typedef struct roue{
int speed;
int current_px;
//pthread_mutex_t lock;
pthread_mutex_t lock;
} wheel;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment