diff --git a/Chip8 Emulator.vcxproj b/Chip8 Emulator.vcxproj index 2fecf82..3d1809a 100644 --- a/Chip8 Emulator.vcxproj +++ b/Chip8 Emulator.vcxproj @@ -93,7 +93,7 @@ Console true $(SolutionDir)lib\debug\SFML;%(AdditionalLibraryDirectories) - sfml-window-s-d.lib;sfml-system-s-d.lib;sfml-graphics-s-d.lib;opengl32.lib;winmm.lib;gdi32.lib;freetype.lib;%(AdditionalDependencies) + sfml-audio-s-d.lib;sfml-window-s-d.lib;sfml-system-s-d.lib;sfml-graphics-s-d.lib;opengl32.lib;winmm.lib;gdi32.lib;freetype.lib;openal32.lib;flac.lib;vorbisenc.lib;vorbisfile.lib;vorbis.lib;ogg.lib;legacy_stdio_definitions.lib;%(AdditionalDependencies) LinkVerbose @@ -126,7 +126,7 @@ true true $(SolutionDir)lib\release\SFML;%(AdditionalLibraryDirectories) - sfml-graphics-s.lib;sfml-system-s.lib;sfml-window-s.lib;opengl32.lib;winmm.lib;gdi32.lib;%(AdditionalDependencies) + sfml-audio-s.lib;sfml-graphics-s.lib;sfml-system-s.lib;sfml-window-s.lib;opengl32.lib;winmm.lib;gdi32.lib;openal32.lib;flac.lib;vorbisenc.lib;vorbisfile.lib;vorbis.lib;ogg.lib;legacy_stdio_definitions.lib;%(AdditionalDependencies) diff --git a/include/Chip8.hpp b/include/Chip8.hpp index dfd4bcc..deb0b3a 100644 --- a/include/Chip8.hpp +++ b/include/Chip8.hpp @@ -38,6 +38,7 @@ class Chip8 /////////////////////// bool drawFlag; //Determines if something was written to the display buffer and needs to be pushed. + bool playSound; //Determines if the beep sound is ready to play. char unsigned gfx[64][32]{0,0}; //Screen buffer, 2048 pixels total. Switched to a 2D array, last buffer used 1D array with a 64 multiple offset for the y value. char unsigned key[16]{0}; //Key buffer that stores the state of a pressed key; the value does not matter. Anything other than 0 indicates a pressed state. @@ -73,6 +74,9 @@ class Chip8 //Checks to make sure the opcode being generated is a legal instruction. bool isValidOp(const char &nibble); + //Checks to see if a sound needs to be played. If so, returns true. + bool isSoundReady(); + //Function responsible for returning the offset value based on the child opcode function requested by the loNibble. enum childFuncOffset getOffset(); diff --git a/main.cpp b/main.cpp index b54f020..49667a1 100644 --- a/main.cpp +++ b/main.cpp @@ -4,9 +4,20 @@ #include "SFML\Graphics.hpp" #include "SFML\Window\Keyboard.hpp" +#include "SFML\Audio.hpp" #include "Chip8.hpp" +//Chunk of code responsible for resolving "unresolved external symbol __iob_func linker" errors. +//Had to include legacy_stdio_definitions.lib as well. Will debug this later. +#define stdin (__acrt_iob_func(0)) +#define stdout (__acrt_iob_func(1)) +#define stderr (__acrt_iob_func(2)) + +FILE _iob[] = { *stdin, *stdout, *stderr }; +extern "C" FILE * __cdecl __iob_func(void) { return _iob; } +// + //MACROS// #define SCALE_FACTOR 12.5f //ratio between resolutions 64x32 and 800x600 @@ -16,7 +27,9 @@ //OBJECT INITIALIZATION// Chip8 chip8; //Creates emulator object and initializes class state using constructor -sf::Event event; +sf::Event event; //Creates event object to contain event types necessary for interactivity. +sf::SoundBuffer beepBuffer; //Creates sound buffer to hold beep.wav file. +sf::Sound beepSound; //Creates sound object to control buffer playback. sf::RenderWindow mainWindow(sf::VideoMode(800, 600), "Chip 8 Emulator"); //Create and declare Window object for rendering. sf::RectangleShape chip8SpriteRect(sf::Vector2f(SCALE_FACTOR, SCALE_FACTOR)); //Create RectangleShape object with a size of 12.5f, which is also the scale factor. @@ -172,10 +185,10 @@ void pushBuffer() //Fills the SFML window buffer with the gfx buffer from the ch } } -void emulationLoop() +void emulationLoop() //Main emulation loop responsible for running cycles, checking events, playing sounds, etc. { mainWindow.setFramerateLimit(60); - + while (mainWindow.isOpen()) { while (mainWindow.pollEvent(event)) @@ -212,6 +225,12 @@ void emulationLoop() chip8.drawFlag = false; } + + if (chip8.playSound) + { + beepSound.play(); + chip8.playSound = false; + } } } @@ -221,6 +240,14 @@ int main(int argc, char* argv[]) { if (chip8.loadROM()) { + if (!beepBuffer.loadFromFile("beep.wav")) + { + std::cerr << "Error loading sound file. Please check that the file name is beep.wav and is located next to the executable. Continuing execution..." << std::endl; + } + else + { + beepSound.setBuffer(beepBuffer); + } emulationLoop(); } else diff --git a/src/Chip8.cpp b/src/Chip8.cpp index 5c69fad..37b2480 100644 --- a/src/Chip8.cpp +++ b/src/Chip8.cpp @@ -91,6 +91,7 @@ Chip8::Chip8() for (int x = 0; x < 16; x++) V[x] = 0; + playSound = false; drawFlag = true; } @@ -189,12 +190,27 @@ void Chip8::emulateCycle() --delayTimer; if (soundTimer > 0) { - if (soundTimer == 1) - std::cout << "BEEP MOFO" << std::endl; + if (isSoundReady()) + { + playSound = true; + } --soundTimer; } } +//Checks to see if a sound needs to be played. If so, returns true. +bool Chip8::isSoundReady() +{ + if (soundTimer == 1) + { + return true; + } + else + { + return false; + } +} + //Find the initial opcode function using the hiNibble. If isValidOP returns true, the nibble is passed to parentFuncTable to initiate the function. If false, it hands off the program to the catch-all function XXXX. void Chip8::findFunc() { @@ -507,7 +523,6 @@ void Chip8::ADDXY() pc += 2; } - //8xy5: Compares VX to VY. If larger, set VF flag to 1, else set to 0, then subtract VY from VX, store the results in VX. void Chip8::SUBXY() {