Move Twos grid state and rules into a TwosGrid class - #2477
Open
xhon-pelushi wants to merge 1 commit into
Open
Conversation
The Twos screen held both the game state and the rules that manipulate it alongside its LVGL objects. Move the grid, the score and the functions that act on them into a TwosGrid class, leaving the screen responsible only for drawing. The four swipe branches of OnTouchEvent were the same algorithm written out once per direction. They are replaced by TwosGrid::Slide, which takes the direction as a row/column step, so the sliding and merging rules now exist in one place. Fixes InfiniTimeOrg#1668
|
Build size and comparison to main:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1668.
Per the request in the issue, the grid and the functions that manipulate it move out of the
Twosscreen and into a newTwosGridclass.TwosGridowns the tiles and the score and knows nothing about LVGL;Twoskeeps the styles, the table and the label and just draws whatever the grid currently holds.While moving the code I also collapsed the four swipe branches. They were the same algorithm written out once per direction, differing only in which axis they walked and in which order.
TwosGrid::Slide(rowStep, colStep)takes the direction as a step pair ({0, -1}for left,{1, 0}for down, and so on), so the sliding and merging rules exist in one place instead of four. That is what removes most of the line count here.Two small things came along with the move:
updateGridDisplay→UpdateGridDisplay, and the moved functions are PascalCase, matching the naming used by the rest of the screens. The score label update is split out intoUpdateScoreDisplaybecauseTryMergecan no longer touch LVGL.UpdateGridDisplaygoes fromchar[7]tochar[11]. Reading the tile value into a local made GCC's range analysis visible across thesnprintf, which surfaced a-Wformat-truncationwarning that the old repeated-member-access form hid. 11 bytes holds anyunsigned int, so the warning goes away rather than being suppressed. Without this the file would have gained a warning it did not have before.No behaviour change is intended.
Verification
I do not have the ARM toolchain here, so rather than reason about it I tested the extracted logic directly. Because
TwosGridno longer depends on LVGL, I could compile the old implementation and the new one side by side on the desktop and compare them:2662144 swipes compared, 0 mismatches. Loop order was the part I most expected to have broken, since the original walks columns-outer for horizontal swipes and rows-outer for vertical ones; rows and columns are independent for their respective directions, and the fuzzing agrees.
Also checked:
lvglsubmodule and generatedApps.h(-Wall -Wextra), producing an object file.char[7]issue, which I fixed rather than shipped).clang-format-14, the version CI installs, reports both files clean.What I could not verify is the app running on real hardware or in the simulator, so the LVGL side of
Twosis unexercised beyond compiling. Worth a quick look on a watch or in InfiniSim before merging.