Dialogs management should work, had not the time to test it yet.

This commit is contained in:
mibi88 2023-07-11 11:23:27 +02:00
parent 5f55b46bad
commit 813c23ad00
3 changed files with 90 additions and 41 deletions

View file

@ -6,10 +6,15 @@
#define BOX_HEIGHT (F_HEIGHT/PXSIZE+8) #define BOX_HEIGHT (F_HEIGHT/PXSIZE+8)
extern font_t fontRPG; extern font_t fontRPG;
#define FONT_USED fontRPG
void showtext(Game *game, bopti_image_t *face, char *text) { void showtext(Game *game, bopti_image_t *face, char *text) {
dfont(&fontRPG); dfont(&FONT_USED);
unsigned int i; unsigned int i, n, y = 1, l = 0;
int line_max_chars;
unsigned int max_lines_amount = BOX_HEIGHT/(FONT_USED.line_height +
FONT_USED.char_spacing);
/* Run a little fancy animation. */
for(i=0;i<BOX_HEIGHT;i++){ for(i=0;i<BOX_HEIGHT;i++){
draw(game); draw(game);
drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE); drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE);
@ -24,13 +29,62 @@ void showtext(Game *game, bopti_image_t *face, char *text) {
* drawing on the face. */ * drawing on the face. */
/* Show a little message that showing text in dialogs is not implemented /* Show a little message that showing text in dialogs is not implemented
* yet. */ * yet. */
dtext(BOX_HEIGHT*PXSIZE, 1, C_BLACK, "Dialogs not implemented");
dtext(BOX_HEIGHT*PXSIZE, 8*PXSIZE, C_BLACK, "Press any key");
dupdate();
getkey();
for(i=0;i<strlen(text);i++){ for(i=0;i<strlen(text);i++){
/**/ /* Get how many chars we can draw on screen with a padding on the left
* and on the right of 1 px. */
drsize(text+i, &FONT_USED, DWIDTH-2, &line_max_chars);
/* TODO: Handle lines that are longer than what I can draw. */
/* Loop from the end to the start for word wrap. */
for(n=line_max_chars; n>0; n--) {
/* If we found a space, we can draw this line and do the same for
* the next line. */
if(text[i+n] == ' '){
dtext_opt(1, y, C_BLACK, C_NONE, DTEXT_TOP, DTEXT_LEFT, text+i,
n); /* Draw everything. */
/* Increment y by the line height plus the space between them.
*/
y += FONT_USED.line_height+FONT_USED.char_spacing;
i += n; /* We drew everything to i+n */
l++; /* We drew one more line. */
break;
}
}
if(l>=max_lines_amount-1){
/* We drew one entire screen, reset everything to draw the next one.
*/
/* Make a little animation :). */
dupdate();
while(game->frame_duration < 20) sleep();
game->frame_duration = 0;
/* Ask the user to press EXE to continue. */
dtext(1, y, C_BLACK, "[EXE] To continue ...");
/* Reset y and l. */
y = 1;
l = 0;
/* Clear the screen. */
drect(0, i*PXSIZE, DWIDTH, (i+1)*PXSIZE, C_BLACK);
}
/* Make a little animation :). */
dupdate();
if(l>=max_lines_amount-1){
while(getkey().key != KEY_EXE) sleep();
}
else{
while(game->frame_duration < 20) sleep();
}
game->frame_duration = 0;
} }
if(l<max_lines_amount-1){
/* If we have not filled everthing with text at the end. */
/* Make a little animation :). */
dupdate();
while(game->frame_duration < 20) sleep();
game->frame_duration = 0;
/* Ask the user to press EXE to continue. */
dtext(1, y, C_BLACK, "[EXE] To continue ...");
while(getkey().key != KEY_EXE) sleep();
}
/* Run another little fancy animation. */
for(i=40;i>0;i--){ for(i=40;i>0;i--){
draw(game); draw(game);
drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE); drect(0, 0, DWIDTH, i*PXSIZE, C_WHITE);
@ -42,5 +96,3 @@ void showtext(Game *game, bopti_image_t *face, char *text) {
game->frame_duration = 0; game->frame_duration = 0;
} }
} }

View file

@ -26,6 +26,8 @@ void render_map(Player *player, Map *map_level) {
unsigned short int xtile, ytile; unsigned short int xtile, ytile;
/* The layer we're drawing */ /* The layer we're drawing */
unsigned char l; unsigned char l;
/* The index of the current tile we're drawing in the layer. */
int current_index;
/* Fix sx. */ /* Fix sx. */
if(player->x<DWIDTH/2){ if(player->x<DWIDTH/2){
/* If I can't center the player because I'm near the left border of /* If I can't center the player because I'm near the left border of
@ -62,7 +64,6 @@ void render_map(Player *player, Map *map_level) {
ty = sy/T_HEIGHT; ty = sy/T_HEIGHT;
mx = sx-tx*T_WIDTH; mx = sx-tx*T_WIDTH;
my = sy-ty*T_HEIGHT; my = sy-ty*T_HEIGHT;
for (l = 0; l < map_level->nblayers-1; l++){ for (l = 0; l < map_level->nblayers-1; l++){
/* Draw a layer of the map on screen. */ /* Draw a layer of the map on screen. */
for(y=0;y<dh;y++){ for(y=0;y<dh;y++){
@ -72,10 +73,10 @@ void render_map(Player *player, Map *map_level) {
if(tx+x>=0 && tx+x < map_level->w && if(tx+x>=0 && tx+x < map_level->w &&
ty+y>=0 && ty+y < map_level->h){ ty+y>=0 && ty+y < map_level->h){
/* index of the current tile */ /* index of the current tile */
int currentIndex = (y+ty) * map_level->w + tx+x; current_index = (y+ty) * map_level->w + tx+x;
/* we get the ID of the tile in the current drawable layers /* we get the ID of the tile in the current drawable layers
*/ */
tile = map_level->layers[l][currentIndex]; tile = map_level->layers[l][current_index];
/* tile == -1 means nothing to be drawn */ /* tile == -1 means nothing to be drawn */
if(tile >= 0){ if(tile >= 0){
@ -115,8 +116,6 @@ void render_map_by_layer(Player *player, Map *map_level, int layer) {
unsigned short int sx, sy; unsigned short int sx, sy;
/* The position of the tile in the tileset. */ /* The position of the tile in the tileset. */
unsigned short int xtile, ytile; unsigned short int xtile, ytile;
/* The layer we're drawing */
unsigned char l;
/* Fix sx. */ /* Fix sx. */
if(player->x<DWIDTH/2){ if(player->x<DWIDTH/2){
/* If I can't center the player because I'm near the left border of /* If I can't center the player because I'm near the left border of
@ -153,34 +152,32 @@ void render_map_by_layer(Player *player, Map *map_level, int layer) {
ty = sy/T_HEIGHT; ty = sy/T_HEIGHT;
mx = sx-tx*T_WIDTH; mx = sx-tx*T_WIDTH;
my = sy-ty*T_HEIGHT; my = sy-ty*T_HEIGHT;
/* Draw a layer of the map on screen. */
for(y=0;y<dh;y++){
/* Draw a layer of the map on screen. */ for(x=0;x<dw;x++){
for(y=0;y<dh;y++){ /* I get the tile number if his position is inside the map. Then
for(x=0;x<dw;x++){ * I draw it. */
/* I get the tile number if his position is inside the map. Then if(tx+x>=0 && tx+x < map_level->w &&
* I draw it. */ ty+y>=0 && ty+y < map_level->h){
if(tx+x>=0 && tx+x < map_level->w && /* index of the current tile */
ty+y>=0 && ty+y < map_level->h){ int currentIndex = (y+ty) * map_level->w + tx+x;
/* index of the current tile */ /* we get the ID of the tile in the current drawable layers
int currentIndex = (y+ty) * map_level->w + tx+x; */
/* we get the ID of the tile in the current drawable layers tile = map_level->layers[layer][currentIndex];
*/
tile = map_level->layers[layer][currentIndex]; /* tile == -1 means nothing to be drawn */
if(tile >= 0){
/* tile == -1 means nothing to be drawn */ /* get x and y position in the tileset image */
if(tile >= 0){ xtile = (tile % map_level->tileset_size) * T_WIDTH;
/* get x and y position in the tileset image */ ytile = (tile / map_level->tileset_size) * T_HEIGHT;
xtile = (tile % map_level->tileset_size) * T_WIDTH; /* render */
ytile = (tile / map_level->tileset_size) * T_HEIGHT; dsubimage(x*T_WIDTH-mx, y*T_HEIGHT-my,
/* render */ map_level->tileset, xtile, ytile, T_WIDTH,
dsubimage(x*T_WIDTH-mx, y*T_HEIGHT-my, T_HEIGHT, DIMAGE_NONE);
map_level->tileset, xtile, ytile, T_WIDTH,
T_HEIGHT, DIMAGE_NONE);
}
} }
} }
} }
}
} }

View file

@ -28,7 +28,8 @@
/* Draws the map map on the entire screen to be viewed by the player player. */ /* Draws the map map on the entire screen to be viewed by the player player. */
void render_map(Player *player, Map *map_level); void render_map(Player *player, Map *map_level);
/* Draws the map layer on the entire screen to be viewed by the player player. */ /* Draws the map layer on the entire screen to be viewed by the player player.
*/
void render_map_by_layer(Player *player, Map *map_level, int layer); void render_map_by_layer(Player *player, Map *map_level, int layer);
/* Get the tile at (x, y) of the map map. If the tile is located outside of the /* Get the tile at (x, y) of the map map. If the tile is located outside of the
@ -39,4 +40,3 @@ short int get_tile(Map *map_level, int x, int y, int l);
short int get_walkable(Map *map_level, int x, int y); short int get_walkable(Map *map_level, int x, int y);
#endif #endif