int Load() { Surface sm(W, H); Event event; Mouse mouse; int mousex = -1, mousey = -1, saveindex = -1; bool clicked = false, released = false; //images Image exitI = Image("images/Exit.png"); //rects Rect exitR = exitI.getRect(); //set rect locations exitR.x = 900 - exitR.w; std::vector< std::string > SaveFiles; //list of all of the save files std::vector< std::string > Contents; //file contents getSaves(SaveFiles); while(1) { if(event.poll()) { if(event.type() == QUIT) { return 0; } else { if(event.type() == MOUSEBUTTONDOWN) { mouse.update(event); mousex = mouse.x(); mousey = mouse.y(); clicked = true; } } } if(clicked) { saveindex= -1; if(MainRectClicked(mousex, mousey, exitR)) { return 0; } for(int i = 0; i < SaveFiles.size(); i++) { TextSurface ts = TextSurface( SaveFiles[i].c_str(), "fonts/FreeSans.ttf", 16, 0, 0, 0); Rect tempR = Rect(20, (i * ts.getHeight()) + 100, ts.getWidth(), ts.getHeight()); if(MainRectClicked(mousex, mousey, tempR)) { saveindex = i; } } if(saveindex > -1) { //this is a hack in place of having time to create //a text box in sdl if(system(NULL)) { std::string c = "pluma saves/" + SaveFiles[saveindex]; system(c.c_str()); } } //reset vars clicked = false; mousex = -1; mousey = -1; } //print all the things!!! sm.lock(); sm.fill(GRAY); //print saves games for(int i = 0; i < SaveFiles.size(); i++) { TextSurface ts = TextSurface( SaveFiles[i].c_str(), "fonts/FreeSans.ttf", 16, 0, 0, 0); sm.put_rect(20, (i * ts.getHeight()) + 100, ts.getWidth(), ts.getHeight(), 0, 200, 0); sm.put_text(ts, 20, (i * ts.getHeight()) + 100); } sm.put_image(exitI, exitR); sm.unlock(); sm.flip(); delay(10); } }
int Welcome() { int ret = -1; Surface sm(W, H); Event event; Mouse mouse; int mousex = -1, mousey = -1; bool clicked = false, released = false; TextSurface welcomes = TextSurface( "Chess!!!", "fonts/FreeSerif.ttf", 200, 255, 255, 255); //images Image exitI = Image("images/Exit.png"); Image makeI = Image("images/Make.png"); Image joinI = Image("images/Join.png"); Image watchI = Image("images/Watch.png"); Image loadI = Image("images/Load.png"); //rects Rect exitR = exitI.getRect(); Rect makeR = makeI.getRect(); Rect joinR = joinI.getRect(); Rect watchR = watchI.getRect(); Rect loadR = loadI.getRect(); //set rect coords exitR.x = 900 - exitR.w; makeR.x = 150; makeR.y = 300; joinR.x = makeR.x; joinR.y = makeR.y + (joinR.h * 1.5); watchR.x = makeR.x + (watchR.w * 1.5); watchR.y = makeR.y; loadR.x = watchR.x; loadR.y = watchR.y + (loadR.h * 1.5); while(1) { if(event.poll()) { if(event.type() == QUIT) { return 0; } else { if(event.type() == MOUSEBUTTONDOWN) { mouse.update(event); mousex = mouse.x(); mousey = mouse.y(); clicked = true; } else if(event.type() == MOUSEBUTTONUP) { mouse.update(event); mousex = mouse.x(); mousey = mouse.y(); released = true; } } } if(clicked) { if(MainRectClicked(mousex, mousey, exitR)) { return 0; } else if(MainRectClicked(mousex, mousey, makeR)) { return 1; } else if(MainRectClicked(mousex, mousey, joinR)) { return 2; } else if(MainRectClicked(mousex, mousey, watchR)) { return 3; } else if(MainRectClicked(mousex, mousey, loadR)) { return 4; } //reset vars clicked = false; mousex = -1; mousey = -1; } //print all the things!!! sm.lock(); sm.fill(GRAY); sm.put_text(welcomes, 100, 0); sm.put_image(exitI, exitR); sm.put_image(makeI, makeR); sm.put_image(joinI, joinR); sm.put_image(watchI, watchR); sm.put_image(loadI, loadR); sm.flip(); sm.unlock(); delay(10); } return ret; }
void TextRenderer::drawTextWithWordWrapping(const Common::String &text, Graphics::Surface &dest) { Common::Array<TextSurface> textSurfaces; Common::Array<uint> lineWidths; Common::Array<TextJustification> lineJustifications; // Create the initial text state TextStyleState currentState; // Create an empty font and bind it to the state StyledTTFont font(_engine); currentState.updateFontWithTextState(font); Common::String currentSentence; // Not a true 'grammatical' sentence. Rather, it's just a collection of words Common::String currentWord; int sentenceWidth = 0; int wordWidth = 0; int lineWidth = 0; int lineHeight = font.getFontHeight(); uint currentLineNumber = 0u; uint numSpaces = 0u; int spaceWidth = 0; // The pixel offset to the currentSentence Common::Point sentencePixelOffset; uint i = 0u; uint stringlen = text.size(); while (i < stringlen) { if (text[i] == '<') { // Flush the currentWord to the currentSentence currentSentence += currentWord; sentenceWidth += wordWidth; // Reset the word variables currentWord.clear(); wordWidth = 0; // Parse the style tag uint startTextPosition = i; while (i < stringlen && text[i] != '>') { ++i; } uint endTextPosition = i; uint32 textColor = currentState.getTextColor(_engine); uint stateChanges = 0u; if ((endTextPosition - startTextPosition - 1) > 0) { stateChanges = currentState.parseStyle(Common::String(text.c_str() + startTextPosition + 1), endTextPosition - startTextPosition - 1); } if (stateChanges & (TEXT_CHANGE_FONT_TYPE | TEXT_CHANGE_FONT_STYLE)) { // Use the last state to render out the current sentence // Styles apply to the text 'after' them if (!currentSentence.empty()) { textSurfaces.push_back(TextSurface(font.renderSolidText(currentSentence, textColor), sentencePixelOffset, currentLineNumber)); lineWidth += sentenceWidth; sentencePixelOffset.x += sentenceWidth; // Reset the sentence variables currentSentence.clear(); sentenceWidth = 0; } // Update the current state with the style information currentState.updateFontWithTextState(font); lineHeight = MAX(lineHeight, font.getFontHeight()); spaceWidth = font.getCharWidth(' '); } if (stateChanges & TEXT_CHANGE_NEWLINE) { // If the current sentence has content, render it out if (!currentSentence.empty()) { textSurfaces.push_back(TextSurface(font.renderSolidText(currentSentence, textColor), sentencePixelOffset, currentLineNumber)); } // Set line width lineWidths.push_back(lineWidth + sentenceWidth - (numSpaces * spaceWidth)); currentSentence.clear(); sentenceWidth = 0; // Update the offsets sentencePixelOffset.x = 0u; sentencePixelOffset.y += lineHeight; // Reset the line variables lineHeight = font.getFontHeight(); lineWidth = 0; ++currentLineNumber; lineJustifications.push_back(currentState._justification); } if (stateChanges & TEXT_CHANGE_HAS_STATE_BOX) { Common::String temp = Common::String::format("%d", _engine->getScriptManager()->getStateValue(currentState._statebox)); wordWidth += font.getStringWidth(temp); // If the word causes the line to overflow, render the sentence and start a new line if (lineWidth + sentenceWidth + wordWidth > dest.w) { textSurfaces.push_back(TextSurface(font.renderSolidText(currentSentence, textColor), sentencePixelOffset, currentLineNumber)); // Set line width lineWidths.push_back(lineWidth + sentenceWidth - (numSpaces * spaceWidth)); currentSentence.clear(); sentenceWidth = 0; // Update the offsets sentencePixelOffset.x = 0u; sentencePixelOffset.y += lineHeight; // Reset the line variables lineHeight = font.getFontHeight(); lineWidth = 0; ++currentLineNumber; lineJustifications.push_back(currentState._justification); } } } else { currentWord += text[i]; wordWidth += font.getCharWidth(text[i]); if (text[i] == ' ') { // When we hit the first space, flush the current word to the sentence if (!currentWord.empty()) { currentSentence += currentWord; sentenceWidth += wordWidth; currentWord.clear(); wordWidth = 0; } // We track the number of spaces so we can disregard their width in lineWidth calculations ++numSpaces; } else { // If the word causes the line to overflow, render the sentence and start a new line if (lineWidth + sentenceWidth + wordWidth > dest.w) { // Only render out content if (!currentSentence.empty()) { textSurfaces.push_back(TextSurface(font.renderSolidText(currentSentence, currentState.getTextColor(_engine)), sentencePixelOffset, currentLineNumber)); } // Set line width lineWidths.push_back(lineWidth + sentenceWidth - (numSpaces * spaceWidth)); currentSentence.clear(); sentenceWidth = 0; // Update the offsets sentencePixelOffset.x = 0u; sentencePixelOffset.y += lineHeight; // Reset the line variables lineHeight = font.getFontHeight(); lineWidth = 0; ++currentLineNumber; lineJustifications.push_back(currentState._justification); } numSpaces = 0u; } } i++; } // Render out any remaining words/sentences if (!currentWord.empty() || !currentSentence.empty()) { currentSentence += currentWord; sentenceWidth += wordWidth; textSurfaces.push_back(TextSurface(font.renderSolidText(currentSentence, currentState.getTextColor(_engine)), sentencePixelOffset, currentLineNumber)); } lineWidths.push_back(lineWidth + sentenceWidth); lineJustifications.push_back(currentState._justification); for (Common::Array<TextSurface>::iterator iter = textSurfaces.begin(); iter != textSurfaces.end(); ++iter) { Common::Rect empty; if (lineJustifications[iter->_lineNumber] == TEXT_JUSTIFY_LEFT) { _engine->getRenderManager()->blitSurfaceToSurface(*iter->_surface, empty, dest, iter->_surfaceOffset.x, iter->_surfaceOffset.y, 0); } else if (lineJustifications[iter->_lineNumber] == TEXT_JUSTIFY_CENTER) { _engine->getRenderManager()->blitSurfaceToSurface(*iter->_surface, empty, dest, ((dest.w - lineWidths[iter->_lineNumber]) / 2) + iter->_surfaceOffset.x, iter->_surfaceOffset.y, 0); } else if (lineJustifications[iter->_lineNumber] == TEXT_JUSTIFY_RIGHT) { _engine->getRenderManager()->blitSurfaceToSurface(*iter->_surface, empty, dest, dest.w - lineWidths[iter->_lineNumber] + iter->_surfaceOffset.x, iter->_surfaceOffset.y, 0); } // Release memory iter->_surface->free(); delete iter->_surface; } }