2018-01-29 15:46:04 +03:00
|
|
|
/**********************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib - Advance Game template
|
|
|
|
*
|
|
|
|
* Ending Screen Functions Definitions (Init, Update, Draw, Unload)
|
|
|
|
*
|
2019-04-07 18:49:12 +03:00
|
|
|
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
|
2018-01-29 15:46:04 +03:00
|
|
|
*
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
#include "screens.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
#define MAX_TITLE_CHAR 256
|
2018-01-29 15:46:04 +03:00
|
|
|
#define MAX_SUBTITLE_CHAR 256
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition (local to this module)
|
|
|
|
//----------------------------------------------------------------------------------
|
2018-12-24 19:46:23 +03:00
|
|
|
static char *codingWords[MAX_CODING_WORDS] = {
|
|
|
|
"pollo\0",
|
|
|
|
"conejo\0",
|
2018-01-29 15:46:04 +03:00
|
|
|
"huevo\0",
|
2018-12-24 19:46:23 +03:00
|
|
|
"nido\0",
|
|
|
|
"aire\0",
|
|
|
|
"armario\0",
|
2018-01-29 15:46:04 +03:00
|
|
|
"agujero\0",
|
|
|
|
"platano\0",
|
|
|
|
"pastel\0",
|
|
|
|
"mercado\0",
|
|
|
|
"raton\0",
|
|
|
|
"melon\0",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ending screen global variables
|
|
|
|
static int framesCounter;
|
|
|
|
static int finishScreen;
|
|
|
|
|
|
|
|
static Texture2D texBackground;
|
|
|
|
static Texture2D texNewspaper;
|
|
|
|
static Texture2D texVignette;
|
|
|
|
|
|
|
|
static Sound fxNews;
|
|
|
|
|
|
|
|
static float rotation = 0.1f;
|
|
|
|
static float scale = 0.05f;
|
|
|
|
static int state = 0;
|
|
|
|
|
|
|
|
static Mission *missions = NULL;
|
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
static char headline[MAX_TITLE_CHAR] = "\0";
|
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
Font fontNews;
|
2018-02-17 23:23:45 +03:00
|
|
|
|
|
|
|
// String (const char *) replacement function
|
|
|
|
static char *StringReplace(char *orig, char *rep, char *with);
|
2018-01-29 15:46:04 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Ending Screen Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Ending Screen Initialization logic
|
|
|
|
void InitEndingScreen(void)
|
|
|
|
{
|
|
|
|
framesCounter = 0;
|
|
|
|
finishScreen = 0;
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
rotation = 0.1f;
|
|
|
|
scale = 0.05f;
|
|
|
|
state = 0;
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
texBackground = LoadTexture("resources/textures/ending_background.png");
|
|
|
|
texVignette = LoadTexture("resources/textures/message_vignette.png");
|
|
|
|
fxNews = LoadSound("resources/audio/fx_batman.ogg");
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
missions = LoadMissions("resources/missions.txt");
|
|
|
|
int wordsCount = missions[currentMission].wordsCount;
|
2018-02-17 23:23:45 +03:00
|
|
|
|
|
|
|
strcpy(headline, missions[currentMission].msg); // Base headline
|
|
|
|
int len = strlen(headline);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
// Remove @ from headline
|
|
|
|
// TODO: Also remove additional spaces
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (headline[i] == '@') headline[i] = ' ';
|
|
|
|
}
|
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
for (int i = 0; i < wordsCount; i++)
|
|
|
|
{
|
2018-02-17 23:23:45 +03:00
|
|
|
if (messageWords[i].id != missions[currentMission].sols[i])
|
2018-01-29 15:46:04 +03:00
|
|
|
{
|
2018-02-17 23:23:45 +03:00
|
|
|
// WARNING: It fails if the last sentence word has a '.' after space
|
|
|
|
char *title = StringReplace(headline, messageWords[i].text, codingWords[messageWords[i].id]);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-10-10 23:05:13 +03:00
|
|
|
if (title != NULL)
|
|
|
|
{
|
|
|
|
strcpy(headline, title); // Base headline updated
|
|
|
|
free(title);
|
|
|
|
}
|
2018-01-29 15:46:04 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
TraceLog(LOG_WARNING, "Titular: %s", headline);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
// Generate newspaper with title and subtitle
|
|
|
|
Image imNewspaper = LoadImage("resources/textures/ending_newspaper.png");
|
2018-12-25 17:17:42 +03:00
|
|
|
fontNews = LoadFontEx("resources/fonts/Lora-Bold.ttf", 32, 0, 250);
|
2018-02-17 23:23:45 +03:00
|
|
|
ImageDrawTextEx(&imNewspaper, (Vector2){ 50, 220 }, fontNews, headline, fontNews.baseSize, 0, DARKGRAY);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
texNewspaper = LoadTextureFromImage(imNewspaper);
|
2018-05-04 17:59:48 +03:00
|
|
|
//UnloadFont(fontNews);
|
2018-01-29 15:46:04 +03:00
|
|
|
UnloadImage(imNewspaper);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ending Screen Update logic
|
|
|
|
void UpdateEndingScreen(void)
|
|
|
|
{
|
|
|
|
framesCounter++;
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
if (framesCounter == 10) PlaySound(fxNews);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
if (state == 0)
|
|
|
|
{
|
|
|
|
rotation += 18.0f;
|
|
|
|
scale += 0.0096f;
|
2018-12-24 19:46:23 +03:00
|
|
|
|
|
|
|
if (scale >= 1.0f)
|
2018-01-29 15:46:04 +03:00
|
|
|
{
|
|
|
|
scale = 1.0f;
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 19:46:23 +03:00
|
|
|
|
|
|
|
if ((state == 1) && (IsKeyPressed(KEY_ENTER) || IsButtonPressed()))
|
2018-01-29 15:46:04 +03:00
|
|
|
{
|
|
|
|
currentMission++;
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
if (currentMission >= totalMissions) finishScreen = 2;
|
|
|
|
else finishScreen = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ending Screen Draw logic
|
|
|
|
void DrawEndingScreen(void)
|
|
|
|
{
|
|
|
|
DrawTexture(texBackground, 0, 0, WHITE);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
DrawTexturePro(texNewspaper, (Rectangle){ 0, 0, texNewspaper.width, texNewspaper.height },
|
2018-12-24 19:46:23 +03:00
|
|
|
(Rectangle){ GetScreenWidth()/2, GetScreenHeight()/2, texNewspaper.width*scale, texNewspaper.height*scale },
|
2018-01-29 15:46:04 +03:00
|
|
|
(Vector2){ (float)texNewspaper.width*scale/2, (float)texNewspaper.height*scale/2 }, rotation, WHITE);
|
|
|
|
|
|
|
|
DrawTextureEx(texVignette, (Vector2){ 0, 0 }, 0.0f, 2.0f, WHITE);
|
2018-02-17 23:23:45 +03:00
|
|
|
|
|
|
|
// Draw debug information
|
|
|
|
DrawTextEx(fontNews, headline, (Vector2){ 10, 10 }, fontNews.baseSize, 0, RAYWHITE);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
for (int i = 0; i < missions[currentMission].wordsCount; i++)
|
2018-01-29 15:46:04 +03:00
|
|
|
{
|
2018-02-17 23:23:45 +03:00
|
|
|
DrawText(codingWords[messageWords[i].id], 10, 60 + 30*i, 20, (messageWords[i].id == missions[currentMission].sols[i]) ? GREEN : RED);
|
2018-01-29 15:46:04 +03:00
|
|
|
}
|
2018-02-17 23:23:45 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
if (state == 1) DrawButton("continuar");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ending Screen Unload logic
|
|
|
|
void UnloadEndingScreen(void)
|
|
|
|
{
|
|
|
|
UnloadTexture(texBackground);
|
|
|
|
UnloadTexture(texNewspaper);
|
|
|
|
UnloadTexture(texVignette);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-01-29 15:46:04 +03:00
|
|
|
UnloadSound(fxNews);
|
2018-12-24 19:46:23 +03:00
|
|
|
free(missions);
|
2018-01-29 15:46:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ending Screen should finish?
|
|
|
|
int FinishEndingScreen(void)
|
|
|
|
{
|
|
|
|
return finishScreen;
|
2018-02-17 23:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String (const char *) replacement function
|
|
|
|
// NOTE: Internally allocated memory must be freed by the user (if return != NULL)
|
|
|
|
// https://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c
|
|
|
|
static char *StringReplace(char *orig, char *rep, char *with)
|
|
|
|
{
|
|
|
|
char *result; // the return string
|
|
|
|
char *ins; // the next insert point
|
|
|
|
char *tmp; // varies
|
|
|
|
int len_rep; // length of rep (the string to remove)
|
|
|
|
int len_with; // length of with (the string to replace rep with)
|
|
|
|
int len_front; // distance between rep and end of last rep
|
|
|
|
int count; // number of replacements
|
|
|
|
|
|
|
|
// Sanity checks and initialization
|
|
|
|
if (!orig || !rep) return NULL;
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
len_rep = strlen(rep);
|
|
|
|
if (len_rep == 0) return NULL; // Empty rep causes infinite loop during count
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
if (!with) with = ""; // Replace with nothing if not provided
|
|
|
|
len_with = strlen(with);
|
|
|
|
|
|
|
|
// Count the number of replacements needed
|
|
|
|
ins = orig;
|
2018-12-24 19:46:23 +03:00
|
|
|
for (count = 0; tmp = strstr(ins, rep); ++count)
|
2018-02-17 23:23:45 +03:00
|
|
|
{
|
|
|
|
ins = tmp + len_rep;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = result = malloc(strlen(orig) + (len_with - len_rep)*count + 1);
|
|
|
|
|
|
|
|
if (!result) return NULL; // Memory could not be allocated
|
|
|
|
|
|
|
|
// First time through the loop, all the variable are set correctly from here on,
|
|
|
|
// tmp points to the end of the result string
|
|
|
|
// ins points to the next occurrence of rep in orig
|
|
|
|
// orig points to the remainder of orig after "end of rep"
|
2018-12-24 19:46:23 +03:00
|
|
|
while (count--)
|
2018-02-17 23:23:45 +03:00
|
|
|
{
|
|
|
|
ins = strstr(orig, rep);
|
|
|
|
len_front = ins - orig;
|
|
|
|
tmp = strncpy(tmp, orig, len_front) + len_front;
|
|
|
|
tmp = strcpy(tmp, with) + len_with;
|
|
|
|
orig += len_front + len_rep; // move to next "end of rep"
|
|
|
|
}
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
strcpy(tmp, orig);
|
2018-12-24 19:46:23 +03:00
|
|
|
|
2018-02-17 23:23:45 +03:00
|
|
|
return result;
|
|
|
|
}
|