Skip to content

Commit

Permalink
Implemented Audio
Browse files Browse the repository at this point in the history
  • Loading branch information
CosminPerRam committed Feb 27, 2022
1 parent ae8a1b3 commit cfd2e71
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 70 deletions.
8 changes: 7 additions & 1 deletion Blockodu.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)\libraries\SFML-2.5.1\include\;$(SolutionDir)\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp14</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)\libraries\SFML-2.5.1\lib\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>winmm.lib;sfml-system-s-d.lib;opengl32.lib;gdi32.lib;sfml-window-s-d.lib;freetype.lib;sfml-graphics-s-d.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>winmm.lib;sfml-system-s-d.lib;ws2_32.lib;sfml-network-s-d.lib;openal32.lib;flac.lib;vorbisenc.lib;vorbisfile.lib;vorbis.lib;ogg.lib;sfml-audio-s-d.lib;opengl32.lib;gdi32.lib;sfml-window-s-d.lib;freetype.lib;sfml-graphics-s-d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -78,6 +80,8 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<FloatingPointModel>Fast</FloatingPointModel>
<LanguageStandard>stdcpp14</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -89,6 +93,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="include\audio.cpp" />
<ClCompile Include="src\block.cpp" />
<ClCompile Include="src\game.cpp" />
<ClCompile Include="src\main.cpp" />
Expand All @@ -108,6 +113,7 @@
<ClInclude Include="include\impl.h" />
<ClInclude Include="include\pickupBoard.h" />
<ClInclude Include="include\score.h" />
<ClInclude Include="include\audio.h" />
<ClInclude Include="include\spacing.h" />
<ClInclude Include="include\table.h" />
<ClInclude Include="include\utilities.h" />
Expand Down
6 changes: 6 additions & 0 deletions Blockodu.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClCompile Include="src\score.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="include\audio.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include=".gitignore" />
Expand Down Expand Up @@ -69,5 +72,8 @@
<ClInclude Include="include\score.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\audio.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
36 changes: 36 additions & 0 deletions include/audio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include "audio.h"

#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML/Audio/Sound.hpp>

bool Audio::initialized = false;
sf::SoundBuffer Audio::goodPlacement, Audio::badPlacement, Audio::completetion;
sf::Sound Audio::playingSound;

void Audio::initialize() {
goodPlacement.loadFromFile("resources/goodPlacement.ogg");
badPlacement.loadFromFile("resources/badPlacement.ogg");
completetion.loadFromFile("resources/completetion.ogg");

initialized = true;
}

void Audio::play(effect theEffect) {
if (!initialized)
return;

switch (theEffect) {
case effect::BadPlacement:
playingSound.setBuffer(badPlacement);
break;
case effect::GoodPlacement:
playingSound.setBuffer(goodPlacement);
break;
case effect::Completetion:
playingSound.setBuffer(completetion);
break;
}

playingSound.play();
}
19 changes: 19 additions & 0 deletions include/audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML/Audio/Sound.hpp>

class Audio
{
private:
static bool initialized;
static sf::SoundBuffer goodPlacement, badPlacement, completetion;
static sf::Sound playingSound;

public:
enum class effect { GoodPlacement, BadPlacement, Completetion };

static void initialize();

static void play(effect theEffect);
};
4 changes: 2 additions & 2 deletions include/block.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <vector>

#include "impl.h"

#include <vector>

