Skip to content
Snippets Groups Projects
Commit fe1396ff authored by benjamin.sitbon's avatar benjamin.sitbon
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
Notes installation SDL2
-----------------------
- Sur Ubuntu:
sudo apt-get install libsdl2-dev libsdl2-image-dev
- les fichiers "include" se retrouvent dans /usr/include/SDL2
- les librairies (SDL2 et SDL2_image) dans /usr/lib/x86_64-linux-gnu/
\ No newline at end of file
coin.png 0 → 100644
coin.png

8.87 KiB

display.c 0 → 100644
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>
#include "utilities.h"
#include "display.h"
#include <SDL2/SDL.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_Texture *one_arm_texture, *objects_texture, *coin_texture;
static SDL_Rect one_arm_rect, coin_rect;
static SDL_Rect object_rect;
int object_height; // global for convenience
void *display_func(void *param)
{
int one_arm_width, one_arm_height;
SDL_Window *window;
int i;
// -------------------------
// Graphic initialization
// -------------------------
assert(SDL_Init(SDL_INIT_VIDEO) == 0);
get_image_file_size("./one_armed_bandit.png", &one_arm_width, &one_arm_height);
assert((window = SDL_CreateWindow("one_armed_bandit", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, one_arm_width,
one_arm_height, SDL_WINDOW_OPENGL))!=NULL);
assert((renderer = SDL_CreateRenderer(window, -1, 0))!=NULL);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); // allow transparent mode!
assert((one_arm_texture=create_texture_from_image_file(renderer, "one_armed_bandit.png",
&one_arm_rect))!=NULL);
assert((coin_texture=create_texture_from_image_file(renderer, "coin.png",
&coin_rect))!=NULL);
// create the global texture containing all objects:
assert((objects_texture=create_texture_from_image_file(renderer, "objects.png",
&object_rect))!=NULL);
object_height=object_rect.h/9;
// -------------------------
// example of board display:
// -------------------------
SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect);
SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h};
SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos);
for (i=0; i<4; i++){
SDL_Rect coin_rect_pos={700, 400-10*i, coin_rect.w, coin_rect.h};
SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos);
}
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
src_rect.y=object_height;
dst_rect.x=85;
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);
// src_rect.y is positionned here on the 2nd object of objects.png
src_rect.y=object_height;
dst_rect.x=190;
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);
// src_rect.y is positionned here on the 2nd object of objects.png
src_rect.y=object_height;
dst_rect.x=295;
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_RenderPresent(renderer);
// -------------------------
// 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(one_arm_texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return NULL;
}
#ifndef DISPLAY_H
#define DISPLAY_H
void *display_func(void *param);
// height of one single object
extern int object_height; // global for convenience
#endif
\ No newline at end of file
display.o 0 → 100644
File added
makefile 0 → 100644
GCC=gcc -O2 -Wall -std=gnu99 -g
LIBS=-lSDL2 -lSDL2_image -lpthread
BIN=one_armed_bandit
SRCS=$(wildcard *.c)
OBJS=$(SRCS:.c=.o)
HEADERS=$(wildcard *.h)
$(BIN): $(OBJS)
$(GCC) $^ -o $@ $(LIBS)
run: $(BIN)
./$<
%.o: %.c $(HEADERS)
$(GCC) $< -c
clean:
rm -f $(BIN) $(OBJS)
objects.png

161 KiB

File added
// Template of one-armed bandit lab
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include "utilities.h"
#include "display.h"
#include "one_armed_bandit.h"
int main()
{
display_func(NULL);
return 0;
}
#ifndef ONE_ARM_H
#define ONE_ARM_H
#define OBJECT_NB 7
#define WHEEL_NB 3
#endif
File added
one_armed_bandit.png

893 KiB

