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
Branches
Tags
No related merge requests found
#include "display.h"
#include "one_armed_bandit.h"
// the following global variables are defined for graphical objects only:
// the images size is 104x128 pixels
void *display_func(void *param){
SDL_Window* init_display(){
int one_arm_width, one_arm_height;
SDL_Window *window;
......@@ -28,8 +28,34 @@ void *display_func(void *param){
&object_rect))!=NULL);
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_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h};
......@@ -39,21 +65,15 @@ void *display_func(void *param){
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);
wait_until(20);
// -------------------------
// managing events:
// -------------------------
SDL_DestroyTexture(objects_texture);
SDL_DestroyTexture(one_arm_texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
return NULL;
}
......
......@@ -19,7 +19,8 @@ static SDL_Rect object_rect;
int object_height; // global for convenience
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
extern int object_height; // global for convenience
......
......@@ -7,7 +7,10 @@
#include <pthread.h>
#include <semaphore.h>
#include "utilities.h"
#include "display.h"
#include "one_armed_bandit.h"
#include "threads.h"
int insert_coin(){
......@@ -34,10 +37,29 @@ wheel create_wheel(int pixel,int speed){
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;
bool quit=false;
......
......@@ -8,7 +8,7 @@
#include "wheel.h";
typedef struct machine_a_sous{
wheel* wheels;
wheel wheels[WHEEL_NB];
int player_wallet;
int machine_wallet;
} machine;
......
......@@ -11,41 +11,37 @@ 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){
printf("\nwheel_func");
wheel id = *((wheel *) param);
while(1){
for(double a = 0; a<8.0; a +=0.1){
wheel_spin(a,id.current_px,id.speed);
// for(double a = 0; a<8.0; a +=0.1){
// 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{
} wheel;
int wheel_spin(double offset, int pixel,int time);
int set_speed(int s);
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