class Block : Drawable
{
private:
Expand Down
8 changes: 5 additions & 3 deletions include/game.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>

#include "table.h"
#include "pickupBoard.h"
#include "score.h"

#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>

class Game
{
private:
Expand All @@ -18,5 +18,7 @@ class Game
void pollEvent(sf::RenderWindow& window, sf::Event& theEvent);

public:
Game();

void start();
};
2 changes: 1 addition & 1 deletion include/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Table : Drawable

sf::Vector2i mousePositionToCellPosition(const sf::Vector2f& mousePosition);

void verifyCompletetion();
bool checkCompletetion();

public:
Table(Score& theScore);
Expand Down
Binary file added resources/badPlacement.ogg
Binary file not shown.
Binary file added resources/completetion.ogg
Binary file not shown.
Binary file added resources/goodPlacement.ogg
Binary file not shown.
5 changes: 2 additions & 3 deletions src/block.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

#include "block.h"

#include <SFML/Graphics/RectangleShape.hpp>

#include "spacing.h"
#include "colors.h"
#include "utilities.h"

#include <SFML/Graphics/RectangleShape.hpp>

const sf::Vector2u Block::getStructureSize() {
return this->structureSize;
}
Expand Down
6 changes: 5 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

#include "game.h"

#include "spacing.h"
#include "audio.h"

#include <SFML/Graphics/RenderWindow.hpp>

Game::Game() {
Audio::initialize();
}

void Game::draw(sf::RenderWindow& window) {
theScore.draw(window);

Expand Down
4 changes: 3 additions & 1 deletion src/pickupBoard.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "pickupBoard.h"

#include "colors.h"
#include "audio.h"

#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Window/Event.hpp>
Expand Down Expand Up @@ -128,6 +128,8 @@ void PickupBoard::pollEvent(sf::RenderWindow& window, sf::Event& theEvent)

pickedUpPreviewCoords = { -1, -1 };
}
else
Audio::play(Audio::effect::BadPlacement);

pickedUpIndex = -1;
}
Expand Down
1 change: 0 additions & 1 deletion src/score.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

#include "score.h"

#include "spacing.h"
#include "colors.h"
#include "utilities.h"
Expand Down
71 changes: 14 additions & 57 deletions src/table.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

#include "table.h"
#include "colors.h"
#include "spacing.h"
#include "audio.h"

#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Rect.hpp>

#include "colors.h"
#include "spacing.h"

Table::Table(Score& theScore) : theScore(theScore)
{

Expand All @@ -28,7 +28,7 @@ sf::Vector2i Table::mousePositionToCellPosition(const sf::Vector2f& mousePositio
return { -1, -1 };
}

void Table::verifyCompletetion() {
bool Table::checkCompletetion() {
std::vector<sf::Vector2u> completedBoxes;
std::vector<unsigned> completedHorizontalLines;
std::vector<unsigned> completedVerticalLines;
Expand Down Expand Up @@ -102,6 +102,14 @@ void Table::verifyCompletetion() {

theScore.addCompletionLine();
}

if (!completedBoxes.empty() || !completedVerticalLines.empty() || !completedHorizontalLines.empty())
{
Audio::play(Audio::effect::Completetion);
return true;
}

return false;
}

void Table::applyBlock(Block& theBlock, const sf::Vector2i& tableCellCoords)
Expand All @@ -117,7 +125,8 @@ void Table::applyBlock(Block& theBlock, const sf::Vector2i& tableCellCoords)
}
}

verifyCompletetion();
if (!checkCompletetion())
Audio::play(Audio::effect::GoodPlacement);
}

sf::Vector2i Table::previewBlock(Block& theHoldingBlock, const sf::Vector2f& mousePosition)
Expand Down Expand Up @@ -265,55 +274,3 @@ void Table::draw(sf::RenderWindow& window)

//DRAW GRID END
}

/*
//blocks
for (unsigned i = 0; i < 3; i++) {
for (unsigned j = 0; j < 3; j++) {
bool isBlockFull = true;
for (unsigned m = 0; m < 3; m++) {
for (unsigned n = 0; n < 3; n++) {
if (cellTable[i * 3 + m][j * 3 + n] == cell::empty)
isBlockFull = false;
}
}
if (isBlockFull) {
for (unsigned m = 0; m < 3; m++) {
for (unsigned n = 0; n < 3; n++)
cellTable[i * 3 + m][j * 3 + n] = cell::empty;
}
theScore.addCompletionSquare();
}
}
}
//lines
for (unsigned i = 0; i < 9; i++) {
for (unsigned j = 0; j < 9; j++) {
bool isVerticalLineFull = true, isHorizontalLineFull = true;
for (unsigned m = 0; m < 9; m++) {
if (cellTable[i][m] == cell::empty)
isHorizontalLineFull = false;
if (cellTable[m][j] == cell::empty)
isVerticalLineFull = false;
}
if (isVerticalLineFull) {
for (unsigned m = 0; m < 9; m++)
cellTable[m][j] = cell::empty;
theScore.addCompletionLine();
}
if (isHorizontalLineFull) {
for (unsigned m = 0; m < 9; m++)
cellTable[i][m] = cell::empty;
theScore.addCompletionLine();
}
}
}
*/

0 comments on commit cfd2e71

Please sign in to comment.