Skip to content
Snippets Groups Projects
Commit 10bcb1fc authored by leonard.beck's avatar leonard.beck :zzz:
Browse files

added game separation, fixed stuff, wheels spinning, not perfect, be real

parent c5f2460f
Branches
Tags
No related merge requests found
#include "display.h" #include "display.h"
#include "one_armed_bandit.h" #include "game.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
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;
...@@ -31,7 +32,7 @@ SDL_Window* init_display(){ ...@@ -31,7 +32,7 @@ SDL_Window* init_display(){
return window; return window;
} }
display_wheel(int pixel,int offset){ void display_wheel(int pixel,int offset){
// ------------------------- // -------------------------
// example of board display: // example of board display:
// ------------------------- // -------------------------
...@@ -42,7 +43,8 @@ display_wheel(int pixel,int offset){ ...@@ -42,7 +43,8 @@ display_wheel(int pixel,int offset){
// src_rect.y is positionned here on the 2nd object of objects.png // src_rect.y is positionned here on the 2nd object of objects.png
//85 + index*104 + 1 //85 + index*104 + 1
src_rect.y=object_height*offset; // src_rect.y= offset;
src_rect.y= offset;
dst_rect.x= pixel; dst_rect.x= pixel;
dst_rect.y=410-object_height/2; // setup the coord. of the icon in the global renderer 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_RenderCopy(renderer, objects_texture, &src_rect, &dst_rect);
...@@ -52,7 +54,7 @@ display_wheel(int pixel,int offset){ ...@@ -52,7 +54,7 @@ display_wheel(int pixel,int offset){
void *display_func(void *param){ void *display_func(void *param){
printf("\ndisplay_func"); printf("\ndisplay_func");
machine mas = *((machine *) param); machine* mas = (machine *) (param);
while(1){ while(1){
...@@ -66,7 +68,10 @@ void *display_func(void *param){ ...@@ -66,7 +68,10 @@ void *display_func(void *param){
} }
for(int i=0; i<3; i++){ for(int i=0; i<3; i++){
display_wheel(85+104*i + 1,mas.wheels[i].current_px); // pthread_mutex_lock(&mas.lock);
int current_px = mas->wheels[i].current_px;
// pthread_mutex_unlock(&mas.lock);
display_wheel(85+104*i + 1, current_px);
} }
...@@ -76,10 +81,3 @@ void *display_func(void *param){ ...@@ -76,10 +81,3 @@ void *display_func(void *param){
} }
return NULL; return NULL;
} }
...@@ -16,14 +16,13 @@ static SDL_Renderer *renderer; ...@@ -16,14 +16,13 @@ static SDL_Renderer *renderer;
static SDL_Texture *one_arm_texture, *objects_texture, *coin_texture; static SDL_Texture *one_arm_texture, *objects_texture, *coin_texture;
static SDL_Rect one_arm_rect, coin_rect; static SDL_Rect one_arm_rect, coin_rect;
static SDL_Rect object_rect; static SDL_Rect object_rect;
int object_height; // global for convenience
void *display_func(void *param);
display_wheel(int pixel,int offset);
SDL_Window* init_display(); SDL_Window* init_display();
void *display_func(void *param);
void display_wheel(int pixel,int offset);
// height of one single object // height of one single object
extern int object_height; // global for convenience // extern int object_height; // global for convenience
#endif #endif
game.c 0 → 100644
#include "game.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;
// ret.lock = PTHREAD_MUTEX_INITIALIZER;
return ret;
}
machine new_machine(machine mas, int nb_wheels){
for (int i = 0; i < nb_wheels; i++) {
mas.wheels[i] = create_wheel(96,nb_wheels-i);
}
}
game.h 0 → 100644
#ifndef GAME_H
#define GAME_H
#define OBJECT_NB 7
#define WHEEL_NB 3
#include "wheel.h"
typedef struct machine_a_sous{
wheel wheels[WHEEL_NB];
int player_wallet;
int machine_wallet;
} machine;
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);
#endif
// Template of one-armed bandit lab // Template of one-armed bandit lab
// KB main, thread handling
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -8,32 +11,11 @@ ...@@ -8,32 +11,11 @@
#include <semaphore.h> #include <semaphore.h>
#include "utilities.h" #include "utilities.h"
#include "display.h" #include "display.h"
#include "game.h"
#include "one_armed_bandit.h" #include "one_armed_bandit.h"
#include "threads.h" #include "threads.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() int main()
{ {
...@@ -45,9 +27,7 @@ int main() ...@@ -45,9 +27,7 @@ int main()
machine mas; machine mas;
mas.wheels[0] = create_wheel(85,6); mas = new_machine(mas, 3);
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++){ 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]);
......
#ifndef ONE_ARM_H
#define ONE_ARM_H
#define OBJECT_NB 7
#define WHEEL_NB 3
#include "wheel.h";
typedef struct machine_a_sous{
wheel wheels[WHEEL_NB];
int player_wallet;
int machine_wallet;
} machine;
int insert_coin();
int set_wheel_speed(int index);
int next_wheel();
#endif
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| | | |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |
*/ */
// TODO: MALLOOOOC
#ifndef THREADS_H
#define THREADS_H
#define __GNU_SOURCE #define __GNU_SOURCE
// #include <stdio.h> // #include <stdio.h>
...@@ -29,3 +35,5 @@ int wait_for_thread(pthread_t t); ...@@ -29,3 +35,5 @@ int wait_for_thread(pthread_t t);
int run_thread_and_wait (void* (*fn )(void*), void *arg); int run_thread_and_wait (void* (*fn )(void*), void *arg);
int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int t_cnt); int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int t_cnt);
int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt); int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt);
#endif
#include "display.h" // #include "display.h"
#include "utilities.h" #include "utilities.h"
#include "wheel.h"; #include "wheel.h"
int set_speed(int s){ int set_speed(int s){
...@@ -19,27 +19,33 @@ int get_current_px(){ ...@@ -19,27 +19,33 @@ int get_current_px(){
void *wheel_func(void *param){ void *wheel_func(void *param){
printf("\nwheel_func"); 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); 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) {
// mutex
this->current_px = (this->current_px + 2) % 896 ; this->current_px = (this->current_px + 2) % 896 ;
// mutex
}
} }
int wheel_delay_ms(wheel this){ int wheel_delay_ms(wheel this){
if (this.speed) {
return this.speed * 2; return this.speed * 2;
} return 2;
} }
int stop_wheel(wheel this){ int stop_wheel(wheel this){
while (this.current_px % 128 != 0) { while (this.current_px % 128 != 32) { // 32 is the offset to center the item
wheel_spin_1_tick(&this); wheel_spin_1_tick(&this);
wait_until(wheel_delay_ms(this)); wait_until(wheel_delay_ms(this));
} }
......
#ifndef WHEEL_H
#define WHEEL_H
typedef struct roue{ typedef struct roue{
int speed; int speed;
int current_px; int current_px;
// pthread_mutex_t lock;
} wheel; } wheel;
...@@ -10,3 +14,6 @@ void *wheel_func(void *param); ...@@ -10,3 +14,6 @@ void *wheel_func(void *param);
int wheel_spin_1_tick(wheel* this); int wheel_spin_1_tick(wheel* this);
int wheel_delay_ms(wheel this); int wheel_delay_ms(wheel this);
int stop_wheel(wheel this); int stop_wheel(wheel this);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment