From e93aba25226cfcb505f4979b59bf60da2d4f4059 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 26 Aug 2021 13:59:25 +0200 Subject: [PATCH] working on new font engine --- source/director.cpp | 6 +- source/game.cpp | 60 +++++++++++-- source/instructions.cpp | 4 +- source/intro.cpp | 4 +- source/lang.h | 22 +++++ source/menu.cpp | 14 +-- source/text.cpp | 185 +++++++++++----------------------------- source/text.h | 27 +++--- source/text2.cpp | 6 +- source/text2.h | 4 +- source/title.cpp | 4 +- 11 files changed, 160 insertions(+), 176 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index 5c09d7d..ff38916 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -74,7 +74,7 @@ void Director::init() } // Sección - mSection.name = PROG_SECTION_LOGO; + mSection.name = PROG_SECTION_GAME; mSection.subsection = 0; // Textos @@ -231,10 +231,12 @@ void Director::setFileList() mFileList[44] = mExecutablePath + "/" + "../media/gfx/player2_legs.png"; mFileList[45] = mExecutablePath + "/" + "../media/gfx/player2_head.png"; + // Fuentes mFileList[27] = mExecutablePath + "/" + "../media/font/8bithud.png"; + mFileList[46] = mExecutablePath + "/" + "../media/font/8bithud.txt"; mFileList[28] = mExecutablePath + "/" + "../media/font/nokia.png"; mFileList[29] = mExecutablePath + "/" + "../media/font/smb2_big.png"; - mFileList[30] = mExecutablePath + "/" + "../media/font/smb.png"; + mFileList[30] = mExecutablePath + "/" + "../media/font/smb2.png"; } // Comprueba que todos los ficheros existen diff --git a/source/game.cpp b/source/game.cpp index 1c03e63..cabb45a 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -44,8 +44,8 @@ Game::Game(int numPlayers, SDL_Renderer *renderer, std::string *filelist, std::s mTextureText = new LTexture(); mTextureText2 = new LTexture(); - mText = new Text(mTextureText, mRenderer); - mTextX2 = new Text(mTextureText2, mRenderer); + mText = new Text(mFileList[46], mTextureText, mRenderer); + mTextX2 = new Text(mFileList[46], mTextureText2, mRenderer); mMenuGameOver = new Menu(mRenderer, mText, mInput[0], mFileList); mMenuPause = new Menu(mRenderer, mText, mInput[0], mFileList); @@ -344,8 +344,8 @@ void Game::init() mFade->init(); // Inicializa los objetos de texto - mText->init(TEXT_FIXED, BLOCK); - mTextX2->init(TEXT_FIXED, BLOCK * 2); + mText->init(); + mTextX2->init(); // Inicializa el objeto con el menu de pausa mMenuPause->init("PAUSE", 0, 12 * BLOCK, MENU_BACKGROUND_SOLID); @@ -485,7 +485,7 @@ bool Game::loadMedia() bool success = true; // Texturas - success &= loadTextureFromFile(mTextureText, mFileList[30], mRenderer); + success &= loadTextureFromFile(mTextureText, mFileList[27], mRenderer); success &= loadTextureFromFile(mTextureText2, mFileList[29], mRenderer); success &= loadTextureFromFile(mTexturePlayer1Legs, mFileList[39], mRenderer); @@ -1552,8 +1552,8 @@ void Game::renderScoreBoard() // Si el jugador esta muerto, no pintes el fondo del marcador, así que pase por encima cuando sale despedido //if (mPlayer[0]->isAlive()) - mSpriteScoreBoard->render(); - + mSpriteScoreBoard->render(); + /* // SCORE mText->writeCentered(PLAY_AREA_CENTER_FIRST_QUARTER_X, SCORE_WORD_Y - 6, mTextStrings[39], 0); mText->writeCentered(PLAY_AREA_CENTER_FIRST_QUARTER_X, SCORE_NUMBER_Y - 6, updateScoreText(mPlayer[0]->getScore()), 0); @@ -1580,6 +1580,52 @@ void Game::renderScoreBoard() const float percent = (mStage[mCurrentStage].currentPower * 40.0f) / mStage[mCurrentStage].powerToComplete; mSpritePowerMeter->setSpriteClip(296, 184, (int)percent, 8); mSpritePowerMeter->render(); + */ + const int offset1 = 163; + const int offset2 = offset1 + 7; + const int offset3 = offset2 + 7; + const int offset4 = offset3 + 7; + // PLAYER1 - SCORE + mText->write(PLAY_AREA_LEFT, offset1, mTextStrings[53], 0); + mText->write(PLAY_AREA_LEFT, offset2, updateScoreText(mPlayer[0]->getScore()), 0); + + // PLAYER1 - MULT + mText->write(PLAY_AREA_LEFT, offset3, mTextStrings[55], 0); + mText->write(PLAY_AREA_LEFT + (8 * 7), offset3, std::to_string(mPlayer[0]->getScoreMultiplier()), 0); + + if (mNumPlayers == 2) + { + // PLAYER2 - SCORE + mText->write(PLAY_AREA_RIGHT - (8 * 8), offset1, mTextStrings[54], 0); + mText->write(PLAY_AREA_RIGHT - (8 * 7), offset2, updateScoreText(mPlayer[1]->getScore()), 0); + + // PLAYER2 - MULT + mText->write(PLAY_AREA_RIGHT - (8 * 10), offset3, mTextStrings[55], 0); + mText->write(PLAY_AREA_RIGHT - (8 * 5), offset3, std::to_string(mPlayer[1]->getScoreMultiplier()), 0); + } + else + { + // PLAYER2 - SCORE + mText->write(PLAY_AREA_RIGHT - (8 * 9), offset1, mTextStrings[54], 0); + mText->write(PLAY_AREA_RIGHT - (8 * 7), offset2, "0000000", 0); + + // PLAYER2 - MULT + mText->write(PLAY_AREA_RIGHT - (8 * 10), offset3, mTextStrings[55], 0); + mText->write(PLAY_AREA_RIGHT - (8 * 3), offset3, "1.0", 0); + } + + // STAGE + mText->writeCentered(PLAY_AREA_CENTER_X, offset1, mTextStrings[42] + std::to_string(mStage[mCurrentStage].number), 0); + + // POWER + //mSpritePowerMeter->setSpriteClip(256, 184, 40, 8); + //mSpritePowerMeter->render(); + //const float percent = (mStage[mCurrentStage].currentPower * 40.0f) / mStage[mCurrentStage].powerToComplete; + //mSpritePowerMeter->setSpriteClip(296, 184, (int)percent, 8); + //mSpritePowerMeter->render(); + + // HI-SCORE + mText->writeCentered(PLAY_AREA_CENTER_X, offset4, mTextStrings[40] + "0000000", 0); } // Actualiza las variables del jugador diff --git a/source/instructions.cpp b/source/instructions.cpp index 62e136e..8bfb63d 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -19,7 +19,7 @@ Instructions::Instructions(SDL_Renderer *renderer, std::string *fileList, std::s mItemTexture = new LTexture(); mTextTexture = new LTexture(); mSprite = new Sprite(); - mText = new Text(mTextTexture, mRenderer); + mText = new Text(mFileList[46], mTextTexture, mRenderer); // Crea un backbuffer para el renderizador mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); @@ -73,7 +73,7 @@ void Instructions::init() mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mItemTexture, mRenderer); mTicks = 0; mTicksSpeed = 15; - mText->init(TEXT_FIXED, BLOCK); + mText->init(); mManualQuit = false; mCounter = 0; } diff --git a/source/intro.cpp b/source/intro.cpp index 11b5b2d..7fa40ad 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -21,7 +21,7 @@ Intro::Intro(SDL_Renderer *renderer, std::string *fileList, std::string *textStr mBitmap[i] = new SmartSprite(); for (int i = 0; i < INTRO_TOTAL_TEXTS; i++) - mText[i] = new Text2(mTextTexture, mRenderer); + mText[i] = new Text2(mFileList[46], mTextTexture, mRenderer); } // Destructor @@ -133,7 +133,7 @@ void Intro::init() // Inicializa los textos de la intro for (int i = 0; i < INTRO_TOTAL_TEXTS; i++) { - mText[i]->init(TEXT_VARIABLE, 10); + mText[i]->init(); mText[i]->setId(6 + i); mText[i]->setIntroEvents(&mEvents[0]); mText[i]->setPosX(BLOCK * 0); diff --git a/source/lang.h b/source/lang.h index d1903e5..86ccd4a 100644 --- a/source/lang.h +++ b/source/lang.h @@ -280,6 +280,28 @@ const std::string gTextStrings[MAX_TEXT_STRINGS][3] = "2 JUGADORS", "2 PLAYERS"}, + // MARCADOR + + // 53 - MENU DEL TITULO (2 JUGADORES) + {"jugador 1", + "jugador 2", + "player 1"}, + + // 54 - MENU DEL TITULO (2 JUGADORES) + {"jugador 2", + "jugador 2", + "player 2"}, + + // 55 - MENU DEL TITULO (2 JUGADORES) + {"mult - ", + "mult - ", + "mult - "}, + + // 56 - MENU DEL TITULO (2 JUGADORES) + {"max. puntuacion - ", + "max. puntuacio - ", + "high score - "}, + }; void initTextStrings(std::string *textStrings, Uint8 lang); diff --git a/source/menu.cpp b/source/menu.cpp index 92932cf..bdaeb4b 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -316,19 +316,19 @@ void Menu::setRectSize() } // La anchura de la cadena más larga, mas un caracter, mas la anchura del sprite del selector - mRectBG.rect.w += (mText->getSize() * 1); // + mSelectorSprite.getWidth(); + mRectBG.rect.w += (mText->getCharacterWidth() * 1); // + mSelectorSprite.getWidth(); // La altura de la suma de los items mas un caracter y menos un pixel (porque el texto en realidad es de 7 pixeles) - mRectBG.rect.h += (mText->getSize() * 1) - 1; + mRectBG.rect.h += (mText->getCharacterWidth() * 1) - 1; // La posición X es la del menú menos la anchura del sprite del selector y menos medio caracter - mRectBG.rect.x = mPosX - (mText->getSize() / 2); // - mSelectorSprite.getWidth(); + mRectBG.rect.x = mPosX - (mText->getCharacterWidth() / 2); // - mSelectorSprite.getWidth(); // La posición Y es la del menu menos la altura de medio caracter i el padding - mRectBG.rect.y = mPosY - (mText->getSize() / 2) - mVerticalPadding; + mRectBG.rect.y = mPosY - (mText->getCharacterWidth() / 2) - mVerticalPadding; // Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo - mSelector.rect.h = (mText->getSize() * 1) + 1; + mSelector.rect.h = (mText->getCharacterWidth() * 1) + 1; mSelector.rect.w = mRectBG.rect.w; mSelector.rect.x = mRectBG.rect.x; } @@ -418,7 +418,7 @@ void Menu::addItem(std::string text, const Uint8 hPaddingUp, const Uint8 hPaddin { mItem[mTotalItems].label = text; mItem[mTotalItems].w = mText->lenght(mItem[mTotalItems].label, 0); - mItem[mTotalItems].h = mText->getSize() + (mVerticalPadding * 2); + mItem[mTotalItems].h = mText->getCharacterWidth() + (mVerticalPadding * 2); mItem[mTotalItems].x = mPosX; mItem[mTotalItems].y = mPosY; mItem[mTotalItems].hPaddingUp = hPaddingUp; @@ -431,7 +431,7 @@ void Menu::addItem(std::string text, const Uint8 hPaddingUp, const Uint8 hPaddin { mItem[mTotalItems].label = text; mItem[mTotalItems].w = mText->lenght(mItem[mTotalItems].label, 0); - mItem[mTotalItems].h = mText->getSize() + (mVerticalPadding * 2); + mItem[mTotalItems].h = mText->getCharacterWidth() + (mVerticalPadding * 2); mItem[mTotalItems].x = mPosX; mItem[mTotalItems].y = mItem[mTotalItems - 1].y + mItem[mTotalItems - 1].h + mItem[mTotalItems - 1].hPaddingDown; mItem[mTotalItems].hPaddingUp = hPaddingUp; diff --git a/source/text.cpp b/source/text.cpp index abae707..0afe730 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -1,12 +1,16 @@ #include "const.h" #include "text.h" +#include +#include +//#include // Constructor -Text::Text(LTexture *texture, SDL_Renderer *renderer) +Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer) { mSprite = new Sprite(); mSprite->setTexture(texture); mSprite->setRenderer(renderer); + mFile = file; } // Destructor @@ -21,24 +25,9 @@ Text::~Text() // y el listado de anchos de cada una // el init ya no necesita type ni size - // Inicializador -void Text::init(Uint8 type, Uint8 size) +void Text::init() { - // Inicializa variables - mType = type; - mSize = size; - - // Inicia los valores del sprite que dibuja las letras - mSprite->setWidth(size); - mSprite->setHeight(size); - mSprite->setPosX(0); - mSprite->setPosY(0); - mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight()); - - // Cadena con los caracteres ascii que se van a inicializar - const std::string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ{\\[]]^_`abcdefghijklmnopqrstuvwxyz"; - // Inicializa a cero el vector con las coordenadas for (int i = 0; i < 128; i++) { @@ -47,106 +36,21 @@ void Text::init(Uint8 type, Uint8 size) mOffset[i].w = 0; } + // Carga los offsets desde el fichero + initOffsetFromFile(); + + // Inicia los valores del sprite que dibuja las letras + mSprite->setWidth(mBoxWidth); + mSprite->setHeight(mBoxHeight); + mSprite->setPosX(0); + mSprite->setPosY(0); + mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight()); + // Establece las coordenadas para cada caracter ascii de la cadena y su ancho - for (int i = 0; i < text.length(); ++i) + for (int i = 32; i < 128; i++) { - mOffset[int(text[i])].x = ((int(text[i]) - 32) % 15) * mSprite->getWidth(); - mOffset[int(text[i])].y = ((int(text[i]) - 32) / 15) * mSprite->getHeight(); - if (type == TEXT_FIXED) - mOffset[int(text[i])].w = size; - } - - // Establece las coordenadas de forma manual para la ñ - mOffset[int('^')].x = mOffset[int('z')].x + size; - mOffset[int('^')].y = mOffset[int('z')].y; - mOffset[int('^')].w = size; - - // Establece las coordenadas de forma manual para la ñç - mOffset[int('~')].x = mOffset[int('z')].x + size * 2; - mOffset[int('~')].y = mOffset[int('z')].y; - mOffset[int('~')].w = size; - - // Establece el ancho de cada caracter - if (type == TEXT_VARIABLE) - { - mOffset[int(' ')].w = 7; - mOffset[int('-')].w = 6; - mOffset[int('/')].w = 5; - mOffset[int('(')].w = 5; - mOffset[int(')')].w = 5; - mOffset[int('.')].w = 4; - mOffset[int(':')].w = 4; - mOffset[int('#')].w = 7; - mOffset[int('!')].w = 7; - mOffset[int('\'')].w = 3; - - mOffset[int('0')].w = 7; - mOffset[int('1')].w = 5; - mOffset[int('2')].w = 7; - mOffset[int('3')].w = 7; - mOffset[int('4')].w = 7; - mOffset[int('5')].w = 7; - mOffset[int('6')].w = 7; - mOffset[int('7')].w = 7; - mOffset[int('8')].w = 7; - mOffset[int('9')].w = 7; - - mOffset[int('A')].w = 7; - mOffset[int('B')].w = 7; - mOffset[int('C')].w = 7; - mOffset[int('D')].w = 7; - mOffset[int('E')].w = 7; - mOffset[int('F')].w = 7; - mOffset[int('G')].w = 7; - mOffset[int('H')].w = 7; - mOffset[int('I')].w = 4; - mOffset[int('J')].w = 6; - mOffset[int('K')].w = 8; - mOffset[int('L')].w = 6; - mOffset[int('M')].w = 9; - mOffset[int('N')].w = 8; - mOffset[int('O')].w = 8; - mOffset[int('P')].w = 7; - mOffset[int('Q')].w = 8; - mOffset[int('R')].w = 7; - mOffset[int('S')].w = 6; - mOffset[int('T')].w = 8; - mOffset[int('U')].w = 7; - mOffset[int('V')].w = 8; - mOffset[int('W')].w = 9; - mOffset[int('X')].w = 8; - mOffset[int('Y')].w = 8; - mOffset[int('Z')].w = 7; - - mOffset[int('^')].w = 7; - mOffset[int('~')].w = 7; - - mOffset[int('a')].w = 7; - mOffset[int('b')].w = 7; - mOffset[int('c')].w = 6; - mOffset[int('d')].w = 7; - mOffset[int('e')].w = 7; - mOffset[int('f')].w = 5; - mOffset[int('g')].w = 7; - mOffset[int('h')].w = 7; - mOffset[int('i')].w = 4; - mOffset[int('j')].w = 5; - mOffset[int('k')].w = 7; - mOffset[int('l')].w = 4; - mOffset[int('m')].w = 10; - mOffset[int('n')].w = 7; - mOffset[int('o')].w = 7; - mOffset[int('p')].w = 7; - mOffset[int('q')].w = 7; - mOffset[int('r')].w = 6; - mOffset[int('s')].w = 6; - mOffset[int('t')].w = 5; - mOffset[int('u')].w = 7; - mOffset[int('v')].w = 7; - mOffset[int('w')].w = 9; - mOffset[int('x')].w = 7; - mOffset[int('y')].w = 7; - mOffset[int('z')].w = 7; + mOffset[i].x = ((i - 32) % 16) * mBoxWidth; + mOffset[i].y = ((i - 32) / 16) * mBoxHeight; } } @@ -222,26 +126,41 @@ Uint16 Text::lenght(std::string text, int kerning) return shift; } -// Obtiene el valor de la variable -Uint8 Text::getType() +// Inicializa el vector de offsets desde un fichero +void Text::initOffsetFromFile() { - return mType; + std::ifstream rfile(mFile); + if (rfile.is_open() && rfile.good()) + { + std::string buffer; + + // Lee los dos primeros valores del fichero + std::getline(rfile, buffer); + std::getline(rfile, buffer); + mBoxWidth = std::stoi(buffer); + + std::getline(rfile, buffer); + std::getline(rfile, buffer); + mBoxHeight = std::stoi(buffer); + + // lee el resto de datos del fichero + int index = 32; + int line_read = 0; + while (std::getline(rfile, buffer)) + { + // Almacena solo las lineas impares + if (line_read % 2 == 1) + mOffset[index++].w = std::stoi(buffer); + + //Then clear the buffer once you're done with it. + buffer.clear(); + line_read++; + }; + } } -// Establece el valor de la variable -void Text::setType(Uint8 type) +// Devuelve el valor de la variable +Uint8 Text::getCharacterWidth() { - mType = type; -} - -// Obtiene el valor de la variable -Uint8 Text::getSize() -{ - return mSize; -} - -// Establece el valor de la variable -void Text::setSize(Uint8 size) -{ - mSize = size; + return mBoxWidth; } \ No newline at end of file diff --git a/source/text.h b/source/text.h index 6ec2b68..52498c6 100644 --- a/source/text.h +++ b/source/text.h @@ -21,20 +21,24 @@ private: int y; Uint8 w; }; - Offset mOffset[255]; // Vector con las posiciones y ancho de cada letra + Offset mOffset[128]; // Vector con las posiciones y ancho de cada letra - Uint8 mType; // Indica si el texto es de anchura fija o variable - Uint8 mSize; // Altura del texto + Uint8 mBoxWidth; // Anchura de la caja de cada caracter en el png + Uint8 mBoxHeight; // Altura de la caja de cada caracter en el png + std::string mFile; // Fichero con los descriptores de la fuente + + // Inicializa el vector de offsets desde un fichero + void initOffsetFromFile(); public: // Constructor - Text(LTexture *texture, SDL_Renderer *renderer); + Text(std::string file, LTexture *texture, SDL_Renderer *renderer); // Destructor ~Text(); // Inicializador - void init(Uint8 type, Uint8 size); + void init(); // Escribe el texto en pantalla void write(int x, int y, std::string text, int kerning = 0, int lenght = -1); @@ -54,17 +58,8 @@ public: // Obtiene la longitud en pixels de una cadena Uint16 lenght(std::string text, int kerning); - // Obtiene el valor de la variable - Uint8 getType(); - - // Establece el valor de la variable - void setType(Uint8 type); - - // Obtiene el valor de la variable - Uint8 getSize(); - - // Establece el valor de la variable - void setSize(Uint8 size); + // Devuelve el valor de la variable + Uint8 getCharacterWidth(); }; #endif diff --git a/source/text2.cpp b/source/text2.cpp index 02a2974..d9cb519 100644 --- a/source/text2.cpp +++ b/source/text2.cpp @@ -2,7 +2,7 @@ #include "text2.h" // Constructor -Text2::Text2(LTexture *texture, SDL_Renderer *renderer) : Text(texture, renderer) +Text2::Text2(std::string file, LTexture *texture, SDL_Renderer *renderer) : Text(file, texture, renderer) { } @@ -12,9 +12,9 @@ Text2::~Text2() } // Inicializador -void Text2::init(Uint8 type, Uint8 size) +void Text2::init() { - Text::init(type, size); + Text::init(); mPosX = 0; mPosY = 0; mKerning = 0; diff --git a/source/text2.h b/source/text2.h index 8275bcf..4b3f008 100644 --- a/source/text2.h +++ b/source/text2.h @@ -10,13 +10,13 @@ class Text2 : public Text { public: // Constructor - Text2(LTexture *texture, SDL_Renderer *renderer); + Text2(std::string file, LTexture *texture, SDL_Renderer *renderer); // Destructor ~Text2(); // Inicializador - void init(Uint8 type, Uint8 size); + void init(); // Establece el valor de la variable void setPosX(int value); diff --git a/source/title.cpp b/source/title.cpp index 741a8c9..e030ac1 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -26,7 +26,7 @@ Title::Title(SDL_Window *window, SDL_Renderer *renderer, Input *input, std::stri mDustBitmapL = new AnimatedSprite(); mDustBitmapR = new AnimatedSprite(); mTile = new Sprite(); - mText = new Text(mTextTexture, mRenderer); + mText = new Text(mFileList[46], mTextTexture, mRenderer); mMenu.title = new Menu(mRenderer, mText, mInput, mFileList); mMenu.options = new Menu(mRenderer, mText, mInput, mFileList); @@ -113,7 +113,7 @@ void Title::init(bool demo, Uint8 subsection) mPostFade = 0; mTicks = 0; mTicksSpeed = 15; - mText->init(TEXT_FIXED, BLOCK); + mText->init(); mFade->init(); mDemo = demo;