Added interactive dialogs, but they are not working properly yet.

This commit is contained in:
mibi88 2023-07-15 19:26:32 +02:00
parent e53c3d612d
commit f71121a79e
4 changed files with 66 additions and 43 deletions

View file

@ -6,6 +6,6 @@
#define GRAYMODEOK 1 #define GRAYMODEOK 1
#endif #endif
#define USB_FEATURE 1 #define USB_FEATURE 0
#endif #endif

View file

@ -35,26 +35,29 @@ void blit()
int showtext_opt(Game *game, bopti_image_t *face, char *text, int showtext_opt(Game *game, bopti_image_t *face, char *text,
int call_before_end(void), bool start_anim, bool end_anim) { int call_before_end(Game *game), bool start_anim,
bool end_anim) {
dfont(&FONT_USED); dfont(&FONT_USED);
unsigned int i, n, y = PXSIZE, l = 0; unsigned int i, n, y = PXSIZE, l = 0;
int line_max_chars, return_int = 0; int line_max_chars, return_int = 0;
unsigned int max_lines_amount = (BOX_HEIGHT-2)*PXSIZE/ unsigned int max_lines_amount = (BOX_HEIGHT-2)*PXSIZE/
(FONT_USED.line_height+PXSIZE); (FONT_USED.line_height+PXSIZE);
const char *c; const char *c;
/* Run a little fancy animation. */ if(start_anim){
for(i=0;i<BOX_HEIGHT;i++){ /* Run a little fancy animation. */
draw(game); for(i=0;i<BOX_HEIGHT;i++){
draw(game);
drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE); drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE);
drect(0, i*PXSIZE, DWIDTH, (i+1)*PXSIZE, C_BLACK); drect(0, i*PXSIZE, DWIDTH, (i+1)*PXSIZE, C_BLACK);
dsubimage(4*PXSIZE, 2*PXSIZE, face, 0, 0, F_WIDTH, (i-8)*PXSIZE, dsubimage(4*PXSIZE, 2*PXSIZE, face, 0, 0, F_WIDTH, (i-8)*PXSIZE,
DIMAGE_NONE); DIMAGE_NONE);
dupdate(); dupdate();
while(game->frame_duration < 20) sleep(); while(game->frame_duration < 20) sleep();
game->frame_duration = 0; game->frame_duration = 0;
}
} }
/* We should start to drawint the text on the x axis at BOX_HEIGHT to avoid /* We should start to drawint the text on the x axis at BOX_HEIGHT to avoid
* drawing on the face. */ * drawing on the face. */
@ -86,7 +89,7 @@ int showtext_opt(Game *game, bopti_image_t *face, char *text,
*/ */
/* Make a little animation :). */ /* Make a little animation :). */
blit(); blit();
while(game->frame_duration < 1000) sleep(); while(game->frame_duration < 100) sleep();
game->frame_duration = 0; game->frame_duration = 0;
/* Ask the user to press EXE to continue. */ /* Ask the user to press EXE to continue. */
dtext(BOX_HEIGHT*PXSIZE, y, C_BLACK, "[EXE] to continue ..."); dtext(BOX_HEIGHT*PXSIZE, y, C_BLACK, "[EXE] to continue ...");
@ -103,7 +106,7 @@ int showtext_opt(Game *game, bopti_image_t *face, char *text,
l = 0; l = 0;
} }
else{ else{
while(game->frame_duration < 1000) sleep(); while(game->frame_duration < 100) sleep();
game->frame_duration = 0; game->frame_duration = 0;
} }
} }
@ -111,26 +114,28 @@ int showtext_opt(Game *game, bopti_image_t *face, char *text,
/* If we have not filled everthing with text at the end. */ /* If we have not filled everthing with text at the end. */
/* Make a little animation :). */ /* Make a little animation :). */
blit(); blit();
while(game->frame_duration < 1000) sleep(); while(game->frame_duration < 100) sleep();
game->frame_duration = 0; game->frame_duration = 0;
/* Ask the user to press EXE to continue. */ /* Ask the user to press EXE to continue. */
dtext(BOX_HEIGHT*PXSIZE, y, C_BLACK, "[EXE] to continue ..."); dtext(BOX_HEIGHT*PXSIZE, y, C_BLACK, "[EXE] to continue ...");
blit(); blit();
while(getkey().key != KEY_EXE) sleep(); while(getkey().key != KEY_EXE) sleep();
} }
if(call_before_end) return_int = call_before_end(); if(call_before_end) return_int = call_before_end(game);
/* Run another little fancy animation. */ if(end_anim){
for(i=40;i>0;i--){ /* Run another little fancy animation. */
draw(game); for(i=40;i>0;i--){
drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE); draw(game);
drect(0, i*PXSIZE, DWIDTH, (i+1)*PXSIZE, C_BLACK); drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE);
dsubimage(4*PXSIZE, 2*PXSIZE, face, 0, 0, F_WIDTH, (i-8)*PXSIZE, drect(0, i*PXSIZE, DWIDTH, (i+1)*PXSIZE, C_BLACK);
DIMAGE_NONE); dsubimage(4*PXSIZE, 2*PXSIZE, face, 0, 0, F_WIDTH, (i-8)*PXSIZE,
DIMAGE_NONE);
dupdate(); dupdate();
while(game->frame_duration < 20) sleep(); while(game->frame_duration < 20) sleep();
game->frame_duration = 0; game->frame_duration = 0;
}
} }
return return_int; return return_int;
} }
@ -154,16 +159,16 @@ void showtext_dialog_end(Game *game, bopti_image_t *face, char *text) {
#define CHOICE_BOX_HEIGHT 10 #define CHOICE_BOX_HEIGHT 10
#define CHOICE_BOX_PADDING_TOP 3 #define CHOICE_BOX_PADDING_TOP 3
char **choices; char *_choices;
int choices_amount, default_choice; int _choices_amount, _default_choice;
int _choice_call_before_end(void) { int _choice_call_before_end(Game *game) {
int i, key; int i, key;
/* Make a little animation because we looove little animations ;) */ /* Make a little animation because we looove little animations ;) */
for(i=0;i<DWIDTH/8+1;i++){ for(i=0;i<DWIDTH/8+1;i++){
drect(0, (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, i*(DWIDTH/8), drect(0, BOX_HEIGHT*PXSIZE, i*(DWIDTH/8),
(BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_WHITE); (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_WHITE);
drect(i*(DWIDTH/8), 0, i*(DWIDTH/8)+PXSIZE, drect(i*(DWIDTH/8), BOX_HEIGHT*PXSIZE, i*(DWIDTH/8)+PXSIZE,
(BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_BLACK); (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_BLACK);
drect(0, (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, i*(DWIDTH/8), drect(0, (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, i*(DWIDTH/8),
(BOX_HEIGHT+CHOICE_BOX_HEIGHT+1)*PXSIZE, C_BLACK); (BOX_HEIGHT+CHOICE_BOX_HEIGHT+1)*PXSIZE, C_BLACK);
@ -171,28 +176,29 @@ int _choice_call_before_end(void) {
while(game->frame_duration < 20) sleep(); while(game->frame_duration < 20) sleep();
game->frame_duration = 0; game->frame_duration = 0;
} }
const int choice_size = DWIDTH/choices_amount; const int choice_size = DWIDTH/_choices_amount;
int arrow_width, selected = default_choice; int arrow_width, selected = _default_choice, pos = 0;
dsize(">", FONT_USED, &arrow_width, NULL); dsize(">", &FONT_USED, &arrow_width, NULL);
do{ do{
/* Clear the box. */ /* Clear the box. */
drect(0, (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, (DWIDTH/8)*(DWIDTH/8), drect(0, (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, (DWIDTH/8)*(DWIDTH/8),
(BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_WHITE); (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_WHITE);
for(i=0;i<choices_amount;i++){ for(i=0;i<_choices_amount;i++){
if(i == selected) dtext(i*choice_size, if(i == selected) dtext(i*choice_size+PXSIZE,
BOX_HEIGHT+PXSIZE*CHOICE_BOX_PADDING_TOP, (BOX_HEIGHT+CHOICE_BOX_PADDING_TOP)*PXSIZE,
C_BLACK, ">"); C_BLACK, ">");
dtext(i*choice_size+arrow_width, pos += strlen(_choices);
BOX_HEIGHT+PXSIZE*CHOICE_BOX_PADDING_TOP, C_BLACK, dtext(i*choice_size+arrow_width+PXSIZE,
choices[i]); (BOX_HEIGHT+CHOICE_BOX_PADDING_TOP)*PXSIZE, C_BLACK,
&(_choices+pos)[i]);
} }
blit(); blit();
key = getkey().key; key = getkey().key;
if(key == KEY_LEFT && selected > 0) selected--; if(key == KEY_LEFT && selected > 0) selected--;
else if(key == KEY_RIGHT && selected < choices_amount) selected++; else if(key == KEY_RIGHT && selected < _choices_amount) selected++;
}while(key != KEY_EXE); }while(key != KEY_EXE);
/* Make a little animation because we looove little animations ;) */ /* Make a little animation because we looove little animations ;) */
for(i=DWIDTH/8+1;i>0;i++){ for(i=DWIDTH/8+1;i>0;i--){
drect(0, (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, i*(DWIDTH/8), drect(0, (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, i*(DWIDTH/8),
(BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_WHITE); (BOX_HEIGHT+CHOICE_BOX_HEIGHT)*PXSIZE, C_WHITE);
drect(i*(DWIDTH/8), 0, i*(DWIDTH/8)+PXSIZE, drect(i*(DWIDTH/8), 0, i*(DWIDTH/8)+PXSIZE,
@ -205,3 +211,12 @@ int _choice_call_before_end(void) {
} }
return selected; return selected;
} }
int showtext_dialog_ask(Game *game, bopti_image_t *face, char *text, bool start,
bool end, char *choices, int choices_amount,
int default_choice) {
_choices = choices;
_choices_amount = choices_amount;
_default_choice = default_choice;
return showtext_opt(game, face, text, _choice_call_before_end, start, end);
}

View file

@ -10,7 +10,8 @@
#define F_HEIGHT (32*PXSIZE) #define F_HEIGHT (32*PXSIZE)
int showtext_opt(Game *game, bopti_image_t *face, char *text, int showtext_opt(Game *game, bopti_image_t *face, char *text,
int call_before_end(void), bool start_anim, bool end_anim); int call_before_end(Game *game), bool start_anim,
bool end_anim);
void showtext(Game *game, bopti_image_t *face, char *text); void showtext(Game *game, bopti_image_t *face, char *text);
@ -20,4 +21,8 @@ void showtext_dialog_mid(Game *game, bopti_image_t *face, char *text);
void showtext_dialog_end(Game *game, bopti_image_t *face, char *text); void showtext_dialog_end(Game *game, bopti_image_t *face, char *text);
int showtext_dialog_ask(Game *game, bopti_image_t *face, char *text, bool start,
bool end, char *choices, int choices_amount,
int default_choice);
#endif #endif

View file

@ -100,6 +100,9 @@ int main(void) {
#endif #endif
showtext(&game, &player_face_img, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet."); showtext(&game, &player_face_img, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet.");
int in = showtext_dialog_ask(&game, &player_face_img, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet.", true, false, "Lorem\0ipsum", 2, 0);
if(in) showtext_dialog_end(&game, &player_face_img, "You choosed ipsum");
else showtext_dialog_end(&game, &player_face_img, "You choosed Lorem");
do{ do{
/* clear screen */ /* clear screen */
dclear(C_WHITE); dclear(C_WHITE);