Skip to content

Commit

Permalink
Clean up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 8, 2024
1 parent c3e5462 commit d423cef
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 56 deletions.
4 changes: 4 additions & 0 deletions src/emulator/Cpu.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "Cpu.h"

#include <fstream>
#include <iostream>
#include <random>

Cpu::Cpu(Renderer* renderer, Keyboard* keyboard, Speaker * speaker) {
this->memory.resize(4096);
this->registers.resize(16);
Expand Down
2 changes: 0 additions & 2 deletions src/emulator/Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include <cstdint>
#include <fstream>
#include <iostream>
#include <random>
#include <vector>

#include "Keyboard.h"
Expand Down
5 changes: 2 additions & 3 deletions src/emulator/Keyboard.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H

#include <unordered_map>
#include <vector>
#include <functional>
#include <cstdint>
#include <functional>
#include <unordered_map>

class Keyboard {
public:
Expand Down
1 change: 1 addition & 0 deletions src/emulator/Speaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class Speaker {
public:
virtual ~Speaker() = default;
virtual void play() = 0;
virtual void stop() = 0;
};
Expand Down
9 changes: 5 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <SFML/Graphics.hpp>

#include <filesystem>
#include <iostream>
#include <thread>
Expand All @@ -23,11 +24,11 @@ int main(int argc, char **argv) {
if (command == "--rom") {
if (i + 1 < argc) {
return Emulator().launch(argv[++i]);
} else {
std::cerr << "Please include the path to the file" << std::endl;
return 0;
}
} else if (command == "--help") {
std::cerr << "Please include the path to the file" << std::endl;
return 0;
}
if (command == "--help") {
std::cerr << "Usage: 8chocchip --rom <rompath>" << std::endl;
return 0;
}
Expand Down
2 changes: 0 additions & 2 deletions src/sfml/Emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include <string>

#include "SFML/Graphics.hpp"

class Emulator {
private:
public:
Expand Down
2 changes: 2 additions & 0 deletions src/sfml/InputHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "InputHandler.h"

#include <algorithm>
#include <iostream>

void InputHandler::addKey(sf::Keyboard::Key key) {
Expand Down
1 change: 0 additions & 1 deletion src/sfml/InputHandler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef INC_8CHOCCHIP_INPUTHANDLER_H
#define INC_8CHOCCHIP_INPUTHANDLER_H

#include <algorithm>
#include <vector>

#include "SFML/Window/Keyboard.hpp"
Expand Down
19 changes: 8 additions & 11 deletions src/sfml/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include <filesystem>
#include <iostream>

#include "libconfig.h"
#include "libconfig.hh"
#include "libconfig.h++"
#include "nfd.hpp"

#include "Emulator.h"
Expand All @@ -14,9 +13,8 @@
MainMenu::MainMenu(std::unordered_map<std::string *, std::vector<std::string>>& romFiles,
std::vector<std::string>& romDirectories, std::vector<std::unique_ptr<std::thread>>& windows,
std::string configFilePath) :
romDirectories(romDirectories), romFiles(romFiles), windows(windows),
window(sf::VideoMode(640, 480), "8ChocChip - Chip8 Emulator"),
inputHandler() {
window(sf::VideoMode(640, 480), "8ChocChip - Chip8 Emulator"), windows(windows),
romDirectories(romDirectories), romFiles(romFiles), inputHandler() {

NFD::Guard nfdGuard;

Expand All @@ -37,7 +35,7 @@ MainMenu::MainMenu(std::unordered_map<std::string *, std::vector<std::string>>&
for (auto& thing : romFiles) {
for (std::string& file : thing.second) {

TextButton romButton(0, 25 * roms.size(), this->window.getSize().x, 25, MiscUtil::getFileFromPath(file), &font);
TextButton romButton(0, 25.0f * roms.size(), this->window.getSize().x, 25, MiscUtil::getFileFromPath(file), &font);

roms.emplace(file, romButton);
}
Expand All @@ -50,14 +48,14 @@ MainMenu::MainMenu(std::unordered_map<std::string *, std::vector<std::string>>&

auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds;
std::chrono::duration<double> elapsed_seconds{};

// Variables to count frames
int frames = 0;
double fps;

while (this->window.isOpen()) {
sf::Event event;
sf::Event event{};
sf::Vector2i pos = sf::Mouse::getPosition(this->window);

this->inputHandler.updateLastKeys();
Expand Down Expand Up @@ -110,9 +108,8 @@ MainMenu::MainMenu(std::unordered_map<std::string *, std::vector<std::string>>&
button.update(&this->inputHandler, pos);

if (button.isJustClicked()) {
nfdresult_t result = NFD::PickFolder(outPath);

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

Expand Down Expand Up @@ -140,7 +137,7 @@ MainMenu::MainMenu(std::unordered_map<std::string *, std::vector<std::string>>&
// Add the file path to the romFiles entry
romFiles.find(&romDirectories.back())->second.emplace_back(file.path().string());

TextButton romButton(0, 25 * roms.size(), this->window.getSize().x, 25, file.path().filename().string(), &font);
TextButton romButton(0, 25.0f * roms.size(), this->window.getSize().x, 25, file.path().filename().string(), &font);
roms.emplace(file.path().string(), romButton);
}
config.writeFile(configFilePath);
Expand Down
2 changes: 0 additions & 2 deletions src/sfml/MainMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

#include <thread>
#include <unordered_map>
#include <utility>

#include "SFML/Graphics.hpp"

#include "InputHandler.h"

class MainMenu {
Expand Down
6 changes: 2 additions & 4 deletions src/sfml/emulator/SfmlRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include "SfmlRenderer.h"

SfmlRenderer::SfmlRenderer(sf::RenderWindow* window) {
this->window = window;

SfmlRenderer::SfmlRenderer(sf::RenderWindow* window) : window(window) {
this->scale = 15; // Scale up because 64 x 32 would be tiny on our screens now
}

void SfmlRenderer::drawPixel(uint16_t x, uint16_t y) {
void SfmlRenderer::drawPixel(const uint16_t x, const uint16_t y) {
sf::RectangleShape rectangle(sf::Vector2f(getScale(), getScale()));
rectangle.setPosition(x * getScale(), y * getScale());
rectangle.setFillColor(sf::Color::White);
Expand Down
5 changes: 3 additions & 2 deletions src/sfml/emulator/SfmlSpeaker.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "SfmlSpeaker.h"

#include <cmath>

SfmlSpeaker::SfmlSpeaker() {

this->sound.setVolume(100.f);
this->sound.setLoop(true);

const unsigned int sampleRate = 44100;
const unsigned int frequency = 440;
constexpr unsigned int sampleRate = 44100;
sf::Int16 samples[sampleRate];
for (unsigned int i = 0; i < sampleRate; ++i) {
constexpr unsigned int frequency = 440;
samples[i] = static_cast<sf::Int16>(32767 * std::sin(2 * 3.14159265 * frequency * i / sampleRate)); // Mmmm yum pi
}
this->soundBuffer.loadFromSamples(samples, sampleRate, 1, sampleRate);
Expand Down
3 changes: 2 additions & 1 deletion src/sfml/emulator/SfmlSpeaker.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef INC_8CHOCCHIP_SFMLSPEAKER_H
#define INC_8CHOCCHIP_SFMLSPEAKER_H

#include "../../emulator/Speaker.h"
#include "SFML/Audio.hpp"

#include "../../emulator/Speaker.h"

class SfmlSpeaker : public Speaker {
private:
sf::SoundBuffer soundBuffer;
Expand Down
12 changes: 5 additions & 7 deletions src/sfml/ui/TextButton.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "TextButton.h"

#include "../../util/MiscUtil.h"

TextButton::TextButton(float x, float y, float width, float height, std::string buttonText, sf::Font* font) {
TextButton::TextButton(float x, float y, float width, float height, const std::string& buttonText, sf::Font* font) {
this->button.setSize(sf::Vector2f(width, height));
this->button.setPosition(sf::Vector2f(x, y));

Expand Down Expand Up @@ -49,23 +47,23 @@ void TextButton::update(InputHandler* inputHandler, sf::Vector2i pos) {
}
}

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

this->button.setFillColor(color);
}

void TextButton::draw(sf::RenderWindow &window) {
void TextButton::draw(sf::RenderWindow &window) const {
window.draw(this->button);
window.draw(this->text);
}

bool TextButton::isClicked() {
bool TextButton::isClicked() const {
return this->isPressed;
}

bool TextButton::isJustClicked() {
bool TextButton::isJustClicked() const {
return !this->lastPressed && this->isPressed;
}
14 changes: 7 additions & 7 deletions src/sfml/ui/TextButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ class TextButton {
sf::Vector2f originalSize;
sf::Vector2f originalPosition;
sf::Text text;
sf::Font* font;
sf::Font* font{};
sf::Color color;
sf::Color idleColor;
sf::Color hoverColor;
sf::Color activeColor;
bool isPressed;
bool lastPressed;
bool lastPressed{};
bool isHovered;

public:
TextButton(float x, float y, float width, float height, std::string buttonText, sf::Font* font);
TextButton(float x, float y, float width, float height, const std::string& buttonText, sf::Font *font);

void updateSize(const sf::Vector2u originalSize, const sf::Vector2u updatedSize);
void updateSize(sf::Vector2u originalSize, sf::Vector2u updatedSize);

void update(InputHandler* inputHandler, sf::Vector2i pos);

void draw(sf::RenderWindow& window);
void draw(sf::RenderWindow& window) const;

bool isClicked();
bool isClicked() const;

bool isJustClicked();
bool isJustClicked() const;

void updateColour(sf::Color color);
};
Expand Down
9 changes: 4 additions & 5 deletions src/util/MiscUtil.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include "MiscUtil.h"

#include <algorithm>

std::string MiscUtil::toLowerCase(const std::string_view string) {
std::string result(string); // Create a mutable string from the string_view
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c) { return std::tolower(c); });
std::transform(result.begin(), result.end(), result.begin(),[](const unsigned char c) { return std::tolower(c); });
return result;
}

std::string MiscUtil::getFileFromPath(std::string path) {
size_t lastSlashPos = path.find_last_of('/');

if (lastSlashPos != std::string::npos) {
if (const size_t lastSlashPos = path.find_last_of('/'); lastSlashPos != std::string::npos) {
return path.substr(lastSlashPos + 1);
}

Expand Down
6 changes: 1 addition & 5 deletions src/util/MiscUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@

#include <string>
#include <string_view>
#include <algorithm>

#include "SFML/Window/Keyboard.hpp"
#include "SFML/Window/Mouse.hpp"

class MiscUtil {
public:
static std::string toLowerCase(const std::string_view string);
static std::string toLowerCase(std::string_view string);

static std::string getFileFromPath(std::string path);
};
Expand Down

0 comments on commit d423cef

Please sign in to comment.