static void showPatchMessage(char *message) { int y; SDL_Rect dest; SDL_Surface *title, *completion; title = generateTextSurface(_("Patching your save game. Please wait..."), game.font, 220, 220, 220, 0, 0, 0); completion = generateTextSurface(message, game.font, 220, 220, 220, 0, 0, 0); if (title == NULL || completion == NULL) { return; } clearScreen(0, 0, 0); y = (SCREEN_HEIGHT - title->h) / 2; dest.x = (SCREEN_WIDTH - title->w) / 2; dest.y = y; dest.w = title->w; dest.h = title->h; SDL_BlitSurface(title, NULL, game.screen, &dest); y += title->h + 15; dest.x = (SCREEN_WIDTH - completion->w) / 2; dest.y = y; dest.w = completion->w; dest.h = completion->h; SDL_BlitSurface(completion, NULL, game.screen, &dest); SDL_FreeSurface(title); SDL_FreeSurface(completion); /* Swap the buffers */ SDL_Flip(game.screen); }
void updateLabelText(Label *l, char *text) { if (l->text != NULL) { SDL_FreeSurface(l->text); l->text = NULL; } l->text = addBorder(generateTextSurface(text, game.font, 255, 255, 255, 0, 0, 0), 255, 255, 255, 0, 0, 0); }
Label *createLabel(char *text, int x, int y) { Label *l; l = malloc(sizeof(Label)); if (l == NULL) { showErrorAndExit("Failed to allocate %d bytes to create Label %s", (int)sizeof(Label), text); } l->text = addBorder(generateTextSurface(text, game.font, 255, 255, 255, 0, 0, 0), 255, 255, 255, 0, 0, 0); l->x = x; l->y = y; return l; }
SDL_Surface *createDialogBox(char *title, char *msg) { char *text, *token, word[MAX_VALUE_LENGTH], *savePtr, *titleText; int i, lines, w, h, maxWidth, lineBreak, *lineBreaks; SDL_Surface **surface, *tempSurface; SDL_Rect dest; savePtr = NULL; freeDialogBox(); text = malloc(strlen(msg) + 1); if (text == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the Dialog Text", (int)strlen(msg) + 1); } STRNCPY(text, msg, strlen(msg) + 1); titleText = NULL; if (title != NULL) { titleText = malloc(strlen(title) + 1); if (titleText == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the Dialog Text", (int)strlen(title) + 1); } STRNCPY(titleText, title, strlen(title) + 1); } token = strtok_r(text, " ", &savePtr); i = 0; while (token != NULL) { i++; token = strtok_r(NULL, " ", &savePtr); } lines = i; if (titleText != NULL) { token = titleText; while (*token != '\0') { if (*token == '_') { *token = ' '; } token++; } lines++; } surface = malloc(sizeof(SDL_Surface *) * lines); if (surface == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the Dialog Surfaces", (int)sizeof(SDL_Surface *) * lines); } lineBreaks = malloc(sizeof(int) * lines); if (lineBreaks == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the line breaks", (int)sizeof(int) * lines); } STRNCPY(text, msg, strlen(msg) + 1); token = strtok_r(text, " ", &savePtr); i = 0; maxWidth = w = h = 0; if (titleText != NULL) { surface[i] = generateTextSurface(titleText, game.font, 255, 255, 0, 0, 0, 0); h = surface[i]->h + 5; maxWidth = surface[i]->w; i++; } while (token != NULL) { lineBreak = FALSE; snprintf(word, sizeof(word), "%d", game.kills); token = replaceString(token, "[GAME_KILLS]", word); token = replaceString(token, "[HOURS]", getPlayTimeHours()); snprintf(word, sizeof(word), "%d", game.continues); token = replaceString(token, "[CONTINUE_COUNT]", word); token = replaceString(token, "[INPUT_LEFT]", getKeyValue(control.button[CONTROL_LEFT])); token = replaceString(token, "[INPUT_RIGHT]", getKeyValue(control.button[CONTROL_RIGHT])); token = replaceString(token, "[INPUT_JUMP]", getKeyValue(control.button[CONTROL_JUMP])); token = replaceString(token, "[INPUT_BLOCK]", getKeyValue(control.button[CONTROL_BLOCK])); token = replaceString(token, "[INPUT_ATTACK]", getKeyValue(control.button[CONTROL_ATTACK])); token = replaceString(token, "[INPUT_INTERACT]", getKeyValue(control.button[CONTROL_INTERACT])); token = replaceString(token, "[INPUT_ACTIVATE]", getKeyValue(control.button[CONTROL_ACTIVATE])); token = replaceString(token, "[INPUT_INVENTORY]", getKeyValue(control.button[CONTROL_INVENTORY])); token = replaceString(token, "[INPUT_PREVIOUS]", getKeyValue(control.button[CONTROL_PREVIOUS])); token = replaceString(token, "[INPUT_NEXT]", getKeyValue(control.button[CONTROL_NEXT])); snprintf(word, sizeof(word), "%s ", token); if (word[strlen(word) - 2] == '\n') { lineBreak = TRUE; word[strlen(word) - 2] = '\0'; } token = strtok_r(NULL, " ", &savePtr); if (token == NULL) { word[strlen(word) - 1] = '\0'; } surface[i] = generateTextSurface(word, game.font, 255, 255, 255, 0, 0, 0); lineBreaks[i] = lineBreak; if (h == 0 || (i == 1 && titleText != NULL)) { h += surface[i]->h + 5; } if (w + surface[i]->w > MAX_DIALOG_WIDTH) { w = 0; h += surface[i]->h + 5; } w += surface[i]->w; if (w > maxWidth) { maxWidth = w; } if (lineBreak == TRUE) { w = 0; h += surface[i]->h + 5; } i++; } h -= 5; tempSurface = createSurface(maxWidth, h); w = h = 0; for (i=0; i<lines; i++) { if (w + surface[i]->w > MAX_DIALOG_WIDTH || (titleText != NULL && i == 1)) { w = 0; h += surface[i]->h + 5; } dest.x = w; dest.y = h; dest.w = surface[i]->w; dest.h = surface[i]->h; SDL_BlitSurface(surface[i], NULL, tempSurface, &dest); w += surface[i]->w; SDL_FreeSurface(surface[i]); if (lineBreaks[i] == TRUE) { w = 0; h += surface[i]->h + 5; } } tempSurface = addBorder(tempSurface, 255, 255, 255, 0, 0, 0); free(surface); free(text); if (titleText != NULL) { free(titleText); } free(lineBreaks); return tempSurface; }
static SDL_Surface *createWidgetText(char *msg, TTF_Font *font, int fr, int fg, int fb, int br, int bg, int bb) { char *text, *token, word[MAX_VALUE_LENGTH], *savePtr; int i, lines, w, h, maxWidth, lineBreak, *lineBreaks; SDL_Surface **surface, *tempSurface; SDL_Rect dest; savePtr = NULL; text = malloc(strlen(msg) + 1); if (text == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the Dialog Text", (int)strlen(msg) + 1); } STRNCPY(text, msg, strlen(msg) + 1); token = strtok_r(text, " ", &savePtr); i = 0; while (token != NULL) { i++; token = strtok_r(NULL, " ", &savePtr); } lines = i; surface = malloc(sizeof(SDL_Surface *) * lines); if (surface == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the Dialog Surfaces", (int)sizeof(SDL_Surface *) * lines); } lineBreaks = malloc(sizeof(int) * lines); if (lineBreaks == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the line breaks", (int)sizeof(int) * lines); } STRNCPY(text, msg, strlen(msg) + 1); token = strtok_r(text, " ", &savePtr); i = 0; maxWidth = w = h = 0; while (token != NULL) { lineBreak = FALSE; snprintf(word, sizeof(word), "%s ", token); if (word[strlen(word) - 2] == '\n') { lineBreak = TRUE; word[strlen(word) - 2] = '\0'; } token = strtok_r(NULL, " ", &savePtr); if (token == NULL) { word[strlen(word) - 1] = '\0'; } surface[i] = generateTextSurface(word, game.font, fr, fg, fb, br, bg, bb); lineBreaks[i] = lineBreak; if (h == 0) { h += surface[i]->h + 5; } if (w + surface[i]->w > MAX_SCRIPT_WIDTH) { w = 0; h += surface[i]->h + 5; } w += surface[i]->w; if (w > maxWidth) { maxWidth = w; } if (lineBreak == TRUE) { w = 0; h += surface[i]->h + 5; } i++; } h -= 5; tempSurface = createSurface(maxWidth, h); w = h = 0; for (i=0;i<lines;i++) { if (w + surface[i]->w > MAX_SCRIPT_WIDTH) { w = 0; h += surface[i]->h + 5; } dest.x = w; dest.y = h; dest.w = surface[i]->w; dest.h = surface[i]->h; SDL_BlitSurface(surface[i], NULL, tempSurface, &dest); w += surface[i]->w; SDL_FreeSurface(surface[i]); if (lineBreaks[i] == TRUE) { w = 0; h += surface[i]->h + 5; } } free(surface); free(text); free(lineBreaks); return tempSurface; }
void showErrorAndExit(char *fmt, ...) { int h, y; SDL_Rect dest; SDL_Surface *title, *error1, *error2, *error3; char text[MAX_MESSAGE_LENGTH]; va_list ap; va_start(ap, fmt); vsnprintf(text, sizeof(text), fmt, ap); va_end(ap); error1 = error2 = error3 = title = NULL; if (game.font != NULL) { title = generateTextSurface(_("The Legend of Edgar has encountered the following error"), game.font, 0, 220, 0, 0, 0, 0); error1 = generateTextSurface(text, game.font, 220, 220, 220, 0, 0, 0); error2 = generateTextSurface(_("Please report this error to Parallel Realities"), game.font, 0, 220, 0, 0, 0, 0); error3 = generateTextSurface(_("Press Escape to exit"), game.font, 0, 220, 0, 0, 0, 0); } printf("%s\n", text); #if DEV == 1 exit(1); #endif if (title == NULL || error1 == NULL || error2 == NULL || error3 == NULL) { exit(1); } if (game.tempSurface != NULL) { SDL_FreeSurface(game.tempSurface); game.tempSurface = NULL; } game.tempSurface = createSurface(SCREEN_WIDTH, SCREEN_HEIGHT); drawBox(game.tempSurface, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0); h = title->h + error1->h + error2->h + error3->h + 45; y = (SCREEN_HEIGHT - h) / 2; dest.x = (SCREEN_WIDTH - title->w) / 2; dest.y = y; dest.w = title->w; dest.h = title->h; SDL_BlitSurface(title, NULL, game.tempSurface, &dest); y += title->h + 15; dest.x = (SCREEN_WIDTH - error1->w) / 2; dest.y = y; dest.w = error1->w; dest.h = error1->h; SDL_BlitSurface(error1, NULL, game.tempSurface, &dest); y += error1->h + 15; dest.x = (SCREEN_WIDTH - error2->w) / 2; dest.y = y; dest.w = error2->w; dest.h = error2->h; SDL_BlitSurface(error2, NULL, game.tempSurface, &dest); y += error2->h + 15; dest.x = (SCREEN_WIDTH - error3->w) / 2; dest.y = y; dest.w = error3->w; dest.h = error3->h; SDL_BlitSurface(error3, NULL, game.tempSurface, &dest); SDL_FreeSurface(title); SDL_FreeSurface(error1); SDL_FreeSurface(error2); SDL_FreeSurface(error3); game.status = IN_ERROR; stopMusic(); drawError(); }