Skip to content

Commit

Permalink
sound effect class created
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinmayMittal committed Apr 12, 2022
1 parent c7d15fb commit f7a2941
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 13 deletions.
Empty file added Entity.cpp
Empty file.
Empty file added Entity.h
Empty file.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
CXX = g++

#OBJS specifies which files to compile as part of the project
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp Text.cpp Tile.cpp TileAtlas.cpp Timer.cpp
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp Text.cpp Tile.cpp TileAtlas.cpp Timer.cpp SoundEffect.cpp

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -g

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf # -lSDL2_mixer
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = experiment.out
Expand Down
22 changes: 14 additions & 8 deletions MyWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <vector>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -75,17 +76,13 @@ void LWindow::begin()
}

handleEvent(e);
// Handle input for the dot
// players[0].handleEvent(e);

if (mCurrScreen != NULL)
{
mCurrScreen->handleEvent(e);
}
}
// camera = {camera.x, camera.y, window.getWidth(), window.getHeight()};
// // Move the dot
// players[0].move();
// players[0].setCamera(camera);

if (mCurrScreen != NULL)
{
mCurrScreen->update();
Expand All @@ -94,7 +91,7 @@ void LWindow::begin()
{
SDL_SetRenderDrawColor(mRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(mRenderer);
// window.render(renderables, camera);

if (mCurrScreen != NULL)
{
mCurrScreen->render(mRenderer);
Expand All @@ -111,6 +108,7 @@ void LWindow::begin()

mCurrScreen->cleanUp();
cleanUp();
Mix_Quit();
IMG_Quit();
SDL_Quit();
}
Expand Down Expand Up @@ -207,7 +205,7 @@ bool LWindow::initLibs()
// Initialization flag

// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return false;
Expand All @@ -233,6 +231,14 @@ bool LWindow::initLibs()
return false;
}

//Initialize SDL_mixer
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) < 0 )
{
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
return false ;
}
Mix_AllocateChannels(32);

return true;
}

Expand Down
12 changes: 10 additions & 2 deletions Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include "MyTexture.h"
#include "Game.h"
#include <iostream>
#include<string>

Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWidth, int right, int left, int top, int bottom, int setVelocity) : mTexture(myTexture), mGame(game)
Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWidth, int right, int left, int top, int bottom, int setVelocity) : mTexture(myTexture), mGame(game) , wallCollisionMusic(std::string("collision.wav") )
{
// Initialize the collision box
mBox.x = 0;
Expand All @@ -17,7 +18,12 @@ Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWi
// Initialize the velocity
mVelX = 0;
mVelY = 0;

// std::cout << "here1\n" ;
// std::cout << "here2\n" ;
// wallCollisionMusic.play() ;
// SDL_Delay(1000) ;
// wallCollisionMusic.play() ;
// SDL_Delay(2000) ;
this->DOT_VEL = setVelocity;
this->direction = 'D';
this->playerHeight = playerHeight;
Expand Down Expand Up @@ -156,6 +162,7 @@ void Player::move()
{
// move back
mBox.x -= mVelX;
// wallCollisionMusic.play() ;
}

// Move the dot up or down
Expand All @@ -166,6 +173,7 @@ void Player::move()
{
// move back
mBox.y -= mVelY;
// wallCollisionMusic.play() ;
}


Expand Down
3 changes: 2 additions & 1 deletion Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "MyTexture.h"
#include "Renderable.h"
#include "MyWindow.h"
#include"SoundEffect.h"
#include <string>

class LGame;
Expand Down Expand Up @@ -49,7 +50,7 @@ class Player : public Renderable
LGame &mGame;
SDL_Rect mBox;
LTexture &mTexture;

SoundEffect wallCollisionMusic ;
// bool touchesWall(std::vector<Tile> tiles);

// The velocity of the dot
Expand Down
75 changes: 75 additions & 0 deletions SoundEffect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include"SoundEffect.h"
#include<SDL2/SDL.h>
#include<SDL2/SDL_mixer.h>
#include<string>
#include<iostream>
#include<memory>

SoundEffect::SoundEffect ()
{
// initialize variables
mMusic = NULL ;
}

SoundEffect :: SoundEffect( std :: string filename )
{
// initialize variables
mMusic = NULL ;
loadMusic( filename ) ;

}

SoundEffect::~SoundEffect()
{
// Deallocate
free();
}

// SoundEffect::SoundEffect(const std::string &path, int volume)
// : chunk(Mix_LoadWAV(path.c_str()), Mix_FreeChunk) {
// if (!chunk.get()) {
// // LOG("Couldn't load audio sample: ", path);
// }

// Mix_VolumeChunk(chunk.get(), volume);
// }

bool SoundEffect::loadMusic( std :: string filename )
{
// get rid of pre-existing music
free() ;
std::cout << "loading music...\n" ;
mMusic = Mix_LoadWAV( filename.c_str() ) ;
if( mMusic == NULL ) {
printf( "Failed to load music!\n" );
std::cout << Mix_GetError() << "\n" ;
return false ;
}
std::cout << "loaded music...\n" ;
return true ;

}

void SoundEffect::free()
{
// Free music if it exists
if (mMusic != NULL)
{
//Free the sound effects
Mix_FreeChunk( mMusic ) ;
mMusic = NULL ;

}
}

void SoundEffect::play()
{
int channel = Mix_PlayChannel( -1, mMusic , 0 ) ;
if( channel == -1 ){
printf("Mix_PlayChannel: %s\n",Mix_GetError());
}else{
std::cout << "playing on channel " << channel << "\n" ;
}
SDL_Delay(1000) ;
// Mix_PlayChannel(-1, chunk.get(), 0);
}
20 changes: 20 additions & 0 deletions SoundEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<SDL2/SDL.h>
#include<SDL2/SDL_mixer.h>
#include<string>
#include<memory>

class SoundEffect
{
public:
SoundEffect() ;
SoundEffect( std :: string filename ) ;
// SoundEffect( const std::string &path, int volume ) ;
~SoundEffect() ;
bool loadMusic( std :: string filename ) ;
void free();
void play() ;
private:
Mix_Chunk *mMusic ;
// std::unique_ptr<Mix_Chunk, void (*)(Mix_Chunk *)> chunk;

} ;
Binary file added collision.wav
Binary file not shown.

0 comments on commit f7a2941

Please sign in to comment.