Skip to content

Commit

Permalink
Merge branch 'master' of github.com:vmbatlle/super-mario-kart
Browse files Browse the repository at this point in the history
  • Loading branch information
vmbatlle committed May 10, 2020
2 parents 71a4a53 + fbde30f commit 2b2538a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ super_mario_kart
*.app

#VScode
.vscode
.vscode

settings.txt
2 changes: 1 addition & 1 deletion src/entities/driveranimator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void DriverAnimator::drawParticles(sf::RenderTarget &window, sf::Sprite &driver,
break;
case LandMaterial::SPOOKY_WOOD:
type = 2;
color = sf::Color(79,44,29, 150);
color = sf::Color(89,54,39, 190);
scale = 0.7;
break;
case LandMaterial::RAINBOW:
Expand Down
2 changes: 1 addition & 1 deletion src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void Map::addEffectCoin(const Driver *driver, const int number,
sf::Time delay = sf::Time::Zero;
for (int i = 0; i < number; i++) {
Map::addItem(ItemPtr(new EffectCoin(driver, delay, positive)));
delay += sf::seconds(0.4f);
delay += sf::seconds(0.25f);
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "settings.h"

void applySetting(const std::string key, std::string value) {
if (key.compare(MUSIC_VOLUME) == 0) {
Audio::setVolume(std::stof(value), Audio::getSfxVolume());
} else if (key.compare(SFX_VOLUME) == 0) {
Audio::setVolume(Audio::getMusicVolume(), std::stof(value));
}
}


void Settings::loadSettings() {
std::string line;
std::ifstream file(FILE_NAME, std::fstream::in);

while(file.is_open() && std::getline(file, line)) {
std::string key = line.substr(0, line.find('='));
std::string value = line.substr(line.find('=')+1);
applySetting(key, value);
}

file.close();

}

void Settings::saveSettings(sf::Vector2u resolution) {
std::string line;
std::ofstream file;
file.open(FILE_NAME, std::fstream::out);

file << MUSIC_VOLUME << "=" << Audio::getMusicVolume() << std::endl;
file << SFX_VOLUME << "=" << Audio::getSfxVolume() << std::endl;
file << RESOLUTION_X << "=" << resolution.x << std::endl;
file << RESOLUTION_Y << "=" << resolution.y << std::endl;

}
27 changes: 27 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

class Settings;

#include <iostream>
#include <fstream>
#include "audio/audio.h"
#include "input/input.h"
#include <SFML/Graphics.hpp>

static const std::string FILE_NAME = "settings.txt";

static const std::string MUSIC_VOLUME = "MUSIC_VOLUME";
static const std::string SFX_VOLUME = "SFX_VOLUME";
static const std::string RESOLUTION_Y = "RESOLUTION_Y";
static const std::string RESOLUTION_X = "RESOLUTION_X";

class Settings {
private:
Settings();
static Settings inst;

public:
static void loadSettings();
static void saveSettings(sf::Vector2u resolution);

};
31 changes: 23 additions & 8 deletions src/states/start.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "start.h"


#include "map/map.h"

sf::Texture StateStart::assetBackground, StateStart::assetLogo;
Expand Down Expand Up @@ -123,6 +124,8 @@ void StateStart::init() {

// load preview for racedemo
loadRandomMap();

Settings::loadSettings();
}

void StateStart::handleEvent(const sf::Event& event) {
Expand Down Expand Up @@ -247,21 +250,25 @@ void StateStart::handleEvent(const sf::Event& event) {
case MenuState::CONTROLS:
if (waitingForKeyPress && event.type == sf::Event::KeyPressed &&
Input::getKeyCodeName(event.key.code) != "?") {
Audio::play(SFX::MENU_SELECTION_ACCEPT);
waitingForKeyPress = false;
Input::set(Key(selectedOption), event.key.code);
} else {
if (Input::pressed(Key::ACCEPT, event) ||
Input::pressed(Key::ACCELERATE, event)) {
Audio::play(SFX::MENU_SELECTION_MOVE);
keyChangeRequested = true;
} else if (Input::pressed(Key::CANCEL, event)) {
Audio::play(SFX::MENU_SELECTION_CANCEL);
currentState = MenuState::CONTROLS_FADE_OUT;
timeSinceStateChange = sf::Time::Zero;
} else if (Input::pressed(Key::MENU_UP, event)) {
Audio::play(SFX::MENU_SELECTION_MOVE);
selectedOption = selectedOption == 0
? (uint)Key::__COUNT - 1
: selectedOption - 1;
} else if (Input::pressed(Key::MENU_DOWN, event)) {
Audio::play(SFX::MENU_SELECTION_MOVE);
selectedOption = selectedOption == (uint)Key::__COUNT - 1
? 0
: selectedOption + 1;
Expand All @@ -274,10 +281,12 @@ void StateStart::handleEvent(const sf::Event& event) {
float volumeSfxPct = Audio::getSfxVolume();
switch (SettingsOption(selectedOption)) {
case SettingsOption::VOLUME_MUSIC:
Audio::play(SFX::MENU_SELECTION_MOVE);
volumeMusicPct =
std::fmaxf(volumeMusicPct - 0.1f, 0.0f);
break;
case SettingsOption::VOLUME_SFX:
Audio::play(SFX::MENU_SELECTION_MOVE);
volumeSfxPct = std::fmaxf(volumeSfxPct - 0.1f, 0.0f);
break;
case SettingsOption::RESOLUTION: {
Expand All @@ -298,10 +307,12 @@ void StateStart::handleEvent(const sf::Event& event) {
float volumeSfxPct = Audio::getSfxVolume();
switch (SettingsOption(selectedOption)) {
case SettingsOption::VOLUME_MUSIC:
Audio::play(SFX::MENU_SELECTION_MOVE);
volumeMusicPct =
std::fminf(volumeMusicPct + 0.1f, 1.0f);
break;
case SettingsOption::VOLUME_SFX:
Audio::play(SFX::MENU_SELECTION_MOVE);
volumeSfxPct = std::fminf(volumeSfxPct + 0.1f, 1.0f);
break;
case SettingsOption::RESOLUTION: {
Expand All @@ -321,11 +332,14 @@ void StateStart::handleEvent(const sf::Event& event) {
Audio::play(SFX::MENU_SELECTION_CANCEL);
currentState = MenuState::SETTINGS_FADE_OUT;
timeSinceStateChange = sf::Time::Zero;
Settings::saveSettings(game.getWindow().getSize());
} else if (Input::pressed(Key::MENU_UP, event)) {
Audio::play(SFX::MENU_SELECTION_MOVE);
selectedOption = selectedOption == 0
? (uint)SettingsOption::__COUNT - 1
: selectedOption - 1;
} else if (Input::pressed(Key::MENU_DOWN, event)) {
Audio::play(SFX::MENU_SELECTION_MOVE);
selectedOption =
selectedOption == (uint)SettingsOption::__COUNT - 1
? 0
Expand Down Expand Up @@ -547,8 +561,9 @@ void StateStart::draw(sf::RenderTarget& window) {
sf::Vector2f text2Pos = ABS_MENU + REL_TEXT2;
sf::Vector2f text3Pos = ABS_MENU + REL_TEXT3;
sf::Vector2f text4Pos = ABS_MENU + REL_TEXT4;
sf::Color color1 = Color::MenuPrimaryOnFocus, color2 = Color::MenuPrimary,
color3 = Color::MenuPrimary, color4 = Color::MenuPrimary;
sf::Color color1 = Color::MenuPrimaryOnFocus,
color2 = Color::MenuPrimary, color3 = Color::MenuPrimary,
color4 = Color::MenuPrimary;
if (selectedOption == 1)
std::swap(color1, color2);
else if (selectedOption == 2)
Expand Down Expand Up @@ -662,12 +677,12 @@ void StateStart::draw(sf::RenderTarget& window) {
if (currentState == MenuState::CIRCUIT) {
sf::Vector2f leftPos = ABS_CIRCUIT + REL_CIRCUIT0;
for (uint i = 0; i < (int)RaceCircuit::__COUNT; i++) {
TextUtils::write(
window, CIRCUIT_DISPLAY_NAMES[i],
sf::Vector2f(leftPos.x * windowSize.x,
leftPos.y * windowSize.y),
scale,
i == selectedOption ? Color::MenuPrimaryOnFocus : Color::MenuPrimary);
TextUtils::write(window, CIRCUIT_DISPLAY_NAMES[i],
sf::Vector2f(leftPos.x * windowSize.x,
leftPos.y * windowSize.y),
scale,
i == selectedOption ? Color::MenuPrimaryOnFocus
: Color::MenuPrimary);
leftPos += REL_CIRCUITDY;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/states/start.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
#include "states/racedemo.h"
#include "states/racemanager.h"
#include "states/statebase.h"
#include <map>
#include <fstream>
#include "../settings.h"

typedef std::map<const std::string, std::string> SettingsMap;

class StateStart : public State {
private:
static sf::Texture assetBackground, assetLogo;
SettingsMap settings;

std::thread randomMapLoadingThread;
bool randomMapLoaded = false, randomMapOverwritten = false;
Expand All @@ -23,6 +29,8 @@ class StateStart : public State {
sf::Texture assetLoadedMap;
void loadPreview(const RaceCircuit circuit);



public:
static void loadBackgroundAssets(const std::string& assetName,
const sf::IntRect& roiBackground,
Expand Down

0 comments on commit 2b2538a

Please sign in to comment.