sound support
This commit is contained in:
parent
d0377a5b7c
commit
9e7ba471ae
@ -5,6 +5,7 @@ A bootleg "game engine" in c++, using SDL2 for rendering and input handling.
|
|||||||
## Dependencies
|
## Dependencies
|
||||||
- SDL2
|
- SDL2
|
||||||
- SDL2_ttf
|
- SDL2_ttf
|
||||||
|
- SDL2_mixer
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
```bash
|
```bash
|
||||||
|
BIN
example_assets/crystal_zone.mp3
Normal file
BIN
example_assets/crystal_zone.mp3
Normal file
Binary file not shown.
@ -5,12 +5,14 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "sound.h"
|
||||||
|
|
||||||
class Engine
|
class Engine
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const char* ctitle;
|
const char* ctitle;
|
||||||
int frameStart = 0;
|
int frameStart = 0;
|
||||||
|
bool soundInitialized = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Input input;
|
Input input;
|
||||||
@ -19,6 +21,7 @@ class Engine
|
|||||||
SDL_Renderer *renderer;
|
SDL_Renderer *renderer;
|
||||||
|
|
||||||
Engine(std::string title, int width, int height);
|
Engine(std::string title, int width, int height);
|
||||||
|
void initSound();
|
||||||
void clear(SDL_Color color);
|
void clear(SDL_Color color);
|
||||||
void startFrame();
|
void startFrame();
|
||||||
void render();
|
void render();
|
||||||
|
26
include/sound.h
Normal file
26
include/sound.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <SDL_mixer.h>
|
||||||
|
|
||||||
|
class Music
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Mix_Music* music;
|
||||||
|
public:
|
||||||
|
Music(const char* path);
|
||||||
|
void play(int loops = -1);
|
||||||
|
void pause();
|
||||||
|
void stop();
|
||||||
|
void resume();
|
||||||
|
void setVolume(int volume);
|
||||||
|
~Music();
|
||||||
|
};
|
||||||
|
|
||||||
|
class Effect
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Mix_Chunk* effect;
|
||||||
|
public:
|
||||||
|
Effect(const char* path);
|
||||||
|
void setVolume(int volume);
|
||||||
|
void play(int loops = 0, int channel = -1);
|
||||||
|
~Effect();
|
||||||
|
};
|
@ -21,6 +21,7 @@ class Sprite
|
|||||||
void draw(SDL_Renderer* renderer, std::string frame, bool autoRect = true, bool flip = false);
|
void draw(SDL_Renderer* renderer, std::string frame, bool autoRect = true, bool flip = false);
|
||||||
void addFrame(std::string name, SDL_Rect rect);
|
void addFrame(std::string name, SDL_Rect rect);
|
||||||
void move(int_vec2 position);
|
void move(int_vec2 position);
|
||||||
|
void setPos(int_vec2 position);
|
||||||
int_vec2 getSize(std::string frame);
|
int_vec2 getSize(std::string frame);
|
||||||
void autorect(std::string frame);
|
void autorect(std::string frame);
|
||||||
~Sprite();
|
~Sprite();
|
||||||
|
2
makefile
2
makefile
@ -1,5 +1,5 @@
|
|||||||
CXX = g++
|
CXX = g++
|
||||||
LDFLAGS = `sdl2-config --libs` -l SDL2_ttf
|
LDFLAGS = `sdl2-config --libs` -l SDL2_ttf -l SDL2_mixer
|
||||||
CXXFLAGS = `sdl2-config --cflags` -pedantic -O2 -std=c++17 -lX11 -lstdc++fs -I include
|
CXXFLAGS = `sdl2-config --cflags` -pedantic -O2 -std=c++17 -lX11 -lstdc++fs -I include
|
||||||
SRC = $(wildcard src/*.cpp)
|
SRC = $(wildcard src/*.cpp)
|
||||||
OBJ = $(addprefix build/, $(notdir $(SRC:.cpp=.o)))
|
OBJ = $(addprefix build/, $(notdir $(SRC:.cpp=.o)))
|
||||||
|
@ -14,6 +14,13 @@ Engine::Engine(std::string title, int width, int height)
|
|||||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
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)
|
void Engine::clear(SDL_Color color)
|
||||||
{
|
{
|
||||||
// Set color
|
// Set color
|
||||||
@ -52,6 +59,12 @@ Engine::~Engine()
|
|||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
|
|
||||||
|
// If sound is initialized, quit Mix
|
||||||
|
if (soundInitialized)
|
||||||
|
{
|
||||||
|
Mix_Quit();
|
||||||
|
}
|
||||||
|
|
||||||
// Quit SDL and TTF
|
// Quit SDL and TTF
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
TTF_Quit();
|
TTF_Quit();
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
SDL_Surface* assets;
|
SDL_Surface* assets;
|
||||||
|
Music* music;
|
||||||
Engine *engine;
|
Engine *engine;
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
@ -20,11 +21,13 @@ void quit()
|
|||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
engine = new Engine("Game", 800, 600);
|
engine = new Engine("Game", 800, 600);
|
||||||
|
engine->initSound();
|
||||||
engine->targetFrameRate = 60;
|
engine->targetFrameRate = 60;
|
||||||
|
|
||||||
// Load assets
|
// Load assets
|
||||||
assets = SDL_LoadBMP("example_assets/sprites.bmp");
|
assets = SDL_LoadBMP("example_assets/sprites.bmp");
|
||||||
|
music = new Music("example_assets/crystal_zone.mp3");
|
||||||
|
|
||||||
// frame map
|
// frame map
|
||||||
std::map<std::string, SDL_Rect> idle = {
|
std::map<std::string, SDL_Rect> idle = {
|
||||||
{ "idle", { 76, 456, 68, 128 } },
|
{ "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->addAnimation("walk_front", front, 10, false, true, true, true);
|
||||||
animations->setAnimation("idle");
|
animations->setAnimation("idle");
|
||||||
|
|
||||||
|
// Play music
|
||||||
|
music->play();
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -68,32 +74,30 @@ int main(int argc, char* argv[])
|
|||||||
engine->update();
|
engine->update();
|
||||||
|
|
||||||
// Exit if window is closed
|
// Exit if window is closed
|
||||||
if (engine->input.exit)
|
if (engine->input.exit || engine->input.activeKeys[SDLK_ESCAPE])
|
||||||
{
|
{
|
||||||
quit();
|
quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (engine->input.activeKeys[SDLK_d])
|
if (engine->input.activeKeys[SDLK_d])
|
||||||
{
|
{
|
||||||
sprite->dstrect.x += 1;
|
|
||||||
animations->setAnimation("walk_right");
|
animations->setAnimation("walk_right");
|
||||||
|
sprite->move({ 1, 0 });
|
||||||
}
|
}
|
||||||
if (engine->input.activeKeys[SDLK_a])
|
if (engine->input.activeKeys[SDLK_a])
|
||||||
{
|
{
|
||||||
sprite->dstrect.x -= 1;
|
|
||||||
animations->setAnimation("walk_left");
|
animations->setAnimation("walk_left");
|
||||||
|
sprite->move({ -1, 0 });
|
||||||
}
|
}
|
||||||
if (engine->input.activeKeys[SDLK_w])
|
if (engine->input.activeKeys[SDLK_w])
|
||||||
{
|
{
|
||||||
sprite->dstrect.y -= 1;
|
|
||||||
animations->setAnimation("walk_back");
|
animations->setAnimation("walk_back");
|
||||||
|
sprite->move({ 0, -1 });
|
||||||
}
|
}
|
||||||
if (engine->input.activeKeys[SDLK_s])
|
if (engine->input.activeKeys[SDLK_s])
|
||||||
{
|
{
|
||||||
sprite->dstrect.y += 1;
|
|
||||||
animations->setAnimation("walk_front");
|
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])
|
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;
|
frames[name] = rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Sprite::setPos(int_vec2 position)
|
||||||
|
{
|
||||||
|
dstrect.x = position.x;
|
||||||
|
dstrect.y = position.y;
|
||||||
|
}
|
||||||
|
|
||||||
void Sprite::move(int_vec2 position)
|
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)
|
int_vec2 Sprite::getSize(std::string frame)
|
||||||
|
Loading…
Reference in New Issue
Block a user