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

comments and cleanup

parent 6948091f
No related branches found
No related tags found
No related merge requests found
// In charge of all the display using SDL2
#include "display.h" #include "display.h"
#include "game.h" #include "game.h"
// the following global variables are defined for graphical objects only:
// the images size is 104x128 pixels
int object_height; // global for convenience int object_height; // global for convenience
...@@ -44,7 +43,6 @@ void display_wheel(int pixel,int offset){ ...@@ -44,7 +43,6 @@ void 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= 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
...@@ -55,7 +53,7 @@ void display_wheel(int pixel,int offset){ ...@@ -55,7 +53,7 @@ void display_wheel(int pixel,int offset){
void *display_func(void *param){ void *display_func(void *param){
SDL_Window* window = init_display(); SDL_Window* window = init_display();
printf("\ndisplay_func"); // printf("\ndisplay_func");
machine* mas = (machine *) (param); machine* mas = (machine *) (param);
while(1){ while(1){
......
...@@ -12,17 +12,19 @@ ...@@ -12,17 +12,19 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
// the following global variables are defined for graphical objects only:
// the images size is 104x128 pixels
static SDL_Renderer *renderer; 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;
SDL_Window* init_display(); SDL_Window* init_display();
// graphic initialization
void *display_func(void *param); void *display_func(void *param);
// run by the display thread, displays the wallets, the wheels
void display_wheel(int pixel,int offset); void display_wheel(int pixel,int offset);
// displays each wheel
// height of one single object
// extern int object_height; // global for convenience
#endif #endif
#include "game.h" // abstraction layer between display and wheels (cf 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){ #include "game.h"
wheel ret;
ret.current_px = pixel;
ret.speed = speed;
pthread_mutex_init(&ret.lock,NULL);
return ret;
}
void new_machine(machine* mas, int nb_wheels,int player, int machine){ void new_machine(machine* mas, int nb_wheels,int player, int machine){
for (int i = 0; i < nb_wheels; i++) { for (int i = 0; i < nb_wheels; i++) {
......
...@@ -6,10 +6,6 @@ ...@@ -6,10 +6,6 @@
#define WHEEL_NB 3 #define WHEEL_NB 3
#include "wheel.h" #include "wheel.h"
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
typedef struct machine_a_sous{ typedef struct machine_a_sous{
wheel wheels[WHEEL_NB]; wheel wheels[WHEEL_NB];
...@@ -17,10 +13,6 @@ typedef struct machine_a_sous{ ...@@ -17,10 +13,6 @@ typedef struct machine_a_sous{
int machine_wallet; int machine_wallet;
} machine; } machine;
int insert_coin();
int set_wheel_speed(int index);
int next_wheel();
wheel create_wheel(int pixel,int speed);
void new_machine(machine* mas, int nb_wheels,int player, int machine); void new_machine(machine* mas, int nb_wheels,int player, int machine);
#endif #endif
// Template of one-armed bandit lab /**
// KB main, thread handling * One Armed Bandit
* 24.05.21
* Authors : Sitbon benjamin, Beck Léonard
**/
// Keyboard thread and game initialization
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -11,9 +14,8 @@ ...@@ -11,9 +14,8 @@
#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 "threads.h" #include "threads.h"
#include "one_armed_bandit.h"
......
#include "game.h"
/*
_____ ___ _ ______ _ _ _
| _ | / _ \ | | | ___ \ | (_) |
| | | |_ __ ___ / /_\ \_ __ _ __ ___ ___ __| | | |_/ / __ _ _ __ __| |_| |_
| | | | '_ \ / _ \ | _ | '__| '_ ` _ \ / _ \/ _` | | ___ \/ _` | '_ \ / _` | | __|
\ \_/ / | | | __/ | | | | | | | | | | | __/ (_| | | |_/ / (_| | | | | (_| | | |_
\___/|_| |_|\___| \_| |_/_| |_| |_| |_|\___|\__,_| \____/ \__,_|_| |_|\__,_|_|\__|
/\/\
| |
(____|_____|____)
| o o |
|__^__|
\#####/
\###/
\#/
*/
int how_many_points(machine mas);
// returns the score based on the positionning of the wheels
// Small library to ease the use of pthread
// TODO: MALLOOOOC
#define __GNU_SOURCE #define __GNU_SOURCE
#include <stdio.h> #include <stdio.h>
......
/**
* Authors : Sitbon Benjamin, Beck Léonard
* Small library to ease the use of pthread
*/
/*
___ _
/ _ )___ _________(_)__ ____
/ _ / _ `/ __/ __/ / -_) __/
/____/\_,_/_/ /_/ /_/\__/_/
|^| |^| |^| |^|
| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |
| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |
| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |
*/
// TODO: MALLOOOOC
#ifndef THREADS_H #ifndef THREADS_H
#define THREADS_H #define THREADS_H
#define __GNU_SOURCE #define __GNU_SOURCE
// #include <stdio.h>
// #include <stdlib.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
......
// #include "display.h" // in charge of each of the game's "wheel" independantly (cf. wheel.h)
#include "utilities.h" #include "utilities.h"
#include "wheel.h" #include "wheel.h"
wheel create_wheel(int pixel,int speed){
int set_speed(int s){ wheel ret;
return -1; ret.current_px = pixel;
} ret.speed = speed;
pthread_mutex_init(&ret.lock,NULL);
int get_current_px(){ return ret;
return -1;
} }
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){
...@@ -38,8 +38,7 @@ int wheel_delay_ms(wheel this){ ...@@ -38,8 +38,7 @@ int wheel_delay_ms(wheel this){
} }
int stop_wheel(wheel* this){ int stop_wheel(wheel* this){
while (this->current_px % 128 != 96) { // 32 is the offset to center the item while (this->current_px % 128 != 96) { // 96 is the offset to center the item
//wheel_spin_1_tick(this);
wait_until(wheel_delay_ms(*this)); wait_until(wheel_delay_ms(*this));
} }
this->speed = 0; this->speed = 0;
......
...@@ -5,17 +5,27 @@ ...@@ -5,17 +5,27 @@
typedef struct roue{ typedef struct roue{
int speed; int speed;
// speed is either 1 (the fastest), 2, 3 (the slowest) or 0 (stopped)
int current_px; int current_px;
// stores the position of the wheel, used to determine what item is visible
pthread_mutex_t lock; pthread_mutex_t lock;
// used to synchronize cache with memory so display is always up to date
} wheel; } wheel;
int set_speed(int s); wheel create_wheel(int pixel,int speed);
int get_current_px();
void *wheel_func(void *param); void *wheel_func(void *param);
// function run by each wheel thread, turns the wheel
int wheel_spin_1_tick(wheel* this); int wheel_spin_1_tick(wheel* this);
// spins the wheel
int wheel_delay_ms(wheel this); int wheel_delay_ms(wheel this);
// calculates the delay based on the speed
int stop_wheel(wheel* this); int stop_wheel(wheel* this);
// stops the wheel on the right pixel to have a centered item
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment