2020-12-06 14:21:56 +01:00
|
|
|
#include "pch.h"
|
|
|
|
#include "high_score.h"
|
|
|
|
|
2020-12-12 14:36:20 +01:00
|
|
|
#include "options.h"
|
2021-09-21 12:14:39 +02:00
|
|
|
#include "pinball.h"
|
|
|
|
#include "score.h"
|
2020-12-12 14:36:20 +01:00
|
|
|
|
|
|
|
int high_score::dlg_enter_name;
|
|
|
|
int high_score::dlg_score;
|
2021-02-02 16:29:54 +01:00
|
|
|
int high_score::dlg_position;
|
2021-09-05 09:47:05 +02:00
|
|
|
char high_score::default_name[32]{};
|
2020-12-12 14:36:20 +01:00
|
|
|
high_score_struct* high_score::dlg_hst;
|
2021-09-05 09:47:05 +02:00
|
|
|
bool high_score::ShowDialog = false;
|
2020-12-12 14:36:20 +01:00
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
|
|
|
|
int high_score::read(high_score_struct* table)
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
|
|
|
char Buffer[20];
|
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
int checkSum = 0;
|
2020-12-12 14:36:20 +01:00
|
|
|
clear_table(table);
|
2021-02-02 16:29:54 +01:00
|
|
|
for (auto position = 0; position < 5; ++position)
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-02-02 16:29:54 +01:00
|
|
|
auto tablePtr = &table[position];
|
2021-09-16 09:57:46 +02:00
|
|
|
|
2021-09-09 10:40:54 +02:00
|
|
|
snprintf(Buffer, sizeof Buffer, "%d", position);
|
|
|
|
strcat(Buffer, ".Name");
|
2021-09-16 09:57:46 +02:00
|
|
|
auto name = options::get_string(Buffer, "");
|
|
|
|
strncpy(tablePtr->Name, name.c_str(), sizeof tablePtr->Name);
|
|
|
|
|
2021-09-09 10:40:54 +02:00
|
|
|
snprintf(Buffer, sizeof Buffer, "%d", position);
|
|
|
|
strcat(Buffer, ".Score");
|
2021-09-16 09:57:46 +02:00
|
|
|
tablePtr->Score = options::get_int(Buffer, tablePtr->Score);
|
|
|
|
|
|
|
|
for (int i = static_cast<int>(strlen(tablePtr->Name)); --i >= 0; checkSum += tablePtr->Name[i])
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
|
|
|
}
|
2021-09-16 09:57:46 +02:00
|
|
|
checkSum += tablePtr->Score;
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-02-02 16:29:54 +01:00
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
auto verification = options::get_int("Verification", 7);
|
|
|
|
if (checkSum != verification)
|
2020-12-12 14:36:20 +01:00
|
|
|
clear_table(table);
|
2020-12-06 14:21:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
int high_score::write(high_score_struct* table)
|
2020-12-06 14:21:56 +01:00
|
|
|
{
|
2020-12-12 14:36:20 +01:00
|
|
|
char Buffer[20];
|
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
int checkSum = 0;
|
2021-02-02 16:29:54 +01:00
|
|
|
for (auto position = 0; position < 5; ++position)
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-16 09:57:46 +02:00
|
|
|
auto tablePtr = &table[position];
|
|
|
|
|
2021-09-09 10:40:54 +02:00
|
|
|
snprintf(Buffer, sizeof Buffer, "%d", position);
|
|
|
|
strcat(Buffer, ".Name");
|
2021-09-16 09:57:46 +02:00
|
|
|
options::set_string(Buffer, tablePtr->Name);
|
|
|
|
|
2021-09-09 10:40:54 +02:00
|
|
|
snprintf(Buffer, sizeof Buffer, "%d", position);
|
|
|
|
strcat(Buffer, ".Score");
|
2021-09-16 09:57:46 +02:00
|
|
|
options::set_int(Buffer, tablePtr->Score);
|
|
|
|
|
|
|
|
for (int i = static_cast<int>(strlen(tablePtr->Name)); --i >= 0; checkSum += tablePtr->Name[i])
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
|
|
|
}
|
2021-09-16 09:57:46 +02:00
|
|
|
checkSum += tablePtr->Score;
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-09-16 09:57:46 +02:00
|
|
|
|
|
|
|
options::set_int("Verification", checkSum);
|
2020-12-06 14:21:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:36:20 +01:00
|
|
|
void high_score::clear_table(high_score_struct* table)
|
|
|
|
{
|
|
|
|
for (int index = 5; index; --index)
|
|
|
|
{
|
|
|
|
table->Score = -999;
|
|
|
|
table->Name[0] = 0;
|
|
|
|
++table;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int high_score::get_score_position(high_score_struct* table, int score)
|
|
|
|
{
|
|
|
|
if (score <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (int position = 0; position < 5; position++)
|
|
|
|
{
|
|
|
|
if (table[position].Score < score)
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int high_score::place_new_score_into(high_score_struct* table, int score, LPSTR scoreStr, int position)
|
|
|
|
{
|
|
|
|
if (position >= 0)
|
|
|
|
{
|
|
|
|
if (position <= 4)
|
|
|
|
{
|
|
|
|
high_score_struct* tablePtr = table + 4;
|
|
|
|
int index = 5 - position;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
--index;
|
|
|
|
memcpy(tablePtr, &tablePtr[-1], sizeof(high_score_struct));
|
|
|
|
--tablePtr;
|
|
|
|
}
|
|
|
|
while (index);
|
|
|
|
}
|
|
|
|
high_score_struct* posTable = &table[position];
|
|
|
|
posTable->Score = score;
|
2021-09-06 15:27:58 +02:00
|
|
|
if (strlen(scoreStr) >= 31)
|
2020-12-12 14:36:20 +01:00
|
|
|
scoreStr[31] = 0;
|
2021-09-09 10:40:54 +02:00
|
|
|
strncpy(posTable->Name, scoreStr, sizeof posTable->Name);
|
2020-12-12 14:36:20 +01:00
|
|
|
posTable->Name[31] = 0;
|
|
|
|
}
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void high_score::show_high_score_dialog(high_score_struct* table)
|
|
|
|
{
|
|
|
|
dlg_enter_name = 0;
|
|
|
|
dlg_score = 0;
|
|
|
|
dlg_hst = table;
|
2021-09-05 09:47:05 +02:00
|
|
|
ShowDialog = true;
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void high_score::show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCSTR defaultName)
|
|
|
|
{
|
2021-02-02 16:29:54 +01:00
|
|
|
dlg_position = pos;
|
2020-12-12 14:36:20 +01:00
|
|
|
dlg_score = score;
|
|
|
|
dlg_hst = table;
|
|
|
|
dlg_enter_name = 1;
|
2021-09-22 14:50:07 +02:00
|
|
|
strncpy(default_name, defaultName, sizeof default_name - 1);
|
2021-09-05 09:47:05 +02:00
|
|
|
ShowDialog = true;
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 09:47:05 +02:00
|
|
|
void high_score::RenderHighScoreDialog()
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
if (ShowDialog == true)
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
ShowDialog = false;
|
2021-09-16 09:57:46 +02:00
|
|
|
if (dlg_position == -1)
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
dlg_enter_name = 0;
|
|
|
|
return;
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::OpenPopup("High Scores");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unused_open = true;
|
|
|
|
if (ImGui::BeginPopupModal("High Scores", &unused_open, ImGuiWindowFlags_AlwaysAutoResize))
|
|
|
|
{
|
|
|
|
if (ImGui::BeginTable("table1", 3, 0))
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
char buf[36];
|
|
|
|
ImGui::TableSetupColumn("Rank");
|
|
|
|
ImGui::TableSetupColumn("Name");
|
|
|
|
ImGui::TableSetupColumn("Score");
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
2021-10-15 12:26:39 +02:00
|
|
|
for (int offset = 0, row = 0; row < 5; row++)
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2021-09-09 10:40:54 +02:00
|
|
|
snprintf(buf, sizeof buf, "%d", row);
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::TextUnformatted(buf);
|
|
|
|
|
2021-10-15 12:26:39 +02:00
|
|
|
auto currentRow = &dlg_hst[row + offset];
|
|
|
|
auto score = currentRow->Score;
|
2021-09-09 10:40:54 +02:00
|
|
|
ImGui::TableNextColumn();
|
2021-09-05 09:47:05 +02:00
|
|
|
if (dlg_enter_name == 1 && dlg_position == row)
|
|
|
|
{
|
2021-10-15 12:26:39 +02:00
|
|
|
offset = -1;
|
2021-09-05 09:47:05 +02:00
|
|
|
score = dlg_score;
|
|
|
|
ImGui::PushItemWidth(200);
|
|
|
|
ImGui::InputText("", default_name, IM_ARRAYSIZE(default_name));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-15 12:26:39 +02:00
|
|
|
ImGui::TextUnformatted(currentRow->Name);
|
2021-09-05 09:47:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
score::string_format(score, buf);
|
|
|
|
ImGui::TextUnformatted(buf);
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::EndTable();
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (ImGui::Button("Ok"))
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
if (dlg_enter_name)
|
2021-09-09 10:40:54 +02:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
default_name[31] = 0;
|
2021-09-09 10:40:54 +02:00
|
|
|
place_new_score_into(dlg_hst, dlg_score, default_name, dlg_position);
|
2021-09-05 09:47:05 +02:00
|
|
|
}
|
|
|
|
ImGui::CloseCurrentPopup();
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-09-05 09:47:05 +02:00
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("Cancel"))
|
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("Clear"))
|
|
|
|
ImGui::OpenPopup("Confirm");
|
|
|
|
if (ImGui::BeginPopupModal("Confirm", nullptr, ImGuiWindowFlags_MenuBar))
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::TextUnformatted(pinball::get_rc_string(40, 0));
|
|
|
|
if (ImGui::Button("OK", ImVec2(120, 0)))
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
clear_table(dlg_hst);
|
|
|
|
ImGui::CloseCurrentPopup();
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::SetItemDefaultFocus();
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("Cancel", ImVec2(120, 0)))
|
2020-12-12 14:36:20 +01:00
|
|
|
{
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::CloseCurrentPopup();
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::EndPopup();
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 09:47:05 +02:00
|
|
|
ImGui::EndPopup();
|
2020-12-12 14:36:20 +01:00
|
|
|
}
|
2020-12-06 14:21:56 +01:00
|
|
|
}
|