Skip to content
Snippets Groups Projects
Commit 72e2e47d authored by maberet's avatar maberet
Browse files

Merge branch 'main' into qlearn

parents 9be22fa0 3f8041c2
No related branches found
No related tags found
No related merge requests found
Showing
with 834 additions and 915 deletions
#include "render.h"
SDL_Window *window;
SDL_Renderer *renderer;
int window_width = 900;
int window_height = 700;
TTF_Font *robotoFont;
int zoom = 40;
SDL_Rect terrain;
SDL_Rect filet;
SDL_Rect canon_rect;
SDL_Rect point_de_chute;
int point_x_rand;
int point_y_rand;
int zone_canon = -1;
int zone_chute = -1;
void createWindow(){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Couldn't create window.");
exit(EXIT_FAILURE);
}
window = SDL_CreateWindow("Badminton learning", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width, window_height, SDL_WINDOW_RESIZABLE);
if (window == NULL){
printf("Couldn't create window");
exit(EXIT_FAILURE);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
printf("Couldn't create renderer.");
exit(EXIT_FAILURE);
}
if (TTF_Init() == -1)
{
exit(EXIT_FAILURE);
}
robotoFont = TTF_OpenFont("assets/Roboto-Black.ttf", 50);
}
void endSDL(){
TTF_CloseFont(robotoFont);
TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void initTerrain(){
terrain.x = 50;
terrain.y = 50;
terrain.h = 13.40 * zoom;
terrain.w = 5.20 * zoom;
}
void initPointDeChute(){
initTerrain();
srand(time(NULL));
point_x_rand = (int)rand()%terrain.w;
point_y_rand = (int)rand()%(terrain.h/2);
}
int getZoneChute(int terrainX, int terrainY, int terrainW, int terrainH){
int z = -1;
// pdc = point de chute
int pdc_x = terrainX + point_x_rand;
int pdc_y = terrainY + terrainH/2 + point_y_rand;
//en haut à gauche => 1
if(point_x_rand>=0 && point_x_rand<terrainW/2 && pdc_y<terrainY+(3*terrainH)/4){
z = 1;
}
//en haut à droite => 2
/*else if(){
z = 2;
}
//en bas à gauche => 3
else if(point_x_rand>=0 && point_x_rand<terrainW/2 && ){
z = 3;
}
//en bas à droite => 4
*/else{
z = -6;
}
printf("point_x_rand : %d, terrainW/2: %d\n", point_x_rand, terrainW/2);
printf("pdc_x : %d, pdc_y : %d, tX : %d, tY : %d\n", pdc_x, pdc_y, terrainX, terrainY);
return z;
}
void newCanon(){
initTerrain();
srand(time(NULL));
canon.x = (int)rand()%terrain.w;
canon.y = (int)rand()%(terrain.h/2);
}
void drawString(char *text, int x, int y, int w, int h, int r, int g, int b, int a){
SDL_Color color = {r, g, b, a};
SDL_Surface *surface = TTF_RenderText_Solid(robotoFont, text, color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect rect = {x, y, w, h};
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void drawTerrainTopView(){
//terrain
initTerrain();
//filet
filet.x = terrain.x;
filet.h = 4;
filet.y = terrain.y + terrain.h/2 - filet.h/2;
filet.w = terrain.w;
//terrain en blanc
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &terrain);
//filet en vert
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderFillRect(renderer, &filet);
//decoupage des zones
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
//verticale zone haut
SDL_RenderDrawLine(renderer, terrain.x+terrain.w/2, terrain.y, terrain.x+terrain.w/2, terrain.y+terrain.h/2);
//horizontale zone haut
SDL_RenderDrawLine(renderer, terrain.x, terrain.y+terrain.h/4, terrain.x+terrain.w, terrain.y+terrain.h/4);
//verticale bas
SDL_RenderDrawLine(renderer, terrain.x+terrain.w/2, terrain.y+terrain.h/2, terrain.x+terrain.w/2, terrain.y+terrain.h);
//horizontale bas
SDL_RenderDrawLine(renderer, terrain.x, terrain.y+(3*terrain.h)/4, terrain.x+terrain.w, terrain.y+(3*terrain.h)/4);
}
void drawTerrainSideView(){
//terrain
terrain.x = terrain.x + terrain.w + 50;
terrain.y = terrain.y + terrain.h;
terrain.h = 4;
terrain.w = 13.40 * zoom;
//filet
filet.w = 4;
filet.x = terrain.x + terrain.w/2 - filet.w/2;
filet.h = -1.55 * zoom;
filet.y = terrain.y;
//terrain en blanc
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &terrain);
//filet en vert
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderFillRect(renderer, &filet);
}
void drawCanonTopView(){
//canon
canon_rect.w = canon.width;
canon_rect.h = canon.length;
canon_rect.x = terrain.x + canon.x;
canon_rect.y = terrain.y + canon.y;
//canon en noir
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &canon_rect);
}
void drawPointDeChuteTopView(){
//point de chute de la balle
point_de_chute.w = 5;
point_de_chute.h = 5;
point_de_chute.x = (int)terrain.x + point_x_rand;
point_de_chute.y = (int)terrain.y + terrain.h/2 + point_y_rand;
//point de chute de la balle
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &point_de_chute);
}
void drawCanonSideView(){
//canon
canon_rect.w = canon.length;
canon_rect.h = canon.height;
canon_rect.x = terrain.x + canon.y;
canon_rect.y = terrain.y - canon.height;
//canon en noir
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &canon_rect);
}
void drawPointDeChuteSideView(){
//point de chute de la balle
point_de_chute.w = 5;
point_de_chute.h = 5;
point_de_chute.x = terrain.x + terrain.w/2 + point_y_rand;
point_de_chute.y = terrain.y;
//point de chute de la balle
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &point_de_chute);
}
void drawInformations(){
int texte_width = 200;
int texte_height = 40;
char str[20];
char str2[20];
char zoneChuteChaine[20] = "zone chute :";
char zoneCanonChaine[20] = "zone canon :";
drawString("informations :", window_width-texte_width, texte_height*0, texte_width, texte_height, 255, 255, 255, 255);
drawString("e : new point chute", window_width-texte_width, texte_height*1, texte_width, texte_height, 255, 255, 255, 255);
sprintf(str2, "%d", zone_chute);
strcat(zoneChuteChaine, str2);
drawString(zoneChuteChaine, window_width-texte_width, texte_height*2, texte_width, texte_height, 255, 255, 255, 255);
drawString("r : new canon", window_width-texte_width, texte_height*4, texte_width, texte_height, 255, 255, 255, 255);
sprintf(str, "%d", zone_canon);
strcat(zoneCanonChaine, str);
drawString(zoneCanonChaine, window_width-texte_width, texte_height*5, texte_width, texte_height, 255, 255, 255, 255);
}
void drawTrajectoireTopView(){
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderDrawLine(renderer, terrain.x+canon.x, terrain.y+canon.y, point_de_chute.x, point_de_chute.y);
}
void drawBall(){
/*int i;
SDL_Rect rect_life;
SDL_Rect rect_mana;
rect_life.h = 50;
rect_life.w = 50;
rect_life.x = 100;
rect_life.y = 100;
rect_mana.h = 50;
rect_mana.w = 50;
rect_mana.x = 100;
rect_mana.y = 200;*/
//draw hero life
/*SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
for(i=0; i<hero.life; i++){
SDL_RenderFillRect(renderer, &rect_life);
rect_life.x = rect_life.x + rect_life.w + 20;
}
//draw hero mana
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
for(i=0; i<hero.mana; i++){
SDL_RenderFillRect(renderer, &rect_mana);
rect_mana.x = rect_mana.x + rect_mana.w + 20;
}*/
}
void mainLoop(){
createWindow();
initCanon();
initPointDeChute();
pthread_t eventThread;
if (pthread_create(&eventThread, NULL, eventLoop, NULL) != 0){
printf("Couldn't create thread.");
exit(EXIT_FAILURE);
}
while (running){
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//top view
drawTerrainTopView();
drawCanonTopView();
drawPointDeChuteTopView();
drawTrajectoireTopView();
initTerrain();
zone_canon = getZone(terrain.x, terrain.y, terrain.w, terrain.h);
zone_chute = getZoneChute(terrain.x, terrain.y, terrain.w, terrain.h);
//side view
drawTerrainSideView();
drawCanonSideView();
drawPointDeChuteSideView();
//
drawInformations();
SDL_RenderPresent(renderer);
}
endSDL();
}
\ No newline at end of file
#ifndef _RENDER_H_
#define _RENDER_H_
#include "gest_event.h"
#include "ball.h"
#include "canon.h"
extern int zone_canon;
extern int zone_chute;
extern SDL_Rect terrain;
void createWindow();
void endSDL();
void drawString(char *, int, int, int, int, int, int, int, int);
void drawTerrainTopView();
void initPointDeChute();
void newCanon();
int getZoneChute(int, int, int, int);
void initTerrain();
//tofinish
void mainLoop();
#endif
\ No newline at end of file
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
\ No newline at end of file
-5.999587 -inf -6.000708 -inf
-6.115787 -6.123141 -6.098257 -inf
-5.971343 -5.992032 -5.971545 -inf
-5.873660 -5.995245 -5.766387 -inf
-5.800052 -5.820107 -5.747762 -inf
-5.737659 -5.803656 -5.643817 -inf
-5.739960 -5.778169 -5.726362 -inf
-5.503284 -5.739911 -5.310387 -inf
-5.480796 -5.468618 -5.357826 -inf
-5.411785 -5.517370 -5.287572 -inf
-5.258291 -5.317937 -5.354254 -inf
-5.473421 -5.461081 -5.501491 -inf
-5.532105 -5.612197 -5.399248 -inf
-5.316842 -5.451564 -5.393478 -inf
-5.300765 -5.312191 -5.293369 -inf
-5.370103 -5.378599 -5.320911 -inf
-5.457528 -5.542829 -5.541480 -inf
-5.541624 -5.493176 -5.514244 -inf
-5.530763 -5.438362 -5.470801 -inf
-inf -5.352129 -5.472239 -inf
-5.883332 -inf -5.905706 -6.024616
-6.022445 -6.029734 -6.015109 -6.098985
-5.835909 -5.867180 -5.816658 -5.927018
-5.570353 -5.554059 -5.499645 -5.518311
-5.481196 -5.542778 -5.541077 -5.515484
-5.410986 -5.449229 -5.408196 -5.451281
-5.370999 -5.343917 -5.299640 -5.334761
-5.238695 -5.264300 -5.224788 -5.204239
-5.164643 -5.121631 -5.192414 -5.261806
-5.059699 -5.014561 -5.024697 -5.136989
-4.962469 -4.934523 -4.936757 -4.920072
-4.947590 -4.884048 -4.623605 -5.108311
-4.884390 -4.912936 -4.770002 -5.043897
-4.898136 -4.874557 -4.834860 -4.886533
-5.030541 -4.990604 -4.909086 -5.045715
-5.074822 -5.033833 -5.014103 -5.077446
-5.399600 -5.274750 -5.326205 -5.490730
-5.502626 -5.349504 -5.526574 -5.560669
-5.423858 -5.419209 -5.540330 -5.494849
-inf -5.386462 -5.532692 -5.421962
-5.786932 -inf -5.909069 -5.827990
-6.020964 -6.005512 -5.761463 -5.992265
-5.574274 -5.583775 -5.575779 -5.589021
-5.569400 -5.559157 -5.501115 -5.423101
-5.452978 -5.470072 -5.592016 -5.489948
-5.330597 -5.422063 -5.407269 -5.463229
-5.352660 -5.269361 -5.241612 -5.227906
-5.228844 -5.234558 -5.220353 -5.229019
-5.201384 -5.170062 -5.115599 -5.131528
-4.961055 -5.063325 -4.955778 -4.977004
-4.728558 -4.900489 -4.527510 -4.846255
-4.694911 -4.877249 -3.765992 -4.697714
-4.721652 -4.630480 -4.084103 -4.837971
-4.589696 -4.706756 -4.046031 -4.749561
-4.879507 -4.712736 -4.198857 -4.814441
-4.946009 -4.889999 -4.351440 -4.865885
-4.911651 -4.929818 -4.767151 -4.977506
-5.295003 -5.319373 -5.239317 -5.215548
-5.419116 -5.281658 -5.336890 -5.230937
-inf -5.287005 -5.463808 -5.360166
-5.734554 -inf -5.917120 -5.763991
-5.533095 -5.543845 -5.548245 -5.533296
-5.503745 -5.512311 -5.600760 -5.470263
-5.478464 -5.492453 -5.494215 -5.510199
-5.440152 -5.531294 -5.417984 -5.448893
-5.305986 -5.310478 -5.322214 -5.466994
-5.222520 -5.229545 -5.241856 -5.243441
-5.218122 -5.154866 -5.161216 -5.161819
-5.030959 -5.093465 -5.076717 -5.135594
-4.604074 -4.807253 -4.801807 -4.827268
-3.731130 -4.875397 -4.485396 -4.790969
-3.708632 -4.042868 -3.010514 -4.082472
-4.045483 -3.102349 -4.000115 -4.159157
-4.208982 -3.159626 -4.298380 -4.507665
-4.236505 -3.085062 -4.267415 -4.510985
-4.381328 -3.332591 -4.462536 -4.481060
-4.563235 -3.721817 -4.579721 -4.680774
-4.601779 -4.200608 -4.758204 -4.736305
-5.043012 -4.909359 -5.034505 -4.949868
-inf -5.387839 -5.416217 -5.398180
-5.723406 -inf -5.803293 -5.702075
-5.472932 -5.507462 -5.495897 -5.514755
-5.471676 -5.462805 -5.433869 -5.474228
-5.438890 -5.441628 -5.368204 -5.400520
-5.306890 -5.348116 -5.338890 -5.410949
-5.241676 -5.294745 -5.226382 -5.241718
-5.123391 -5.157984 -5.175442 -5.234229
-5.101183 -5.040148 -5.058510 -5.130351
-4.926968 -4.967814 -4.945916 -5.069884
-4.483562 -4.704262 -4.835513 -4.679079
-3.727230 -4.806101 -4.442467 -4.444439
-3.832859 -3.946901 -2.922231 -3.533305
-4.752441 -3.698097 -4.573927 -4.447706
-4.673178 -4.589768 -4.735845 -4.015316
-4.744286 -4.717236 -4.873719 -4.488198
-4.922619 -4.760816 -4.891270 -4.485592
-4.881086 -4.827312 -4.899242 -4.734613
-4.944488 -4.929986 -4.916793 -4.871585
-4.999414 -5.004271 -4.937363 -5.005962
-inf -5.402955 -5.443607 -5.335206
-5.562029 -inf -5.570046 -5.538298
-5.367098 -5.287259 -5.280838 -5.396978
-5.323115 -5.292929 -5.293140 -5.302058
-5.240952 -5.199930 -5.220736 -5.304507
-5.238338 -5.197123 -5.174519 -5.228764
-5.082417 -5.183495 -5.120059 -5.226588
-5.046874 -5.028898 -5.064498 -5.071411
-4.973039 -4.955203 -4.976862 -5.032572
-4.817924 -4.962741 -4.859799 -4.875142
-4.465225 -4.739770 -4.766272 -4.750819
-3.626671 -4.911417 -4.305828 -4.509270
-3.730031 -3.821236 -2.766830 -3.698375
-4.735385 -3.676175 -4.518805 -4.448219
-4.700977 -4.554731 -4.645864 -4.697730
-4.804932 -4.761657 -4.761981 -4.784901
-4.800592 -4.840657 -4.795710 -4.820528
-4.850407 -4.849106 -4.802816 -4.830717
-4.864778 -4.845556 -4.846007 -4.877154
-4.901458 -4.896811 -4.922678 -4.848764
-inf -5.228255 -5.216120 -5.242392
-5.469372 -inf -5.562521 -5.459583
-5.168056 -5.225873 -5.129481 -5.130392
-5.144759 -5.139950 -5.108735 -5.156501
-5.052024 -5.154452 -5.077599 -5.053806
-5.050935 -5.043865 -5.039484 -5.059418
-4.990170 -4.995522 -5.000864 -5.004323
-4.974539 -4.877721 -4.933116 -4.956892
-4.882500 -4.909320 -4.855592 -4.856445
-4.707372 -4.782498 -4.715254 -4.799369
-4.181911 -4.740185 -4.394048 -4.805815
-3.773714 -4.736783 -3.403245 -4.470236
-3.589829 -3.532092 -2.527869 -3.624344
-4.521617 -3.568636 -4.166979 -4.556535
-4.667947 -4.449648 -4.145311 -4.590928
-4.687582 -4.549190 -4.274675 -4.834456
-4.618218 -4.647694 -4.454792 -4.792371
-4.723839 -4.748837 -4.568777 -4.664941
-4.739237 -4.680163 -4.682358 -4.790591
-4.803948 -4.805639 -4.808933 -4.849748
-inf -5.089301 -5.083399 -5.072153
-5.273885 -inf -5.094636 -5.283986
-4.863442 -5.103369 -4.784067 -5.022673
-4.856285 -4.845354 -4.774544 -4.936130
-4.897448 -4.808293 -4.788370 -4.901742
-4.813973 -4.899330 -4.758564 -4.958058
-4.746685 -4.795064 -4.703592 -4.965778
-4.709310 -4.756377 -4.664590 -4.813633
-4.674639 -4.698591 -4.605477 -4.797256
-4.311794 -4.668172 -4.646950 -4.562903
-3.402379 -4.723268 -4.375210 -4.690308
-3.295727 -3.311283 -2.517308 -3.545362
-3.250073 -2.590825 -3.265863 -3.318517
-3.561526 -2.877144 -4.149506 -3.761566
-4.027589 -2.983354 -4.027579 -4.340091
-4.170490 -3.187644 -4.223140 -4.259254
-4.213890 -3.426030 -4.359468 -4.370697
-4.183178 -3.548921 -4.366338 -4.313767
-4.345176 -4.063098 -4.451314 -4.495918
-4.588083 -4.477612 -4.372214 -4.589857
-inf -4.765185 -4.948094 -5.084010
-4.938820 -inf -4.448590 -5.338396
-4.546601 -4.647551 -4.212884 -4.731712
-4.385912 -4.574110 -4.250876 -4.714617
-4.474149 -4.496759 -4.336542 -4.334732
-4.476619 -4.450898 -4.272126 -4.689345
-4.391579 -4.476804 -4.271119 -4.431028
-4.368614 -4.415308 -4.235847 -4.536328
-4.434269 -4.411304 -4.139746 -4.452839
-4.310540 -4.418248 -4.307587 -4.593357
-3.399956 -4.607183 -4.046021 -4.313784
-3.391392 -3.347090 -2.480567 -3.134549
-4.655002 -3.618945 -3.227787 -3.630651
-4.501378 -4.344441 -4.341650 -3.864580
-4.441674 -4.492365 -4.551948 -4.026830
-4.559146 -4.474650 -4.522934 -4.259787
-4.623824 -4.624231 -4.511266 -4.421790
-4.526725 -4.544061 -4.609820 -4.428474
-4.711288 -4.678917 -4.667341 -4.629354
-4.758955 -4.746998 -4.740409 -4.666603
-inf -4.995453 -4.874980 -4.962065
-4.167826 -inf -3.342716 -4.443152
-4.049601 -3.428611 -4.108094 -4.184231
-3.949580 -3.561056 -4.186950 -4.141445
-3.791015 -3.795020 -4.137671 -4.038601
-3.446924 -3.770379 -4.170410 -4.416228
-3.439372 -3.841094 -4.192804 -4.116822
-3.322445 -3.695304 -3.897246 -3.925955
-3.821879 -3.712514 -2.949265 -3.890608
-3.930052 -3.465301 -3.992854 -4.053552
-2.949818 -3.550049 -3.931609 -3.834839
-2.254919 -3.299717 -3.290615 -3.295281
-3.300522 -3.261735 -2.197532 -3.763709
-4.455523 -3.343809 -4.169010 -4.562687
-4.399353 -4.241612 -4.411580 -4.465381
-4.448401 -4.473382 -4.442786 -4.473026
-4.473579 -4.531849 -4.508469 -4.460610
-4.504795 -4.469562 -4.459661 -4.481815
-4.530588 -4.556777 -4.472053 -4.529362
-4.661583 -4.543662 -4.582835 -4.555426
-inf -4.773626 -4.794379 -4.922255
-4.129664 -inf -3.317781 -4.002905
-4.680190 -4.033772 -4.469037 -4.263876
-4.345931 -4.552772 -4.645657 -4.267175
-4.531117 -4.515775 -4.586367 -4.331232
-4.503066 -4.457451 -4.362631 -4.270011
-4.408936 -4.477107 -4.350220 -4.201446
-3.894439 -4.307234 -4.378216 -4.178966
-3.926513 -3.881186 -2.860892 -3.968382
-4.369661 -3.890910 -4.348284 -4.259730
-4.211438 -4.368579 -4.439425 -4.093381
-3.255453 -4.587732 -4.167777 -3.834630
-3.188686 -3.400848 -2.324055 -3.092891
-4.301967 -3.254434 -4.114567 -4.278042
-4.198344 -4.148522 -4.223066 -4.403904
-4.247835 -4.218153 -4.239651 -4.400035
-4.255032 -4.218619 -4.223714 -4.526314
-4.272638 -4.221611 -4.097295 -4.296379
-4.227607 -4.243012 -4.201847 -4.394953
-4.565442 -4.331966 -4.309563 -4.289824
-inf -4.675293 -4.757389 -4.710393
-4.068853 -inf -3.481641 -4.008940
-4.594266 -4.032300 -4.455900 -4.532152
-4.617791 -4.554962 -4.504003 -4.547670
-4.594370 -4.597417 -4.610580 -4.557191
-4.467619 -4.496215 -4.450722 -4.476948
-4.386063 -4.435979 -4.367855 -4.378368
-3.896489 -4.428636 -4.342230 -4.348308
-3.908355 -3.861073 -2.901381 -3.691821
-4.348402 -3.950660 -4.303751 -4.429322
-4.178869 -4.191628 -4.358466 -4.256270
-3.271395 -4.278406 -4.053997 -4.134064
-3.172456 -3.317029 -2.526927 -3.245630
-4.450685 -3.565629 -3.283319 -4.271282
-4.221383 -4.068326 -4.097912 -4.183058
-4.108253 -4.125710 -4.087973 -4.079968
-3.918066 -4.058568 -3.900016 -4.042934
-3.891408 -4.014734 -3.471605 -4.181603
-3.974669 -3.915844 -3.870515 -3.938684
-4.144689 -4.038240 -4.012027 -4.215271
-inf -4.462291 -4.337853 -4.446998
-4.021098 -inf -3.333517 -4.001985
-4.599640 -3.950194 -4.545701 -4.503545
-4.646832 -4.519535 -4.580091 -4.551448
-4.609380 -4.625016 -4.588582 -4.641229
-4.432153 -4.476772 -4.657375 -4.484006
-4.408076 -4.381405 -4.428627 -4.335690
-3.708720 -4.485780 -4.283988 -4.374472
-3.742249 -3.702740 -2.802659 -3.675746
-4.269272 -3.750299 -4.219966 -4.414907
-4.107192 -4.230473 -4.244479 -4.199290
-3.284182 -4.383574 -4.312454 -3.989305
-2.603134 -3.390902 -3.252810 -3.126784
-3.462795 -3.160702 -2.498239 -3.520990
-4.111286 -3.309892 -4.011419 -4.492897
-3.857806 -3.851149 -3.842647 -3.917279
-3.397767 -3.741438 -3.256959 -3.927255
-3.546387 -3.642526 -2.607716 -3.489052
-3.691275 -3.367056 -3.360713 -3.792476
-3.865280 -3.729209 -3.743787 -3.788759
-inf -4.258395 -4.145636 -4.190529
-4.041581 -inf -3.312448 -3.930874
-4.670794 -3.862971 -4.615700 -4.481231
-4.577213 -4.549326 -4.668853 -4.582168
-4.560032 -4.605442 -4.604248 -4.626458
-4.555256 -4.547618 -4.516462 -4.515810
-4.316777 -4.347301 -4.440023 -4.453682
-3.702966 -4.294015 -4.198078 -4.271128
-3.748653 -3.827656 -2.703283 -3.699163
-4.295186 -3.724008 -4.220506 -4.202180
-4.211028 -4.174326 -4.269841 -4.264264
-4.000237 -4.132816 -4.217379 -4.066126
-3.264122 -4.332593 -4.017648 -3.442563
-3.189782 -3.309644 -2.281359 -3.300678
-3.908235 -3.312788 -3.854948 -3.957419
-3.380364 -3.701120 -3.204345 -3.757506
-2.642254 -3.626650 -2.384438 -3.544850
-2.976636 -3.110828 -1.506541 -3.189187
-3.457305 -2.521039 -2.741255 -3.395296
-3.309338 -3.292102 -3.302829 -3.586560
-inf -3.654563 -3.915617 -3.724059
-4.018977 -inf -2.954873 -3.877733
-4.676555 -3.842555 -4.488478 -4.550412
-4.603847 -4.561415 -4.616545 -4.653631
-4.544080 -4.556794 -4.569382 -4.593994
-4.449533 -4.496561 -4.440518 -4.538758
-4.283742 -4.460602 -4.394331 -4.363028
-3.655015 -4.211648 -4.263724 -4.242168
-3.447526 -3.617322 -2.782768 -3.576226
-4.086315 -3.660141 -4.169856 -3.942109
-4.217034 -4.174541 -4.182391 -4.195138
-4.035008 -4.218562 -4.072922 -4.128690
-3.234898 -4.240942 -4.085608 -3.899279
-2.986774 -3.387075 -2.327744 -3.210048
-3.152808 -3.406938 -3.998976 -3.979424
-2.301348 -3.903299 -3.793766 -3.914793
-1.443775 -3.153886 -3.201967 -3.298553
-0.752263 -0.781207 -1.704705 -0.633227
-3.149634 -1.564586 -3.098671 -3.218545
-2.900608 -2.652021 -3.198750 -3.094512
-inf -3.626827 -3.772052 -3.740927
-3.841061 -inf -2.647042 -3.616624
-4.729805 -3.710165 -4.467142 -4.563748
-4.647367 -4.579730 -4.386206 -4.549932
-4.324551 -4.510249 -4.226055 -4.499158
-4.385776 -4.379984 -4.228340 -4.378569
-4.233671 -4.316308 -4.165789 -4.389934
-3.705438 -4.386006 -4.127016 -4.304864
-3.489311 -3.805978 -2.855551 -3.376597
-4.307333 -3.670846 -4.024063 -4.019098
-4.312559 -4.146460 -4.001632 -4.235463
-4.031646 -4.163243 -3.921114 -4.227365
-3.165001 -4.227619 -3.926759 -4.060217
-3.346608 -3.073055 -2.425380 -3.247547
-3.925115 -3.251494 -3.960229 -3.888887
-3.337945 -3.875691 -3.713181 -3.132356
-2.554563 -3.856578 -3.743422 -2.314562
-3.103246 -3.125180 -3.073552 -1.463917
-3.693690 -2.304001 -3.723344 -2.545621
-3.579757 -3.169533 -3.792359 -3.287515
-inf -3.912711 -4.097747 -4.062635
-3.433983 -inf -2.605635 -3.782795
-4.380702 -4.064973 -3.504308 -4.611326
-4.274684 -4.428434 -3.570369 -4.744442
-4.317234 -4.359912 -3.506466 -4.415165
-4.171198 -4.251018 -3.489580 -4.674591
-4.174264 -4.249848 -3.509826 -4.490972
-3.694769 -4.130803 -3.506049 -4.228218
-3.291286 -3.328271 -2.815494 -3.485610
-3.998160 -3.688374 -3.325845 -4.193850
-3.938600 -4.018683 -3.318206 -4.525633
-3.974706 -3.939766 -3.158525 -4.455584
-3.416984 -3.937146 -3.043001 -4.125093
-2.884591 -3.196118 -2.427359 -3.016063
-3.887817 -3.389905 -3.046496 -4.134821
-3.808719 -3.752057 -2.984864 -3.891833
-3.056129 -3.817666 -3.190964 -3.164329
-3.772391 -3.759118 -3.033390 -2.295167
-4.203061 -3.062097 -3.932610 -3.230104
-4.167539 -3.770622 -4.039357 -3.776124
-inf -4.426603 -4.340320 -4.423751
-2.818650 -inf -3.676372 -3.731011
-2.791791 -3.322086 -3.468118 -3.691949
-2.936094 -3.397873 -3.683692 -3.650086
-2.782628 -3.197387 -3.486232 -3.716688
-2.824812 -3.308753 -3.343093 -3.783490
-3.044396 -3.449509 -3.523225 -3.296920
-2.975739 -3.341967 -3.314768 -3.492297
-2.560245 -3.312148 -3.408112 -3.109035
-2.461789 -3.317984 -3.131017 -3.238105
-2.576342 -3.318207 -3.063424 -3.050825
-2.389051 -3.123220 -3.159826 -3.083838
-2.282560 -2.996267 -3.007324 -3.145957
-2.276853 -2.796608 -3.032709 -2.579368
-2.279041 -2.901797 -2.755256 -2.886774
-2.250440 -3.012134 -3.123468 -3.050709
-2.151897 -2.994858 -2.977894 -2.940694
-2.955106 -3.010862 -2.910270 -2.252791
-5.420279 -3.075840 -5.197310 -4.698555
-4.640415 -4.184811 -4.581524 -4.711491
-inf -4.600481 -4.645338 -4.525770
-3.934530 -inf -4.231647 -3.538148
-4.255975 -4.348168 -4.366329 -3.619936
-4.299023 -4.227643 -4.318477 -3.507970
-4.216578 -4.299522 -4.269083 -3.491396
-4.137414 -4.280465 -4.271883 -3.521917
-4.153812 -4.173707 -4.230963 -3.481973
-4.129182 -4.160716 -4.772897 -3.447453
-4.904127 -4.430572 -5.278437 -3.524160
-3.950509 -4.061928 -4.652014 -3.339042
-3.923741 -3.978586 -4.628314 -3.282597
-3.877675 -3.955269 -4.527513 -3.178292
-3.838283 -3.903291 -4.459068 -3.054844
-3.795549 -3.939755 -4.747976 -2.985951
-3.879206 -3.916518 -4.512143 -2.974633
-3.919636 -3.840687 -4.414874 -2.980991
-3.802038 -3.898223 -4.709444 -2.997096
-4.469299 -3.825821 -4.692483 -3.015206
-4.319503 -3.763150 -4.497626 -3.962577
-4.492043 -4.373247 -4.585911 -4.377111
-inf -4.981826 -4.700496 -4.682447
-4.659919 -inf -inf -4.602056
-3.858708 -4.071139 -inf -4.071683
-3.925473 -4.200690 -inf -4.107955
-3.816062 -4.251507 -inf -3.790082
-3.701226 -4.128475 -inf -3.829603
-4.278519 -4.037702 -inf -3.587905
-4.825212 -4.727412 -inf -4.482201
-4.715589 -4.861682 -inf -4.441503
-4.856976 -4.605167 -inf -4.204421
-4.576381 -4.425590 -inf -4.475908
-4.672855 -4.461985 -inf -4.169064
-4.983971 -4.751189 -inf -4.182868
-4.844379 -4.891663 -inf -4.357478
-4.872033 -4.993572 -inf -4.050974
-4.670817 -4.549875 -inf -4.332658
-4.986157 -4.591868 -inf -4.298233
-5.029018 -5.106446 -inf -4.097755
-4.844039 -4.829643 -inf -4.740407
-4.934577 -4.996975 -inf -4.933465
-inf -5.103185 -inf -5.062854
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <math.h>
#include <time.h>
#define M_PI 3.14159265358979323846
#define MAPSIZE 20
int running = 1;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
void initSDL(){
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
if(TTF_Init() == -1){
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
exit(1);
}
if(IMG_Init(IMG_INIT_PNG) < 0){
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
exit(1);
}
}
void createWindow(){
window = SDL_CreateWindow("moving ball", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 800, SDL_WINDOW_SHOWN);
if(window == NULL){
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == NULL){
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
}
void endSDL(){
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
IMG_Quit();
TTF_Quit();
SDL_Quit();
}
void readMapFromFile(char * filename, int map[][MAPSIZE]){
FILE * fp;
fp = fopen(filename, "r");
if(fp == NULL){
printf("Error opening file\n");
exit(1);
}
int i, j;
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
fscanf(fp, "%d", &map[i][j]);
}
}
fclose(fp);
}
typedef struct Ball {
int x;
int y;
int radius;
int weight;
int isGrounded;
} Ball_t;
void printMap(int map[][MAPSIZE]){
int i, j;
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
printf("%d ", map[i][j]);
}
printf("\n");
}
}
void drawMap(SDL_Renderer * renderer, int map[][MAPSIZE], Ball_t * ball){
int i, j;
SDL_Rect rect = {0, 0, 800/MAPSIZE, 800/MAPSIZE};
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
if (i == ball->x && j == ball->y){
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
else {
if (map[i][j] == 2){
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
else {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &rect);
}
}
rect.x += 800/MAPSIZE;
}
rect.y += 800/MAPSIZE;
rect.x = 0;
}
}
#define GRAVITY 1
int hitAngle;
int hitIntensity;
void moveBall(Ball_t * ball, int choice){
switch (choice)
{
case 0:
ball->x += 1;
break;
case 1:
ball->x -= 1;
break;
case 2:
ball->y += 1;
break;
case 3:
ball->y -= 1;
break;
}
}
float *** allocateAndInitiateQ(){
float *** q = malloc(sizeof(int **) * MAPSIZE);
int i, j, k;
for(i = 0; i < MAPSIZE; i++){
q[i] = malloc(sizeof(float *) * MAPSIZE);
for(j = 0; j < MAPSIZE; j++){
q[i][j] = malloc(sizeof(float) * 4);
if (i > 0 && i < MAPSIZE - 1 && j > 0 && j < MAPSIZE - 1){
for(k = 0; k < 4; k++){
q[i][j][k] = -1;
}
}
else if (i == 0 && j == 0){
int kPossibleValues[2] = {0, 2};
for(k = 0; k < 2; k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][3] = -INFINITY;
q[i][j][1] = -INFINITY;
}
else if (i == 0 && j == MAPSIZE - 1){
int kPossibleValues[2] = {1, 2};
for(k = 0; k < 2; k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][0] = -INFINITY;
q[i][j][3] = -INFINITY;
}
else if (i == MAPSIZE - 1 && j == 0){
int kPossibleValues[2] = {0, 3};
for(k = 0; k < 2; k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][1] = -INFINITY;
q[i][j][2] = -INFINITY;
}
else if (i == MAPSIZE - 1 && j == MAPSIZE - 1){
int kPossibleValues[2] = {1, 3};
for(k = 0; k < 2; k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][0] = -INFINITY;
q[i][j][2] = -INFINITY;
}
else if (j == MAPSIZE - 1){
int kPossibleValues[3] = {1, 2, 3};
for(k = 0; k < 3 ;k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][0] = -INFINITY;
}
else if (i == MAPSIZE - 1){
int kPossibleValues[3] = {0, 1, 3};
for(k = 0; k < 3; k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][2] = -INFINITY;
}
else if (j == 0){
int kPossibleValues[3] = {0, 2, 3};
for(k = 0; k < 3; k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][1] = -INFINITY;
}
else if (i == 0){
int kPossibleValues[3] = {0, 1, 2};
for(k = 0; k < 3; k++){
q[i][j][kPossibleValues[k]] = -1;
}
q[i][j][3] = -INFINITY;
}
}
}
return q;
}
int argmax(float * arr, int size){
int i;
float max = arr[0];
int maxIndex = 0;
printf("argmax: %f ", arr[0]);
for(i = 1; i < size; i++){
printf("%f ", arr[i]);
if (arr[i] > max){
max = arr[i];
maxIndex = i;
}
}
printf("\n");
return maxIndex;
}
int take_action(Ball_t * ball, float *** Q, float eps){
int action;
int proba = rand() % 100;
if (proba < eps * 100){
if (ball->x > 0 && ball->x < MAPSIZE - 1 && ball->y > 0 && ball->y < MAPSIZE - 1){
action = rand() % 4;
}
else if (ball->x == 0 && ball->y > 0 && ball->y < MAPSIZE - 1){
int possibleActions[3] = {0, 3, 2};
action = possibleActions[rand() % 3];
}
else if (ball->x == MAPSIZE - 1 && ball->y > 0 && ball->y < MAPSIZE - 1){
int possibleActions[3] = {1, 3, 2};
action = possibleActions[rand() % 3];
}
else if (ball->y == 0 && ball->x > 0 && ball->x < MAPSIZE - 1){
int possibleActions[3] = {0, 1, 2};
action = possibleActions[rand() % 3];
}
else if (ball->y == MAPSIZE - 1 && ball->x > 0 && ball->x < MAPSIZE - 1){
int possibleActions[3] = {0, 1, 3};
action = possibleActions[rand() % 3];
}
else if (ball->x == 0 && ball->y == 0){
int possibleActions[2] = {0, 2};
action = possibleActions[rand() % 2];
}
else if (ball->x == 0 && ball->y == MAPSIZE - 1){
int possibleActions[2] = {0, 3};
action = possibleActions[rand() % 2];
}
else if (ball->x == MAPSIZE - 1 && ball->y == 0){
int possibleActions[2] = {1, 2};
action = possibleActions[rand() % 2];
}
else if (ball->x == MAPSIZE - 1 && ball->y == MAPSIZE - 1){
int possibleActions[2] = {1, 3};
action = possibleActions[rand() % 2];
}
else{
action = rand() % 4;
}
}
else{
action = argmax(Q[ball->y][ball->x],4);
//printf("wtf");
}
return action;
}
int setReward(Ball_t * ball, int map[][MAPSIZE]){
if (map[ball->y][ball->x] == 1){
return 0;
}
else if (map[ball->y][ball->x] == 2){
return 1;
}
else if (map[ball->y][ball->x] == 3){
return -2;
}
else{
return -1;
}
}
void printQ(float *** Q){
int i, j, k;
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
printf("Q for %d %d: ", i, j);
for(k = 0; k < 4; k++){
printf("%f ", Q[i][j][k]);
}
printf("\n");
}
printf("\n");
}
}
void writeQ(float *** Q){
int i, j, k;
FILE * fp = fopen("q.txt", "w+");
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
for(k = 0; k < 4; k++){
fprintf(fp, "%f ", Q[i][j][k]);
}
fprintf(fp, "\n");
}
fprintf(fp, "\n");
}
fflush(fp);
fclose(fp);
}
void readQ(float *** Q){
int i, j, k;
FILE * fp = fopen("q.txt", "r");
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
for(k = 0; k < 4; k++){
fscanf(fp, "%f ", &Q[i][j][k]);
}
fscanf(fp, "\n");
}
fscanf(fp, "\n");
}
fflush(fp);
fclose(fp);
}
typedef struct path_t{
int x;
int y;
struct path_t * next;
} path_t;
// chain list to track the path taken
void insertPointToPath(path_t ** head, int x, int y){
path_t * newPoint = (path_t *)malloc(sizeof(path_t));
newPoint->x = x;
newPoint->y = y;
newPoint->next = *head;
*head = newPoint;
}
int checkIfPointInPath(path_t * head, int x, int y){
path_t * current = head;
while(current != NULL){
if (current->x == x && current->y == y){
return 1;
}
current = current->next;
}
return 0;
}
void freePath(path_t * head){
path_t * current = head;
while(current != NULL){
path_t * temp = current;
current = current->next;
free(temp);
}
}
int main(){
Ball_t ball;
ball.weight = 100;
ball.isGrounded = 1;
path_t * path = NULL;
path = (path_t *)malloc(sizeof(path_t));
srand(time(NULL));
int map[MAPSIZE][MAPSIZE];
readMapFromFile("map.txt", map);
//printMap(map);
float *** Q = allocateAndInitiateQ();
for (int i = 0; i < 1000; i++){
freePath(path);
path = (path_t *)malloc(sizeof(path_t));
ball.x = rand() % MAPSIZE;
ball.y = rand() % MAPSIZE;
while (running){
if (map[ball.y][ball.x] == 2){
printf("%d : found at %d %d\n", i, ball.x, ball.y);
running = 0;
}
insertPointToPath(&path, ball.x, ball.y);
int action = take_action(&ball, Q, 0.5);
printf("[%d] Action: %d\n", i, action);
int reward = setReward(&ball, map);
printf("[%d] Reward: %d\n", i, reward);
printf("Ball: %d %d\n", ball.x, ball.y);
fflush(stdout);
Ball_t * nextBall = malloc(sizeof(Ball_t));
nextBall->x = ball.x;
nextBall->y = ball.y;
nextBall->weight = ball.weight;
nextBall->isGrounded = ball.isGrounded;
moveBall(nextBall, action);
if (checkIfPointInPath(path, nextBall->x, nextBall->y)){
reward = -1;
}
printf("Next Ball: %d, %d\n", nextBall->x, nextBall->y);
int nextAction = argmax(Q[nextBall->y][nextBall->x],4);
Q[ball.y][ball.x][action] = Q[ball.y][ball.x][action] + 0.1 * (reward + 0.9 * Q[nextBall->y][nextBall->x][nextAction] - Q[ball.y][ball.x][action]);
printf("Q: %f\n", Q[ball.y][ball.x][action] + 0.1 * (reward + 0.9 * Q[nextBall->y][nextBall->x][nextAction] - Q[ball.y][ball.x][action]));
moveBall(&ball, action);
}
running = 1;
}
//printQ(Q);
writeQ(Q);
readQ(Q);
// use Q after training
ball.x = rand() % MAPSIZE;
ball.y = rand() % MAPSIZE;
// use Q
createWindow();
int nextIteration = 0;
while (running){
SDL_Event event;
while (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = 0;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE){
running = 0;
}
if (event.key.keysym.sym == SDLK_UP){
nextIteration = 1;
}
break;
default:
break;
}
}
if (nextIteration){
nextIteration = 0;
SDL_RenderClear(renderer);
drawMap(renderer, map, &ball);
SDL_RenderPresent(renderer);
if (map[ball.y][ball.x] == 2){
printf("found at final %d %d\n", ball.x, ball.y);
running = 0;
}
int action = take_action(&ball, Q, 0);
printf("Action: %d\n", action);
int reward = setReward(&ball, map);
printf("Reward: %d\n", reward);
moveBall(&ball, action);
printf("Ball: %d %d\n", ball.x, ball.y);
fflush(stdout);
}
}
endSDL();
}
\ No newline at end of file
#include "gest_event.h"
void gestMenu(){
SDL_Event event;
while (SDL_PollEvent(&event)){
switch(event.type)
{
case SDL_QUIT:
running = 0;
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = 0;
continue;
default:
continue;
}
break;
default:
continue;
}
}
SDL_Delay(5);
}
void *EventLoop(void *arg){
while(running){
switch(game_state){
case MENU : gestMenu();break;
case GAME : gestMenu();break;
default:printf("game state fault");break;
}
}
return NULL;
}
\ No newline at end of file
#ifndef _GEST_EVENT_H_
#define _GEST_EVENT_H_
#include "main.h"
#include "render.h"
void *EventLoop(void *arg);
#endif
\ No newline at end of file
#include "main.h"
int running;
int game_state;
int main(){
running = 1;
game_state = GAME;
readMapFromFile("map.txt");
printMap();
mainLoop();
}
\ No newline at end of file
#ifndef _MAIN_HEADER_
#define _MAIN_HEADER_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <pthread.h>
#include "render.h"
#include "gest_event.h"
#include "map.h"
#define MENU 0
#define GAME 1
extern int running;
extern int game_state;
#endif
\ No newline at end of file
#include "map.h"
int map[MAP_HEIGHT][MAP_WIDTH];
void readMapFromFile(char *file_name){
FILE *file = fopen(file_name, "r");
if (file == NULL){
printf("Couldn't open map file.");
exit(EXIT_FAILURE);
}
int i, j;
for (i = 0; i < MAP_HEIGHT ; i++){
for (j = 0; j < MAP_WIDTH; j++){
fscanf(file, "%d", &map[i][j]);
}
}
fclose(file);
}
void printMap(){
int i, j;
for (i = 0; i < MAP_HEIGHT; i++){
for (j = 0; j < MAP_WIDTH; j++){
printf("%d ", map[i][j]);
}
printf("\n");
}
}
\ No newline at end of file
#ifndef MAP_HEADER_
#define MAP_HEADER_
#include "main.h"
#define MAP_WIDTH 31
#define MAP_HEIGHT 17
extern int map[MAP_HEIGHT][MAP_WIDTH];
void readMapFromFile(char *file_name);
void printMap();
#endif
\ No newline at end of file
#include "player.h"
player_t player;
void initPlayer(){
player.x= 8*BLOCK_SIZE;
player.y= 8*BLOCK_SIZE;
player.speed = 1;
player.isMoving = 0;
player.HPMax = 3;
player.currentHP = player.HPMax;
player.coins = 0;
player.angle=0;
}
\ No newline at end of file
#ifndef PLAYER_H
#define PLAYER_H
#include "map.h"
#define ENTITIES_UP 0
#define ENTITIES_DOWN 1
#define ENTITIES_LEFT 2
#define ENTITIES_RIGHT 3
typedef struct player{
int x;
int y;
int speed;
int isMoving;
int direction;
int HPMax;
int currentHP;
int coins;
float angle;
} player_t;
extern player_t player;
void initPlayer();
#endif
\ No newline at end of file
#include "render.h"
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *RobotoFont;
SDL_DisplayMode screenDimension;
SDL_Rect rect;
SDL_Rect sky;
SDL_Rect ground;
// ray casting variables
float htexture;
int r, mx, my, dof;
double rx, ry, xo, yo, distT;
double ra;
// end ray casting variables
void createWindow(){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Couldn't create window.");
exit(EXIT_FAILURE);
}
SDL_GetCurrentDisplayMode(0, &screenDimension);
window = SDL_CreateWindow("Mat Le King", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenDimension.w, screenDimension.h, SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP);
if (window == NULL){
printf("Couldn't create window");
exit(EXIT_FAILURE);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
printf("Couldn't create renderer.");
exit(EXIT_FAILURE);
}
if (TTF_Init() == -1)
{
exit(EXIT_FAILURE);
}
RobotoFont = TTF_OpenFont("Res/Roboto-Black.ttf", 50);
}
void endSDL(){
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
TTF_CloseFont(RobotoFont);
TTF_Quit();
SDL_Quit();
}
void drawRays(int map[][MAP_WIDTH]){
ra = player.angle - DR * FOV_ANGLE/4;
if (ra < 0) ra -= 2*pi;
if (ra > 2*pi) ra -= 2*pi;
for (r = 0; r<NB_RAYS; r++){
// check horizontal rays
printf("ray %d\n", r);
dof = 0;
float disH = 100000, hx = player.x, hy = player.y;
float aTan = -1/tan(ra);
if (ra > pi){ // looking up
ry = (((int)player.y>>6)<<6) - 0.0001;
rx = (player.y - ry) * aTan + player.x;
yo = -BLOCK_SIZE;
xo = -yo*aTan;
}
if (ra<pi){ // looking down
ry = (((int)player.y>>6)<<6) + BLOCK_SIZE;
rx = (player.y - ry) * aTan + player.x;
yo = BLOCK_SIZE;
xo = -yo*aTan;
}
if (ra == pi){
ry = player.y;
rx = player.x;
dof = DOF;
}
while (dof < DOF){
mx = (int)rx>>6;
my = (int)ry>>6;
if (mx >= 0 && mx < MAP_WIDTH && my >= 0 && my < MAP_HEIGHT){
if (map[my][mx] == 1){
hx = rx;
hy = ry;
disH = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y));
dof = DOF;
}
}
else {
rx += xo;
ry += yo;
dof++;
}
}
printf("hx %f hy %f\n", hx, hy);
// check vertical rays
dof = 0;
float disV = 100000, vx = player.x, vy = player.y;
float nTan = -tan(ra);
if (ra > pi/2 && ra < 3*pi/2){ // looking left
rx = (((int)player.x>>6)<<6) - 0.0001;
ry = player.y + (player.x - rx) * nTan;
xo = -BLOCK_SIZE;
yo = -xo*nTan;
}
if (ra<pi/2 || ra > 3*pi/2){ // looking right
rx = (((int)player.x>>6)<<6) + BLOCK_SIZE;
ry = player.y + (player.x - rx) * nTan;
xo = BLOCK_SIZE;
yo = -xo*nTan;
}
if (ra == pi || ra == 0){ // looking horizontally
rx = player.x;
ry = player.y;
dof = DOF;
}
while (dof < DOF){
mx = (int)rx>>6;
my = (int)ry>>6;
if (mx >= 0 && mx < MAP_WIDTH && my >= 0 && my < MAP_HEIGHT){
if (map[my][mx] == 1){
vx = rx;
vy = ry;
disV = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y));
dof = DOF;
}
}
else {
rx += xo;
ry += yo;
dof++;
}
}
printf("vx %f vy %f\n", vx, vy);
if (disH < disV) {
rx = hx;
ry = hy;
distT = disH;
}
else {
rx = vx;
ry = vy;
distT = disV;
}
ra = ra + ANGLE_INC/2;
if (ra > 2*pi) ra -= 2*pi;
if (ra < 0) ra += 2*pi;
// draw column
float ca = player.angle - ra;
if (ca < 0) ca += 2*pi;
if (ca > 2*pi) ca -= 2*pi;
distT = distT * cos(ca);
float lineH = (screenDimension.h/2)/distT;
rect.x = r;
rect.y = screenDimension.h/2 - lineH;
rect.w = 1;
rect.h = (int)(2 * screenDimension.h * lineH/200);
if (disH < disV) {
SDL_SetRenderDrawColor(renderer, 255, rand() % 255, 0, 255);
}
else {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
}
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
}
}
void drawMap2D(int map[][MAP_WIDTH]){
int i, j;
rect.w = CELL_SIZE;
rect.h = CELL_SIZE;
rect.x = 0;
rect.y = 0;
for (i = 0; i < MAP_HEIGHT; i++){
for (j = 0; j < MAP_WIDTH; j++){
if (map[i][j] == 1){
SDL_SetRenderDrawColor(renderer, 5, 255, 255, 255);
SDL_RenderFillRect(renderer, &rect);
}
else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
rect.x += CELL_SIZE;
}
rect.y += CELL_SIZE;
rect.x = 0;
}
}
void drawGame(){
SDL_RenderClear(renderer);
drawRays(map);
//drawMap2D(map);
SDL_RenderPresent(renderer);
}
void mainLoop(){
createWindow();
unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks();
double delta = 0;
pthread_t eventThread;
if (pthread_create(&eventThread, NULL, EventLoop, NULL) != 0){
printf("Couldn't create thread.");
exit(EXIT_FAILURE);
}
while (running){
a = SDL_GetTicks();
delta = (a - b);
if (delta > 1000/FPS_TO_GET){
//printf("fps: %f\n", 1000/delta);
b = a;
switch (game_state){
case MENU:
//Menu();
break;
case GAME:
drawGame();
break;
}
}
else {
// fait dormir le thread pour garder des ressources
usleep(1000 * (1000/FPS_TO_GET - delta));
}
}
endSDL();
}
\ No newline at end of file
#ifndef _RENDER_H_
#define _RENDER_H_
#include "main.h"
#include "gest_event.h"
#include "map.h"
#include "player.h"
#define FPS_TO_GET 60
#define CELL_SIZE 10
#define DOF 8
#define BLOCK_SIZE 64
#define DR 0.0174533
#define FOV_ANGLE 60
#define pi 3.14159265358979323846
#define NB_RAYS (screenDimension.w/40)
#define ANGLE_INC ((DR * FOV_ANGLE) / NB_RAYS)
extern SDL_Window *window;
extern SDL_Renderer *renderer;
void mainLoop();
#endif
\ No newline at end of file
File added
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/SDL2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
\ No newline at end of file
{
"files.associations": {
"render.h": "c",
"stdlib.h": "c",
"event.h": "c"
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment