From 4ade04d760817ec88a06c004ee0394345540a5f4 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 2 Sep 2022 20:52:49 +0200 Subject: [PATCH] Some minor code cleanup. --- es-app/src/guis/GuiMetaDataEd.cpp | 4 +- es-core/src/ImageIO.cpp | 16 +++---- es-core/src/components/ComponentGrid.cpp | 54 ++++++++++++------------ es-core/src/components/MenuComponent.cpp | 16 +++---- 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/es-app/src/guis/GuiMetaDataEd.cpp b/es-app/src/guis/GuiMetaDataEd.cpp index 8565b7d8d..cc4976c18 100644 --- a/es-app/src/guis/GuiMetaDataEd.cpp +++ b/es-app/src/guis/GuiMetaDataEd.cpp @@ -698,7 +698,7 @@ GuiMetaDataEd::GuiMetaDataEd(MetaDataList* md, void GuiMetaDataEd::onSizeChanged() { - const float titleSubtitleSpacing = mSize.y * 0.03f; + const float titleSubtitleSpacing {mSize.y * 0.03f}; mGrid.setRowHeightPerc(0, TITLE_HEIGHT / mSize.y / 2.0f); mGrid.setRowHeightPerc(1, TITLE_HEIGHT / mSize.y / 2.0f); @@ -715,7 +715,7 @@ void GuiMetaDataEd::onSizeChanged() (Renderer::getScreenHeight() - mSize.y) / 2.0f); // Add some extra margins to the file/folder name. - const float newSizeX = mSize.x * 0.96f; + const float newSizeX {mSize.x * 0.96f}; mSubtitle->setSize(newSizeX, mSubtitle->getSize().y); mSubtitle->setPosition((mSize.x - newSizeX) / 2.0f, mSubtitle->getPosition().y); } diff --git a/es-core/src/ImageIO.cpp b/es-core/src/ImageIO.cpp index 13f66648c..d66426fc9 100644 --- a/es-core/src/ImageIO.cpp +++ b/es-core/src/ImageIO.cpp @@ -25,14 +25,14 @@ std::vector ImageIO::loadFromMemoryRGBA32(const unsigned char* da if (fiMemory != nullptr) { // Detect the filetype from data. - FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory); + FREE_IMAGE_FORMAT format {FreeImage_GetFileTypeFromMemory(fiMemory)}; if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format)) { // File type is supported, load image. - FIBITMAP* fiBitmap = FreeImage_LoadFromMemory(format, fiMemory); + FIBITMAP* fiBitmap {FreeImage_LoadFromMemory(format, fiMemory)}; if (fiBitmap != nullptr) { // Loaded. convert to 32-bit if necessary. if (FreeImage_GetBPP(fiBitmap) != 32) { - FIBITMAP* fiConverted = FreeImage_ConvertTo32Bits(fiBitmap); + FIBITMAP* fiConverted {FreeImage_ConvertTo32Bits(fiBitmap)}; if (fiConverted != nullptr) { // Free original bitmap data. FreeImage_Unload(fiBitmap); @@ -44,14 +44,14 @@ std::vector ImageIO::loadFromMemoryRGBA32(const unsigned char* da height = FreeImage_GetHeight(fiBitmap); // Loop through scanlines and add all pixel data to the return vector. // This is necessary, because width*height*bpp might not be == pitch. - unsigned char* tempData = new unsigned char[width * height * 4]; + unsigned char* tempData {new unsigned char[width * height * 4]}; for (size_t i = 0; i < height; ++i) { - const BYTE* scanLine = FreeImage_GetScanLine(fiBitmap, static_cast(i)); + const BYTE* scanLine {FreeImage_GetScanLine(fiBitmap, static_cast(i))}; memcpy(tempData + (i * width * 4), scanLine, width * 4); } // Convert from BGRA to RGBA. for (size_t i = 0; i < width * height; ++i) { - RGBQUAD bgra = reinterpret_cast(tempData)[i]; + RGBQUAD bgra {reinterpret_cast(tempData)[i]}; RGBQUAD rgba; rgba.rgbBlue = bgra.rgbRed; rgba.rgbGreen = bgra.rgbGreen; @@ -59,7 +59,7 @@ std::vector ImageIO::loadFromMemoryRGBA32(const unsigned char* da rgba.rgbReserved = bgra.rgbReserved; reinterpret_cast(tempData)[i] = rgba; } - rawData = std::vector(tempData, tempData + width * height * 4); + rawData = std::vector {tempData, tempData + width * height * 4}; // Free bitmap data. FreeImage_Unload(fiBitmap); delete[] tempData; @@ -82,7 +82,7 @@ std::vector ImageIO::loadFromMemoryRGBA32(const unsigned char* da void ImageIO::flipPixelsVert(unsigned char* imagePx, const size_t& width, const size_t& height) { unsigned int temp; - unsigned int* arr = reinterpret_cast(imagePx); + unsigned int* arr {reinterpret_cast(imagePx)}; for (size_t y = 0; y < height / 2; ++y) { for (size_t x = 0; x < width; ++x) { temp = arr[x + (y * width)]; diff --git a/es-core/src/components/ComponentGrid.cpp b/es-core/src/components/ComponentGrid.cpp index bef61668f..f2d30b598 100644 --- a/es-core/src/components/ComponentGrid.cpp +++ b/es-core/src/components/ComponentGrid.cpp @@ -63,8 +63,8 @@ float ComponentGrid::getRowHeight(int row) return mRowHeights[row] * mSize.y; // Calculate automatic height. - float freeHeightPerc = 1; - int between = 0; + float freeHeightPerc {1.0f}; + int between {0}; for (int y = 0; y < mGridSize.y; ++y) { freeHeightPerc -= mRowHeights[y]; // If it's 0 it won't do anything. if (mRowHeights[y] == 0) @@ -105,7 +105,7 @@ void ComponentGrid::setEntry(const std::shared_ptr& comp, assert(comp != nullptr); assert(comp->getParent() == nullptr); - GridEntry entry(pos, size, comp, canFocus, resize, updateType, border); + GridEntry entry {pos, size, comp, canFocus, resize, updateType, border}; mCells.push_back(entry); addChild(comp.get()); @@ -163,7 +163,7 @@ void ComponentGrid::updateSeparators() { mSeparators.clear(); - bool drawAll = Settings::getInstance()->getBool("DebugGrid"); + bool drawAll {Settings::getInstance()->getBool("DebugGrid")}; glm::vec2 pos; glm::vec2 size; @@ -173,8 +173,8 @@ void ComponentGrid::updateSeparators() continue; // Find component position + size. - pos = glm::vec2 {}; - size = glm::vec2 {}; + pos = glm::vec2 {0.0f, 0.0f}; + size = glm::vec2 {0.0f, 0.0f}; for (int x = 0; x < it->pos.x; ++x) pos[0] += getColWidth(x); for (int y = 0; y < it->pos.y; ++y) @@ -184,7 +184,7 @@ void ComponentGrid::updateSeparators() for (int y = it->pos.y; y < it->pos.y + it->dim.y; ++y) size[1] += getRowHeight(y); - if (size == glm::vec2 {}) + if (size == glm::vec2 {0.0f, 0.0f}) return; if (it->border & BORDER_TOP || drawAll) { @@ -235,10 +235,10 @@ const ComponentGrid::GridEntry* ComponentGrid::getCellAt(int x, int y) const assert(x >= 0 && x < mGridSize.x && y >= 0 && y < mGridSize.y); for (auto it = mCells.cbegin(); it != mCells.cend(); ++it) { - int xmin = it->pos.x; - int xmax = xmin + it->dim.x; - int ymin = it->pos.y; - int ymax = ymin + it->dim.y; + int xmin {it->pos.x}; + int xmax {xmin + it->dim.x}; + int ymin {it->pos.y}; + int ymax {ymin + it->dim.y}; if (x >= xmin && y >= ymin && x < xmax && y < ymax) return &(*it); @@ -249,14 +249,14 @@ const ComponentGrid::GridEntry* ComponentGrid::getCellAt(int x, int y) const bool ComponentGrid::input(InputConfig* config, Input input) { - const GridEntry* cursorEntry = getCellAt(mCursor); + const GridEntry* cursorEntry {getCellAt(mCursor)}; if (cursorEntry && cursorEntry->component->input(config, input)) return true; if (!input.value) return false; - bool withinBoundary = false; + bool withinBoundary {false}; if (config->isMappedLike("down", input)) withinBoundary = moveCursor(glm::ivec2 {0, 1}); @@ -283,7 +283,7 @@ void ComponentGrid::resetCursor() for (auto it = mCells.cbegin(); it != mCells.cend(); ++it) { if (it->canFocus) { - glm::ivec2 origCursor = mCursor; + glm::ivec2 origCursor {mCursor}; mCursor = it->pos; onCursorMoved(origCursor, mCursor); break; @@ -296,8 +296,8 @@ bool ComponentGrid::moveCursor(glm::ivec2 dir) assert(dir.x || dir.y); const glm::ivec2 origCursor {mCursor}; - const GridEntry* currentCursorEntry = getCellAt(mCursor); - glm::ivec2 searchAxis(dir.x == 0, dir.y == 0); + const GridEntry* currentCursorEntry {getCellAt(mCursor)}; + glm::ivec2 searchAxis {dir.x == 0, dir.y == 0}; // Logic to handle entries that span several cells. if (currentCursorEntry->dim.x > 1) { @@ -392,28 +392,28 @@ void ComponentGrid::moveCursorTo(int xPos, int yPos, bool selectLeftCell) void ComponentGrid::onFocusLost() { - const GridEntry* cursorEntry = getCellAt(mCursor); + const GridEntry* cursorEntry {getCellAt(mCursor)}; if (cursorEntry) cursorEntry->component->onFocusLost(); } void ComponentGrid::onFocusGained() { - const GridEntry* cursorEntry = getCellAt(mCursor); + const GridEntry* cursorEntry {getCellAt(mCursor)}; if (cursorEntry) cursorEntry->component->onFocusGained(); } bool ComponentGrid::cursorValid() { - const GridEntry* e = getCellAt(mCursor); + const GridEntry* e {getCellAt(mCursor)}; return (e != nullptr && e->canFocus); } void ComponentGrid::update(int deltaTime) { // Update everything. - const GridEntry* cursorEntry = getCellAt(mCursor); + const GridEntry* cursorEntry {getCellAt(mCursor)}; for (auto it = mCells.cbegin(); it != mCells.cend(); ++it) { if (it->updateType == UPDATE_ALWAYS || (it->updateType == UPDATE_WHEN_SELECTED && cursorEntry == &(*it))) { @@ -438,14 +438,14 @@ void ComponentGrid::render(const glm::mat4& parentTrans) void ComponentGrid::textInput(const std::string& text) { - const GridEntry* selectedEntry = getCellAt(mCursor); + const GridEntry* selectedEntry {getCellAt(mCursor)}; if (selectedEntry != nullptr && selectedEntry->canFocus) selectedEntry->component->textInput(text); } void ComponentGrid::onCursorMoved(glm::ivec2 from, glm::ivec2 to) { - const GridEntry* cell = getCellAt(from); + const GridEntry* cell {getCellAt(from)}; if (cell) cell->component->onFocusLost(); @@ -474,22 +474,22 @@ void ComponentGrid::setCursorTo(const std::shared_ptr& comp) std::vector ComponentGrid::getHelpPrompts() { std::vector prompts; - const GridEntry* e = getCellAt(mCursor); + const GridEntry* e {getCellAt(mCursor)}; if (e) prompts = e->component->getHelpPrompts(); - bool canScrollVert = false; + bool canScrollVert {false}; // If the currently selected cell does not fill the entire Y axis, then check if the cells // above or below are actually focusable as otherwise they should not affect the help prompts. if (mGridSize.y > 1 && e->dim.y < mGridSize.y) { if (e->pos.y - e->dim.y >= 0) { - const GridEntry* cell = getCellAt(glm::ivec2 {e->pos.x, e->pos.y - e->dim.y}); + const GridEntry* cell {getCellAt(glm::ivec2 {e->pos.x, e->pos.y - e->dim.y})}; if (cell != nullptr && cell->canFocus) canScrollVert = true; } if (e->pos.y + e->dim.y < mGridSize.y) { - const GridEntry* cell = getCellAt(glm::ivec2 {e->pos.x, e->pos.y + e->dim.y}); + const GridEntry* cell {getCellAt(glm::ivec2 {e->pos.x, e->pos.y + e->dim.y})}; if (cell != nullptr && cell->canFocus) canScrollVert = true; } @@ -498,7 +498,7 @@ std::vector ComponentGrid::getHelpPrompts() // There is currently no situation in the application where unfocusable cells are located // next to each other horizontally, so this code is good enough. If this changes in the // future, code similar to the the vertical cell handling above needs to be added. - bool canScrollHoriz = (mGridSize.x > 1 && e->dim.x < mGridSize.x); + bool canScrollHoriz {mGridSize.x > 1 && e->dim.x < mGridSize.x}; // Check existing capabilities as indicated by the help prompts, and if the prompts should // be combined into "up/down/left/right" then also remove the single-axis prompts. diff --git a/es-core/src/components/MenuComponent.cpp b/es-core/src/components/MenuComponent.cpp index 0b6e808e1..12336350a 100644 --- a/es-core/src/components/MenuComponent.cpp +++ b/es-core/src/components/MenuComponent.cpp @@ -91,15 +91,15 @@ float MenuComponent::getButtonGridHeight() const void MenuComponent::updateSize() { - const float maxHeight = Renderer::getScreenHeight() * 0.80f; - float height = TITLE_HEIGHT + mList->getTotalRowHeight() + getButtonGridHeight() + - (2.0f * Renderer::getScreenHeightModifier()); + const float maxHeight {Renderer::getScreenHeight() * 0.80f}; + float height {TITLE_HEIGHT + mList->getTotalRowHeight() + getButtonGridHeight() + + (2.0f * Renderer::getScreenHeightModifier())}; if (height > maxHeight) { height = TITLE_HEIGHT + getButtonGridHeight(); - int i = 0; + int i {0}; while (i < mList->size()) { // Add the separator height to the row height so that it also gets properly rendered. - float rowHeight = mList->getRowHeight(i) + (1.0f * Renderer::getScreenHeightModifier()); + float rowHeight {mList->getRowHeight(i) + (1.0f * Renderer::getScreenHeightModifier())}; if (height + rowHeight < maxHeight) height += rowHeight; else @@ -161,11 +161,11 @@ void MenuComponent::updateGrid() std::shared_ptr makeButtonGrid( const std::vector>& buttons) { - std::shared_ptr buttonGrid = - std::make_shared(glm::ivec2 {static_cast(buttons.size()), 2}); + std::shared_ptr buttonGrid { + std::make_shared(glm::ivec2 {static_cast(buttons.size()), 2})}; // Initialize to padding. - float buttonGridWidth = BUTTON_GRID_HORIZ_PADDING * buttons.size(); + float buttonGridWidth {BUTTON_GRID_HORIZ_PADDING * buttons.size()}; for (int i = 0; i < static_cast(buttons.size()); ++i) { buttonGrid->setEntry(buttons.at(i), glm::ivec2 {i, 0}, true, false);