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