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

restructure and added threads lib

parent 5188ca59
Branches
Tags
No related merge requests found
...@@ -56,38 +56,6 @@ void *display_func(void *param) ...@@ -56,38 +56,6 @@ void *display_func(void *param)
// ------------------------- // -------------------------
// managing events: // managing events:
// ------------------------- // -------------------------
SDL_Event event;
bool quit=false;
do {
SDL_WaitEvent(&event); // passive waiting on an event
switch (event.type)
{
case SDL_QUIT: // if SDL_Quit() called
quit=true;
break;
case SDL_WINDOWEVENT:
if (event.window.event==SDL_WINDOWEVENT_CLOSE) // if windows closed by user
{
quit=true;
}
break;
case SDL_KEYDOWN: // if key pressed
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit=true;
break;
case SDLK_SPACE:
wait_key_release();
break;
case SDLK_s:
wait_key_release();
break;
}
}
} while(!quit);
SDL_DestroyTexture(objects_texture); SDL_DestroyTexture(objects_texture);
......
...@@ -10,11 +10,53 @@ ...@@ -10,11 +10,53 @@
#include "display.h" #include "display.h"
#include "one_armed_bandit.h" #include "one_armed_bandit.h"
int insert_coin(){
}
int set_wheel_speed(int index){
}
int next_wheel(){
}
int main() int main()
{ {
display_func(NULL); display_func(NULL);
SDL_Event event;
bool quit=false;
do {
SDL_WaitEvent(&event); // passive waiting on an event
switch (event.type)
{
case SDL_QUIT: // if SDL_Quit() called
quit=true;
break;
case SDL_WINDOWEVENT:
if (event.window.event==SDL_WINDOWEVENT_CLOSE) // if windows closed by user
{
quit=true;
}
break;
case SDL_KEYDOWN: // if key pressed
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit=true;
break;
case SDLK_SPACE:
wait_key_release();
break;
case SDLK_s:
wait_key_release();
break;
}
}
} while(!quit);
return 0; return 0;
} }
...@@ -5,5 +5,14 @@ ...@@ -5,5 +5,14 @@
#define OBJECT_NB 7 #define OBJECT_NB 7
#define WHEEL_NB 3 #define WHEEL_NB 3
typedef struct machine_a_sous{
wheel* wheels;
int player_wallet;
int machine_wallet;
} machine;
int insert_coin();
int set_wheel_speed(int index);
int next_wheel();
#endif #endif
#define __GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include "threads.h"
int run_thread(pthread_t* t, void* (*fn )(void*), void *arg){
int err = pthread_create(t, NULL, fn, arg);
if (err) perror("Thread creation error");
return err;
}
int wait_for_thread(pthread_t t){
int err = pthread_join(t, NULL);
if (err) perror("Thread join error");
// else printf("thread joined\n");
return err;
}
int run_thread_and_wait (void* (*fn )(void*), void *arg){
pthread_t t;
int err = pthread_create(&t, NULL, fn, arg);
if (err) {
perror("Thread creation error");
return err;
}
err = pthread_join(t, NULL);
if (err) perror("Thread join error");
//else printf("thread joined\n");
return err;
}
int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int t_cnt){
pthread_t *t = malloc(t_cnt*sizeof(pthread_t));
for(int i = 0; i<t_cnt; i++){
int err = run_thread(&t[i], fn, (*arg) + size_of_arg_type*i);
if(err) return 1;
}
for(int i = 0; i<t_cnt; i++){
int err = wait_for_thread(t[i]);
if(err) return 2;
}
free(t);
return 0;
}
int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt){
pthread_t *t = malloc(t_cnt*sizeof(pthread_t));
for(int i = 0; i<t_cnt; i++){
int err = run_thread(&t[i], fn, arg);
if(err) return 1;
}
for(int i = 0; i<t_cnt; i++){
int err = wait_for_thread(t[i]);
if(err) return 2;
}
free(t);
return 0;
}
/**
* Authors : Sitbon Benjamin, Beck Léonard
* Small library to ease the use of pthread
*/
/*
___ _
/ _ )___ _________(_)__ ____
/ _ / _ `/ __/ __/ / -_) __/
/____/\_,_/_/ /_/ /_/\__/_/
|^| |^| |^| |^|
| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |
| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |
| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |-| |
*/
#define __GNU_SOURCE
// #include <stdio.h>
// #include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
int run_thread(pthread_t* t, void* (*fn )(void*), void *arg);
int wait_for_thread(pthread_t t);
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_single_param(void* (*fn )(void*), void* arg, int t_cnt);
int set_speed(int s){
}
int get_current_px(){
}
typedef struct roue{
int speed;
int current_px;
} wheel;
int set_speed(int s);
int get_current_px();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment