Skip to content

Commit

Permalink
More slight menu optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Apr 5, 2024
1 parent 9786fad commit 9bd4afb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 60 deletions.
4 changes: 0 additions & 4 deletions src/emulator/Cpu.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by doom on 19/03/24.
//

#include "Cpu.h"

Cpu::Cpu(Renderer* renderer, Keyboard* keyboard, Speaker * speaker) {
Expand Down
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ int main(int argc, char **argv) {
romFiles.find(&romDirectories.back())->second.emplace_back(file.path());
}
}

} else {
config_t cfg;
config_init(&cfg);
Expand Down
91 changes: 43 additions & 48 deletions src/sfml/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ MainMenu::MainMenu(std::unordered_map<std::string *, std::vector<std::string>>&
while (this->window.isOpen()) {
sf::Event event;
sf::Vector2i pos = sf::Mouse::getPosition(this->window);
while (window.pollEvent(event)) {
while (this->window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}

if (event.type == sf::Event::LostFocus)
this->window.close();
} else if (event.type == sf::Event::LostFocus) {
focus = false;
else if (event.type == sf::Event::GainedFocus)
} else if (event.type == sf::Event::GainedFocus) {
focus = true;

if (event.type == sf::Event::Resized) {
} else if (event.type == sf::Event::Resized) {
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
this->window.setView(sf::View(visibleArea));

Expand All @@ -76,65 +73,63 @@ MainMenu::MainMenu(std::unordered_map<std::string *, std::vector<std::string>>&

button.updateSize(originalWindowSize, this->window.getSize());
button.update(this->window, pos);
}

if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::L) {
debug = !debug;
} else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Key::L) {
debug = !debug;
}
}
}

if (focus) {
for (auto &romButton: roms) {
for (auto& romButton : roms) {
romButton.second.update(this->window, pos);

if (!romButton.second.isJustClicked()) continue;

Emulator emulator;
std::thread newWindow(&Emulator::launch, &emulator, romButton.first);
newWindow.detach();
windows.emplace_back(&newWindow);
}
button.update(this->window, pos);
}

for (auto& romButton : roms) {
if (!romButton.second.isJustClicked()) continue;

Emulator emulator;
std::thread newWindow(&Emulator::launch, &emulator, romButton.first);
newWindow.detach();
windows.emplace_back(&newWindow);
}
if (button.isClicked()) {
nfdchar_t* outPath = nullptr;
nfdresult_t result = NFD_PickFolder(nullptr, &outPath);
if (button.isClicked()) {
nfdchar_t* outPath = nullptr;
nfdresult_t result = NFD_PickFolder(nullptr, &outPath);

if (result == NFD_OKAY) {
libconfig::Config config;
config.readFile(configFilePath);
if (result == NFD_OKAY) {
libconfig::Config config;
config.readFile(configFilePath);

libconfig::Setting& settings = config.getRoot();
libconfig::Setting& settings = config.getRoot();

if (!settings.exists("directories")) {
settings.add("directories", libconfig::Setting::TypeArray);
}
if (!settings.exists("directories")) {
settings.add("directories", libconfig::Setting::TypeArray);
}

libconfig::Setting& directories = settings["directories"];
directories.add(libconfig::Setting::TypeString) = outPath;
libconfig::Setting& directories = settings["directories"];
directories.add(libconfig::Setting::TypeString) = outPath;

romDirectories.emplace_back(outPath);
romDirectories.emplace_back(outPath);

for (const auto& file : std::filesystem::directory_iterator(outPath)) {
if (file.is_directory()) continue; // TODO: Make sure its a file that can be emulated, at least basic checks so it isn't like a word doc
for (const auto& file : std::filesystem::directory_iterator(outPath)) {
if (file.is_directory()) continue; // TODO: Make sure its a file that can be emulated, at least basic checks so it isn't like a word doc

printf("Processing file - : %s\n", file.path().c_str());
printf("Processing file - : %s\n", file.path().c_str());

// Check if the rom directory doesn't exist in romFiles, then add it
if (romFiles.find(&romDirectories.back()) == romFiles.end()) {
romFiles.emplace(&romDirectories.back(), std::vector<std::string>());
}
// Check if the rom directory doesn't exist in romFiles, then add it
if (romFiles.find(&romDirectories.back()) == romFiles.end()) {
romFiles.emplace(&romDirectories.back(), std::vector<std::string>());
}

// Add the file path to the romFiles entry
romFiles.find(&romDirectories.back())->second.emplace_back(file.path());
// Add the file path to the romFiles entry
romFiles.find(&romDirectories.back())->second.emplace_back(file.path());

TextButton romButton(0, 25 * roms.size(), this->window.getSize().x, 25, file.path().filename().string(), &font);
roms.emplace(file.path().string(), romButton);
TextButton romButton(0, 25 * roms.size(), this->window.getSize().x, 25, file.path().filename().string(), &font);
roms.emplace(file.path().string(), romButton);
}
config.writeFile(configFilePath);
}
;
config.writeFile(configFilePath);
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/sfml/ui/TextButton.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by doom on 3/27/24.
//

#include "TextButton.h"

TextButton::TextButton(float x, float y, float width, float height, std::string buttonText, sf::Font* font) {
Expand Down Expand Up @@ -40,15 +36,22 @@ void TextButton::update(sf::RenderWindow& window, sf::Vector2i pos) {

if (this->isHovered && sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
this->isPressed = true;
this->button.setFillColor(this->activeColor);
updateColour(this->activeColor);
} else if (this->isHovered) {
this->button.setFillColor(this->hoverColor);
updateColour(this->hoverColor);
} else {
this->isPressed = false;
this->button.setFillColor(this->idleColor);
updateColour(this->idleColor);
}
}

void TextButton::updateColour(sf::Color color) {
if (this->color == color)
return;

this->button.setFillColor(color);
}

void TextButton::draw(sf::RenderWindow &window) {
window.draw(this->button);
window.draw(this->text);
Expand Down
3 changes: 3 additions & 0 deletions src/sfml/ui/TextButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TextButton {
sf::Vector2f originalPosition;
sf::Text text;
sf::Font* font;
sf::Color color;
sf::Color idleColor;
sf::Color hoverColor;
sf::Color activeColor;
Expand All @@ -34,6 +35,8 @@ class TextButton {
bool isClicked();

bool isJustClicked();

void updateColour(sf::Color color);
};


Expand Down

0 comments on commit 9bd4afb

Please sign in to comment.