diff --git a/Chip 8/Chip 8.vcxproj b/Chip 8/Chip 8.vcxproj index 9363020..211439d 100644 --- a/Chip 8/Chip 8.vcxproj +++ b/Chip 8/Chip 8.vcxproj @@ -70,8 +70,8 @@ - C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2_image-2.0.5\include;C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2-2.0.20\include;$(IncludePath) - C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2_image-2.0.5\lib\x64;C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2-2.0.20\lib\x64;$(LibraryPath) + C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2_mixer-2.0.4\include;C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2_image-2.0.5\include;C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2-2.0.20\include;$(IncludePath) + C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2_mixer-2.0.4\lib\x64;C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2_image-2.0.5\lib\x64;C:\Users\juanr\OneDrive - Universidad Politécnica de Madrid\Documents\Visual Studio 2019\SDL2-2.0.20\lib\x64;$(LibraryPath) @@ -93,7 +93,7 @@ Console - SDL2.lib;SDL2main.lib;SDL2_image.lib;%(AdditionalDependencies) + SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_mixer.lib;%(AdditionalDependencies) diff --git a/Chip 8/Chip8.cpp b/Chip 8/Chip8.cpp index 8d4ebc7..b09d9bc 100644 --- a/Chip 8/Chip8.cpp +++ b/Chip 8/Chip8.cpp @@ -386,7 +386,9 @@ void Chip8::emulateCycle() if (sound_timer > 0) { if (sound_timer != 0) - std::cout << "BEEP!\n"; // TODO: add sound support + playSound++; sound_timer--; } + else + playSound = 0; } diff --git a/Chip 8/Chip8.h b/Chip 8/Chip8.h index 01222db..0d99abc 100644 --- a/Chip 8/Chip8.h +++ b/Chip 8/Chip8.h @@ -15,6 +15,8 @@ class Chip8 public: bool drawFlag = false; // Flag to see if it's needed to draw on the screen + int playSound = 0; + unsigned short opcode; // Operation Code -- 2 bytes /* diff --git a/Chip 8/main.cpp b/Chip 8/main.cpp index d4f5190..803bd3d 100644 --- a/Chip 8/main.cpp +++ b/Chip 8/main.cpp @@ -1,5 +1,6 @@ //Using SDL, SDL_image, standard IO, math, and strings #include +#include #include #include #include @@ -53,6 +54,13 @@ int main(int argc, char* args[]) SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); SDL_RenderClear(renderer); + //Load music + Mix_Chunk* sinWave = Mix_LoadWAV("../assets/sine_wave_440.wav"); + if (sinWave == NULL) + { + std::cout << "Failed to load beep sound! SDL_mixer Error: " << Mix_GetError() << std::endl; + } + if (chip8.loadProgram("../roms/PONG")) // TODO: better rom selection { //While application is running @@ -95,6 +103,15 @@ int main(int argc, char* args[]) } //Update screen SDL_RenderPresent(renderer); + + // Play sound + for (size_t i = 0; i < chip8.playSound; i++) + { + if (sinWave == NULL) + std::cout << "BEEP!\n"; // Print to console if no sound loaded + else + Mix_PlayChannel(-1, sinWave, 0); + } } } } @@ -118,28 +135,36 @@ bool init(SDL_Window** window, SDL_Renderer** renderer) } else { - //Set texture filtering to linear - if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) - { - printf("Warning: Linear texture filtering not enabled!"); - } - - //Create window - *window = SDL_CreateWindow("CHIP-8 emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); - if (*window == NULL) + //Initialize SDL_mixer + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { - printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); + printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError()); success = false; } - else - { - //Create renderer for window - *renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - if (renderer == NULL) + else { + //Set texture filtering to linear + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Warning: Linear texture filtering not enabled!"); + } + + //Create window + *window = SDL_CreateWindow("CHIP-8 emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (*window == NULL) { - printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); + printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); success = false; } + else + { + //Create renderer for window + *renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (renderer == NULL) + { + printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); + success = false; + } + } } } diff --git a/assets/sine_wave_440.wav b/assets/sine_wave_440.wav new file mode 100644 index 0000000..510436b Binary files /dev/null and b/assets/sine_wave_440.wav differ