Skip to content

Commit

Permalink
Custom Logos - Major Refactor
Browse files Browse the repository at this point in the history
- Add custom functions preloadLogos and preloadAssets to keep it easy to read and maintain.

- Disable the shift key behavior when not at LOGO0.

------------------------

I guess this solves the issues? Lemme know if you guys need anything else.
  • Loading branch information
jetrotal committed Sep 25, 2023
1 parent 110cfa1 commit d8ced11
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 59 deletions.
3 changes: 3 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace Player {
int message_box_offset_x = (screen_width - MENU_WIDTH) / 2;
bool has_custom_resolution = false;

std::vector<BitmapRef> logos_container;
int current_logo;

bool exit_flag;
Expand Down Expand Up @@ -206,6 +207,8 @@ void Player::Run() {
Instrumentation::Init("EasyRPG-Player");

current_logo = 0;
logos_container.clear();

Scene::Push(std::make_shared<Scene_Logo>());
Graphics::UpdateSceneCallback();

Expand Down
3 changes: 3 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ namespace Player {
/** Set the desired rendering frames per second */
void SetTargetFps(int fps);

/** Logos Container - A collection of logo files */
extern std::vector<BitmapRef> logos_container;

/** Current Logo - for startup */
extern int current_logo;

Expand Down
144 changes: 85 additions & 59 deletions src/scene_logo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,96 @@ Scene_Logo::Scene_Logo() :
type = Scene::Logo;
}

Filesystem_Stream::InputStream logo_stream;
Filesystem_Stream::InputStream next_logo;
static bool preloaded_assets = false;
void PreloadAssets() {

void Scene_Logo::Start() {
if (!Player::debug_flag && !Game_Battle::battle_test.enabled) {
std::time_t t = std::time(nullptr);
std::tm* tm = std::localtime(&t);
if (preloaded_assets) {
return;
}

if (FileFinder::Game()) logo_stream = FileFinder::OpenImage("Font", "LOGO" + std::to_string(Player::current_logo));
//TODO: Maybe get LOGO1,LOGO2,LOGO3 from rpg_rt too?
auto fs = FileFinder::Game();
if (!fs) {
fs = FileFinder::Root().Create(Main_Data::GetDefaultProjectPath());
if (!fs) {
Output::Error("{} is not a valid path", Main_Data::GetDefaultProjectPath());
}
FileFinder::SetGameFilesystem(fs);
}

if (!logo_stream && Player::current_logo == 0) {
#ifdef EMSCRIPTEN
static bool once = true;
if (once) {
FileRequestAsync* index = AsyncHandler::RequestFile("index.json");
index->SetImportantFile(true);
request_id = index->Bind(&Scene_Logo::OnIndexReady, this);
once = false;
index->Start();
return;
}
if (!async_ready) {
return;
}
#endif

if (Rand::ChanceOf(1, 32) || (tm->tm_mday == 1 && tm->tm_mon == 3)) {
logo_img = Bitmap::Create(easyrpg_logo2, sizeof(easyrpg_logo2), false);
}
else {
logo_img = Bitmap::Create(easyrpg_logo, sizeof(easyrpg_logo), false);
}
if (FileFinder::IsValidProject(fs)) {
Player::CreateGameObjects();
preloaded_assets = true;
}
}

// Define this function outside of Scene_Logo class
std::vector<BitmapRef> preloadLogos() {
std::vector<BitmapRef> logos;
std::time_t t = std::time(nullptr);
std::tm* tm = std::localtime(&t);

for (int logoIndex = 0; ; logoIndex++) {
Filesystem_Stream::InputStream logoStream;
if (logoIndex != 0) logoStream = FileFinder::OpenImage("Font", "LOGO" + std::to_string(logoIndex));
//TODO: Maybe get LOGO1,LOGO2,LOGO3 from rpg_rt too?

if (!logoStream) {
if (logoIndex == 0) {
if (Rand::ChanceOf(1, 32) || (tm->tm_mday == 1 && tm->tm_mon == 3)) {
logos.push_back(Bitmap::Create(easyrpg_logo2, sizeof(easyrpg_logo2), false));
}
else {
logos.push_back(Bitmap::Create(easyrpg_logo, sizeof(easyrpg_logo), false));
}
} else break; // Stop when no more logos can be found
}
else {
// Read the data from logo_stream and store it in a variable
std::vector<uint8_t> logoData = Utils::ReadStream(logo_stream);
// Read the data from logoStream and store it in a variable
std::vector<uint8_t> logoData = Utils::ReadStream(logoStream);

// Access the data as needed
const uint8_t* cached_logo = logoData.data();
const uint8_t* cachedLogo = logoData.data();
size_t logoSize = logoData.size();

// Create a bitmap using the logo data
logo_img = Bitmap::Create(cached_logo, logoSize, false);
// Create a bitmap using the logo data and store it
logos.push_back(Bitmap::Create(cachedLogo, logoSize, false));
}
}

return logos;
}

void Scene_Logo::Start() {

if (!Player::debug_flag && !Game_Battle::battle_test.enabled) {
// Preload logos if the container is empty
if (Player::logos_container.empty()) {
Player::logos_container = preloadLogos();
}

// Access the logo based on Player::current_logo
if (Player::current_logo < Player::logos_container.size()) {
logo_img = Player::logos_container[Player::current_logo];
}
else {
// Handle the case when Player::current_logo exceeds the number of logos
// You may want to add error handling here.
}

DrawText(false);

Expand All @@ -85,63 +142,31 @@ void Scene_Logo::Start() {

void Scene_Logo::vUpdate() {
static bool is_valid = false;
if (frame_counter == 0) PreloadAssets();

if (frame_counter == 0) {
auto fs = FileFinder::Game();

if (!fs) {
fs = FileFinder::Root().Create(Main_Data::GetDefaultProjectPath());
if (!fs) {
Output::Error("{} is not a valid path", Main_Data::GetDefaultProjectPath());
}
FileFinder::SetGameFilesystem(fs);
}

#ifdef EMSCRIPTEN
static bool once = true;
if (once) {
FileRequestAsync* index = AsyncHandler::RequestFile("index.json");
index->SetImportantFile(true);
request_id = index->Bind(&Scene_Logo::OnIndexReady, this);
once = false;
index->Start();
return;
}

if (!async_ready) {
return;
}
#endif

if (FileFinder::IsValidProject(fs)) {
if (Player::current_logo == 0) Player::CreateGameObjects(); // changed to stop loading the same assets multiple times.
is_valid = true;
}
}

is_valid = preloaded_assets;
++frame_counter;

if (Input::IsPressed(Input::SHIFT)) {
if (Input::IsPressed(Input::SHIFT) && Player::current_logo == 0) {
DrawText(true);
--frame_counter;
}

if (Player::debug_flag ||
Game_Battle::battle_test.enabled ||
frame_counter == 90 || //had to be longer to cover when Player::CreateGameObjects() doesn't happen
frame_counter == (Player::current_logo == 0 ? 60 : 90) || //had to be longer to cover when Player::CreateGameObjects() doesn't happen
Input::IsTriggered(Input::DECISION) ||
Input::IsTriggered(Input::CANCEL)) {

Player::current_logo++;
next_logo = FileFinder::OpenImage("Font", "LOGO" + std::to_string(Player::current_logo));

if (next_logo) {
if (Player::current_logo < Player::logos_container.size()) {
Scene::Pop();
Scene::Push(std::make_shared<Scene_Logo>());
return;
}

Player::current_logo = 0;
Player::logos_container.clear();

if (is_valid) {
if (!Player::startup_language.empty()) {
Expand Down Expand Up @@ -173,7 +198,7 @@ void Scene_Logo::DrawBackground(Bitmap& dst) {
void Scene_Logo::DrawText(bool verbose) {
if (Player::current_logo != 0) return;

Rect text_rect = {17, 215, 320 - 32, 0}; //last argument (rect height) is now 0 to remove a black rectangle that appears as text background color.
Rect text_rect = { 17, 215, 320 - 32, 16 * verbose }; //last argument (rect height) is now 0 to remove a black rectangle that appears as text background color.
Color text_color = {185, 199, 173, 255};
Color shadow_color = {69, 69, 69, 255};
logo_img->ClearRect(text_rect);
Expand All @@ -186,6 +211,7 @@ void Scene_Logo::DrawText(bool verbose) {
text_rect.x--;
text_rect.y--;
}

}

void Scene_Logo::OnIndexReady(FileRequestResult*) {
Expand Down

0 comments on commit d8ced11

Please sign in to comment.