Got sound working
This commit is contained in:
parent
26b0fda620
commit
668bc35226
6
Makefile
6
Makefile
|
@ -16,7 +16,7 @@ test: tests.o logger.o renderwindow.o values.o timestep.o utils.o math.o
|
|||
run_tests: test
|
||||
./build/test
|
||||
|
||||
pong: pong_main.o logger.o renderwindow.o timestep.o entity.o utils.o
|
||||
pong: pong_main.o logger.o renderwindow.o timestep.o entity.o utils.o audio.o
|
||||
mkdir -p build
|
||||
rsync -av ./games/Ping-Pong/res ./build/
|
||||
$(CC) $^ $(SDL_FLAGS) $(WarningFlags) -o $(outputDir)/pong
|
||||
|
@ -24,6 +24,10 @@ pong: pong_main.o logger.o renderwindow.o timestep.o entity.o utils.o
|
|||
pong_main.o: $(PONG_SRC)/pong_entry.cpp
|
||||
$(CC) -c $(INCLUDES) $(SDL_FLAGS) $(WarningFlags) $^ -o pong_main.o
|
||||
|
||||
|
||||
audio.o: $(SRCDIR)/audio.cpp
|
||||
$(CC) -c $(INCLUDES) $^ -o audio.o
|
||||
|
||||
entity.o: $(SRCDIR)/entity.cpp
|
||||
$(CC) -c $(INCLUDES) $^ -o entity.o
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,5 +1,6 @@
|
|||
#include <iostream>
|
||||
#include <SDL2/SDL.h>
|
||||
#include "Audio.hpp"
|
||||
#include "Entity.hpp"
|
||||
#include "Logger.hpp"
|
||||
#include "Values.hpp"
|
||||
|
@ -21,13 +22,26 @@ bool GameRunning = true;
|
|||
const Vector2f baseBallVector = { 6, 6 };
|
||||
PlayerType lastCollider = PlayerType::NoOne;
|
||||
Side lastSide = Side::None;
|
||||
TTF_Font* scoreFont;
|
||||
|
||||
Birb2D::Entity UpdatedScore(int score, Birb2D::Entity scoreEntity)
|
||||
{
|
||||
if (scoreEntity.textComponent.text != std::to_string(score))
|
||||
{
|
||||
Birb2D::TextComponent textComponent(std::to_string(score), scoreFont, &Colors::White);
|
||||
Birb2D::Entity newScore(scoreEntity.name, Vector2int(scoreEntity.rect.x, scoreEntity.rect.y), textComponent);
|
||||
newScore.textComponent = textComponent;
|
||||
return newScore;
|
||||
}
|
||||
return scoreEntity;
|
||||
}
|
||||
|
||||
void MirrorBallVector(Vector2f* ballVector, Side side, Side playerMovementDirection)
|
||||
{
|
||||
float movementMultiplier = 1.00f;
|
||||
if (playerMovementDirection != Side::None && side == Left && side != Top && side != Bottom)
|
||||
{
|
||||
movementMultiplier = 1.10f;
|
||||
movementMultiplier = utils::randomFloat(1.10f, 1.30f);
|
||||
|
||||
/* Change the ball movement direction depending on the player movement */
|
||||
ballVector->x *= -1;
|
||||
|
@ -171,7 +185,7 @@ int main(int argc, char **argv)
|
|||
/* Gameloop variables */
|
||||
SDL_Event event;
|
||||
bool holdingKey = false;
|
||||
TTF_Font* manaspaceFont = Birb2D::Resources::LoadFont(workdir + "/res/fonts/manaspace/manaspc.ttf", 16);
|
||||
scoreFont = Birb2D::Resources::LoadFont(workdir + "/res/fonts/manaspace/manaspc.ttf", 64);
|
||||
|
||||
/* Ball variables */
|
||||
Vector2f ballVector = { 6, 6 };
|
||||
|
@ -185,10 +199,22 @@ int main(int argc, char **argv)
|
|||
Rect playerDimensions = { (float)playerSideOffset, window.window_dimensions.y / 2.00f - 50, 10, 100 };
|
||||
Side playerMovementDirection = Side::None;
|
||||
|
||||
int botMovementSpeed = 4;
|
||||
int botMovementSpeed = 5;
|
||||
Rect botDimensions = playerDimensions;
|
||||
botDimensions.x = window.window_dimensions.x - playerDimensions.x - playerDimensions.w;
|
||||
|
||||
/* Score variables */
|
||||
int playerScore = 0;
|
||||
int botScore = 0;
|
||||
Birb2D::Entity e_playerScore("Player score", Vector2int(window.window_dimensions.x / 2 - 150, 32), Birb2D::TextComponent("0", scoreFont, &Colors::White));
|
||||
Birb2D::Entity e_botScore("Bot score", Vector2int(window.window_dimensions.x / 2 + 150 - 64, 32), Birb2D::TextComponent("0", scoreFont, &Colors::White));
|
||||
|
||||
/* Sounds */
|
||||
Birb2D::Audio::Init(MIX_INIT_MP3);
|
||||
Birb2D::Audio::SoundFile paddle_collision(workdir + "/res/sounds/paddle_collision.wav");
|
||||
Birb2D::Audio::SoundFile player_lose(workdir + "/res/sounds/player_lose.wav");
|
||||
Birb2D::Audio::SoundFile player_point(workdir + "/res/sounds/player_point.wav");
|
||||
|
||||
while (GameRunning)
|
||||
{
|
||||
timeStep.Start();
|
||||
|
@ -202,7 +228,6 @@ int main(int argc, char **argv)
|
|||
/* Player movement */
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
std::cout << event.key.keysym.scancode << std::endl;
|
||||
switch (event.key.keysym.scancode)
|
||||
{
|
||||
/* Up arrow */
|
||||
|
@ -258,6 +283,9 @@ int main(int argc, char **argv)
|
|||
botDimensions.y -= botMovementSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update the horizontal position of the bot in case the window dimensions are changed */
|
||||
botDimensions.x = window.window_dimensions.x - playerDimensions.x - playerDimensions.w;
|
||||
}
|
||||
|
||||
/* Ball movemement and colliders */
|
||||
|
@ -275,6 +303,18 @@ int main(int argc, char **argv)
|
|||
/* Ball hit the side walls. Reset the game */
|
||||
if (hitSide == Side::Left || hitSide == Side::Right)
|
||||
{
|
||||
/* Handle scoring and audio */
|
||||
if (hitSide == Side::Left)
|
||||
{
|
||||
player_lose.play();
|
||||
botScore++;
|
||||
}
|
||||
else
|
||||
{
|
||||
player_point.play();
|
||||
playerScore++;
|
||||
}
|
||||
|
||||
MirrorBallVector(&ballVector, hitSide, playerMovementDirection);
|
||||
ResetBall(&ballPosition, &ballVector, window);
|
||||
}
|
||||
|
@ -282,7 +322,10 @@ int main(int argc, char **argv)
|
|||
/* Check for paddle hits */
|
||||
hitSide = BallPlayerCollision(playerDimensions, botDimensions, ballCollider);
|
||||
if (hitSide != Side::None)
|
||||
{
|
||||
paddle_collision.play();
|
||||
MirrorBallVector(&ballVector, hitSide, playerMovementDirection);
|
||||
}
|
||||
}
|
||||
|
||||
/* Render stuff */
|
||||
|
@ -311,7 +354,11 @@ int main(int argc, char **argv)
|
|||
Vector2int(ballPosition.x, ballPosition.y),
|
||||
ballSize);
|
||||
|
||||
|
||||
/* Draw score */
|
||||
e_playerScore = UpdatedScore(playerScore, e_playerScore);
|
||||
e_botScore = UpdatedScore(botScore, e_botScore);
|
||||
Birb2D::Render::DrawEntity(e_playerScore);
|
||||
Birb2D::Render::DrawEntity(e_botScore);
|
||||
}
|
||||
|
||||
window.Display();
|
||||
|
@ -319,6 +366,12 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
Debug::Log("Starting cleanup...");
|
||||
|
||||
/* Free sound effects */
|
||||
player_lose.free();
|
||||
player_point.free();
|
||||
paddle_collision.free();
|
||||
|
||||
window.Cleanup();
|
||||
SDL_Quit();
|
||||
Debug::Log("Game should be closed now!");
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
|
||||
namespace Birb2D
|
||||
{
|
||||
struct Audio
|
||||
{
|
||||
static bool Init(int flags);
|
||||
|
||||
class SoundFile
|
||||
{
|
||||
public:
|
||||
SoundFile(std::string p_filePath);
|
||||
void play();
|
||||
void free()
|
||||
{
|
||||
Mix_FreeChunk(sound);
|
||||
}
|
||||
bool isPlaying();
|
||||
private:
|
||||
Mix_Chunk* sound;
|
||||
std::string filePath;
|
||||
};
|
||||
|
||||
class MusicFile
|
||||
{
|
||||
public:
|
||||
MusicFile(std::string p_filePath);
|
||||
void play();
|
||||
void play(bool loop);
|
||||
void free()
|
||||
{
|
||||
Mix_FreeMusic(music);
|
||||
}
|
||||
bool isPlaying();
|
||||
private:
|
||||
Mix_Music* music;
|
||||
std::string filePath;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
|
@ -9,6 +9,7 @@ namespace Birb2D
|
|||
{
|
||||
struct TextComponent
|
||||
{
|
||||
TextComponent();
|
||||
TextComponent(std::string p_text, TTF_Font* font, SDL_Color* p_color);
|
||||
std::string text;
|
||||
TTF_Font* font;
|
||||
|
@ -29,7 +30,7 @@ namespace Birb2D
|
|||
Rect rect;
|
||||
Vector2f localScale;
|
||||
|
||||
TextComponent* textComponent;
|
||||
TextComponent textComponent;
|
||||
void LoadSprite();
|
||||
void SetBaseEntityValues();
|
||||
};
|
||||
|
|
105
src/audio.cpp
105
src/audio.cpp
|
@ -0,0 +1,105 @@
|
|||
#include <iostream>
|
||||
#include "Audio.hpp"
|
||||
#include "Logger.hpp"
|
||||
|
||||
namespace Birb2D
|
||||
{
|
||||
//** Initializes the SDL_mixer library. Needs to be called before any other audio features are used
|
||||
bool Audio::Init(int flags)
|
||||
{
|
||||
Debug::Log("Initializing audio...");
|
||||
int initted = Mix_Init(flags);
|
||||
if ((initted&flags) != flags)
|
||||
{
|
||||
// Something went wrong
|
||||
Debug::Log("Error initializing SDL_mixer: " + (std::string)Mix_GetError(), Debug::error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_AUDIO) > 0)
|
||||
{
|
||||
Debug::Log("Error initializing SDL_INIT_AUDIO: " + (std::string)SDL_GetError(), Debug::error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 2048) != 0)
|
||||
{
|
||||
Debug::Log("Error during Mix_OpenAudio: " + (std::string)Mix_GetError(), Debug::error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Audio initialization was successful
|
||||
Debug::Log("Audio initialized successfully!");
|
||||
return true;
|
||||
}
|
||||
|
||||
//** Loads up a music file from given path. Shouldn't be used for short sound effects
|
||||
Audio::MusicFile::MusicFile(std::string p_filePath)
|
||||
:filePath(p_filePath)
|
||||
{
|
||||
music = Mix_LoadMUS(p_filePath.c_str());
|
||||
if (!music)
|
||||
{
|
||||
Debug::Log("Error loading music file [" + p_filePath + "] Error: " + (std::string)Mix_GetError(), Debug::error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//** Loads up a sound file from given path. Used for shorter sound effects and not music
|
||||
Audio::SoundFile::SoundFile(std::string p_filePath)
|
||||
:filePath(p_filePath)
|
||||
{
|
||||
sound = Mix_LoadWAV(p_filePath.c_str());
|
||||
if (!sound)
|
||||
{
|
||||
Debug::Log("Error loading sound file [" + p_filePath + "] Error: " + (std::string)Mix_GetError(), Debug::error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Audio::MusicFile::play(bool loop)
|
||||
{
|
||||
int loopValue;
|
||||
if (loop)
|
||||
loopValue = -1;
|
||||
else
|
||||
loopValue = 0;
|
||||
|
||||
if (Mix_PlayMusic(music, loopValue) == -1)
|
||||
{
|
||||
Debug::Log("Error playing music file [" + filePath + "] Error: " + Mix_GetError(), Debug::error);
|
||||
}
|
||||
}
|
||||
|
||||
void Audio::MusicFile::play()
|
||||
{
|
||||
if (Mix_PlayMusic(music, 0) == -1)
|
||||
{
|
||||
Debug::Log("Error playing music file [" + filePath + "] Error: " + (std::string)Mix_GetError(), Debug::error);
|
||||
}
|
||||
}
|
||||
|
||||
void Audio::SoundFile::play()
|
||||
{
|
||||
if (Mix_PlayChannel(-1, sound, 0) == -1)
|
||||
{
|
||||
Debug::Log("Error playing sound file [" + filePath + "] Error: " + (std::string)Mix_GetError(), Debug::error);
|
||||
}
|
||||
}
|
||||
|
||||
bool Audio::MusicFile::isPlaying()
|
||||
{
|
||||
if (Mix_PlayingMusic())
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Audio::SoundFile::isPlaying()
|
||||
{
|
||||
if (Mix_Playing(-1) > 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,13 @@
|
|||
|
||||
namespace Birb2D
|
||||
{
|
||||
TextComponent::TextComponent()
|
||||
{
|
||||
text = "";
|
||||
font = NULL;
|
||||
color = NULL;
|
||||
}
|
||||
|
||||
TextComponent::TextComponent(std::string p_text, TTF_Font* p_font, SDL_Color* p_color)
|
||||
:text(p_text), font(p_font), color(p_color)
|
||||
{}
|
||||
|
@ -25,17 +32,12 @@ namespace Birb2D
|
|||
{
|
||||
/* Load the text sprite */
|
||||
SetBaseEntityValues();
|
||||
textComponent = &p_textComponent;
|
||||
textComponent = p_textComponent;
|
||||
LoadSprite();
|
||||
|
||||
rect.x = pos.x;
|
||||
rect.y = pos.y;
|
||||
|
||||
/* Get texture scale automatically */
|
||||
int w, h;
|
||||
utils::GetTextureDimensions(sprite, w, h);
|
||||
rect.w = w;
|
||||
rect.h = h;
|
||||
}
|
||||
|
||||
Entity::Entity(std::string p_name, Vector2int pos, SDL_Texture* p_texture)
|
||||
|
@ -55,13 +57,21 @@ namespace Birb2D
|
|||
void Entity::LoadSprite()
|
||||
{
|
||||
/* There's a text component. Let's generate a text sprite for it */
|
||||
if (textComponent != nullptr)
|
||||
if (textComponent.text != "")
|
||||
{
|
||||
Debug::Log("Loading a text sprite '" + name + "'");
|
||||
sprite = Resources::TextSprite(textComponent->text, textComponent->font, *textComponent->color);
|
||||
sprite = Resources::TextSprite(textComponent.text, textComponent.font, *textComponent.color);
|
||||
|
||||
if (sprite == nullptr)
|
||||
Debug::Log("Something went wrong while creating the text sprite for '" + name + "'", Debug::error);
|
||||
else
|
||||
{
|
||||
/* Get texture scale automatically */
|
||||
int w, h;
|
||||
utils::GetTextureDimensions(sprite, w, h);
|
||||
rect.w = w;
|
||||
rect.h = h;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -151,6 +151,13 @@ namespace Birb2D
|
|||
|
||||
SDL_Texture* Resources::TextSprite(std::string text, TTF_Font* font, SDL_Color& color)
|
||||
{
|
||||
/* Check if the arguments are valid */
|
||||
if (font == nullptr)
|
||||
{
|
||||
Debug::Log("Tried to render text with invalid font!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(), color);
|
||||
if (surface == nullptr)
|
||||
Debug::Log("Error creating SDL_Surface. Text: " + (std::string)text + ". SDL Error: " + (std::string)SDL_GetError(), Debug::error);
|
||||
|
|
Loading…
Reference in New Issue