Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maniac Patch: Implement GetPictureInfo #2735

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/async_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class AsyncOp {
eTerminateBattle,
eSave,
eLoad,
eYield
eYield,
eYieldRepeat
};

AsyncOp() = default;
Expand Down Expand Up @@ -76,6 +77,9 @@ class AsyncOp {
/** @return a Yield for one frame to e.g. fetch an important asset */
static AsyncOp MakeYield();

/** @return a Yield for one frame and repeat the command to e.g. fetch an important asset */
static AsyncOp MakeYieldRepeat();

/** @return the type of async operation */
Type GetType() const;

Expand Down Expand Up @@ -222,4 +226,8 @@ inline AsyncOp AsyncOp::MakeYield() {
return AsyncOp(eYield);
}

inline AsyncOp AsyncOp::MakeYieldRepeat() {
return AsyncOp(eYieldRepeat);
}

#endif
69 changes: 67 additions & 2 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ void Game_Interpreter::Update(bool reset_loop_count) {

// Previous command triggered an async operation.
if (IsAsyncPending()) {
if (_async_op.GetType() == AsyncOp::Type::eYieldRepeat) {
// This will cause an incorrect execution when the yielding
// command changed the index.
// Only use YieldRepeat for commands that do not do this!
auto& frame = GetFrame();
--frame.current_command;
}
break;
}

Expand Down Expand Up @@ -3973,12 +3980,70 @@ bool Game_Interpreter::CommandManiacShowStringPicture(lcf::rpg::EventCommand con
return true;
}

bool Game_Interpreter::CommandManiacGetPictureInfo(lcf::rpg::EventCommand const&) {
bool Game_Interpreter::CommandManiacGetPictureInfo(lcf::rpg::EventCommand const& com) {
if (!Player::IsPatchManiac()) {
return true;
}

Output::Warning("Maniac Patch: Command GetPictureInfo not supported");
int pic_id = ValueOrVariable(com.parameters[0], com.parameters[3]);
auto& pic = Main_Data::game_pictures->GetPicture(pic_id);

if (pic.IsRequestPending()) {
// Cannot do anything useful here without the dimensions
pic.MakeRequestImportant();
_async_op = AsyncOp::MakeYieldRepeat();
return true;
}

const auto& data = pic.data;

int x = 0;
int y = 0;
int width = pic.sprite ? pic.sprite->GetWidth() : 0;
int height = pic.sprite ? pic.sprite->GetHeight() : 0;

switch (com.parameters[1]) {
case 0:
x = Utils::RoundTo<int>(data.current_x);
y = Utils::RoundTo<int>(data.current_y);
break;
case 1:
x = Utils::RoundTo<int>(data.current_x);
y = Utils::RoundTo<int>(data.current_y);
width = Utils::RoundTo<int>(width * data.current_magnify / 100.0);
height = Utils::RoundTo<int>(height * data.current_magnify / 100.0);
break;
case 2:
x = Utils::RoundTo<int>(data.finish_x);
y = Utils::RoundTo<int>(data.finish_y);
width = Utils::RoundTo<int>(width * data.finish_magnify / 100.0);
height = Utils::RoundTo<int>(height * data.finish_magnify / 100.0);
break;
}

switch (com.parameters[2]) {
case 1:
// X/Y is top-left corner
x -= (width / 2);
y -= (height / 2);
break;
case 2: {
// Left, Top, Right, Bottom
x -= (width / 2);
y -= (height / 2);
width += x;
height += y;
break;
}
}

Main_Data::game_variables->Set(com.parameters[4], x);
Main_Data::game_variables->Set(com.parameters[5], y);
Main_Data::game_variables->Set(com.parameters[6], width);
Main_Data::game_variables->Set(com.parameters[7], height);

Game_Map::SetNeedRefresh(true);

return true;
}

Expand Down
16 changes: 13 additions & 3 deletions src/game_pictures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ bool Game_Pictures::Picture::Exists() const {
return !data.name.empty();
}

bool Game_Pictures::Picture::IsRequestPending() const {
return request_id != nullptr;
}

void Game_Pictures::Picture::MakeRequestImportant() const {
FileRequestAsync* request = AsyncHandler::RequestFile("Picture", data.name);
request->SetImportantFile(true);
}

void Game_Pictures::RequestPictureSprite(Picture& pic) {
const auto& name = pic.data.name;
if (name.empty()) {
Expand All @@ -333,13 +342,13 @@ void Game_Pictures::RequestPictureSprite(Picture& pic) {
int pic_id = pic.data.ID;

pic.request_id = request->Bind([this, pic_id](FileRequestResult*) {
OnPictureSpriteReady(pic_id);
});
OnPictureSpriteReady(pic_id);
});
request->Start();
}


void Game_Pictures::Picture::OnPictureSpriteReady() {
void Game_Pictures::Picture::OnPictureSpriteReady() const {
auto bitmap = Cache::Picture(data.name, data.use_transparent_color);

sprite->SetBitmap(bitmap);
Expand All @@ -350,6 +359,7 @@ void Game_Pictures::Picture::OnPictureSpriteReady() {
void Game_Pictures::OnPictureSpriteReady(int id) {
auto* pic = GetPicturePtr(id);
if (EP_LIKELY(pic)) {
pic->request_id = nullptr;
if (!pic->sprite) {
sprites.emplace_back(pic->data.ID, Drawable::Flags::Shared);
pic->sprite = &sprites.back();
Expand Down
5 changes: 4 additions & 1 deletion src/game_pictures.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ class Game_Pictures {
void Erase();
bool Exists() const;

void OnPictureSpriteReady();
bool IsRequestPending() const;
void MakeRequestImportant() const;

void OnPictureSpriteReady() const;
void OnMapScrolled(int dx, int dy);
};

Expand Down