From 6a411c7e80fdfbf652d5dc52828e56164220bdf8 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 5 May 2021 14:37:12 +0200 Subject: [PATCH 1/3] Create new draw_time function replacing the one generated --- CMakeLists.txt | 1 + editor.sh | 2 +- include/times.h | 2 +- src/draw_time.c | 24 ++++++++++++++++++++++++ time.py | 36 +++++++++++------------------------- 5 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 src/draw_time.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 093cec8..d6c8c39 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ add_custom_command( set(SOURCES src/collide.c src/drawlevel.c + src/draw_time.c src/main.c src/menu.c src/save.c diff --git a/editor.sh b/editor.sh index 65fa523..631883a 100755 --- a/editor.sh +++ b/editor.sh @@ -1,3 +1,3 @@ -#!/usr/bin/bash +#!/bin/sh echo "opening editor" python3 editor/AST3generator.py diff --git a/include/times.h b/include/times.h index f4c669b..f6d23d3 100755 --- a/include/times.h +++ b/include/times.h @@ -1,4 +1,4 @@ #pragma once void check_medal(unsigned int frame_level, int id_level, int x, int y); -void draw_time(int id_level); \ No newline at end of file +void draw_time(int id_level); diff --git a/src/draw_time.c b/src/draw_time.c new file mode 100644 index 0000000..d74ae67 --- /dev/null +++ b/src/draw_time.c @@ -0,0 +1,24 @@ +#include "times.h" +#include + +extern bopti_image_t img_medals; + +extern float level_time[]; + +void draw_time(int id_level) +{ + static const float medal_multiplier[4] = {1.0, 1.2, 1.5, 2}; + const float time = level_time[id_level - 1]; + int draw_y = 140; + int medal_x = 36; + int i; + + for (i = 0; i < 4; i += 1) { + dsubimage(340, draw_y, &img_medals, medal_x, 0, 12, 12, + DIMAGE_NONE); + dprint(355, draw_y + 1, C_BLACK, "%.2j", + (int)(time * medal_multiplier[i] * 100)); + draw_y += 20; + medal_x -= 12; + } +} diff --git a/time.py b/time.py index b8fb8ec..b25a040 100755 --- a/time.py +++ b/time.py @@ -1,5 +1,5 @@ gold = 1.2 -argent = 1.5 +silver = 1.5 bronze = 2 x = 340 @@ -11,7 +11,7 @@ lvm = open("generated/include/define.h","r") ids=timeslevel.readlines() times=[] -for i in ids: +for i in ids: times.append(i.rstrip()) nblvl = int(''.join(filter(str.isdigit,lvm.readline()))) @@ -19,12 +19,12 @@ nblvl = int(''.join(filter(str.isdigit,lvm.readline()))) while(len(times) < nblvl): times.append("0.00") -dest.write("#include \"times.h\"\n\ -#include \"define.h\"\n\ -#include \n\ -#include \n\n\ -extern bopti_image_t img_medals;\n\ -float level_time[] = {") +dest.write("""#include "times.h" +#include "define.h" +#include +#include +extern bopti_image_t img_medals; +float level_time[] = {""") dest.write(f"{','.join(times)}") @@ -39,23 +39,9 @@ if(frame_level/FPS < (unsigned int)time || (frame_level/FPS <= (unsigned int)tim (unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time)*100-(int)(time)*100))) dsubimage(x+45,y,&img_medals,36,0,12,12,DIMAGE_NONE);\n\ if(frame_level/FPS < (unsigned int)(time*"+str(gold)+") || (frame_level/FPS <= (unsigned int)(time*"+str(gold)+") && \n\ (unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time*"+str(gold)+")*100-(int)(time*"+str(gold)+")*100))) dsubimage(x+30,y,&img_medals,24,0,12,12,DIMAGE_NONE);\n\ -if(frame_level/FPS < (unsigned int)(time*"+str(argent)+") || (frame_level/FPS <= (unsigned int)(time*"+str(argent)+") && \n\ -(unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time*"+str(argent)+")*100-(int)(time*"+str(argent)+")*100))) dsubimage(x+15,y,&img_medals,12,0,12,12,DIMAGE_NONE);\n\ +if(frame_level/FPS < (unsigned int)(time*"+str(silver)+") || (frame_level/FPS <= (unsigned int)(time*"+str(silver)+") && \n\ +(unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time*"+str(silver)+")*100-(int)(time*"+str(silver)+")*100))) dsubimage(x+15,y,&img_medals,12,0,12,12,DIMAGE_NONE);\n\ if(frame_level/FPS < (unsigned int)(time*"+str(bronze)+") || (frame_level/FPS <= (unsigned int)(time*"+str(bronze)+") && \n\ (unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time*"+str(bronze)+")*100-(int)(time*"+str(bronze)+")*100))) dsubimage(x,y,&img_medals,0,0,12,12,DIMAGE_NONE);\n\ -}\n\ -\n\ -void draw_time(int id_level)\n\ -{\n\ -float time=1.0;\n") +}\n") -dest.write("\ntime = level_time[id_level-1];\n\n\ -dsubimage("+str(x)+","+str(y)+",&img_medals,36,0,12,12,DIMAGE_NONE);\n\ -dprint("+str(x+15)+","+str(y)+",C_BLACK,\"%.2j\",(int)(time*100));\n\ -dsubimage("+str(x)+","+str(y+20)+",&img_medals,24,0,12,12,DIMAGE_NONE);\n\ -dprint("+str(x+15)+","+str(y+20)+",C_BLACK,\"%.2j\",(int)(time*"+str(gold)+"*100));\n\ -dsubimage("+str(x)+","+str(y+40)+",&img_medals,12,0,12,12,DIMAGE_NONE); \n\ -dprint("+str(x+15)+","+str(y+40)+",C_BLACK,\"%.2j\",(int)(time*"+str(argent)+"*100));\n\ -dsubimage("+str(x)+","+str(y+60)+",&img_medals,0,0,12,12,DIMAGE_NONE); \n\ -dprint("+str(x+15)+","+str(y+60)+",C_BLACK,\"%.2j\",(int)(time*"+str(bronze)+"*100));\n\ -}") From d9620de1066e2754d30300024b80f92146997a51 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 5 May 2021 15:46:08 +0200 Subject: [PATCH 2/3] This was hell --- src/draw_time.c | 23 ++++++++++++++++++++++- time.py | 23 ++++------------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/draw_time.c b/src/draw_time.c index d74ae67..821da59 100644 --- a/src/draw_time.c +++ b/src/draw_time.c @@ -1,3 +1,4 @@ +#include "define.h" #include "times.h" #include @@ -5,9 +6,29 @@ extern bopti_image_t img_medals; extern float level_time[]; +static const float medal_multiplier[4] = {1.0, 1.2, 1.5, 2}; + +void check_medal(unsigned int frame_level, int id_level, int x, int y) +{ + const float time = level_time[id_level - 1]; + const int player_time = (int)((float)frame_level / FPS * 100.0); + int i; + + /* OK, this doesn't have the exact same behavior than the previously + * generated code had. But the conditions were so dirty and hard to + * understand than I simplified it. + * -- KikooDX */ + for (i = 0; i < 4; i += 1) { + const int mtime = (int)(time * medal_multiplier[i] * 100.0); + if (player_time <= mtime) { + dsubimage(x + 15 * (3 - i), y, &img_medals, + 12 * (3 - i), 0, 12, 12, DIMAGE_NONE); + } + } +} + void draw_time(int id_level) { - static const float medal_multiplier[4] = {1.0, 1.2, 1.5, 2}; const float time = level_time[id_level - 1]; int draw_y = 140; int medal_x = 36; diff --git a/time.py b/time.py index b25a040..9da41d6 100755 --- a/time.py +++ b/time.py @@ -23,25 +23,10 @@ dest.write("""#include "times.h" #include "define.h" #include #include + extern bopti_image_t img_medals; -float level_time[] = {""") +float level_time[] = {\n\t""") -dest.write(f"{','.join(times)}") - -dest.write("};\n\ -void check_medal(unsigned int frame_level, int id_level, int x, int y)\n\ -{\n\ -float time=1.0;\n\ -float framefloat = frame_level;") - -dest.write("\ntime = level_time[id_level-1];\n\n\ -if(frame_level/FPS < (unsigned int)time || (frame_level/FPS <= (unsigned int)time && \n\ -(unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time)*100-(int)(time)*100))) dsubimage(x+45,y,&img_medals,36,0,12,12,DIMAGE_NONE);\n\ -if(frame_level/FPS < (unsigned int)(time*"+str(gold)+") || (frame_level/FPS <= (unsigned int)(time*"+str(gold)+") && \n\ -(unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time*"+str(gold)+")*100-(int)(time*"+str(gold)+")*100))) dsubimage(x+30,y,&img_medals,24,0,12,12,DIMAGE_NONE);\n\ -if(frame_level/FPS < (unsigned int)(time*"+str(silver)+") || (frame_level/FPS <= (unsigned int)(time*"+str(silver)+") && \n\ -(unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time*"+str(silver)+")*100-(int)(time*"+str(silver)+")*100))) dsubimage(x+15,y,&img_medals,12,0,12,12,DIMAGE_NONE);\n\ -if(frame_level/FPS < (unsigned int)(time*"+str(bronze)+") || (frame_level/FPS <= (unsigned int)(time*"+str(bronze)+") && \n\ -(unsigned int)((framefloat)/FPS*100-frame_level/FPS*100) <= (unsigned int)((time*"+str(bronze)+")*100-(int)(time*"+str(bronze)+")*100))) dsubimage(x,y,&img_medals,0,0,12,12,DIMAGE_NONE);\n\ -}\n") +dest.write("{}".format(',\n\t'.join(times))) +dest.write("\n};") From d6461931ad9f8a42ad6fa61d9b98fe3fa07cb1ca Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 5 May 2021 15:56:14 +0200 Subject: [PATCH 3/3] simplify code --- src/draw_time.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/draw_time.c b/src/draw_time.c index 821da59..65a930f 100644 --- a/src/draw_time.c +++ b/src/draw_time.c @@ -18,7 +18,8 @@ void check_medal(unsigned int frame_level, int id_level, int x, int y) * generated code had. But the conditions were so dirty and hard to * understand than I simplified it. * -- KikooDX */ - for (i = 0; i < 4; i += 1) { + i = 4; + while (i-- > 0) { const int mtime = (int)(time * medal_multiplier[i] * 100.0); if (player_time <= mtime) { dsubimage(x + 15 * (3 - i), y, &img_medals, @@ -30,16 +31,14 @@ void check_medal(unsigned int frame_level, int id_level, int x, int y) void draw_time(int id_level) { const float time = level_time[id_level - 1]; - int draw_y = 140; - int medal_x = 36; int i; - for (i = 0; i < 4; i += 1) { - dsubimage(340, draw_y, &img_medals, medal_x, 0, 12, 12, + i = 4; + while (i-- > 0) { + const int draw_y = 140 + 20 * i; + dsubimage(340, draw_y, &img_medals, 12 * (3 - i), 0, 12, 12, DIMAGE_NONE); dprint(355, draw_y + 1, C_BLACK, "%.2j", (int)(time * medal_multiplier[i] * 100)); - draw_y += 20; - medal_x -= 12; } }