sound support
This commit is contained in:
@@ -14,6 +14,13 @@ Engine::Engine(std::string title, int width, int height)
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
}
|
||||
|
||||
void Engine::initSound()
|
||||
{
|
||||
// Initialize sound
|
||||
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
|
||||
soundInitialized = true;
|
||||
}
|
||||
|
||||
void Engine::clear(SDL_Color color)
|
||||
{
|
||||
// Set color
|
||||
@@ -52,6 +59,12 @@ Engine::~Engine()
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
// If sound is initialized, quit Mix
|
||||
if (soundInitialized)
|
||||
{
|
||||
Mix_Quit();
|
||||
}
|
||||
|
||||
// Quit SDL and TTF
|
||||
SDL_Quit();
|
||||
TTF_Quit();
|
||||
|
@@ -3,6 +3,7 @@
|
||||
|
||||
// Variables
|
||||
SDL_Surface* assets;
|
||||
Music* music;
|
||||
Engine *engine;
|
||||
|
||||
// Functions
|
||||
@@ -20,11 +21,13 @@ void quit()
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
engine = new Engine("Game", 800, 600);
|
||||
engine->initSound();
|
||||
engine->targetFrameRate = 60;
|
||||
|
||||
// Load assets
|
||||
assets = SDL_LoadBMP("example_assets/sprites.bmp");
|
||||
|
||||
music = new Music("example_assets/crystal_zone.mp3");
|
||||
|
||||
// frame map
|
||||
std::map<std::string, SDL_Rect> idle = {
|
||||
{ "idle", { 76, 456, 68, 128 } },
|
||||
@@ -58,6 +61,9 @@ int main(int argc, char* argv[])
|
||||
animations->addAnimation("walk_front", front, 10, false, true, true, true);
|
||||
animations->setAnimation("idle");
|
||||
|
||||
// Play music
|
||||
music->play();
|
||||
|
||||
// Main loop
|
||||
while (true)
|
||||
{
|
||||
@@ -68,32 +74,30 @@ int main(int argc, char* argv[])
|
||||
engine->update();
|
||||
|
||||
// Exit if window is closed
|
||||
if (engine->input.exit)
|
||||
if (engine->input.exit || engine->input.activeKeys[SDLK_ESCAPE])
|
||||
{
|
||||
quit();
|
||||
}
|
||||
|
||||
if (engine->input.activeKeys[SDLK_d])
|
||||
{
|
||||
sprite->dstrect.x += 1;
|
||||
animations->setAnimation("walk_right");
|
||||
sprite->move({ 1, 0 });
|
||||
}
|
||||
if (engine->input.activeKeys[SDLK_a])
|
||||
{
|
||||
sprite->dstrect.x -= 1;
|
||||
animations->setAnimation("walk_left");
|
||||
|
||||
sprite->move({ -1, 0 });
|
||||
}
|
||||
if (engine->input.activeKeys[SDLK_w])
|
||||
{
|
||||
sprite->dstrect.y -= 1;
|
||||
animations->setAnimation("walk_back");
|
||||
sprite->move({ 0, -1 });
|
||||
}
|
||||
if (engine->input.activeKeys[SDLK_s])
|
||||
{
|
||||
sprite->dstrect.y += 1;
|
||||
animations->setAnimation("walk_front");
|
||||
|
||||
sprite->move({ 0, 1 });
|
||||
}
|
||||
if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s])
|
||||
{
|
||||
|
56
src/sound.cpp
Normal file
56
src/sound.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "sound.h"
|
||||
|
||||
Music::Music(const char* path)
|
||||
{
|
||||
music = Mix_LoadMUS(path);
|
||||
}
|
||||
|
||||
void Music::play(int loops)
|
||||
{
|
||||
Mix_PlayMusic(music, loops);
|
||||
}
|
||||
|
||||
void Music::pause()
|
||||
{
|
||||
Mix_PauseMusic();
|
||||
}
|
||||
|
||||
void Music::stop()
|
||||
{
|
||||
Mix_HaltMusic();
|
||||
}
|
||||
|
||||
void Music::resume()
|
||||
{
|
||||
Mix_ResumeMusic();
|
||||
}
|
||||
|
||||
void Music::setVolume(int volume)
|
||||
{
|
||||
Mix_VolumeMusic(volume);
|
||||
}
|
||||
|
||||
Music::~Music()
|
||||
{
|
||||
Mix_FreeMusic(music);
|
||||
}
|
||||
|
||||
Effect::Effect(const char* path)
|
||||
{
|
||||
effect = Mix_LoadWAV(path);
|
||||
}
|
||||
|
||||
void Effect::play(int loops, int channel)
|
||||
{
|
||||
Mix_PlayChannel(channel, effect, loops);
|
||||
}
|
||||
|
||||
void Effect::setVolume(int volume)
|
||||
{
|
||||
Mix_VolumeChunk(effect, volume);
|
||||
}
|
||||
|
||||
Effect::~Effect()
|
||||
{
|
||||
Mix_FreeChunk(effect);
|
||||
}
|
@@ -33,9 +33,16 @@ void Sprite::addFrame(std::string name, SDL_Rect rect)
|
||||
frames[name] = rect;
|
||||
}
|
||||
|
||||
void Sprite::setPos(int_vec2 position)
|
||||
{
|
||||
dstrect.x = position.x;
|
||||
dstrect.y = position.y;
|
||||
}
|
||||
|
||||
void Sprite::move(int_vec2 position)
|
||||
{
|
||||
dstrect = { position.x, position.y, dstrect.w, dstrect.h };
|
||||
dstrect.x += position.x;
|
||||
dstrect.y += position.y;
|
||||
}
|
||||
|
||||
int_vec2 Sprite::getSize(std::string frame)
|
||||
|
Reference in New Issue
Block a user