Skip to content

Commit

Permalink
Implement audio support in SDL
Browse files Browse the repository at this point in the history
  • Loading branch information
moto999999 committed Feb 23, 2022
1 parent cb288d3 commit 6df3799
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Chip 8/Chip 8.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>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)</IncludePath>
<LibraryPath>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)</LibraryPath>
<IncludePath>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)</IncludePath>
<LibraryPath>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)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand All @@ -93,7 +93,7 @@
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_mixer.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand Down
4 changes: 3 additions & 1 deletion Chip 8/Chip8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions Chip 8/Chip8.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down
57 changes: 41 additions & 16 deletions Chip 8/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//Using SDL, SDL_image, standard IO, math, and strings
#include <SDL.h>
#include <SDL_mixer.h>
#include <stdio.h>
#include <string>
#include <cmath>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
}
Expand All @@ -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;
}
}
}
}

Expand Down
Binary file added assets/sine_wave_440.wav
Binary file not shown.

0 comments on commit 6df3799

Please sign in to comment.