From 39a8c992e10098ee05b3b5809f87975a3b244be6 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 14 Oct 2024 17:12:07 +0200 Subject: [PATCH] Ja duplica la ultima lletra al posar el nom --- source/enter_name.cpp | 51 ++++++---- source/enter_name.h | 22 +++-- source/jail_audio_sdlmixer.cpp | 166 --------------------------------- source/player.cpp | 6 +- 4 files changed, 50 insertions(+), 195 deletions(-) delete mode 100644 source/jail_audio_sdlmixer.cpp diff --git a/source/enter_name.cpp b/source/enter_name.cpp index acd24d9..e9380fa 100644 --- a/source/enter_name.cpp +++ b/source/enter_name.cpp @@ -15,7 +15,7 @@ void EnterName::init() // Inicia la lista de caracteres permitidos character_list_ = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+-*/=?¿<>!\"#$%&/()"; - pos_ = 0; + position_ = 0; num_characters_ = (int)character_list_.size(); // Pone la lista de indices para que refleje el nombre @@ -26,26 +26,27 @@ void EnterName::init() } // Incrementa la posición -void EnterName::incPos() +void EnterName::incPosition() { - pos_++; - pos_ = std::min(pos_, NAME_LENGHT - 1); + position_++; + position_ = std::min(position_, NAME_LENGHT - 1); + checkIfPositionHasBeenUsed(); } // Decrementa la posición -void EnterName::decPos() +void EnterName::decPosition() { - pos_--; - pos_ = std::max(pos_, 0); + position_--; + position_ = std::max(position_, 0); } // Incrementa el índice void EnterName::incIndex() { - ++character_index_[pos_]; - if (character_index_[pos_] >= num_characters_) + ++character_index_[position_]; + if (character_index_[position_] >= num_characters_) { - character_index_[pos_] = 0; + character_index_[position_] = 0; } updateName(); } @@ -53,15 +54,15 @@ void EnterName::incIndex() // Decrementa el índice void EnterName::decIndex() { - --character_index_[pos_]; - if (character_index_[pos_] < 0) + --character_index_[position_]; + if (character_index_[position_] < 0) { - character_index_[pos_] = num_characters_ - 1; + character_index_[position_] = num_characters_ - 1; } updateName(); } -// Actualiza la variable +// Actualiza el nombre a partir de la lista de índices void EnterName::updateName() { name_.clear(); @@ -74,16 +75,18 @@ void EnterName::updateName() // Actualiza la variable void EnterName::updateCharacterIndex() { - // Rellena de espacios + // Rellena de espacios y marca como no usados for (int i = 0; i < NAME_LENGHT; ++i) { character_index_[i] = 0; + position_has_been_used_[i] = false; } // Coloca los índices en funcion de los caracteres que forman el nombre for (int i = 0; i < (int)name_.size(); ++i) { character_index_[i] = findIndex(name_.at(i)); + position_has_been_used_[i] = true; } } @@ -107,7 +110,21 @@ std::string EnterName::getName() const } // Obtiene la posición que se está editando -int EnterName::getPos() const +int EnterName::getPosition() const { - return pos_; + return position_; +} + +// Comprueba la posición y copia el caracter si es necesario +void EnterName::checkIfPositionHasBeenUsed() +{ + auto used = position_has_been_used_[position_]; + + if (!used && position_ > 0) + { + character_index_[position_] = character_index_[position_ - 1]; + } + + position_has_been_used_[position_] = true; + updateName(); } \ No newline at end of file diff --git a/source/enter_name.h b/source/enter_name.h index 238cdc5..ec2262a 100644 --- a/source/enter_name.h +++ b/source/enter_name.h @@ -16,13 +16,14 @@ constexpr int NAME_LENGHT = 8; class EnterName { private: - std::string character_list_; // Lista de todos los caracteres permitidos - std::string name_; // Nombre introducido - int pos_; // Posición a editar del nombre - int num_characters_; // Cantidad de caracteres de la lista de caracteres - int character_index_[NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre + std::string character_list_; // Lista de todos los caracteres permitidos + std::string name_; // Nombre introducido + int position_; // Posición a editar del nombre + int num_characters_; // Cantidad de caracteres de la lista de caracteres + int character_index_[NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre + bool position_has_been_used_[NAME_LENGHT]; // Indica si en esa posición se ha puesto ya alguna letra. Se utiliza para replicar la letra anterior la primera vez - // Actualiza la variable + // Actualiza el nombre a partir de la lista de índices void updateName(); // Actualiza la variable @@ -31,6 +32,9 @@ private: // Encuentra el indice de un caracter en "characterList" int findIndex(char character); + // Comprueba la posición y copia el caracter si es necesario + void checkIfPositionHasBeenUsed(); + public: // Constructor EnterName(); @@ -42,10 +46,10 @@ public: void init(); // Incrementa la posición - void incPos(); + void incPosition(); // Decrementa la posición - void decPos(); + void decPosition(); // Incrementa el índice void incIndex(); @@ -57,5 +61,5 @@ public: std::string getName() const; // Obtiene la posición que se está editando - int getPos() const; + int getPosition() const; }; \ No newline at end of file diff --git a/source/jail_audio_sdlmixer.cpp b/source/jail_audio_sdlmixer.cpp deleted file mode 100644 index 8ee32a7..0000000 --- a/source/jail_audio_sdlmixer.cpp +++ /dev/null @@ -1,166 +0,0 @@ -#ifdef JA_USESDLMIXER -#include "jail_audio.h" -#include -#include -#include - -struct JA_Sound_t {}; // Dummy structs -struct JA_Music_t {}; - -int JA_freq {48000}; -SDL_AudioFormat JA_format {AUDIO_S16}; -Uint8 JA_channels {2}; -int JA_musicVolume = 128; -int JA_soundVolume = 64; -bool JA_musicEnabled = true; -bool JA_soundEnabled = true; - -void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) { - JA_freq = freq; - JA_format = format; - JA_channels = channels; - Mix_OpenAudio(JA_freq, JA_format, JA_channels, 1024); -} - -void JA_Quit() { - Mix_CloseAudio(); -} - -JA_Music_t *JA_LoadMusic(const char* filename) { - return (JA_Music_t*)Mix_LoadMUS(filename); -} - -void JA_PlayMusic(JA_Music_t *music, const int loop) -{ - if (!JA_musicEnabled) return; - Mix_PlayMusic((Mix_Music*)music, loop); - Mix_VolumeMusic(JA_musicVolume); -} - -void JA_PauseMusic() -{ - if (!JA_musicEnabled) return; - Mix_PauseMusic(); -} - -void JA_ResumeMusic() -{ - if (!JA_musicEnabled) return; - Mix_ResumeMusic(); -} - -void JA_StopMusic() -{ - if (!JA_musicEnabled) return; - Mix_HaltMusic(); -} - -JA_Music_state JA_GetMusicState() -{ - if (!JA_musicEnabled) return JA_MUSIC_DISABLED; - - if (Mix_PausedMusic()) { - return JA_MUSIC_PAUSED; - } else if (Mix_PlayingMusic()) { - return JA_MUSIC_PLAYING; - } else { - return JA_MUSIC_STOPPED; - } -} - -void JA_DeleteMusic(JA_Music_t *music) -{ - Mix_FreeMusic((Mix_Music*)music); -} - -int JA_SetMusicVolume(int volume) -{ - JA_musicVolume = volume; - Mix_VolumeMusic(JA_musicVolume); - return JA_musicVolume; -} - -void JA_EnableMusic(const bool value) -{ - if (Mix_PlayingMusic()) Mix_HaltMusic(); - JA_musicEnabled = value; -} - - - - - -JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) -{ - return NULL; -} - -JA_Sound_t *JA_LoadSound(const char* filename) { - JA_Sound_t *sound = (JA_Sound_t*)Mix_LoadWAV(filename); - return sound; -} - -int JA_PlaySound(JA_Sound_t *sound, const int loop) { - if (!JA_soundEnabled) return -1; - const int channel = Mix_PlayChannel(-1, (Mix_Chunk*)sound, loop); - Mix_Volume(-1, JA_soundVolume); - return channel; -} - -void JA_DeleteSound(JA_Sound_t *sound) -{ - Mix_FreeChunk((Mix_Chunk*)sound); -} - -void JA_PauseChannel(const int channel) -{ - if (!JA_soundEnabled) return; - Mix_Pause(channel); -} - -void JA_ResumeChannel(const int channel) -{ - if (!JA_soundEnabled) return; - Mix_Resume(channel); -} - -void JA_StopChannel(const int channel) -{ - if (!JA_soundEnabled) return; - Mix_HaltChannel(channel); -} - -JA_Channel_state JA_GetChannelState(const int channel) -{ - if (!JA_soundEnabled) return JA_SOUND_DISABLED; - - if (Mix_Paused(channel)) { - return JA_CHANNEL_PAUSED; - } else if (Mix_Playing(channel)) { - return JA_CHANNEL_PLAYING; - } else { - return JA_CHANNEL_FREE; - } -} - -int JA_SetSoundVolume(int volume) -{ - JA_musicVolume = volume; - Mix_Volume(-1, JA_musicVolume); - return JA_musicVolume; -} - -void JA_EnableSound(const bool value) -{ - Mix_HaltChannel(-1); - JA_soundEnabled = value; -} - - -int JA_SetVolume(int volume) -{ - JA_SetSoundVolume(volume); - return JA_SetMusicVolume(volume); -} - -#endif diff --git a/source/player.cpp b/source/player.cpp index 8ae99ef..3f85476 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -149,11 +149,11 @@ void Player::setInputEnteringName(InputType input) switch (input) { case InputType::LEFT: - enter_name_->decPos(); + enter_name_->decPosition(); break; case InputType::RIGHT: - enter_name_->incPos(); + enter_name_->incPosition(); break; case InputType::UP: @@ -763,7 +763,7 @@ int Player::getRecordNamePos() const { if (enter_name_) { - return enter_name_->getPos(); + return enter_name_->getPosition(); } return 0;