#include <stdbool.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL.h>
typedef long long LL;
static bool img_init=false;
void get_image_file_size(const char *img_filename, int *im_width, int *im_height)
{
SDL_Surface *img_surface;
int img_flags=IMG_INIT_PNG;
if (!img_init)
{
img_init=true;
if( !( IMG_Init( img_flags ) & img_flags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
exit(1);
}
}
assert((img_surface = IMG_Load(img_filename))!=NULL);
*im_width=img_surface->w;
*im_height=img_surface->h;
SDL_FreeSurface(img_surface);
}
SDL_Texture *create_texture_from_image_file(SDL_Renderer *renderer, const char *img_filename, SDL_Rect *img_rect)
{
SDL_Surface *img_surface;
SDL_Texture *texture;
int img_flags=IMG_INIT_PNG;
if (!img_init)
{
img_init=true;
if( !( IMG_Init( img_flags ) & img_flags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
exit(1);
}
}
img_surface = IMG_Load(img_filename);
SDL_GetClipRect(img_surface, img_rect);
assert((texture = SDL_CreateTextureFromSurface(renderer, img_surface))!=NULL);
SDL_FreeSurface(img_surface);
return texture;
}
void wait_key_release()
{
SDL_Event event;
while( 1 )
{
if (SDL_PollEvent(&event)) {
if (event.type==SDL_KEYUP)
{
printf("key released\n");
break;
}
}
}
}
/* return updated value: time_start + offset_ms. wait until current time reaches
the time value returned. If delta time is negative, do not wait. */
struct timespec wait_until(struct timespec time_start, unsigned long offset_ms)
{
struct timespec current_time, ret_time;
LL end_ns, delay_ns;
end_ns=(LL)time_start.tv_sec*(LL)1000000000+(LL)time_start.tv_nsec + (LL)offset_ms*(LL)1000000;
ret_time.tv_sec=end_ns/1000000000;
ret_time.tv_nsec=end_ns-((LL)ret_time.tv_sec*(LL)1000000000);
clock_gettime(CLOCK_MONOTONIC, &current_time);
delay_ns=end_ns-((LL)current_time.tv_sec*(LL)1000000000+(LL)current_time.tv_nsec);
if (delay_ns>0)
{
//printf("wait: %ld us\n", (long)(delay_ns/1000));
usleep((unsigned long)(delay_ns/1000));
}
return ret_time;
}
long timespec_diff_ms(struct timespec time_start)
{
struct timespec current_time;
long start_ms;
start_ms=(LL)time_start.tv_sec*(LL)1000+(LL)time_start.tv_nsec/1000000;
clock_gettime(CLOCK_MONOTONIC, &current_time);
return (long int)((current_time.tv_sec*1000+(long)(current_time.tv_nsec/1000000LL))-start_ms);
}
#ifndef UTILS_H
#define UTILS_H
#include <time.h>
#include <SDL2/SDL.h>
/* Description: get the image size from an image file
* Parameters: bmp_filename: file name string
im_width: pointer on returned width
im_height: pointer on returned height
*/
void get_image_file_size(char *bmp_filename, int *im_width, int *im_height);
/* Description: create a SDL texture from an image file for the specified renderer
* Parameters: renderer: pointer on the SDL renderer
img_filename: file name string
img_rect: pointer on the image dimmensions
*/
SDL_Texture *create_texture_from_image_file(SDL_Renderer *renderer, const char *img_filename, SDL_Rect *img_rect);
/* Description: wait for any key to be released (SDL based function) */
void wait_key_release();
/* Description: passive waiting from a timestamp for a duration of offset_ms
* Parameters: time_start: start timestamp. Can be get at first with clock_gettime(CLOCK_MONOTONIC, &time_start)
offset_ms: duration of the passive waiting [ms]
*/
struct timespec wait_until(struct timespec time_start, unsigned long offset_ms);
/* Description: calculate the difference of time between a timestamp and present time
* Parameters: time_start: past start timestamp. Can be get at first with clock_gettime(CLOCK_MONOTONIC, &time_start)
Return : time elapsed between time_start and now [ms]
*/
long timespec_diff_ms(struct timespec time_start);
#endif
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment