├── .github ├── FUNDING.yml ├── hbappstore.json └── workflows │ └── update_hbappstore.yml ├── .pics ├── banner.png └── screenshot_1.png ├── .gitmodules ├── README.md ├── Makefile ├── LICENSE └── source └── main.cpp /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: ppkantorski 2 | ko_fi: ppkantorski 3 | -------------------------------------------------------------------------------- /.github/hbappstore.json: -------------------------------------------------------------------------------- 1 | {"schemaVersion":1,"label":"hb app store","message":"17k"} 2 | -------------------------------------------------------------------------------- /.pics/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ppkantorski/Tetris-Overlay/HEAD/.pics/banner.png -------------------------------------------------------------------------------- /.pics/screenshot_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ppkantorski/Tetris-Overlay/HEAD/.pics/screenshot_1.png -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "lib/libultrahand"] 2 | path = lib/libultrahand 3 | url = https://github.com/ppkantorski/libultrahand 4 | -------------------------------------------------------------------------------- /.github/workflows/update_hbappstore.yml: -------------------------------------------------------------------------------- 1 | name: Update HB App Store downloads 2 | 3 | on: 4 | schedule: 5 | - cron: "0 */6 * * *" # every 6 hours 6 | workflow_dispatch: 7 | 8 | jobs: 9 | update: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout repo 13 | uses: actions/checkout@v4 14 | 15 | - name: Fetch and format download count 16 | run: | 17 | set -e 18 | JSON_URL="https://switch.cdn.fortheusers.org/repo.json" 19 | 20 | # Fetch download count 21 | count=$(curl -s "$JSON_URL" \ 22 | | jq '.packages | map(select(.name == "TetrisOverlay"))[0].app_dls // 0') 23 | 24 | # Format as compact number (k for thousands) 25 | if [ "$count" -ge 1000 ]; then 26 | display=$(printf "%.0fk" $(echo "$count/1000" | bc -l)) 27 | else 28 | display=$count 29 | fi 30 | 31 | # Write JSON to file 32 | mkdir -p .github 33 | echo "{\"schemaVersion\":1,\"label\":\"hb app store\",\"message\":\"$display\"}" > .github/hbappstore.json 34 | 35 | - name: Commit and push 36 | run: | 37 | git config user.name "github-actions" 38 | git config user.email "github-actions@github.com" 39 | git add .github/hbappstore.json 40 | git diff-index --quiet HEAD || git commit -m "Update download count" 41 | 42 | # Fetch latest remote changes and rebase to avoid push rejection 43 | git fetch origin main 44 | git rebase origin/main || echo "Rebase completed or nothing to rebase" 45 | 46 | # Push changes 47 | git push origin main 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tetris Overlay (HOS 16.0.0+) 2 | [![platform](https://img.shields.io/badge/platform-Switch-898c8c?logo=C++.svg)](https://gbatemp.net/forums/nintendo-switch.283/?prefix_id=44) 3 | [![language](https://img.shields.io/badge/language-C++-ba1632?logo=C++.svg)](https://github.com/topics/cpp) 4 | [![GPLv2 License](https://img.shields.io/badge/license-GPLv2-189c11.svg)](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html) 5 | [![Latest Version](https://img.shields.io/github/v/release/ppkantorski/Tetris-Overlay?label=latest&color=blue)](https://github.com/ppkantorski/Tetris-Overlay/releases/latest) 6 | [![GitHub Downloads](https://img.shields.io/github/downloads/ppkantorski/Tetris-Overlay/total?color=6f42c1)](https://somsubhra.github.io/github-release-stats/?username=ppkantorski&repository=Tetris-Overlay&page=1&per_page=300) 7 | [![HB App Store](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/ppkantorski/Tetris-Overlay/main/.github/hbappstore.json&label=hb%20app%20store&color=6f42c1)](https://hb-app.store/switch/TetrisOverlay) 8 | [![GitHub issues](https://img.shields.io/github/issues/ppkantorski/Tetris-Overlay?color=222222)](https://github.com/ppkantorski/Tetris-Overlay/issues) 9 | [![GitHub stars](https://img.shields.io/github/stars/ppkantorski/Tetris-Overlay)](https://github.com/ppkantorski/Tetris-Overlay/stargazers) 10 | 11 | [![Banner](.pics/banner.png)](https://gbatemp.net/threads/tetris-overlay.661021/) 12 | 13 | An Ultrahand-enhanced Tesla overlay that brings classic Tetris gameplay to the overlay menu. This overlay allows for in-menu gameplay with dynamic UI elements, saving and loading of game state, and high score tracking. 14 | 15 | ## Screenshot 16 | [![Screenshot](.pics/screenshot_1.png)](https://github.com/ppkantorski/Tetris-Overlay/blob/main/.pics/screenshot_1.png?raw=true) 17 | 18 | ## Features 19 | 20 | - **Ultrahand Integration:** Enhanced with Ultrahand libraries for smooth and seamless gameplay with Ultrahand system settings and improved rendering. 21 | - **Classic Tetris Mechanics:** Enjoy traditional Tetris gameplay, including line clears, level progression, and scoring. 22 | - **Dynamic UI:** Provides a responsive interface with previews of the next and stored Tetriminos. 23 | - **Save and Load:** Save your game progress and load previous games seamlessly. 24 | - **Pause and Resume:** Easily pause and resume the game without losing progress. 25 | - **High Score Tracking:** Tracks your highest score across sessions. 26 | - **In-Game Access:** Launch the overlay directly within games using Ultrahand Overlay (or Tesla Menu). 27 | 28 | ## Installation 29 | 30 | 1. Ensure you have a homebrew-enabled Nintendo Switch with [Ultrahand Overlay](https://github.com/ppkantorski/Ultrahand-Overlay) (or Tesla Menu) installed. 31 | 2. Download the latest release of Tetris Overlay from the [Releases](https://github.com/ppkantorski/Tetris-Overlay/releases). 32 | 3. Copy `tetris.ovl` to the `sdmc:/switch/.overlays/` directory on your Nintendo Switch's SD card. 33 | 4. Open the [Ultrahand Overlay](https://github.com/ppkantorski/Ultrahand-Overlay) (or Tesla Menu) and launch the Tetris Overlay. 34 | 35 | ## Controls 36 | 37 | - **D-Pad Left/Right:** Move the Tetrimino left or right. 38 | - **D-Pad Down:** Soft drop the Tetrimino. 39 | - **D-Pad Up:** Hard drop the Tetrimino. 40 | - **A Button:** Rotate the Tetrimino clockwise. 41 | - **B Button:** Rotate the Tetrimino counterclockwise. 42 | - **L Button:** Swap the current Tetrimino with the stored one. 43 | - **Plus (+) Button:** Pause or resume the game. 44 | - **A or Plus (+) on Game Over:** Restart the game. 45 | - **B on Pause:** Exit the game. 46 | 47 | ## Saving and Loading 48 | 49 | - The game state is automatically saved upon pausing or exiting the overlay. 50 | - To load a previous session, start the overlay again. 51 | 52 | ## Building the Project 53 | 54 | ### Prerequisites 55 | 56 | - [DevkitPro](https://devkitpro.org/) with libnx installed. 57 | - Nintendo Switch Homebrew Development Environment. 58 | 59 | ### Building 60 | 61 | 1. Clone the repository and pull the latest overlay libraries from Ultrahand Overlay: 62 | ```bash 63 | git clone https://github.com/ppkantorski/Tetris-Overlay.git 64 | cd Tetris-Overlay 65 | chmod +x ./update_libs.sh 66 | ./update_libs.sh 67 | ``` 68 | The `update_libs.sh` script automates the process of downloading and updating the required `libultra` and `libtesla` libraries from the **Ultrahand Overlay** repository. It ensures the latest versions are correctly placed within the `lib` directory for the project. 69 | 70 | 2. Build the project: 71 | ```bash 72 | make 73 | ``` 74 | 75 | 3. The compiled overlay file (`tetris.ovl`) will be in the project directory. 76 | 77 | ## Contributing 78 | 79 | Contributions are welcome. Fork the repository and create a pull request, or report issues/suggestions via the [Issues](https://github.com/ppkantorski/Tetris-Overlay/issues) section. 80 | 81 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/X8X3VR194) 82 | 83 | ## License 84 | 85 | This project is licensed and distributed under [GPLv2](LICENSE) with a custom library utilizing CC-BY-4.0. 86 | 87 | Copyright (c) 2024 ppkantorski 88 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ################################################################################## 2 | # Makefile for Tetris Overlay 3 | # Author: ppkantorski 4 | # Description: 5 | # This Makefile is used to build the Tetris Overlay homebrew application for 6 | # Nintendo Switch. 7 | # 8 | # For more details and usage instructions, please refer to the project's 9 | # documentation and README.md. 10 | # 11 | # GitHub Repository: https://github.com/ppkantorski/Tetris-Overlay 12 | # 13 | # Licensed under GPLv2 14 | # Copyright (c) 2024 ppkantorski 15 | ################################################################################## 16 | 17 | #--------------------------------------------------------------------------------- 18 | .SUFFIXES: 19 | #--------------------------------------------------------------------------------- 20 | 21 | ifeq ($(strip $(DEVKITPRO)),) 22 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") 23 | endif 24 | 25 | TOPDIR ?= $(CURDIR) 26 | include $(DEVKITPRO)/libnx/switch_rules 27 | 28 | #--------------------------------------------------------------------------------- 29 | # TARGET is the name of the output 30 | # BUILD is the directory where object files & intermediate files will be placed 31 | # SOURCES is a list of directories containing source code 32 | # DATA is a list of directories containing data files 33 | # INCLUDES is a list of directories containing header files 34 | # ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional) 35 | # 36 | # NO_ICON: if set to anything, do not use icon. 37 | # NO_NACP: if set to anything, no .nacp file is generated. 38 | # APP_TITLE is the name of the app stored in the .nacp file (Optional) 39 | # APP_AUTHOR is the author of the app stored in the .nacp file (Optional) 40 | # APP_VERSION is the version of the app stored in the .nacp file (Optional) 41 | # APP_TITLEID is the titleID of the app stored in the .nacp file (Optional) 42 | # ICON is the filename of the icon (.jpg), relative to the project folder. 43 | # If not set, it attempts to use one of the following (in this order): 44 | # - .jpg 45 | # - icon.jpg 46 | # - /default_icon.jpg 47 | # 48 | # CONFIG_JSON is the filename of the NPDM config file (.json), relative to the project folder. 49 | # If not set, it attempts to use one of the following (in this order): 50 | # - .json 51 | # - config.json 52 | # If a JSON file is provided or autodetected, an ExeFS PFS0 (.nsp) is built instead 53 | # of a homebrew executable (.nro). This is intended to be used for sysmodules. 54 | # NACP building is skipped as well. #lib/Atmosphere-libs/libexosphere/source/pmic 55 | #--------------------------------------------------------------------------------- 56 | APP_TITLE := Tetris 57 | APP_AUTHOR := ppkantorski 58 | APP_VERSION := 0.4.5 59 | TARGET := tetris 60 | BUILD := build 61 | SOURCES := source 62 | INCLUDES := source include 63 | NO_ICON := 1 64 | 65 | # This location should reflect where you place the libultrahand directory (lib can vary between projects). 66 | include ${TOPDIR}/lib/libultrahand/ultrahand.mk 67 | 68 | #--------------------------------------------------------------------------------- 69 | # options for code generation 70 | #--------------------------------------------------------------------------------- 71 | ARCH := -march=armv8-a+simd+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE 72 | 73 | CFLAGS := -g -Wall -O3 -ffunction-sections -fdata-sections -flto \ 74 | -fuse-linker-plugin -fomit-frame-pointer -finline-small-functions \ 75 | -fno-strict-aliasing -frename-registers -falign-functions=16 \ 76 | $(ARCH) $(DEFINES) 77 | 78 | CFLAGS += $(INCLUDE) -D__SWITCH__ -DAPP_VERSION="\"$(APP_VERSION)\"" -D_FORTIFY_SOURCE=2 79 | 80 | # Enable appearance overriding 81 | UI_OVERRIDE_PATH := /config/tetris/ 82 | CFLAGS += -DUI_OVERRIDE_PATH="\"$(UI_OVERRIDE_PATH)\"" 83 | 84 | # Enable Widget 85 | USING_WIDGET_DIRECTIVE := 1 # or true 86 | CFLAGS += -DUSING_WIDGET_DIRECTIVE=$(USING_WIDGET_DIRECTIVE) 87 | 88 | # Enable Widget 89 | NO_BACK_KEY_DIRECTIVE := 1 # or true 90 | CFLAGS += -DNO_BACK_KEY_DIRECTIVE=$(NO_BACK_KEY_DIRECTIVE) 91 | 92 | # For theme / wallpaper loading conducted in GUI class method (add to project if theme does not appear) 93 | #INITIALIZE_IN_GUI_DIRECTIVE := 1 94 | #CFLAGS += -DINITIALIZE_IN_GUI_DIRECTIVE=$(INITIALIZE_IN_GUI_DIRECTIVE) 95 | 96 | 97 | 98 | CXXFLAGS := $(CFLAGS) -std=c++26 -Wno-dangling-else -ffast-math -fno-unwind-tables -fno-asynchronous-unwind-tables 99 | 100 | ASFLAGS := $(ARCH) 101 | LDFLAGS += -specs=$(DEVKITPRO)/libnx/switch.specs $(ARCH) -Wl,-Map,$(notdir $*.map) 102 | 103 | LIBS := -lcurl -lz -lmbedtls -lmbedx509 -lmbedcrypto -lnx 104 | 105 | CXXFLAGS += -fno-exceptions -ffunction-sections -fdata-sections -fno-rtti 106 | LDFLAGS += -Wl,--gc-sections -Wl,--as-needed 107 | 108 | # For Ensuring Parallel LTRANS Jobs w/ GCC, make -j6 109 | CXXFLAGS += -flto -fuse-linker-plugin -flto=6 110 | LDFLAGS += -flto=6 111 | 112 | # Add -z notext to LDFLAGS to allow dynamic relocations in read-only segments 113 | #LDFLAGS += -z notext 114 | 115 | # For Ensuring Parallel LTRANS Jobs w/ Clang, make -j6 116 | #CXXFLAGS += -flto -flto-jobs=6 117 | #LDFLAGS += -flto-jobs=6 118 | 119 | #--------------------------------------------------------------------------------- 120 | # list of directories containing libraries, this must be the top level containing 121 | # include and lib 122 | #--------------------------------------------------------------------------------- 123 | LIBDIRS := $(PORTLIBS) $(LIBNX) 124 | 125 | 126 | #--------------------------------------------------------------------------------- 127 | # no real need to edit anything past this point unless you need to add additional 128 | # rules for different file extensions 129 | #--------------------------------------------------------------------------------- 130 | ifneq ($(BUILD),$(notdir $(CURDIR))) 131 | #--------------------------------------------------------------------------------- 132 | 133 | export OUTPUT := $(CURDIR)/$(TARGET) 134 | export TOPDIR := $(CURDIR) 135 | 136 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 137 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 138 | 139 | export DEPSDIR := $(CURDIR)/$(BUILD) 140 | 141 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 142 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 143 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 144 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 145 | 146 | #--------------------------------------------------------------------------------- 147 | # use CXX for linking C++ projects, CC for standard C 148 | #--------------------------------------------------------------------------------- 149 | ifeq ($(strip $(CPPFILES)),) 150 | #--------------------------------------------------------------------------------- 151 | export LD := $(CC) 152 | #--------------------------------------------------------------------------------- 153 | else 154 | #--------------------------------------------------------------------------------- 155 | export LD := $(CXX) 156 | #--------------------------------------------------------------------------------- 157 | endif 158 | #--------------------------------------------------------------------------------- 159 | 160 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) 161 | export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 162 | export OFILES := $(OFILES_BIN) $(OFILES_SRC) 163 | export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) 164 | 165 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 166 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 167 | -I$(CURDIR)/$(BUILD) 168 | 169 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 170 | 171 | ifeq ($(strip $(CONFIG_JSON)),) 172 | jsons := $(wildcard *.json) 173 | ifneq (,$(findstring $(TARGET).json,$(jsons))) 174 | export APP_JSON := $(TOPDIR)/$(TARGET).json 175 | else 176 | ifneq (,$(findstring config.json,$(jsons))) 177 | export APP_JSON := $(TOPDIR)/config.json 178 | endif 179 | endif 180 | else 181 | export APP_JSON := $(TOPDIR)/$(CONFIG_JSON) 182 | endif 183 | 184 | ifeq ($(strip $(ICON)),) 185 | icons := $(wildcard *.jpg) 186 | ifneq (,$(findstring $(TARGET).jpg,$(icons))) 187 | export APP_ICON := $(TOPDIR)/$(TARGET).jpg 188 | else 189 | ifneq (,$(findstring icon.jpg,$(icons))) 190 | export APP_ICON := $(TOPDIR)/icon.jpg 191 | endif 192 | endif 193 | else 194 | export APP_ICON := $(TOPDIR)/$(ICON) 195 | endif 196 | 197 | ifeq ($(strip $(NO_ICON)),) 198 | export NROFLAGS += --icon=$(APP_ICON) 199 | endif 200 | 201 | ifeq ($(strip $(NO_NACP)),) 202 | export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp 203 | endif 204 | 205 | ifneq ($(APP_TITLEID),) 206 | export NACPFLAGS += --titleid=$(APP_TITLEID) 207 | endif 208 | 209 | ifneq ($(ROMFS),) 210 | export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS) 211 | endif 212 | 213 | .PHONY: $(BUILD) clean all 214 | 215 | #--------------------------------------------------------------------------------- 216 | all: $(BUILD) 217 | 218 | 219 | $(BUILD): 220 | @[ -d $@ ] || mkdir -p $@ 221 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 222 | 223 | 224 | #--------------------------------------------------------------------------------- 225 | clean: 226 | @rm -fr $(BUILD) $(TARGET).ovl $(TARGET).nro $(TARGET).nacp $(TARGET).elf 227 | 228 | 229 | #--------------------------------------------------------------------------------- 230 | else 231 | .PHONY: all 232 | 233 | DEPENDS := $(OFILES:.o=.d) 234 | 235 | #--------------------------------------------------------------------------------- 236 | # main targets 237 | #--------------------------------------------------------------------------------- 238 | all : $(OUTPUT).ovl 239 | 240 | $(OUTPUT).ovl: $(OUTPUT).elf $(OUTPUT).nacp 241 | @elf2nro $< $@ $(NROFLAGS) 242 | @echo "built ... $(notdir $(OUTPUT).ovl)" 243 | @printf 'ULTR' >> $@ 244 | @printf "Ultrahand signature has been added.\n" 245 | 246 | 247 | 248 | $(OUTPUT).elf: $(OFILES) 249 | 250 | $(OFILES_SRC): $(HFILES_BIN) 251 | 252 | #--------------------------------------------------------------------------------- 253 | # you need a rule like this for each extension you use as binary data 254 | #--------------------------------------------------------------------------------- 255 | %.bin.o %_bin.h : %.bin 256 | #--------------------------------------------------------------------------------- 257 | @echo $(notdir $<) 258 | @$(bin2o) 259 | 260 | -include $(DEPENDS) 261 | 262 | #--------------------------------------------------------------------------------------- 263 | endif 264 | #--------------------------------------------------------------------------------------- 265 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /source/main.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * File: main.cpp 3 | * Author: ppkantorski 4 | * Description: 5 | * This file contains the main logic for the Tetris Overlay project, 6 | * a graphical overlay implementation of the classic Tetris game for the 7 | * Nintendo Switch. It integrates game state management, rendering, 8 | * and user input handling to provide a complete Tetris experience 9 | * within an overlay. 10 | * 11 | * Key Features: 12 | * - Classic Tetris gameplay mechanics with level and score tracking. 13 | * - Smooth animations and intuitive controls. 14 | * - Save and load game state functionality. 15 | * - Dynamic UI rendering with next and stored Tetrimino previews. 16 | * - Integration with the Tesla Menu system for in-game overlay management. 17 | * 18 | * For the latest updates, documentation, and source code, visit the project's 19 | * GitHub repository: 20 | * (GitHub Repository: https://github.com/ppkantorski/Tetris-Overlay) 21 | * 22 | * Note: This notice is part of the project's documentation and must remain intact. 23 | * 24 | * Licensed under GPLv2 25 | * Copyright (c) 2024 ppkantorski 26 | ********************************************************************************/ 27 | 28 | #define NDEBUG 29 | #define STBTT_STATIC 30 | #define TESLA_INIT_IMPL 31 | 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | using namespace ult; 44 | 45 | std::mutex boardMutex; // Declare a mutex for board access 46 | std::mutex particleMutex; 47 | 48 | bool isGameOver = false; 49 | bool firstLoad = false; // Track if it's the first frame after loading 50 | 51 | struct Particle { 52 | float x, y; // Position 53 | float vx, vy; // Velocity 54 | float life; // Lifespan 55 | float alpha; // Transparency (fades out) 56 | }; 57 | 58 | 59 | std::vector particles; 60 | 61 | 62 | // Define the Tetrimino shapes 63 | constexpr std::array, 7> tetriminoShapes = {{ 64 | // I 65 | { 0,0,0,0, 66 | 1,1,1,1, 67 | 0,0,0,0, 68 | 0,0,0,0 }, 69 | 70 | // J 71 | { 1,0,0,0, 72 | 1,1,1,0, 73 | 0,0,0,0, 74 | 0,0,0,0 }, 75 | 76 | // L 77 | { 0,0,1,0, 78 | 1,1,1,0, 79 | 0,0,0,0, 80 | 0,0,0,0 }, 81 | 82 | // O 83 | { 1,1,0,0, 84 | 1,1,0,0, 85 | 0,0,0,0, 86 | 0,0,0,0 }, 87 | 88 | // S 89 | { 0,1,1,0, 90 | 1,1,0,0, 91 | 0,0,0,0, 92 | 0,0,0,0 }, 93 | 94 | // T 95 | { 0,1,0,0, 96 | 1,1,1,0, 97 | 0,0,0,0, 98 | 0,0,0,0 }, 99 | 100 | // Z 101 | { 1,1,0,0, 102 | 0,1,1,0, 103 | 0,0,0,0, 104 | 0,0,0,0 } 105 | }}; 106 | 107 | // Adjusted rotation centers based on official Tetris SRS 108 | constexpr std::array, 7> rotationCenters = {{ 109 | {1.5f, 1.5f}, // I piece (rotating around the second cell in a 4x4 grid) 110 | {1, 1}, // J piece 111 | {1, 1}, // L piece 112 | {1, 1}, // O piece 113 | {1, 1}, // S piece 114 | {1, 1}, // T piece 115 | {1, 1} // Z piece 116 | }}; 117 | 118 | // Wall kicks for I piece (SRS) 119 | constexpr std::array, 5>, 4> wallKicksI = {{ 120 | // 0 -> 1, 1 -> 0 121 | {{ {0, 0}, {-2, 0}, {1, 0}, {-2, -1}, {1, 2} }}, 122 | // 1 -> 2, 2 -> 1 123 | {{ {0, 0}, {-1, 0}, {2, 0}, {-1, 2}, {2, -1} }}, 124 | // 2 -> 3, 3 -> 2 125 | {{ {0, 0}, {2, 0}, {-1, 0}, {2, 1}, {-1, -2} }}, 126 | // 3 -> 0, 0 -> 3 127 | {{ {0, 0}, {1, 0}, {-2, 0}, {1, -2}, {-2, 1} }} 128 | }}; 129 | 130 | // Wall kicks for J, L, S, T, Z pieces (SRS) 131 | constexpr std::array, 5>, 4> wallKicksJLSTZ = {{ 132 | // 0 -> 1, 1 -> 0 133 | {{ {0, 0}, {-1, 0}, {-1, -1}, {0, 2}, {-1, 2} }}, 134 | // 1 -> 2, 2 -> 1 135 | {{ {0, 0}, {1, 0}, {1, 1}, {0, -2}, {1, -2} }}, 136 | // 2 -> 3, 3 -> 2 137 | {{ {0, 0}, {1, 0}, {1, -1}, {0, 2}, {1, 2} }}, 138 | // 3 -> 0, 0 -> 3 139 | {{ {0, 0}, {-1, 0}, {-1, 1}, {0, -2}, {-1, -2} }} 140 | }}; 141 | 142 | 143 | // Define colors for each Tetrimino 144 | constexpr std::array tetriminoColors = {{ 145 | {0x0, 0xE, 0xF, 0xF}, // Cyan - I (R=0, G=F, B=F, A=F) 146 | {0x2, 0x2, 0xF, 0xF}, // Blue - J (R=0, G=0, B=F, A=F) 147 | {0xF, 0xA, 0x0, 0xF}, // Orange - L (R=F, G=A, B=0, A=F) 148 | {0xE, 0xE, 0x0, 0xF}, // Yellow - O (R=F, G=F, B=0, A=F) 149 | {0x0, 0xE, 0x0, 0xF}, // Green - S (R=0, G=F, B=0, A=F) 150 | {0x8, 0x0, 0xF, 0xF}, // Purple - T (R=8, G=0, B=F, A=F) 151 | {0xE, 0x0, 0x0, 0xF} // Red - Z (R=F, G=0, B=0, A=F) 152 | }}; 153 | 154 | // Board dimensions 155 | constexpr int BOARD_WIDTH = 10; 156 | constexpr int BOARD_HEIGHT = 20; 157 | 158 | // Updated helper function to get rotated index 159 | int getRotatedIndex(int type, int i, int j, int rotation) { 160 | // Ensure i and j are within bounds 161 | if (i < 0 || i >= 4 || j < 0 || j >= 4) return -1; 162 | 163 | if (type == 0) { // I piece 164 | int rotatedIndex = 0; 165 | switch (rotation) { 166 | case 0: rotatedIndex = i * 4 + j; break; 167 | case 1: rotatedIndex = (3 - i) + j * 4; break; 168 | case 2: rotatedIndex = (3 - j) + (3 - i) * 4; break; 169 | case 3: rotatedIndex = i + (3 - j) * 4; break; 170 | } 171 | return rotatedIndex; 172 | } else if (type == 3) { // O piece doesn't rotate 173 | return i * 4 + j; 174 | } else { 175 | // General case for other pieces 176 | const float centerX = rotationCenters[type].first; 177 | const float centerY = rotationCenters[type].second; 178 | const int relX = j - centerX; 179 | const int relY = i - centerY; 180 | int rotatedX, rotatedY; 181 | 182 | switch (rotation) { 183 | case 0: rotatedX = relX; rotatedY = relY; break; 184 | case 1: rotatedX = -relY; rotatedY = relX; break; 185 | case 2: rotatedX = -relX; rotatedY = -relY; break; 186 | case 3: rotatedX = relY; rotatedY = -relX; break; 187 | } 188 | 189 | const int finalX = static_cast(round(rotatedX + centerX)); 190 | const int finalY = static_cast(round(rotatedY + centerY)); 191 | 192 | // Ensure the rotated index is within the 4x4 grid 193 | if (finalX < 0 || finalX >= 4 || finalY < 0 || finalY >= 4) return -1; 194 | return finalY * 4 + finalX; 195 | } 196 | } 197 | 198 | 199 | 200 | 201 | 202 | struct Tetrimino { 203 | int x, y; 204 | int type; 205 | int rotation; 206 | Tetrimino(int t) : x(BOARD_WIDTH / 2 - 2), y(0), type(t), rotation(0) {} 207 | }; 208 | 209 | // Function to check if the current position of a Tetrimino is valid 210 | bool isPositionValid(const Tetrimino& tet, const std::array, BOARD_HEIGHT>& board) { 211 | int rotatedIndex; 212 | int x, y; 213 | for (int i = 0; i < 4; ++i) { 214 | for (int j = 0; j < 4; ++j) { 215 | rotatedIndex = getRotatedIndex(tet.type, i, j, tet.rotation); 216 | 217 | // Only check cells that contain a block 218 | if (tetriminoShapes[tet.type][rotatedIndex] != 0) { 219 | x = tet.x + j; 220 | y = tet.y + i; 221 | 222 | // Check if x and y are within the bounds of the board horizontally 223 | if (x < 0 || x >= BOARD_WIDTH) { 224 | return false; // Invalid if out of bounds 225 | } 226 | 227 | // Allow blocks above the board but not below the bottom 228 | if (y >= BOARD_HEIGHT) { 229 | return false; // Invalid if out of bounds vertically 230 | } 231 | 232 | // If the block is above the visible board, ignore it 233 | if (y < 0) { 234 | continue; // Skip rows above the board 235 | } 236 | 237 | // Check if the block space is occupied 238 | if (board[y][x] != 0) { 239 | return false; // Invalid if space is occupied 240 | } 241 | } 242 | } 243 | } 244 | return true; // Position is valid 245 | } 246 | 247 | 248 | 249 | 250 | float countOffset = 0.0f; 251 | float counter; 252 | 253 | // Helper function to calculate where the Tetrimino will land if hard dropped 254 | int calculateDropDistance(const Tetrimino& tet, const std::array, BOARD_HEIGHT>& board) { 255 | int dropDistance = 0; 256 | Tetrimino tempTetrimino = tet; // Create a temporary copy for simulation 257 | while (isPositionValid(tempTetrimino, board)) { 258 | tempTetrimino.y += 1; // Move down one row 259 | dropDistance++; 260 | } 261 | return std::max(dropDistance - 1, 0); // Ensure the dropDistance doesn't go negative 262 | } 263 | 264 | 265 | class TetrisElement : public tsl::elm::Element { 266 | public: 267 | static bool paused; 268 | static uint64_t maxHighScore; // Change to a larger data type 269 | bool gameOver = false; // Add this line 270 | 271 | // Variables for line clear text animation 272 | std::string linesClearedText; // Text to show (Single, Double, etc.) 273 | int linesClearedScore; 274 | 275 | float fadeAlpha = 0.0f; // Alpha value for fade-in/fade-out 276 | bool showText = false; // Flag to control when to show the text 277 | int clearedLinesYPosition = 0; // Y-position of cleared lines to center text 278 | std::chrono::time_point textStartTime; 279 | 280 | // Rain effect variables for Game Over 281 | std::chrono::time_point lastRainSpawn; 282 | static constexpr int RAIN_SPAWN_INTERVAL_MS = 50; // Spawn new rain particles every 50ms 283 | 284 | TetrisElement(u16 w, u16 h, std::array, BOARD_HEIGHT> *board, 285 | Tetrimino *current, Tetrimino *next, Tetrimino *stored, 286 | Tetrimino *next1, Tetrimino *next2) 287 | : board(board), currentTetrimino(current), nextTetrimino(next), 288 | storedTetrimino(stored), nextTetrimino1(next1), nextTetrimino2(next2), 289 | _w(w), _h(h) {} 290 | 291 | virtual void draw(tsl::gfx::Renderer* renderer) override { 292 | // Center the board in the frame 293 | const int boardWidthInPixels = BOARD_WIDTH * _w; 294 | const int boardHeightInPixels = BOARD_HEIGHT * _h; 295 | const int offsetX = (this->getWidth() - boardWidthInPixels) / 2; 296 | const int offsetY = (this->getHeight() - boardHeightInPixels) / 2; 297 | 298 | 299 | // Define the semi-transparent black background color 300 | static constexpr tsl::Color overlayColor = tsl::Color({0x0, 0x0, 0x0, 0x8}); // Semi-transparent black color 301 | 302 | // Draw the black background rectangle (slightly larger than the frame) 303 | static constexpr int backgroundPadding = 4; // Padding around the frame for the black background 304 | renderer->drawRect(offsetX - backgroundPadding, offsetY - backgroundPadding, 305 | boardWidthInPixels + 2 * backgroundPadding, boardHeightInPixels + 2 * backgroundPadding, a(overlayColor)); 306 | 307 | 308 | // Draw the board frame 309 | static constexpr tsl::Color frameColor = tsl::Color({0xF, 0xF, 0xF, 0xF}); // White color for frame 310 | static constexpr int frameThickness = 2; 311 | 312 | // Top line 313 | renderer->drawRect(offsetX - frameThickness, offsetY - frameThickness, BOARD_WIDTH * _w + 2 * frameThickness, frameThickness, frameColor); 314 | // Bottom line 315 | renderer->drawRect(offsetX - frameThickness, offsetY + BOARD_HEIGHT * _h, BOARD_WIDTH * _w + 2 * frameThickness, frameThickness, frameColor); 316 | // Left line 317 | renderer->drawRect(offsetX - frameThickness, offsetY - frameThickness, frameThickness, BOARD_HEIGHT * _h + 2 * frameThickness, frameColor); 318 | // Right line 319 | renderer->drawRect(offsetX + BOARD_WIDTH * _w, offsetY - frameThickness, frameThickness, BOARD_HEIGHT * _h + 2 * frameThickness, frameColor); 320 | 321 | 322 | static constexpr int innerPadding = 3; // Adjust this to control the inner rectangle size 323 | 324 | // Draw the board 325 | int drawX, drawY; 326 | tsl::Color innerColor(0), outerColor(0); 327 | tsl::Color highlightColor(0); 328 | for (int y = 0; y < BOARD_HEIGHT; ++y) { 329 | for (int x = 0; x < BOARD_WIDTH; ++x) { 330 | if ((*board)[y][x] != 0) { 331 | drawX = offsetX + x * _w; 332 | drawY = offsetY + y * _h; 333 | 334 | // Get the color for the current block (this will be the inner block color) 335 | innerColor = tetriminoColors[(*board)[y][x] - 1]; 336 | 337 | // Calculate a darker shade for the outer block 338 | outerColor = { 339 | static_cast(innerColor.r * 0xC / 0xF), // Slightly darker, closer to 60% brightness 340 | static_cast(innerColor.g * 0xC / 0xF), 341 | static_cast(innerColor.b * 0xC / 0xF), 342 | static_cast(innerColor.a) // Ensure this is within the range of 0-15 343 | }; 344 | 345 | // Draw the outer block (darker color) 346 | renderer->drawRect(drawX, drawY, _w, _h, outerColor); 347 | 348 | // Draw the inner block (smaller rectangle with original color) 349 | renderer->drawRect(drawX + innerPadding, drawY + innerPadding, _w - 2 * innerPadding, _h - 2 * innerPadding, innerColor); 350 | 351 | // Highlight at the top-left corner (lighter shade for the inner block) 352 | highlightColor = { 353 | static_cast(std::min(innerColor.r + 0x4, 0xF)), // Lighter shade for highlight 354 | static_cast(std::min(innerColor.g + 0x4, 0xF)), 355 | static_cast(std::min(innerColor.b + 0x4, 0xF)), 356 | static_cast(innerColor.a) // Ensure this is within the range of 0-15 357 | }; 358 | 359 | renderer->drawRect(drawX + innerPadding, drawY + innerPadding, _w / 4, _h / 4, highlightColor); 360 | } 361 | } 362 | } 363 | 364 | 365 | score.str(std::string()); 366 | score << "Score\n" << getScore(); 367 | 368 | static constexpr auto whiteColor = tsl::Color({0xF, 0xF, 0xF, 0xF}); 369 | 370 | renderer->drawString(score.str().c_str(), false, 64, 124, 20, whiteColor); 371 | 372 | highScore.str(std::string()); 373 | highScore << "High Score\n" << maxHighScore; 374 | renderer->drawString(highScore.str().c_str(), false, 268, 124, 20, whiteColor); 375 | 376 | 377 | // Draw the stored Tetrimino 378 | drawStoredTetrimino(renderer, offsetX - 61, offsetY); // Adjust the position to fit on the left side 379 | 380 | // Draw the next Tetrimino preview 381 | drawNextTetrimino(renderer, offsetX + BOARD_WIDTH * _w + 12, offsetY); 382 | 383 | drawNextTwoTetriminos(renderer, offsetX + BOARD_WIDTH * _w + 12, offsetY + BORDER_HEIGHT + 12); 384 | 385 | renderer->drawString("", false, offsetX - 85, offsetY + (BORDER_HEIGHT + 12)*0.5 +1, 18, whiteColor); 386 | 387 | renderer->drawString("", false, offsetX + BOARD_WIDTH * _w + 64, offsetY + (BORDER_HEIGHT + 12)*0.5, 18, whiteColor); 388 | renderer->drawString("", false, offsetX + BOARD_WIDTH * _w + 64, offsetY + (BORDER_HEIGHT + 12)*1.5, 18, whiteColor); 389 | renderer->drawString("", false, offsetX + BOARD_WIDTH * _w + 64, offsetY + (BORDER_HEIGHT + 12)*2.5, 18, whiteColor); 390 | 391 | // Draw the number of lines cleared 392 | ult::StringStream linesStr; 393 | linesStr << "Lines\n" << linesCleared; 394 | renderer->drawString(linesStr.str().c_str(), false, offsetX + BOARD_WIDTH * _w + 14, offsetY + (BORDER_HEIGHT + 12)*3 + 18, 18, whiteColor); 395 | 396 | // Draw the current level 397 | ult::StringStream levelStr; 398 | levelStr << "Level\n" << level; 399 | renderer->drawString(levelStr.str().c_str(), false, offsetX + BOARD_WIDTH * _w + 14, offsetY + (BORDER_HEIGHT + 12)*3 + 63, 18, whiteColor); 400 | 401 | 402 | renderer->drawString("", false, 74, offsetY + 74, 18, whiteColor); 403 | 404 | std::lock_guard lock(boardMutex); // Lock the mutex while rendering 405 | 406 | // Draw the current Tetrimino 407 | drawTetrimino(renderer, *currentTetrimino, offsetX, offsetY); 408 | 409 | 410 | // Update the particles 411 | updateParticles(offsetX, offsetY); 412 | if (!gameOver) { 413 | drawParticles(renderer, offsetX, offsetY); 414 | } 415 | 416 | 417 | static std::chrono::time_point gameOverStartTime; // Track the time when game over starts 418 | static bool gameOverTextDisplayed = false; // Track if the game over text is displayed after the delay 419 | 420 | // Draw score and status text 421 | if (gameOver || paused) { 422 | // Draw a semi-transparent black overlay over the board 423 | renderer->drawRect(offsetX, offsetY, boardWidthInPixels, boardHeightInPixels, tsl::Color({0x0, 0x0, 0x0, 0xA})); 424 | 425 | // Calculate the center position of the board 426 | const int centerX = offsetX + (BOARD_WIDTH * _w) / 2; 427 | const int centerY = offsetY + (BOARD_HEIGHT * _h) / 2; 428 | 429 | 430 | 431 | if (gameOver) { 432 | // If this is the first frame or the game was loaded into a game over state, skip the delay 433 | if (firstLoad) { 434 | gameOverTextDisplayed = true; 435 | firstLoad = false; 436 | } 437 | 438 | // If the game over text has not been displayed yet, start the timer 439 | if (!gameOverTextDisplayed) { 440 | if (gameOverStartTime == std::chrono::time_point()) { 441 | // Store the time when game over was triggered 442 | gameOverStartTime = std::chrono::steady_clock::now(); 443 | } 444 | 445 | // Calculate the time since game over was triggered 446 | const auto elapsedTime = std::chrono::steady_clock::now() - gameOverStartTime; 447 | 448 | // If 0.5 seconds have passed, display the "Game Over" text 449 | if (elapsedTime >= std::chrono::milliseconds(500)) { 450 | gameOverTextDisplayed = true; 451 | lastRainSpawn = std::chrono::steady_clock::now(); // Initialize rain spawn timer 452 | } 453 | } 454 | 455 | // If the game over text is set to be displayed, draw it 456 | if (gameOverTextDisplayed) { 457 | // Set the text color to red 458 | static constexpr tsl::Color redColor = tsl::Color({0xF, 0x0, 0x0, 0xF}); 459 | 460 | // Calculate text width to center the text 461 | const int textWidth = tsl::gfx::calculateStringWidth("Game Over", 24); 462 | const int textX = centerX - textWidth / 2; 463 | 464 | // Draw "Game Over" at the center of the board 465 | renderer->drawString("Game Over", false, textX, centerY, 24, redColor); 466 | 467 | // Create rain particles periodically 468 | const auto currentTime = std::chrono::steady_clock::now(); 469 | const auto timeSinceLastRain = std::chrono::duration_cast( 470 | currentTime - lastRainSpawn 471 | ); 472 | 473 | if (timeSinceLastRain.count() >= RAIN_SPAWN_INTERVAL_MS) { 474 | createRainParticles(textX, textWidth, centerY, offsetX, offsetY); 475 | lastRainSpawn = currentTime; 476 | } 477 | // Draw rain particles ON TOP of the black overlay 478 | drawParticles(renderer, offsetX, offsetY); 479 | } 480 | 481 | } else if (paused) { 482 | // Set the text color to green 483 | static constexpr tsl::Color greenColor = tsl::Color({0x0, 0xF, 0x0, 0xF}); 484 | 485 | // Calculate text width to center the text 486 | const int textWidth = tsl::gfx::calculateStringWidth("Paused", 24); 487 | 488 | // Draw "Paused" at the center of the board 489 | renderer->drawString("Paused", false, centerX - textWidth / 2, centerY, 24, greenColor); 490 | } 491 | } 492 | if (!gameOver) { 493 | firstLoad = false; 494 | gameOverTextDisplayed = false; 495 | gameOverStartTime = std::chrono::time_point(); 496 | } 497 | 498 | 499 | // Draw the lines-cleared text with smooth sine wave-based color effect for "Tetris" and other lines 500 | if (showText) { 501 | 502 | 503 | // Calculate the center position of the board 504 | const int centerX = offsetX + (BOARD_WIDTH * _w) / 2; 505 | const int centerY = offsetY + (BOARD_HEIGHT * _h) / 2; 506 | 507 | renderer->drawRect(offsetX, centerY - 22, boardWidthInPixels, 26, tsl::Color({0x0, 0x0, 0x0, 0x5})); 508 | 509 | // Calculate text width to center the text 510 | const std::string scoreLine = "+" + std::to_string(linesClearedScore); 511 | const int textWidth = tsl::gfx::calculateStringWidth(scoreLine, 20); 512 | renderer->drawString(scoreLine, false, centerX - textWidth / 2, centerY, 20, tsl::Color({0x0, 0xF, 0x0, 0xF})); 513 | 514 | 515 | const auto currentTime = std::chrono::steady_clock::now(); 516 | std::chrono::duration elapsedTime = currentTime - textStartTime; 517 | 518 | // Define the durations for each phase 519 | static constexpr float scrollInDuration = 300.0f; // 0.3 seconds to scroll in 520 | static constexpr float pauseDuration = 1000.0f; // 1 second pause 521 | static constexpr float scrollOutDuration = 300.0f; // 0.3 seconds to scroll out 522 | const float totalDuration = scrollInDuration + pauseDuration + scrollOutDuration; 523 | 524 | // Calculate board dimensions 525 | const int boardWidthInPixels = BOARD_WIDTH * _w +2; // +2 to account for padding 526 | const int boardHeightInPixels = BOARD_HEIGHT * _h; 527 | const int offsetX = (this->getWidth() - boardWidthInPixels) / 2; // Horizontal offset to center the board 528 | const int offsetY = (this->getHeight() - boardHeightInPixels) / 2; // Vertical offset to center the board 529 | 530 | // Font size for non-Tetris text 531 | static constexpr int regularFontSize = 20; 532 | static constexpr int dynamicFontSize = 24; 533 | 534 | // Calculate the Y position of the text (vertically centered on the board) 535 | const int textY = offsetY + (boardHeightInPixels / 2); 536 | 537 | // Calculate the X position of the text based on the phase 538 | int textX; 539 | int totalTextWidth = 0; 540 | 541 | // For "Tetris" and "2x Tetris", we need to handle the different font sizes and effects 542 | if (linesClearedText.find("x Tetris") != std::string::npos) { 543 | // Extract the prefix (e.g., "2x ", "10x ") 544 | size_t xPos = linesClearedText.find("x Tetris"); 545 | std::string prefix = linesClearedText.substr(0, xPos + 2); // Get the "2x " or "10x " 546 | std::string remainingText = "Tetris"; // The remaining part is always "Tetris" 547 | 548 | int prefixWidth = tsl::gfx::calculateStringWidth(prefix.c_str(), regularFontSize); 549 | int tetrisWidth = tsl::gfx::calculateStringWidth(remainingText.c_str(), dynamicFontSize); 550 | totalTextWidth = prefixWidth + tetrisWidth + 9; 551 | 552 | } else if (linesClearedText == "Tetris") { 553 | totalTextWidth = tsl::gfx::calculateStringWidth("Tetris", dynamicFontSize) + 12; 554 | 555 | } else if (linesClearedText.find("\n") != std::string::npos) { 556 | // Handle multiline text (e.g., "T-Spin\nSingle") 557 | std::vector lines = splitString(linesClearedText, "\n"); 558 | int maxLineWidth = 0; 559 | 560 | int lineWidth; 561 | // Calculate the maximum width among the lines 562 | for (const std::string &line : lines) { 563 | lineWidth = tsl::gfx::calculateStringWidth(line.c_str(), regularFontSize); 564 | if (lineWidth > maxLineWidth) { 565 | maxLineWidth = lineWidth; 566 | } 567 | } 568 | totalTextWidth = maxLineWidth + 18; // Adjust the total width to include padding 569 | } else { 570 | totalTextWidth = tsl::gfx::calculateStringWidth(linesClearedText.c_str(), regularFontSize) + 18; 571 | } 572 | 573 | // Handle the sliding phases 574 | if (elapsedTime.count() < scrollInDuration) { 575 | const float progress = elapsedTime.count() / scrollInDuration; 576 | textX = offsetX - (progress) * totalTextWidth; // Move left from hidden to fully visible 577 | } else if (elapsedTime.count() < scrollInDuration + pauseDuration) { 578 | textX = offsetX - totalTextWidth; // Fully visible, just to the left of the gameboard 579 | } else if (elapsedTime.count() < totalDuration) { 580 | const float progress = (elapsedTime.count() - scrollInDuration - pauseDuration) / scrollOutDuration; 581 | textX = offsetX - totalTextWidth + progress * totalTextWidth; // Move right, getting scissored 582 | } else { 583 | // End the animation after the total duration 584 | showText = false; 585 | return; 586 | } 587 | 588 | // Enable scissoring to clip the text at the left edge of the gameboard 589 | renderer->enableScissoring(0, offsetY, offsetX, boardHeightInPixels); 590 | 591 | static constexpr tsl::Color textColor(0xF, 0xF, 0xF, 0xF); // White text for non-Tetris strings 592 | const auto currentTimeCount = std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); 593 | static auto dynamicLogoRGB1 = tsl::RGB888("#6929ff"); 594 | static auto dynamicLogoRGB2 = tsl::RGB888("#fff429"); 595 | countOffset = 0.0f; 596 | 597 | static tsl::Color highlightColor(0); 598 | float counter, transitionProgress; 599 | 600 | int charWidth; 601 | // Handle "2x Tetris" special case 602 | if (linesClearedText.find("x Tetris") != std::string::npos) { 603 | //std::string prefix = "2x "; 604 | const size_t xPos = linesClearedText.find("x Tetris"); 605 | const std::string prefix = linesClearedText.substr(0, xPos + 2); // Get the "2x " or "10x " 606 | const int prefixWidth = tsl::gfx::calculateStringWidth(prefix.c_str(), regularFontSize); 607 | tsl::Color whiteColor(0xF, 0xF, 0xF, 0xF); 608 | renderer->drawString(prefix.c_str(), false, textX, textY, regularFontSize, whiteColor); 609 | textX += prefixWidth; 610 | 611 | static const std::string remainingText = "Tetris"; 612 | 613 | for (char letter : remainingText) { 614 | counter = (2 * ult::M_PI * (fmod(currentTimeCount / 4.0, 2.0) + countOffset) / 2.0); 615 | transitionProgress = std::sin(3.0 * (counter - (2.0 * ult::M_PI / 3.0))); 616 | 617 | highlightColor = { 618 | static_cast((dynamicLogoRGB2.r - dynamicLogoRGB1.r) * (transitionProgress + 1.0) / 2.0 + dynamicLogoRGB1.r), 619 | static_cast((dynamicLogoRGB2.g - dynamicLogoRGB1.g) * (transitionProgress + 1.0) / 2.0 + dynamicLogoRGB1.g), 620 | static_cast((dynamicLogoRGB2.b - dynamicLogoRGB1.b) * (transitionProgress + 1.0) / 2.0 + dynamicLogoRGB1.b), 621 | 15 // Alpha remains constant, or you can interpolate it as well 622 | }; 623 | 624 | std::string charStr(1, letter); 625 | charWidth = tsl::gfx::calculateStringWidth(charStr.c_str(), dynamicFontSize); 626 | renderer->drawString(charStr.c_str(), false, textX, textY, dynamicFontSize, highlightColor); 627 | textX += charWidth; 628 | countOffset -= 0.2f; 629 | } 630 | } else if (linesClearedText == "Tetris") { 631 | // Handle "Tetris" with dynamic color effect 632 | for (char letter : linesClearedText) { 633 | counter = (2 * ult::M_PI * (fmod(currentTimeCount / 4.0, 2.0) + countOffset) / 2.0); 634 | transitionProgress = std::sin(3.0 * (counter - (2.0 * ult::M_PI / 3.0))); 635 | 636 | highlightColor = { 637 | static_cast((dynamicLogoRGB2.r - dynamicLogoRGB1.r) * (transitionProgress + 1.0) / 2.0 + dynamicLogoRGB1.r), 638 | static_cast((dynamicLogoRGB2.g - dynamicLogoRGB1.g) * (transitionProgress + 1.0) / 2.0 + dynamicLogoRGB1.g), 639 | static_cast((dynamicLogoRGB2.b - dynamicLogoRGB1.b) * (transitionProgress + 1.0) / 2.0 + dynamicLogoRGB1.b), 640 | 15 // Alpha remains constant, or you can interpolate it as well 641 | }; 642 | 643 | std::string charStr(1, letter); 644 | charWidth = tsl::gfx::calculateStringWidth(charStr.c_str(), dynamicFontSize); 645 | renderer->drawString(charStr.c_str(), false, textX, textY, dynamicFontSize, highlightColor); 646 | textX += charWidth; 647 | countOffset -= 0.2f; 648 | } 649 | } else if (linesClearedText.find("\n") != std::string::npos) { 650 | // Handle multiline text (e.g., "T-Spin\nSingle") 651 | std::vector lines = splitString(linesClearedText, "\n"); 652 | const int lineSpacing = regularFontSize + 4; 653 | const int totalHeight = lines.size() * lineSpacing; 654 | int startY = textY - (totalHeight / 2); 655 | 656 | // Find the maximum width 657 | int maxLineWidth = 0; 658 | int lineWidth; 659 | for (const std::string &line : lines) { 660 | lineWidth = tsl::gfx::calculateStringWidth(line.c_str(), regularFontSize); 661 | if (lineWidth > maxLineWidth) { 662 | maxLineWidth = lineWidth; 663 | } 664 | } 665 | int centeredTextX; 666 | // Draw each line centered based on max width 667 | for (const std::string &line : lines) { 668 | lineWidth = tsl::gfx::calculateStringWidth(line.c_str(), regularFontSize); 669 | centeredTextX = textX + (maxLineWidth - lineWidth) / 2; // Center each line based on the max width 670 | renderer->drawString(line.c_str(), false, centeredTextX, startY, regularFontSize, textColor); 671 | startY += lineSpacing; 672 | } 673 | } else { 674 | // Handle single-line text like "Single", "Double" 675 | renderer->drawString(linesClearedText.c_str(), false, textX, textY, regularFontSize, textColor); 676 | } 677 | 678 | // Disable scissoring after drawing 679 | renderer->disableScissoring(); 680 | } 681 | } 682 | 683 | virtual void layout(u16 parentX, u16 parentY, u16 parentWidth, u16 parentHeight) override { 684 | // Define layout boundaries 685 | this->setBoundaries(parentX, parentY, parentWidth, parentHeight); 686 | } 687 | 688 | void updateParticles(int offsetX, int offsetY) { 689 | std::lock_guard lock(particleMutex); // Lock when modifying the particle list 690 | 691 | bool allParticlesExpired = true; 692 | 693 | // Update all particles and check if all of them are expired 694 | for (auto& particle : particles) { 695 | particle.x += particle.vx; 696 | particle.y += particle.vy; 697 | particle.alpha -= 0.04f; 698 | particle.life -= 0.02f; 699 | 700 | // Ensure the particle stays within bounds of the entire screen (448x720) 701 | if (particle.x + offsetX < 0 || particle.x + offsetX > 448 || particle.y + offsetY < 0 || particle.y + offsetY > 720) { 702 | particle.life = 0; // Mark the particle as dead if out of bounds 703 | } 704 | 705 | // If any particle is still alive, we won't remove the vector 706 | if (particle.life > 0.0f && particle.alpha > 0.0f) { 707 | allParticlesExpired = false; 708 | } 709 | } 710 | 711 | // If all particles are expired (alpha <= 0 or life <= 0), clear the entire vector 712 | if (allParticlesExpired) { 713 | particles.clear(); // Clear the vector in one bulk operation 714 | } 715 | } 716 | 717 | void createRainParticles(int textX, int textWidth, int textY, int offsetX, int offsetY) { 718 | std::lock_guard lock(particleMutex); 719 | 720 | // Spawn 3-5 particles across the text width 721 | const int particleCount = 3 + rand() % 3; 722 | 723 | for (int i = 0; i < particleCount; ++i) { 724 | // Random X position across the text width (convert from screen coords to board coords) 725 | const float startX = (textX - offsetX) + (rand() % textWidth); 726 | const float startY = (textY - offsetY) + 10; // Start just below the text (convert to board coords) 727 | 728 | // Slight horizontal drift and consistent downward velocity 729 | const float horizontalDrift = (rand() % 100 / 100.0f - 0.5f) * 0.5f; // Very slight drift 730 | const float downwardVelocity = 2.0f + (rand() % 100 / 100.0f); // 2-3 pixels per frame 731 | 732 | Particle particle = { 733 | startX, 734 | startY, 735 | horizontalDrift, 736 | downwardVelocity, 737 | 1.0f, // Lifespan 738 | 1.0f // Alpha (fully visible) 739 | }; 740 | particles.push_back(particle); 741 | } 742 | } 743 | 744 | uint64_t getScore() { 745 | return scoreValue; 746 | } 747 | 748 | void setScore(uint64_t s) { 749 | scoreValue = s; 750 | if (scoreValue > maxHighScore) { 751 | maxHighScore = scoreValue; // Update the max high score 752 | } 753 | } 754 | 755 | 756 | int getLinesCleared() { return linesCleared; } 757 | int getLevel() { return level; } 758 | void setLinesCleared(int lines) { linesCleared = lines; } 759 | void setLevel(int lvl) { level = lvl; } 760 | 761 | private: 762 | std::array, BOARD_HEIGHT> *board; 763 | Tetrimino *currentTetrimino; 764 | Tetrimino *nextTetrimino; 765 | Tetrimino *storedTetrimino; 766 | Tetrimino *nextTetrimino1; // First next Tetrimino 767 | Tetrimino *nextTetrimino2; // Second next Tetrimino 768 | 769 | u16 _w; 770 | u16 _h; 771 | 772 | std::ostringstream score; 773 | std::ostringstream highScore; 774 | uint64_t scoreValue = 0; 775 | 776 | int linesCleared = 0; 777 | int level = 1; 778 | 779 | 780 | void drawParticles(tsl::gfx::Renderer* renderer, int offsetX, int offsetY) { 781 | tsl::Color particleColor(0); 782 | int particleDrawX, particleDrawY; 783 | 784 | // Lock the particles vector while drawing to avoid race conditions 785 | std::lock_guard lock(particleMutex); 786 | 787 | for (const auto& particle : particles) { 788 | if (particle.life > 0 && particle.alpha > 0) { 789 | // Calculate particle position relative to the board 790 | particleDrawX = offsetX + static_cast(particle.x); 791 | particleDrawY = offsetY + static_cast(particle.y); 792 | 793 | // Generate a random color for each particle in RGB4444 format 794 | particleColor = tsl::Color({ 795 | static_cast(rand() % 16), // Random Red component (4 bits, 0x0 to 0xF) 796 | static_cast(rand() % 16), // Random Green component (4 bits, 0x0 to 0xF) 797 | static_cast(rand() % 16), // Random Blue component (4 bits, 0x0 to 0xF) 798 | static_cast(particle.alpha * 15) // Alpha component (scaled to 0x0 to 0xF) 799 | }); 800 | 801 | // Draw the particle 802 | renderer->drawRect(particleDrawX, particleDrawY, 4, 4, particleColor); 803 | } 804 | } 805 | } 806 | 807 | 808 | // Helper function to draw a single Tetrimino (handles both ghost and normal rendering) 809 | void drawSingleTetrimino(tsl::gfx::Renderer* renderer, const Tetrimino& tet, int offsetX, int offsetY, bool isGhost) { 810 | static tsl::Color color(0); 811 | static tsl::Color outerColor(0); 812 | static tsl::Color highlightColor(0); 813 | int rotatedIndex; 814 | int x, y; 815 | 816 | static constexpr int innerPadding = 3; // Adjust padding for a more balanced 3D look 817 | 818 | for (int i = 0; i < 4; ++i) { 819 | for (int j = 0; j < 4; ++j) { 820 | rotatedIndex = getRotatedIndex(tet.type, i, j, tet.rotation); 821 | if (tetriminoShapes[tet.type][rotatedIndex] != 0) { 822 | x = offsetX + (tet.x + j) * _w; 823 | y = offsetY + (tet.y + i) * _h; 824 | 825 | // Skip rendering for blocks above the top of the visible board 826 | if (tet.y + i < 0) { 827 | continue; 828 | } 829 | 830 | color = tetriminoColors[tet.type]; // The regular color for the inner block 831 | if (isGhost) { 832 | // Make the ghost piece semi-transparent 833 | color.a = static_cast(color.a * 0.4); // Adjust transparency for ghost piece 834 | } 835 | 836 | // Calculate and draw the outer block (slightly darker than the regular color) 837 | outerColor = { 838 | static_cast(color.r * 0xC / 0xF), // Slightly darker, closer to 60% brightness 839 | static_cast(color.g * 0xC / 0xF), 840 | static_cast(color.b * 0xC / 0xF), 841 | static_cast(color.a) // Maintain the alpha channel 842 | }; 843 | 844 | // Draw the outer block (darker color) 845 | renderer->drawRect(x, y, _w, _h, outerColor); 846 | 847 | // Draw the inner block (original color) 848 | renderer->drawRect(x + innerPadding, y + innerPadding, _w - 2 * innerPadding, _h - 2 * innerPadding, color); 849 | 850 | // Add a 3D highlight at the top-left corner for light effect 851 | highlightColor = { 852 | static_cast(std::min(color.r + 0x4, 0xF)), // Increase brightness more subtly (max out at 0xF) 853 | static_cast(std::min(color.g + 0x4, 0xF)), 854 | static_cast(std::min(color.b + 0x4, 0xF)), 855 | static_cast(color.a) // Keep alpha unchanged 856 | }; 857 | 858 | renderer->drawRect(x + innerPadding, y + innerPadding, _w / 4, _h / 4, highlightColor); 859 | } 860 | } 861 | } 862 | } 863 | 864 | void drawTetrimino(tsl::gfx::Renderer* renderer, const Tetrimino& tet, int offsetX, int offsetY) { 865 | // Calculate the drop position for the ghost piece 866 | Tetrimino ghostTetrimino = tet; 867 | const int dropDistance = calculateDropDistance(ghostTetrimino, *board); 868 | ghostTetrimino.y += dropDistance; 869 | 870 | // Draw the ghost piece first (semi-transparent) 871 | drawSingleTetrimino(renderer, ghostTetrimino, offsetX, offsetY, true); // `true` indicates ghost 872 | 873 | // Draw the active Tetrimino 874 | drawSingleTetrimino(renderer, tet, offsetX, offsetY, false); // `false` indicates normal piece 875 | } 876 | 877 | // Constants for borders and padding 878 | const int BORDER_WIDTH = _w * 2 + 8; 879 | const int BORDER_HEIGHT = _w * 2 + 8; 880 | static constexpr int BORDER_THICKNESS = 2; 881 | static constexpr int PADDING = 2; 882 | static constexpr tsl::Color BACKGROUND_COLOR = {0x0, 0x0, 0x0, 0x8}; 883 | static constexpr tsl::Color BORDER_COLOR = {0xF, 0xF, 0xF, 0xF}; 884 | 885 | // Helper function to draw a 3D block with highlight and shadow 886 | void draw3DBlock(tsl::gfx::Renderer* renderer, int x, int y, int width, int height, tsl::Color color) { 887 | // Calculate outer block color (darker than the original color) 888 | const tsl::Color outerColor = { 889 | static_cast(color.r * 0xC / 0xF), // Slightly darker, closer to 60% brightness 890 | static_cast(color.g * 0xC / 0xF), 891 | static_cast(color.b * 0xC / 0xF), 892 | static_cast(color.a) // Maintain the alpha channel 893 | }; 894 | 895 | // Draw the outer block (darker color) 896 | renderer->drawRect(x, y, width, height, outerColor); 897 | 898 | // Draw the inner block (original color) 899 | static constexpr int innerPadding = 1; 900 | renderer->drawRect(x + innerPadding, y + innerPadding, width - 2 * innerPadding, height - 2 * innerPadding, color); 901 | 902 | // Highlight at the top-left corner (lighter shade) 903 | const tsl::Color highlightColor = { 904 | static_cast(std::min(color.r + 0x4, 0xF)), // Slightly lighter than the original color 905 | static_cast(std::min(color.g + 0x4, 0xF)), 906 | static_cast(std::min(color.b + 0x4, 0xF)), 907 | static_cast(color.a) // Keep alpha unchanged 908 | }; 909 | 910 | renderer->drawRect(x + innerPadding, y + innerPadding, width / 4, height / 4, highlightColor); 911 | } 912 | 913 | 914 | // Helper function to draw preview frame (borders and background) 915 | void drawPreviewFrame(tsl::gfx::Renderer* renderer, int posX, int posY) { 916 | // Draw the background for the preview 917 | renderer->drawRect( 918 | posX - PADDING - BORDER_THICKNESS, posY - PADDING - BORDER_THICKNESS, 919 | BORDER_WIDTH + 2 * PADDING + 2 * BORDER_THICKNESS, BORDER_HEIGHT + 2 * PADDING + 2 * BORDER_THICKNESS, 920 | BACKGROUND_COLOR 921 | ); 922 | 923 | // Draw the white border around the preview area 924 | renderer->drawRect(posX - PADDING, posY - PADDING, BORDER_WIDTH + 2 * PADDING, BORDER_THICKNESS, BORDER_COLOR); 925 | renderer->drawRect(posX - PADDING, posY + BORDER_HEIGHT, BORDER_WIDTH + 2 * PADDING, BORDER_THICKNESS, BORDER_COLOR); 926 | renderer->drawRect(posX - PADDING, posY - PADDING, BORDER_THICKNESS, BORDER_HEIGHT + 2 * PADDING, BORDER_COLOR); 927 | renderer->drawRect(posX + BORDER_WIDTH, posY - PADDING, BORDER_THICKNESS, BORDER_HEIGHT + 2 * PADDING, BORDER_COLOR); 928 | } 929 | 930 | // Helper function to calculate Tetrimino bounding box 931 | void calculateTetriminoBounds(const Tetrimino& tetrimino, int& minX, int& maxX, int& minY, int& maxY) { 932 | minX = 4; maxX = -1; minY = 4; maxY = -1; 933 | int index; 934 | 935 | for (int i = 0; i < 4; ++i) { 936 | for (int j = 0; j < 4; ++j) { 937 | index = getRotatedIndex(tetrimino.type, i, j, tetrimino.rotation); 938 | if (tetriminoShapes[tetrimino.type][index] != 0) { 939 | if (j < minX) minX = j; 940 | if (j > maxX) maxX = j; 941 | if (i < minY) minY = i; 942 | if (i > maxY) maxY = i; 943 | } 944 | } 945 | } 946 | } 947 | 948 | // Helper function to draw a centered Tetrimino 949 | void drawCenteredTetrimino(tsl::gfx::Renderer* renderer, const Tetrimino& tetrimino, int posX, int posY) { 950 | static int minX, maxX, minY, maxY; 951 | calculateTetriminoBounds(tetrimino, minX, maxX, minY, maxY); 952 | 953 | // Calculate width and height of the Tetrimino 954 | const float tetriminoWidth = (maxX - minX + 1) * (_w / 2); 955 | const float tetriminoHeight = (maxY - minY + 1) * (_h / 2); 956 | 957 | // Center the Tetrimino in the preview area 958 | const int offsetX = std::ceil((BORDER_WIDTH - tetriminoWidth) / 2. - 2.); 959 | const int offsetY = std::ceil((BORDER_HEIGHT - tetriminoHeight) / 2. - 2.); 960 | 961 | static int blockWidth, blockHeight, drawX, drawY; 962 | 963 | static int index; 964 | // Draw each block of the Tetrimino 965 | for (int i = 0; i < 4; ++i) { 966 | for (int j = 0; j < 4; ++j) { 967 | index = getRotatedIndex(tetrimino.type, i, j, tetrimino.rotation); 968 | if (tetriminoShapes[tetrimino.type][index] != 0) { 969 | blockWidth = _w / 2; 970 | blockHeight = _h / 2; 971 | drawX = posX + (j - minX) * blockWidth + PADDING + offsetX; 972 | drawY = posY + (i - minY) * blockHeight + PADDING + offsetY; 973 | 974 | // Use the reusable function to draw the 3D block 975 | draw3DBlock(renderer, drawX, drawY, blockWidth, blockHeight, tetriminoColors[tetrimino.type]); 976 | } 977 | } 978 | } 979 | } 980 | 981 | // Updated method to draw the next Tetrimino with 3D effect 982 | void drawNextTetrimino(tsl::gfx::Renderer* renderer, int posX, int posY) { 983 | // Draw the frame for the next Tetrimino preview 984 | drawPreviewFrame(renderer, posX, posY); 985 | 986 | // Draw the centered next Tetrimino 987 | drawCenteredTetrimino(renderer, *nextTetrimino, posX, posY); 988 | } 989 | 990 | // Updated method to draw the next two Tetriminos 991 | void drawNextTwoTetriminos(tsl::gfx::Renderer* renderer, int posX, int posY) { 992 | const int posY2 = posY + BORDER_HEIGHT + 12; 993 | 994 | // Draw the first next Tetrimino with frame and centered logic 995 | drawPreviewFrame(renderer, posX, posY); 996 | drawCenteredTetrimino(renderer, *nextTetrimino1, posX, posY); 997 | 998 | // Draw the second next Tetrimino with frame and centered logic 999 | drawPreviewFrame(renderer, posX, posY2); 1000 | drawCenteredTetrimino(renderer, *nextTetrimino2, posX, posY2); 1001 | } 1002 | 1003 | // Updated method to draw the stored Tetrimino 1004 | void drawStoredTetrimino(tsl::gfx::Renderer* renderer, int posX, int posY) { 1005 | drawPreviewFrame(renderer, posX, posY); 1006 | 1007 | if (storedTetrimino->type != -1) { 1008 | drawCenteredTetrimino(renderer, *storedTetrimino, posX, posY); 1009 | } 1010 | } 1011 | 1012 | }; 1013 | 1014 | bool TetrisElement::paused = false; 1015 | uint64_t TetrisElement::maxHighScore = 0; // Initialize the max high score 1016 | 1017 | 1018 | class CustomOverlayFrame : public tsl::elm::OverlayFrame { 1019 | public: 1020 | CustomOverlayFrame(const std::string& title, const std::string& subtitle, const bool& _noClickableItems = false) 1021 | : tsl::elm::OverlayFrame(title, subtitle, _noClickableItems) {} 1022 | 1023 | // Override the draw method to customize rendering logic for Tetris 1024 | virtual void draw(tsl::gfx::Renderer* renderer) override { 1025 | if (m_noClickableItems != ult::noClickableItems.load(std::memory_order_acquire)) { 1026 | ult::noClickableItems.store(m_noClickableItems, std::memory_order_release); 1027 | } 1028 | 1029 | if (!ult::themeIsInitialized.load(std::memory_order_acquire)) { 1030 | ult::themeIsInitialized.store(true, std::memory_order_release); 1031 | tsl::initializeThemeVars(); 1032 | } 1033 | 1034 | renderer->fillScreen(a(tsl::defaultBackgroundColor)); 1035 | renderer->drawWallpaper(); 1036 | renderer->drawWidget(); 1037 | 1038 | if (touchingMenu && inMainMenu) { 1039 | renderer->drawRoundedRect(0.0f, 12.0f, 245.0f, 73.0f, 6.0f, a(tsl::clickColor)); 1040 | } 1041 | 1042 | x = 20; 1043 | y = 62; 1044 | fontSize = 54; 1045 | offset = 6; 1046 | countOffset = 0; 1047 | 1048 | if (ult::useDynamicLogo) { 1049 | const auto currentTimeCount = std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); 1050 | float progress; 1051 | static const auto dynamicLogoRGB1 = tsl::RGB888("#6929ff"); 1052 | static const auto dynamicLogoRGB2 = tsl::RGB888("#fff429"); 1053 | static tsl::Color highlightColor(0); 1054 | for (char letter : m_title) { 1055 | counter = (2 * ult::M_PI * (fmod(currentTimeCount/4.0, 2.0) + countOffset) / 2.0); 1056 | progress = std::sin(3.0 * (counter - (2.0 * ult::M_PI / 3.0))); 1057 | 1058 | highlightColor = { 1059 | static_cast((dynamicLogoRGB2.r - dynamicLogoRGB1.r) * (progress + 1.0) / 2.0 + dynamicLogoRGB1.r), 1060 | static_cast((dynamicLogoRGB2.g - dynamicLogoRGB1.g) * (progress + 1.0) / 2.0 + dynamicLogoRGB1.g), 1061 | static_cast((dynamicLogoRGB2.b - dynamicLogoRGB1.b) * (progress + 1.0) / 2.0 + dynamicLogoRGB1.b), 1062 | 15 1063 | }; 1064 | 1065 | renderer->drawString(std::string(1, letter), false, x, y + offset, fontSize, a(highlightColor)); 1066 | x += tsl::gfx::calculateStringWidth(std::string(1, letter), fontSize); 1067 | countOffset -= 0.2F; 1068 | } 1069 | } else { 1070 | for (char letter : m_title) { 1071 | renderer->drawString(std::string(1, letter), false, x, y + offset, fontSize, a(tsl::logoColor1)); 1072 | x += tsl::gfx::calculateStringWidth(std::string(1, letter), fontSize); 1073 | countOffset -= 0.2F; 1074 | } 1075 | } 1076 | 1077 | renderer->drawString(this->m_subtitle, false, 184, y-8, 15, (tsl::bannerVersionTextColor)); 1078 | renderer->drawRect(15, tsl::cfg::FramebufferHeight - 73, tsl::cfg::FramebufferWidth - 30, 1, a(tsl::bottomSeparatorColor)); 1079 | 1080 | // Calculate gap width and store half gap (matching original code) 1081 | const float gapWidth = renderer->getTextDimensions(ult::GAP_1, false, 23).first; 1082 | const float _halfGap = gapWidth / 2.0f; 1083 | if (_halfGap != ult::halfGap.load(std::memory_order_acquire)) 1084 | ult::halfGap.store(_halfGap, std::memory_order_release); 1085 | 1086 | // Determine button commands based on game state 1087 | static std::string bCommand; 1088 | static std::string aCommand; 1089 | 1090 | if (isGameOver) { 1091 | bCommand = BACK; 1092 | aCommand = "New Game"; 1093 | m_noClickableItems = false; 1094 | } else if (TetrisElement::paused) { 1095 | bCommand = BACK; 1096 | aCommand = ""; 1097 | m_noClickableItems = true; 1098 | } else { 1099 | bCommand = "Rotate Left"; 1100 | aCommand = "Rotate Right"; 1101 | m_noClickableItems = false; 1102 | } 1103 | 1104 | // Calculate text widths for buttons 1105 | const float backTextWidth = renderer->getTextDimensions( 1106 | "\uE0E1" + ult::GAP_2 + bCommand, false, 23).first; 1107 | const float selectTextWidth = renderer->getTextDimensions( 1108 | "\uE0E0" + ult::GAP_2 + aCommand, false, 23).first; 1109 | 1110 | // Total button widths include half-gap padding on both sides (matching original) 1111 | const float _backWidth = backTextWidth + gapWidth; 1112 | if (_backWidth != ult::backWidth.load(std::memory_order_acquire)) 1113 | ult::backWidth.store(_backWidth, std::memory_order_release); 1114 | const float _selectWidth = selectTextWidth + gapWidth; 1115 | if (_selectWidth != ult::selectWidth.load(std::memory_order_acquire)) 1116 | ult::selectWidth.store(_selectWidth, std::memory_order_release); 1117 | 1118 | // Set button positions (matching original) 1119 | static constexpr float buttonStartX = 30; 1120 | const float buttonY = static_cast(tsl::cfg::FramebufferHeight - 73 + 1); 1121 | 1122 | // Draw back button if touched 1123 | if (ult::touchingBack.load(std::memory_order_acquire)) { 1124 | renderer->drawRoundedRect(buttonStartX+2 - _halfGap, buttonY, _backWidth-1, 73.0f, 10.0f, a(tsl::clickColor)); 1125 | } 1126 | 1127 | // Draw select button if touched 1128 | if (ult::touchingSelect.load(std::memory_order_acquire) && !m_noClickableItems) { 1129 | renderer->drawRoundedRect(buttonStartX+2 - _halfGap + _backWidth+1, buttonY, 1130 | _selectWidth-2, 73.0f, 10.0f, a(tsl::clickColor)); 1131 | } 1132 | 1133 | // Build menu bottom line 1134 | const std::string menuBottomLine = m_noClickableItems ? 1135 | "\uE0E1" + ult::GAP_2 + bCommand + ult::GAP_1 : 1136 | "\uE0E1" + ult::GAP_2 + bCommand + ult::GAP_1 + "\uE0E0" + ult::GAP_2 + aCommand + ult::GAP_1; 1137 | 1138 | // Render the text with special character handling 1139 | static const std::vector symbols = {"\uE0E1","\uE0E0","\uE0ED","\uE0EE"}; 1140 | renderer->drawStringWithColoredSections(menuBottomLine, false, symbols, 1141 | buttonStartX, 693, 23, 1142 | (tsl::bottomTextColor), (tsl::buttonColor)); 1143 | 1144 | if (this->m_contentElement != nullptr) 1145 | this->m_contentElement->frame(renderer); 1146 | } 1147 | }; 1148 | 1149 | 1150 | class TetrisGui : public tsl::Gui { 1151 | public: 1152 | Tetrimino storedTetrimino{-1}; // -1 indicates no stored Tetrimino 1153 | bool hasSwapped = false; // To track if a swap has already occurred 1154 | 1155 | int linesClearedForLevelUp = 0; // Track how many lines cleared for leveling up 1156 | static constexpr int LINES_PER_LEVEL = 10; // Increment level every 10 lines 1157 | 1158 | // Variables to track time of last rotation or movement 1159 | std::chrono::time_point lastRotationOrMoveTime; 1160 | static constexpr std::chrono::milliseconds lockDelayExtension = std::chrono::milliseconds(500); // 500ms extension 1161 | 1162 | TetrisGui() : board(), currentTetrimino(rand() % 7), nextTetrimino(rand() % 7), 1163 | nextTetrimino1(rand() % 7), nextTetrimino2(rand() % 7) { 1164 | 1165 | std::srand(std::time(0)); 1166 | _w = 20; 1167 | _h = _w; 1168 | lockDelayTime = std::chrono::milliseconds(500); // Set lock delay to 500ms 1169 | lockDelayCounter = std::chrono::milliseconds(0); 1170 | 1171 | // Initial fall speed (1000 ms = 1 second) 1172 | initialFallSpeed = std::chrono::milliseconds(500); 1173 | fallCounter = std::chrono::milliseconds(0); 1174 | 1175 | lastRotationOrMoveTime = std::chrono::steady_clock::now(); // Initialize with current time 1176 | } 1177 | 1178 | ~TetrisGui() { 1179 | TetrisElement::paused = true; 1180 | saveGameState(); 1181 | } 1182 | 1183 | virtual tsl::elm::Element* createUI() override { 1184 | //auto rootFrame = new tsl::elm::OverlayFrame("Tetris", APP_VERSION); 1185 | auto rootFrame = new CustomOverlayFrame("Tetris", APP_VERSION); 1186 | tetrisElement = new TetrisElement(_w, _h, &board, ¤tTetrimino, &nextTetrimino, &storedTetrimino, &nextTetrimino1, &nextTetrimino2); 1187 | rootFrame->setContent(tetrisElement); 1188 | timeSinceLastFrame = std::chrono::steady_clock::now(); 1189 | 1190 | loadGameState(); 1191 | return rootFrame; 1192 | } 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | virtual void update() override { 1199 | if (!TetrisElement::paused && !tetrisElement->gameOver) { 1200 | const auto currentTime = std::chrono::steady_clock::now(); 1201 | const auto elapsed = currentTime - timeSinceLastFrame; 1202 | 1203 | // Handle piece falling 1204 | fallCounter += std::chrono::duration_cast(elapsed); 1205 | if (fallCounter >= getFallSpeed()) { 1206 | // Try to move the piece down 1207 | if (!move(0, 1)) { // Move down failed, piece touched the ground 1208 | lockDelayCounter += fallCounter; // Add elapsed time to lock delay counter 1209 | 1210 | // Check if more than 500ms has passed since the last move/rotation 1211 | const auto timeSinceLastRotationOrMove = std::chrono::duration_cast(currentTime - lastRotationOrMoveTime); 1212 | 1213 | if (lockDelayCounter >= lockDelayTime && timeSinceLastRotationOrMove >= lockDelayExtension) { 1214 | // Lock the piece after the lock delay has passed and no rotation occurred recently 1215 | placeTetrimino(); 1216 | clearLines(); 1217 | spawnNewTetrimino(); 1218 | lockDelayCounter = std::chrono::milliseconds(0); // Reset the lock delay counter 1219 | } 1220 | } else { 1221 | // Piece successfully moved down, reset lock delay 1222 | lockDelayCounter = std::chrono::milliseconds(0); 1223 | } 1224 | fallCounter = std::chrono::milliseconds(0); // Reset fall counter 1225 | } 1226 | 1227 | timeSinceLastFrame = currentTime; 1228 | } 1229 | } 1230 | 1231 | 1232 | 1233 | void resetGame() { 1234 | // Create an explosion effect before resetting the game 1235 | createCenterExplosionParticles(); 1236 | 1237 | // Delay the actual reset slightly to allow the explosion to be visible 1238 | std::this_thread::sleep_for(std::chrono::milliseconds(300)); 1239 | 1240 | isGameOver = false; 1241 | 1242 | // Reset variables related to game state 1243 | lastWallKickApplied = false; 1244 | previousClearWasTetris = false; 1245 | previousClearWasTSpin = false; 1246 | backToBackCount = 1; 1247 | 1248 | // Clear the board 1249 | for (auto& row : board) { 1250 | row.fill(0); 1251 | } 1252 | 1253 | // Reset tetriminos 1254 | spawnNewTetrimino(); // Spawn the first piece here 1255 | nextTetrimino = Tetrimino(rand() % 7); 1256 | nextTetrimino1 = Tetrimino(rand() % 7); 1257 | nextTetrimino2 = Tetrimino(rand() % 7); 1258 | 1259 | // Reset the stored tetrimino 1260 | storedTetrimino = Tetrimino(-1); // Reset stored piece to no stored state 1261 | hasSwapped = false; // Reset swap flag 1262 | 1263 | // Reset score 1264 | tetrisElement->setScore(0); 1265 | 1266 | // Reset linesCleared and level 1267 | tetrisElement->setLinesCleared(0); // Reset lines cleared 1268 | tetrisElement->setLevel(1); // Reset level to 1 1269 | 1270 | // Reset game over state 1271 | tetrisElement->gameOver = false; 1272 | 1273 | // Unpause the game 1274 | TetrisElement::paused = false; 1275 | } 1276 | 1277 | 1278 | 1279 | void swapStoredTetrimino() { 1280 | if (storedTetrimino.type == -1) { 1281 | // No stored Tetrimino, store the current one and spawn a new one 1282 | storedTetrimino = currentTetrimino; 1283 | storedTetrimino.rotation = 0; // Reset the rotation of the stored piece to its default 1284 | spawnNewTetrimino(); 1285 | } else { 1286 | // Swap the current Tetrimino with the stored one 1287 | std::swap(currentTetrimino, storedTetrimino); 1288 | currentTetrimino.x = BOARD_WIDTH / 2 - 2; 1289 | currentTetrimino.y = 0; 1290 | currentTetrimino.rotation = 0; // Reset the swapped piece's rotation to default 1291 | storedTetrimino.rotation = 0; // Reset the stored piece's rotation to default 1292 | } 1293 | } 1294 | 1295 | void createImpactParticles(int dropDistance) { 1296 | std::lock_guard lock(particleMutex); // Lock to ensure safe access to the particle list 1297 | 1298 | // Cap the maximum drop distance to avoid excessive velocity 1299 | const float velocityFactor = std::min(dropDistance / 10.0f, 2.0f); // Adjust the divisor and cap for desired effect 1300 | 1301 | // Set minimum and maximum horizontal and vertical velocities 1302 | static constexpr float minVelocity = 0.5f; // Minimum velocity value 1303 | const float maxHorizontalVelocity = 2.0f * velocityFactor; 1304 | const float maxVerticalVelocity = 4.0f * velocityFactor; 1305 | 1306 | // Calculate lifespan based on drop distance with a minimum of 0.2 and a maximum of 0.6 1307 | const float lifespanFactor = std::clamp(dropDistance / 20.0f, 0.2f, 0.6f); 1308 | 1309 | // Calculate the number of particles based on drop distance, clamped between 2 and 5 particles 1310 | const int particleCount = std::clamp(2 + dropDistance / 5, 2, 5); 1311 | 1312 | int bottomRow; 1313 | int rotatedIndex; 1314 | int blockX, blockY; 1315 | Particle particle; 1316 | float horizontalVelocity, verticalVelocity; 1317 | 1318 | // Iterate over each column of the Tetrimino to find the bottom edge 1319 | for (int j = 0; j < 4; ++j) { 1320 | bottomRow = -1; 1321 | 1322 | for (int i = 0; i < 4; ++i) { 1323 | rotatedIndex = getRotatedIndex(currentTetrimino.type, i, j, currentTetrimino.rotation); 1324 | if (tetriminoShapes[currentTetrimino.type][rotatedIndex] != 0) { 1325 | bottomRow = i; // Keep track of the bottom-most row for this column 1326 | } 1327 | } 1328 | 1329 | // If a bottom block is found, generate particles 1330 | if (bottomRow != -1) { 1331 | blockX = currentTetrimino.x + j; 1332 | blockY = currentTetrimino.y + bottomRow; 1333 | 1334 | // Create several particles falling from this block 1335 | for (int p = 0; p < particleCount; ++p) { // Adjust this number to control particle count 1336 | // Generate horizontal and vertical velocities, clamped between min and max 1337 | horizontalVelocity = std::clamp((rand() % 100 / 50.0f - 1.0f) * velocityFactor, -maxHorizontalVelocity, maxHorizontalVelocity); 1338 | verticalVelocity = std::clamp((rand() % 100 / 50.0f) * (2.0f * velocityFactor), minVelocity, maxVerticalVelocity); 1339 | 1340 | particle = { 1341 | static_cast(blockX * _w + rand() % _w), // X-position within the block 1342 | static_cast(blockY * _h + _h), // Y-position at the bottom of the block 1343 | horizontalVelocity, // Clamped horizontal velocity 1344 | verticalVelocity, // Clamped downward velocity 1345 | lifespanFactor, // Lifespan based on drop distance, clamped between 0.2 and 0.6 1346 | 1.0f // Alpha (fully visible) 1347 | }; 1348 | particles.push_back(particle); 1349 | } 1350 | } 1351 | } 1352 | } 1353 | 1354 | 1355 | 1356 | void hardDrop() { 1357 | // Calculate how far the piece will fall 1358 | hardDropDistance = calculateDropDistance(currentTetrimino, board); 1359 | currentTetrimino.y += hardDropDistance; 1360 | 1361 | // Award points for hard drop (e.g., 2 points per row) 1362 | const int hardDropScore = hardDropDistance * 2; 1363 | tetrisElement->setScore(tetrisElement->getScore() + hardDropScore); 1364 | 1365 | createImpactParticles(hardDropDistance); 1366 | 1367 | // Place the piece and reset drop distance trackers 1368 | placeTetrimino(); 1369 | clearLines(); 1370 | spawnNewTetrimino(); 1371 | 1372 | // Reset distances after placing 1373 | totalSoftDropDistance = 0; 1374 | hardDropDistance = 0; 1375 | 1376 | if (!isPositionValid(currentTetrimino, board)) { 1377 | tetrisElement->gameOver = true; 1378 | } 1379 | } 1380 | 1381 | 1382 | 1383 | 1384 | void saveGameState() { 1385 | 1386 | cJSON* root = cJSON_CreateObject(); 1387 | 1388 | // Save general game state 1389 | cJSON_AddStringToObject(root, "score", std::to_string(tetrisElement->getScore()).c_str()); 1390 | cJSON_AddStringToObject(root, "maxHighScore", std::to_string(TetrisElement::maxHighScore).c_str()); 1391 | cJSON_AddBoolToObject(root, "paused", TetrisElement::paused); 1392 | cJSON_AddBoolToObject(root, "gameOver", tetrisElement->gameOver); 1393 | cJSON_AddNumberToObject(root, "linesCleared", tetrisElement->getLinesCleared()); 1394 | cJSON_AddNumberToObject(root, "level", tetrisElement->getLevel()); 1395 | cJSON_AddBoolToObject(root, "hasSwapped", hasSwapped); 1396 | 1397 | // Save additional variables 1398 | cJSON_AddBoolToObject(root, "lastWallKickApplied", lastWallKickApplied); 1399 | cJSON_AddBoolToObject(root, "previousClearWasTetris", previousClearWasTetris); 1400 | cJSON_AddBoolToObject(root, "previousClearWasTSpin", previousClearWasTSpin); 1401 | cJSON_AddNumberToObject(root, "backToBackCount", backToBackCount); 1402 | 1403 | // Save current Tetrimino 1404 | cJSON* currentTetriminoJson = cJSON_CreateObject(); 1405 | cJSON_AddNumberToObject(currentTetriminoJson, "type", currentTetrimino.type); 1406 | cJSON_AddNumberToObject(currentTetriminoJson, "rotation", currentTetrimino.rotation); 1407 | cJSON_AddNumberToObject(currentTetriminoJson, "x", currentTetrimino.x); 1408 | cJSON_AddNumberToObject(currentTetriminoJson, "y", currentTetrimino.y); 1409 | cJSON_AddItemToObject(root, "currentTetrimino", currentTetriminoJson); 1410 | 1411 | // Save stored Tetrimino 1412 | cJSON* storedTetriminoJson = cJSON_CreateObject(); 1413 | cJSON_AddNumberToObject(storedTetriminoJson, "type", storedTetrimino.type); 1414 | cJSON_AddNumberToObject(storedTetriminoJson, "rotation", storedTetrimino.rotation); 1415 | cJSON_AddNumberToObject(storedTetriminoJson, "x", storedTetrimino.x); 1416 | cJSON_AddNumberToObject(storedTetriminoJson, "y", storedTetrimino.y); 1417 | cJSON_AddItemToObject(root, "storedTetrimino", storedTetriminoJson); 1418 | 1419 | // Save next Tetrimino states (including the two new next pieces) 1420 | cJSON* nextTetriminoJson = cJSON_CreateObject(); 1421 | cJSON_AddNumberToObject(nextTetriminoJson, "type", nextTetrimino.type); 1422 | cJSON_AddItemToObject(root, "nextTetrimino", nextTetriminoJson); 1423 | 1424 | cJSON* nextTetrimino1Json = cJSON_CreateObject(); 1425 | cJSON_AddNumberToObject(nextTetrimino1Json, "type", nextTetrimino1.type); 1426 | cJSON_AddItemToObject(root, "nextTetrimino1", nextTetrimino1Json); 1427 | 1428 | cJSON* nextTetrimino2Json = cJSON_CreateObject(); 1429 | cJSON_AddNumberToObject(nextTetrimino2Json, "type", nextTetrimino2.type); 1430 | cJSON_AddItemToObject(root, "nextTetrimino2", nextTetrimino2Json); 1431 | 1432 | // Save the board state 1433 | cJSON* boardJson = cJSON_CreateArray(); 1434 | for (int i = 0; i < BOARD_HEIGHT; ++i) { 1435 | cJSON* rowJson = cJSON_CreateArray(); 1436 | for (int j = 0; j < BOARD_WIDTH; ++j) { 1437 | cJSON_AddItemToArray(rowJson, cJSON_CreateNumber(board[i][j])); 1438 | } 1439 | cJSON_AddItemToArray(boardJson, rowJson); 1440 | } 1441 | cJSON_AddItemToObject(root, "board", boardJson); 1442 | 1443 | // Write to the file 1444 | FILE* file = fopen("sdmc:/config/tetris/save_state.json", "w"); 1445 | if (file) { 1446 | char* jsonString = cJSON_Print(root); 1447 | if (jsonString) { 1448 | fwrite(jsonString, 1, strlen(jsonString), file); 1449 | free(jsonString); 1450 | } 1451 | fclose(file); 1452 | } 1453 | 1454 | cJSON_Delete(root); 1455 | } 1456 | 1457 | void loadGameState() { 1458 | // Open file using C-style file I/O 1459 | FILE* file = fopen("sdmc:/config/tetris/save_state.json", "r"); 1460 | if (!file) return; 1461 | 1462 | // Get file size 1463 | fseek(file, 0, SEEK_END); 1464 | long fileSize = ftell(file); 1465 | fseek(file, 0, SEEK_SET); 1466 | 1467 | // Read file content 1468 | std::string jsonContent; 1469 | jsonContent.resize(fileSize); 1470 | size_t bytesRead = fread(&jsonContent[0], 1, fileSize, file); 1471 | fclose(file); 1472 | 1473 | // Resize string to actual bytes read (in case of early EOF) 1474 | jsonContent.resize(bytesRead); 1475 | 1476 | cJSON* root = cJSON_Parse(jsonContent.c_str()); 1477 | if (!root) return; 1478 | 1479 | // Load general game state 1480 | cJSON* scoreJson = cJSON_GetObjectItem(root, "score"); 1481 | cJSON* maxHighScoreJson = cJSON_GetObjectItem(root, "maxHighScore"); 1482 | 1483 | if (cJSON_IsString(scoreJson)) tetrisElement->setScore(std::stoull(scoreJson->valuestring)); 1484 | if (cJSON_IsString(maxHighScoreJson)) TetrisElement::maxHighScore = std::stoull(maxHighScoreJson->valuestring); 1485 | 1486 | cJSON* pausedJson = cJSON_GetObjectItem(root, "paused"); 1487 | if (cJSON_IsBool(pausedJson)) TetrisElement::paused = cJSON_IsTrue(pausedJson); 1488 | 1489 | cJSON* gameOverJson = cJSON_GetObjectItem(root, "gameOver"); 1490 | if (cJSON_IsBool(gameOverJson)) tetrisElement->gameOver = cJSON_IsTrue(gameOverJson); 1491 | 1492 | cJSON* linesClearedJson = cJSON_GetObjectItem(root, "linesCleared"); 1493 | if (cJSON_IsNumber(linesClearedJson)) tetrisElement->setLinesCleared(linesClearedJson->valueint); 1494 | 1495 | cJSON* levelJson = cJSON_GetObjectItem(root, "level"); 1496 | if (cJSON_IsNumber(levelJson)) tetrisElement->setLevel(levelJson->valueint); 1497 | 1498 | cJSON* hasSwappedJson = cJSON_GetObjectItem(root, "hasSwapped"); 1499 | if (cJSON_IsBool(hasSwappedJson)) hasSwapped = cJSON_IsTrue(hasSwappedJson); 1500 | 1501 | // Load additional variables 1502 | cJSON* lastWallKickAppliedJson = cJSON_GetObjectItem(root, "lastWallKickApplied"); 1503 | if (cJSON_IsBool(lastWallKickAppliedJson)) lastWallKickApplied = cJSON_IsTrue(lastWallKickAppliedJson); 1504 | 1505 | cJSON* previousClearWasTetrisJson = cJSON_GetObjectItem(root, "previousClearWasTetris"); 1506 | if (cJSON_IsBool(previousClearWasTetrisJson)) previousClearWasTetris = cJSON_IsTrue(previousClearWasTetrisJson); 1507 | 1508 | cJSON* previousClearWasTSpinJson = cJSON_GetObjectItem(root, "previousClearWasTSpin"); 1509 | if (cJSON_IsBool(previousClearWasTSpinJson)) previousClearWasTSpin = cJSON_IsTrue(previousClearWasTSpinJson); 1510 | 1511 | cJSON* backToBackCountJson = cJSON_GetObjectItem(root, "backToBackCount"); 1512 | if (cJSON_IsNumber(backToBackCountJson)) backToBackCount = backToBackCountJson->valueint; 1513 | 1514 | // Load current Tetrimino 1515 | cJSON* currentTetriminoJson = cJSON_GetObjectItem(root, "currentTetrimino"); 1516 | if (cJSON_IsObject(currentTetriminoJson)) { 1517 | cJSON* typeJson = cJSON_GetObjectItem(currentTetriminoJson, "type"); 1518 | if (cJSON_IsNumber(typeJson)) currentTetrimino.type = typeJson->valueint; 1519 | 1520 | cJSON* rotationJson = cJSON_GetObjectItem(currentTetriminoJson, "rotation"); 1521 | if (cJSON_IsNumber(rotationJson)) currentTetrimino.rotation = rotationJson->valueint; 1522 | 1523 | cJSON* xJson = cJSON_GetObjectItem(currentTetriminoJson, "x"); 1524 | if (cJSON_IsNumber(xJson)) currentTetrimino.x = xJson->valueint; 1525 | 1526 | cJSON* yJson = cJSON_GetObjectItem(currentTetriminoJson, "y"); 1527 | if (cJSON_IsNumber(yJson)) currentTetrimino.y = yJson->valueint; 1528 | } 1529 | 1530 | // Load stored Tetrimino 1531 | cJSON* storedTetriminoJson = cJSON_GetObjectItem(root, "storedTetrimino"); 1532 | if (cJSON_IsObject(storedTetriminoJson)) { 1533 | cJSON* typeJson = cJSON_GetObjectItem(storedTetriminoJson, "type"); 1534 | if (cJSON_IsNumber(typeJson)) storedTetrimino.type = typeJson->valueint; 1535 | 1536 | cJSON* rotationJson = cJSON_GetObjectItem(storedTetriminoJson, "rotation"); 1537 | if (cJSON_IsNumber(rotationJson)) storedTetrimino.rotation = rotationJson->valueint; 1538 | 1539 | cJSON* xJson = cJSON_GetObjectItem(storedTetriminoJson, "x"); 1540 | if (cJSON_IsNumber(xJson)) storedTetrimino.x = xJson->valueint; 1541 | 1542 | cJSON* yJson = cJSON_GetObjectItem(storedTetriminoJson, "y"); 1543 | if (cJSON_IsNumber(yJson)) storedTetrimino.y = yJson->valueint; 1544 | } 1545 | 1546 | // Load next Tetrimino states (including the two new next pieces) 1547 | cJSON* nextTetriminoJson = cJSON_GetObjectItem(root, "nextTetrimino"); 1548 | if (cJSON_IsObject(nextTetriminoJson)) { 1549 | cJSON* typeJson = cJSON_GetObjectItem(nextTetriminoJson, "type"); 1550 | if (cJSON_IsNumber(typeJson)) nextTetrimino.type = typeJson->valueint; 1551 | } 1552 | 1553 | cJSON* nextTetrimino1Json = cJSON_GetObjectItem(root, "nextTetrimino1"); 1554 | if (cJSON_IsObject(nextTetrimino1Json)) { 1555 | cJSON* typeJson = cJSON_GetObjectItem(nextTetrimino1Json, "type"); 1556 | if (cJSON_IsNumber(typeJson)) nextTetrimino1.type = typeJson->valueint; 1557 | } 1558 | 1559 | cJSON* nextTetrimino2Json = cJSON_GetObjectItem(root, "nextTetrimino2"); 1560 | if (cJSON_IsObject(nextTetrimino2Json)) { 1561 | cJSON* typeJson = cJSON_GetObjectItem(nextTetrimino2Json, "type"); 1562 | if (cJSON_IsNumber(typeJson)) nextTetrimino2.type = typeJson->valueint; 1563 | } 1564 | 1565 | // Load the board state 1566 | cJSON* boardJson = cJSON_GetObjectItem(root, "board"); 1567 | if (cJSON_IsArray(boardJson)) { 1568 | for (int i = 0; i < BOARD_HEIGHT; ++i) { 1569 | cJSON* rowJson = cJSON_GetArrayItem(boardJson, i); 1570 | if (cJSON_IsArray(rowJson)) { 1571 | for (int j = 0; j < BOARD_WIDTH; ++j) { 1572 | cJSON* cellJson = cJSON_GetArrayItem(rowJson, j); 1573 | if (cJSON_IsNumber(cellJson)) { 1574 | board[i][j] = cellJson->valueint; 1575 | } 1576 | } 1577 | } 1578 | } 1579 | } 1580 | 1581 | cJSON_Delete(root); 1582 | } 1583 | 1584 | 1585 | // Define constants for DAS (Delayed Auto-Shift) and ARR (Auto-Repeat Rate) 1586 | static constexpr int DAS = 300; // DAS delay in milliseconds 1587 | static constexpr int ARR = 40; // ARR interval in milliseconds 1588 | 1589 | // Variables to track key hold states and timing 1590 | std::chrono::time_point lastLeftMove, lastRightMove, lastDownMove; 1591 | bool leftHeld = false, rightHeld = false, downHeld = false; 1592 | bool leftARR = false, rightARR = false, downARR = false; 1593 | 1594 | bool handleInput(u64 keysDown, u64 keysHeld, touchPosition touchInput, JoystickPosition leftJoyStick, JoystickPosition rightJoyStick) override { 1595 | const auto currentTime = std::chrono::steady_clock::now(); 1596 | bool moved = false; 1597 | 1598 | // Handle the rest of the input only if the game is not paused and not over 1599 | if (simulatedBack) { 1600 | keysDown |= KEY_B; 1601 | simulatedBack = false; 1602 | } 1603 | 1604 | if (simulatedSelect) { 1605 | keysDown |= KEY_A; 1606 | simulatedSelect = false; 1607 | } 1608 | 1609 | // Handle input when the game is paused or over 1610 | if (TetrisElement::paused || tetrisElement->gameOver) { 1611 | if (tetrisElement->gameOver) { 1612 | isGameOver = true; 1613 | if (keysDown & KEY_A || keysDown & KEY_PLUS) { 1614 | // Restart game 1615 | disableSound.exchange(true, std::memory_order_acq_rel); 1616 | triggerRumbleDoubleClick.store(true, std::memory_order_release); 1617 | resetGame(); 1618 | return true; 1619 | } 1620 | // Allow closing the overlay with KEY_B only when paused or game over 1621 | if (keysDown & KEY_B) { 1622 | //resetGame(); 1623 | } 1624 | } 1625 | // Unpause if KEY_PLUS is pressed 1626 | if (keysDown & KEY_PLUS) { 1627 | disableSound.exchange(true, std::memory_order_acq_rel); 1628 | triggerRumbleClick.store(true, std::memory_order_release); 1629 | TetrisElement::paused = false; 1630 | } 1631 | // Allow closing the overlay with KEY_B only when paused or game over 1632 | if (keysDown & KEY_B) { 1633 | //saveGameState(); 1634 | triggerRumbleDoubleClick.store(true, std::memory_order_release); 1635 | tsl::Overlay::get()->close(); 1636 | } 1637 | 1638 | // Return true to indicate input was handled 1639 | return true; 1640 | } else { 1641 | 1642 | // Handle swapping with the stored Tetrimino 1643 | if (keysDown & KEY_L && !(keysHeld & ~(KEY_L|KEY_LEFT|KEY_RIGHT|KEY_DOWN|KEY_UP) & ALL_KEYS_MASK) && !hasSwapped) { 1644 | triggerRumbleDoubleClick.store(true, std::memory_order_release); 1645 | swapStoredTetrimino(); 1646 | hasSwapped = true; 1647 | } 1648 | 1649 | // Handle left movement with DAS and ARR 1650 | if (keysHeld & KEY_LEFT) { 1651 | if (!leftHeld) { 1652 | // First press 1653 | moved = move(-1, 0); 1654 | if (moved) { 1655 | triggerRumbleClick.store(true, std::memory_order_release); 1656 | } 1657 | lastLeftMove = currentTime; 1658 | leftHeld = true; 1659 | leftARR = false; // Reset ARR phase 1660 | } else { 1661 | // DAS check 1662 | const auto elapsed = std::chrono::duration_cast(currentTime - lastLeftMove).count(); 1663 | if (!leftARR && elapsed >= DAS) { 1664 | // Once DAS is reached, start ARR 1665 | moved = move(-1, 0); 1666 | if (moved) { 1667 | triggerRumbleClick.store(true, std::memory_order_release); 1668 | } 1669 | lastLeftMove = currentTime; // Reset time for ARR phase 1670 | leftARR = true; 1671 | } else if (leftARR && elapsed >= ARR) { 1672 | // Auto-repeat after ARR interval 1673 | moved = move(-1, 0); 1674 | if (moved) { 1675 | triggerRumbleClick.store(true, std::memory_order_release); 1676 | } 1677 | lastLeftMove = currentTime; // Keep resetting for ARR 1678 | } 1679 | } 1680 | 1681 | } else { 1682 | leftHeld = false; 1683 | } 1684 | 1685 | // Handle right movement with DAS and ARR 1686 | if (keysHeld & KEY_RIGHT) { 1687 | if (!rightHeld) { 1688 | // First press 1689 | 1690 | moved = move(1, 0); 1691 | if (moved) { 1692 | triggerRumbleClick.store(true, std::memory_order_release); 1693 | } 1694 | lastRightMove = currentTime; 1695 | rightHeld = true; 1696 | rightARR = false; // Reset ARR phase 1697 | } else { 1698 | // DAS check 1699 | const auto elapsed = std::chrono::duration_cast(currentTime - lastRightMove).count(); 1700 | if (!rightARR && elapsed >= DAS) { 1701 | // Once DAS is reached, start ARR 1702 | moved = move(1, 0); 1703 | if (moved) { 1704 | triggerRumbleClick.store(true, std::memory_order_release); 1705 | } 1706 | lastRightMove = currentTime; 1707 | rightARR = true; 1708 | } else if (rightARR && elapsed >= ARR) { 1709 | // Auto-repeat after ARR interval 1710 | moved = move(1, 0); 1711 | if (moved) { 1712 | triggerRumbleClick.store(true, std::memory_order_release); 1713 | } 1714 | lastRightMove = currentTime; // Keep resetting for ARR 1715 | } 1716 | } 1717 | } else { 1718 | rightHeld = false; 1719 | } 1720 | 1721 | // Handle down movement with DAS and ARR for soft dropping 1722 | if (keysHeld & KEY_DOWN) { 1723 | if (!downHeld) { 1724 | // Check if the piece is on the floor and lock it immediately 1725 | if (isOnFloor()) { 1726 | triggerRumbleClick.store(true, std::memory_order_release); 1727 | hardDrop(); 1728 | } else { 1729 | // First press 1730 | moved = move(0, 1); 1731 | lastDownMove = currentTime; 1732 | downHeld = true; 1733 | downARR = false; // Reset ARR phase 1734 | } 1735 | 1736 | } else { 1737 | // DAS check 1738 | const auto elapsed = std::chrono::duration_cast(currentTime - lastDownMove).count(); 1739 | if (!downARR && elapsed >= DAS) { 1740 | if (isOnFloor()) { 1741 | triggerRumbleClick.store(true, std::memory_order_release); 1742 | hardDrop(); 1743 | } else { 1744 | // Once DAS is reached, start ARR 1745 | moved = move(0, 1); 1746 | lastDownMove = currentTime; 1747 | downARR = true; 1748 | } 1749 | } else if (downARR && elapsed >= ARR) { 1750 | if (isOnFloor()) { 1751 | triggerRumbleClick.store(true, std::memory_order_release); 1752 | hardDrop(); 1753 | } else { 1754 | // Auto-repeat after ARR interval 1755 | moved = move(0, 1); 1756 | lastDownMove = currentTime; 1757 | } 1758 | } 1759 | } 1760 | 1761 | } else { 1762 | downHeld = false; 1763 | } 1764 | 1765 | // Handle hard drop with the Up key 1766 | if (keysDown & KEY_UP) { 1767 | triggerRumbleClick.store(true, std::memory_order_release); 1768 | hardDrop(); // Perform hard drop immediately 1769 | } 1770 | 1771 | // Handle rotation inputs 1772 | if (keysDown & KEY_A) { 1773 | triggerRumbleClick.store(true, std::memory_order_release); 1774 | //bool rotated = rotate(); // Rotate clockwise 1775 | if (rotate()) { 1776 | moved = true; 1777 | } 1778 | } else if (keysDown & KEY_B) { 1779 | triggerRumbleClick.store(true, std::memory_order_release); 1780 | //bool rotated = rotateCounterclockwise(); // Rotate counterclockwise 1781 | if (rotateCounterclockwise()) { 1782 | moved = true; 1783 | } 1784 | } 1785 | 1786 | // Handle pause/unpause 1787 | if (keysDown & KEY_PLUS) { 1788 | triggerRumbleClick.store(true, std::memory_order_release); 1789 | TetrisElement::paused = !TetrisElement::paused; 1790 | } 1791 | 1792 | // Reset the lock delay timer if the piece has moved or rotated 1793 | if (moved) { 1794 | lockDelayCounter = std::chrono::milliseconds(0); 1795 | return true; 1796 | } 1797 | 1798 | } 1799 | return false; 1800 | } 1801 | 1802 | 1803 | 1804 | private: 1805 | std::array, BOARD_HEIGHT> board{}; 1806 | Tetrimino currentTetrimino; 1807 | Tetrimino nextTetrimino; 1808 | Tetrimino nextTetrimino1; 1809 | Tetrimino nextTetrimino2; 1810 | TetrisElement* tetrisElement; 1811 | u16 _w; 1812 | u16 _h; 1813 | std::chrono::time_point timeSinceLastFrame; 1814 | 1815 | // Lock delay variables 1816 | std::chrono::milliseconds lockDelayTime; 1817 | std::chrono::milliseconds lockDelayCounter; 1818 | 1819 | // Fall speed variables 1820 | std::chrono::milliseconds initialFallSpeed; // No fallSpeed in game state now 1821 | std::chrono::milliseconds fallCounter; 1822 | 1823 | int totalSoftDropDistance = 0; // Tracks the number of rows dropped for soft drops 1824 | int hardDropDistance = 0; // Tracks the number of rows dropped for hard drops 1825 | 1826 | static constexpr int maxLockDelayMoves = 15; // Maximum number of times the player can move left/right before the piece locks 1827 | int lockDelayMoves = 0; // Number of times the player has moved left/right since the piece hit the ground 1828 | 1829 | // Add a member variable to track if a wall kick was applied 1830 | bool lastWallKickApplied = false; 1831 | bool previousClearWasTetris = false; // Track if the previous clear was a Tetris 1832 | bool previousClearWasTSpin = false; // Track if the previous clear was a T-Spin 1833 | int backToBackCount = 1; 1834 | 1835 | bool pieceWasKickedUp = false; 1836 | 1837 | // Function to adjust the fall speed based on the current level 1838 | //void adjustFallSpeed() { 1839 | // int minSpeed = 200; // Minimum fall speed (200 ms) 1840 | // int speedDecrease = tetrisElement->getLevel() * 50; // Decrease fall time by 50ms per level 1841 | // fallSpeed = std::max(initialFallSpeed - std::chrono::milliseconds(speedDecrease), std::chrono::milliseconds(minSpeed)); 1842 | //} 1843 | 1844 | // Function to dynamically calculate fall speed based on the current level 1845 | std::chrono::milliseconds getFallSpeed() { 1846 | // Define the fall speeds in milliseconds based on levels (simulating classic Tetris) 1847 | static constexpr std::array fallSpeeds = { 1848 | 800, // Level 0: 800ms per row drop 1849 | 720, // Level 1 1850 | 630, // Level 2 1851 | 550, // Level 3 1852 | 470, // Level 4 1853 | 380, // Level 5 1854 | 300, // Level 6 1855 | 220, // Level 7 1856 | 130, // Level 8 1857 | 100, // Level 9 1858 | 80, // Level 10 1859 | 80, // Level 11 1860 | 80, // Level 12 1861 | 80, // Level 13 1862 | 70, // Level 14 1863 | 70, // Level 15 1864 | 70, // Level 16 1865 | 50, // Level 17 1866 | 50, // Level 18 1867 | 50, // Level 19 1868 | 30, // Level 20 1869 | 30, // Level 21 1870 | 30, // Level 22 1871 | 20, // Level 23 1872 | 20, // Level 24 1873 | 20, // Level 25 1874 | 20, // Level 26 1875 | 20, // Level 27 1876 | 20, // Level 28 1877 | 16 // Level 29 and above (maximum speed, 16ms per row) 1878 | }; 1879 | 1880 | // Get the appropriate fall speed for the current level, clamping if necessary 1881 | const int level = std::min(tetrisElement->getLevel(), static_cast(fallSpeeds.size() - 1)); 1882 | 1883 | // Set a minimum threshold for fall speed to avoid it becoming too fast 1884 | const int fallSpeed = std::max(fallSpeeds[level], 16); // Minimum 16ms 1885 | 1886 | return std::chrono::milliseconds(fallSpeed); 1887 | } 1888 | 1889 | bool isOnFloor() { 1890 | // If the piece was kicked up, it's not on the floor 1891 | if (pieceWasKickedUp) { 1892 | return true; 1893 | } 1894 | 1895 | int rotatedIndex; 1896 | int x, y; 1897 | 1898 | for (int i = 0; i < 4; ++i) { 1899 | for (int j = 0; j < 4; ++j) { 1900 | rotatedIndex = getRotatedIndex(currentTetrimino.type, i, j, currentTetrimino.rotation); 1901 | 1902 | if (tetriminoShapes[currentTetrimino.type][rotatedIndex] != 0) { 1903 | x = currentTetrimino.x + j; 1904 | y = currentTetrimino.y + i; 1905 | 1906 | // Check if it's at the bottom of the board or on top of another block 1907 | if (y + 1 >= BOARD_HEIGHT || board[y + 1][x] != 0) { 1908 | return true; 1909 | } 1910 | } 1911 | } 1912 | } 1913 | return false; 1914 | } 1915 | 1916 | bool move(int dx, int dy) { 1917 | std::lock_guard lock(boardMutex); // Lock to prevent race conditions 1918 | bool success = false; 1919 | 1920 | // Attempt to move the Tetrimino 1921 | currentTetrimino.x += dx; 1922 | currentTetrimino.y += dy; 1923 | 1924 | // Check if the new position is valid 1925 | if (!isPositionValid(currentTetrimino, board)) { 1926 | // Revert the move if invalid 1927 | currentTetrimino.x -= dx; 1928 | currentTetrimino.y -= dy; 1929 | } else { 1930 | success = true; 1931 | 1932 | // If the piece moved down 1933 | if (dy > 0) { 1934 | totalSoftDropDistance += dy; // Accumulate soft drop distance for scoring 1935 | 1936 | // Only reset lock delay if not recently kicked up and not on the floor 1937 | if (!pieceWasKickedUp) { 1938 | lockDelayMoves = 0; // Reset horizontal move counter 1939 | lockDelayCounter = std::chrono::milliseconds(0); // Reset lock delay 1940 | } 1941 | } 1942 | 1943 | // Horizontal movement logic remains the same 1944 | else if (dx != 0) { 1945 | if (isOnFloor()) { 1946 | if (lockDelayMoves < maxLockDelayMoves) { 1947 | lockDelayCounter = std::chrono::milliseconds(0); 1948 | lastRotationOrMoveTime = std::chrono::steady_clock::now(); 1949 | lockDelayMoves++; 1950 | } 1951 | } else { 1952 | lockDelayCounter = std::chrono::milliseconds(0); 1953 | lastRotationOrMoveTime = std::chrono::steady_clock::now(); 1954 | } 1955 | } 1956 | } 1957 | 1958 | return success; 1959 | } 1960 | 1961 | bool rotate() { 1962 | const int previousRotation = currentTetrimino.rotation; 1963 | const int previousX = currentTetrimino.x; 1964 | const int previousY = currentTetrimino.y; 1965 | 1966 | rotatePiece(-1); // Clockwise rotation 1967 | 1968 | // Check if rotation actually succeeded by comparing state 1969 | return (currentTetrimino.rotation != previousRotation || 1970 | currentTetrimino.x != previousX || 1971 | currentTetrimino.y != previousY); 1972 | } 1973 | 1974 | bool rotateCounterclockwise() { 1975 | const int previousRotation = currentTetrimino.rotation; 1976 | const int previousX = currentTetrimino.x; 1977 | const int previousY = currentTetrimino.y; 1978 | 1979 | rotatePiece(1); // Counterclockwise rotation 1980 | 1981 | // Check if rotation actually succeeded by comparing state 1982 | return (currentTetrimino.rotation != previousRotation || 1983 | currentTetrimino.x != previousX || 1984 | currentTetrimino.y != previousY); 1985 | } 1986 | 1987 | bool tSpinOccurred = false; // Add this member to TetrisGui class 1988 | 1989 | void rotatePiece(int direction) { 1990 | std::lock_guard lock(boardMutex); 1991 | 1992 | const int previousRotation = currentTetrimino.rotation; 1993 | const int previousX = currentTetrimino.x; 1994 | const int previousY = currentTetrimino.y; 1995 | 1996 | // O piece doesn't rotate - early return 1997 | if (currentTetrimino.type == 3) { 1998 | return; 1999 | } 2000 | 2001 | // Perform rotation 2002 | currentTetrimino.rotation = (currentTetrimino.rotation + direction + 4) % 4; 2003 | 2004 | const auto& kicks = (currentTetrimino.type == 0) ? wallKicksI : wallKicksJLSTZ; 2005 | 2006 | lastWallKickApplied = false; 2007 | bool rotationSuccessful = false; 2008 | 2009 | // First, check if the piece can fit without any kick 2010 | if (isPositionValid(currentTetrimino, board)) { 2011 | rotationSuccessful = true; 2012 | pieceWasKickedUp = false; 2013 | } else { 2014 | // Calculate the correct wall kick index based on rotation transition 2015 | int kickIndex; 2016 | if (direction < 0) { 2017 | // Clockwise: 0->1, 1->2, 2->3, 3->0 2018 | kickIndex = previousRotation; 2019 | } else { 2020 | // Counter-clockwise: 1->0, 2->1, 3->2, 0->3 2021 | kickIndex = currentTetrimino.rotation; 2022 | } 2023 | 2024 | // Try the standard wall kicks FIRST 2025 | for (int i = 0; i < 5; ++i) { 2026 | const auto& kick = kicks[kickIndex][i]; 2027 | 2028 | // Apply the kick 2029 | currentTetrimino.x = previousX + kick.first; 2030 | currentTetrimino.y = previousY + kick.second; 2031 | 2032 | if (isPositionValid(currentTetrimino, board)) { 2033 | rotationSuccessful = true; 2034 | lastWallKickApplied = (kick.first != 0 || kick.second != 0); 2035 | pieceWasKickedUp = (kick.second < 0); 2036 | break; 2037 | } 2038 | } 2039 | 2040 | // If standard kicks fail, try extra kicks with MORE aggressive options for ALL pieces 2041 | if (!rotationSuccessful) { 2042 | // Extended kicks that work for L, J, and other pieces against walls 2043 | std::array, 16> extraKicks; 2044 | 2045 | if (currentTetrimino.type == 0) { 2046 | // I-piece needs more upward kicks 2047 | extraKicks = {{ 2048 | {0, -1}, {0, -2}, {0, -3}, {0, 1}, 2049 | {1, 0}, {-1, 0}, {2, 0}, {-2, 0}, 2050 | {1, -1}, {-1, -1}, {0, 2}, 2051 | {1, 1}, {-1, 1}, {2, -1}, {-2, -1}, {1, -2} 2052 | }}; 2053 | } else { 2054 | // More comprehensive kicks for L, J, S, T, Z pieces 2055 | extraKicks = {{ 2056 | {0, 1}, {0, -1}, {1, 0}, {-1, 0}, 2057 | {0, 2}, {2, 0}, {-2, 0}, 2058 | {1, 1}, {-1, 1}, {1, -1}, {-1, -1}, 2059 | {0, -2}, {2, 1}, {-2, 1}, {2, -1}, {-2, -1} 2060 | }}; 2061 | } 2062 | 2063 | for (const auto& kick : extraKicks) { 2064 | currentTetrimino.x = previousX + kick.first; 2065 | currentTetrimino.y = previousY + kick.second; 2066 | 2067 | if (isPositionValid(currentTetrimino, board)) { 2068 | rotationSuccessful = true; 2069 | lastWallKickApplied = true; 2070 | pieceWasKickedUp = (kick.second < 0); 2071 | break; 2072 | } 2073 | } 2074 | } 2075 | } 2076 | 2077 | // If rotation failed, revert to the previous state 2078 | if (!rotationSuccessful) { 2079 | currentTetrimino.rotation = previousRotation; 2080 | currentTetrimino.x = previousX; 2081 | currentTetrimino.y = previousY; 2082 | pieceWasKickedUp = false; 2083 | return; // Exit early - no lock delay reset needed 2084 | } 2085 | 2086 | // Reset lock delay only if the rotation was successful 2087 | if (isOnFloor()) { 2088 | if (lockDelayMoves < maxLockDelayMoves) { 2089 | lockDelayCounter = std::chrono::milliseconds(0); 2090 | lastRotationOrMoveTime = std::chrono::steady_clock::now(); 2091 | lockDelayMoves++; 2092 | } 2093 | } else { 2094 | lockDelayCounter = std::chrono::milliseconds(0); 2095 | lastRotationOrMoveTime = std::chrono::steady_clock::now(); 2096 | } 2097 | } 2098 | 2099 | 2100 | bool performedWallKick() { 2101 | return lastWallKickApplied; // Simply return whether the last rotation involved a wall kick 2102 | } 2103 | 2104 | bool isMiniTSpin() { 2105 | if (currentTetrimino.type != 5) return false; // Only T piece can T-Spin 2106 | 2107 | // Mini T-Spins often occur when a rotation involves a wall kick but isn't surrounded as a full T-spin. 2108 | return !isTSpin() && lastWallKickApplied; 2109 | } 2110 | 2111 | bool isTSpin() { 2112 | if (currentTetrimino.type != 5) return false; // Only T piece can T-Spin 2113 | 2114 | // Check corners around the T piece center 2115 | const int centerX = currentTetrimino.x + 1; 2116 | const int centerY = currentTetrimino.y + 1; 2117 | int blockedCorners = 0; 2118 | 2119 | // Check four corners 2120 | if (!isWithinBounds(centerX - 1, centerY - 1) || board[centerY - 1][centerX - 1] != 0) blockedCorners++; 2121 | if (!isWithinBounds(centerX + 1, centerY - 1) || board[centerY - 1][centerX + 1] != 0) blockedCorners++; 2122 | if (!isWithinBounds(centerX - 1, centerY + 1) || board[centerY + 1][centerX - 1] != 0) blockedCorners++; 2123 | if (!isWithinBounds(centerX + 1, centerY + 1) || board[centerY + 1][centerX + 1] != 0) blockedCorners++; 2124 | 2125 | // A T-Spin occurs if 3 or more corners are blocked 2126 | return blockedCorners >= 3 && lastWallKickApplied; 2127 | } 2128 | 2129 | bool isWithinBounds(int x, int y) { 2130 | return x >= 0 && x < BOARD_WIDTH && y >= 0 && y < BOARD_HEIGHT; 2131 | } 2132 | 2133 | 2134 | 2135 | void placeTetrimino() { 2136 | std::lock_guard lock(boardMutex); // Lock the mutex for board access 2137 | bool pieceAboveTop = false; // Track if any part of the piece is above the top of the board 2138 | 2139 | int rotatedIndex; 2140 | int x, y; 2141 | // Place the Tetrimino on the board 2142 | for (int i = 0; i < 4; ++i) { 2143 | for (int j = 0; j < 4; ++j) { 2144 | rotatedIndex = getRotatedIndex(currentTetrimino.type, i, j, currentTetrimino.rotation); 2145 | 2146 | if (tetriminoShapes[currentTetrimino.type][rotatedIndex] != 0) { 2147 | x = currentTetrimino.x + j; 2148 | y = currentTetrimino.y + i; 2149 | 2150 | // If any part of the piece is above the top of the board (y < 0) 2151 | if (y < 0) { 2152 | pieceAboveTop = true; 2153 | continue; // Skip placing this block 2154 | } 2155 | 2156 | // Only place the block if y is within the board (y >= 0) 2157 | if (y >= 0) { 2158 | board[y][x] = currentTetrimino.type + 1; // Place the block 2159 | } 2160 | } 2161 | } 2162 | } 2163 | pieceWasKickedUp = false; 2164 | 2165 | // If any part of the piece was above the top of the board, trigger game over 2166 | if (pieceAboveTop) { 2167 | tetrisElement->gameOver = true; 2168 | return; // Early return to prevent further processing 2169 | } 2170 | 2171 | // Award points for soft drops (apply accumulated points) 2172 | if (totalSoftDropDistance > 0) { 2173 | const int softDropScore = totalSoftDropDistance * 1; // 1 point per row for soft drops 2174 | tetrisElement->setScore(tetrisElement->getScore() + softDropScore); 2175 | } 2176 | 2177 | // Reset drop distance trackers after placement 2178 | totalSoftDropDistance = 0; 2179 | hardDropDistance = 0; 2180 | 2181 | // Reset the swap flag after placing a Tetrimino 2182 | hasSwapped = false; 2183 | 2184 | 2185 | } 2186 | 2187 | // Create line clear particles outside the main line-clear loop to reduce mutex locking time 2188 | void createLineClearParticles(int row) { 2189 | for (int x = 0; x < BOARD_WIDTH; ++x) { 2190 | for (int p = 0; p < 10; ++p) { 2191 | particles.push_back(Particle{ 2192 | static_cast(x * _w + _w / 2), 2193 | static_cast(row * _h + _h / 2), 2194 | (rand() % 100 / 50.0f - 1.0f) * 8, 2195 | (rand() % 100 / 50.0f - 1.0f) * 8, 2196 | 0.5f, 2197 | 1.0f 2198 | }); 2199 | } 2200 | } 2201 | } 2202 | 2203 | void createCenterExplosionParticles() { 2204 | // Calculate the center row of the board 2205 | //int centerRow = BOARD_HEIGHT / 2; 2206 | for (int y = 0; y < BOARD_HEIGHT; ++y) { 2207 | // Generate particles at the center row 2208 | for (int x = 0; x < BOARD_WIDTH; ++x) { 2209 | for (int p = 0; p < 10; ++p) { 2210 | particles.push_back(Particle{ 2211 | static_cast(x * _w + _w / 2), // X position in the center row 2212 | static_cast(y * _h + _h / 2), // Y position in the center row 2213 | (rand() % 100 / 50.0f - 1.0f) * 8, // Random velocity in X direction 2214 | (rand() % 100 / 50.0f - 1.0f) * 8, // Random velocity in Y direction 2215 | 0.5f, // Lifespan 2216 | 1.0f // Initial alpha (fully visible) 2217 | }); 2218 | } 2219 | } 2220 | } 2221 | } 2222 | 2223 | 2224 | // Modify the clearLines function to handle scoring and leveling up 2225 | void clearLines() { 2226 | std::lock_guard particleLock(particleMutex); // Lock the particle system to avoid concurrent access 2227 | std::lock_guard lock(boardMutex); // Lock during line clearing 2228 | 2229 | int linesClearedInThisTurn = 0; 2230 | int totalYPosition = 0; 2231 | 2232 | bool fullLine; 2233 | for (int i = 0; i < BOARD_HEIGHT; ++i) { 2234 | fullLine = true; 2235 | 2236 | for (int j = 0; j < BOARD_WIDTH; ++j) { 2237 | if (board[i][j] == 0) { 2238 | fullLine = false; 2239 | break; 2240 | } 2241 | } 2242 | 2243 | if (fullLine) { 2244 | linesClearedInThisTurn++; 2245 | totalYPosition += i * _h; 2246 | 2247 | // Particle creation moved to another method to reduce mutex lock time 2248 | createLineClearParticles(i); 2249 | 2250 | // Shift rows down after clearing the full line 2251 | for (int y = i; y > 0; --y) { 2252 | for (int x = 0; x < BOARD_WIDTH; ++x) { 2253 | board[y][x] = board[y - 1][x]; 2254 | } 2255 | } 2256 | 2257 | // Clear the top row (with extra check to prevent top-bound crashes) 2258 | for (int x = 0; x < BOARD_WIDTH; ++x) { 2259 | if (board[0][x] != 0) { 2260 | board[0][x] = 0; 2261 | } 2262 | } 2263 | } 2264 | } 2265 | 2266 | // If lines were cleared, update the score and level, and show feedback text 2267 | if (linesClearedInThisTurn > 0) { 2268 | // Update the total lines cleared 2269 | tetrisElement->setLinesCleared(tetrisElement->getLinesCleared() + linesClearedInThisTurn); 2270 | 2271 | int baseScore = 0; 2272 | float backToBackBonus = 1.0f; 2273 | 2274 | // Handle back-to-back bonus 2275 | const bool isBackToBack = (previousClearWasTetris || previousClearWasTSpin) && 2276 | (linesClearedInThisTurn == 4 || isTSpin()); 2277 | 2278 | 2279 | // Track the back-to-back chain count 2280 | //static int backToBackCount = 1; 2281 | if (isBackToBack) { 2282 | backToBackBonus = 1.5f; // 50% bonus for back-to-back Tetrises or T-Spins 2283 | backToBackCount++; // Increment back-to-back count 2284 | } else { 2285 | backToBackCount = 1; // Reset back-to-back count 2286 | } 2287 | 2288 | // Update score based on how many lines were cleared 2289 | switch (linesClearedInThisTurn) { 2290 | case 1: 2291 | if (isTSpin()) { 2292 | baseScore = isMiniTSpin() ? 100 : 400; // Mini T-Spin or T-Spin Single 2293 | } else { 2294 | baseScore = 100; // Single line clear 2295 | } 2296 | break; 2297 | case 2: 2298 | if (isTSpin()) { 2299 | baseScore = 700; // T-Spin Double 2300 | } else { 2301 | baseScore = 300; // Double line clear 2302 | } 2303 | break; 2304 | case 3: 2305 | baseScore = 500; // Triple line clear 2306 | break; 2307 | case 4: 2308 | baseScore = 800; // Base Tetris 2309 | break; 2310 | } 2311 | 2312 | // Apply back-to-back bonus for Tetrises and T-Spins 2313 | if ((linesClearedInThisTurn == 4 || isTSpin()) && isBackToBack) { 2314 | baseScore = static_cast(baseScore * backToBackBonus); // Apply bonus 2315 | } 2316 | 2317 | // Multiply base score by the current level 2318 | const int newScore = baseScore * tetrisElement->getLevel(); 2319 | tetrisElement->setScore(tetrisElement->getScore() + newScore); 2320 | 2321 | // Store the score for the current lines-cleared move in linesClearedScore 2322 | tetrisElement->linesClearedScore = newScore; 2323 | 2324 | // Handle back-to-back state 2325 | if (linesClearedInThisTurn == 4) { 2326 | previousClearWasTetris = true; 2327 | previousClearWasTSpin = false; 2328 | } else if (isTSpin()) { 2329 | previousClearWasTSpin = true; 2330 | previousClearWasTetris = false; 2331 | } else { 2332 | previousClearWasTetris = false; 2333 | previousClearWasTSpin = false; 2334 | } 2335 | 2336 | // Level up after clearing a certain number of lines 2337 | linesClearedForLevelUp += linesClearedInThisTurn; 2338 | if (linesClearedForLevelUp >= LINES_PER_LEVEL) { 2339 | linesClearedForLevelUp -= LINES_PER_LEVEL; // Reset the count for the next level 2340 | tetrisElement->setLevel(tetrisElement->getLevel() + 1); // Increase the level 2341 | } 2342 | 2343 | // Show feedback text based on the number of lines cleared 2344 | switch (linesClearedInThisTurn) { 2345 | case 1: 2346 | tetrisElement->linesClearedText = isTSpin() ? "T-Spin\nSingle" : "Single"; 2347 | break; 2348 | case 2: 2349 | tetrisElement->linesClearedText = isTSpin() ? "T-Spin\nDouble" : "Double"; 2350 | break; 2351 | case 3: 2352 | tetrisElement->linesClearedText = "Triple"; 2353 | break; 2354 | case 4: 2355 | tetrisElement->linesClearedText = isBackToBack ? std::to_string(backToBackCount) + "x Tetris" : "Tetris"; 2356 | break; 2357 | } 2358 | 2359 | tetrisElement->showText = true; 2360 | tetrisElement->fadeAlpha = 0.0f; // Start fade animation 2361 | tetrisElement->textStartTime = std::chrono::steady_clock::now(); // Track animation start time 2362 | 2363 | triggerRumbleDoubleClick.store(true, std::memory_order_release); 2364 | } 2365 | 2366 | 2367 | } 2368 | 2369 | 2370 | void spawnNewTetrimino() { 2371 | triggerRumbleClick.store(true, std::memory_order_release); 2372 | // Move nextTetrimino to currentTetrimino 2373 | currentTetrimino = nextTetrimino; 2374 | 2375 | int rotatedIndex; 2376 | // Calculate the width of the current Tetrimino 2377 | int minX = 4, maxX = -1; // Initialize with the opposite extremes 2378 | for (int i = 0; i < 4; ++i) { 2379 | for (int j = 0; j < 4; ++j) { 2380 | rotatedIndex = getRotatedIndex(currentTetrimino.type, i, j, currentTetrimino.rotation); 2381 | if (tetriminoShapes[currentTetrimino.type][rotatedIndex] != 0) { 2382 | if (j < minX) minX = j; 2383 | if (j > maxX) maxX = j; 2384 | } 2385 | } 2386 | } 2387 | 2388 | // Calculate the actual width of the Tetrimino 2389 | const int pieceWidth = maxX - minX + 1; 2390 | 2391 | // Set the X position to center the Tetrimino on the board 2392 | currentTetrimino.x = (BOARD_WIDTH - pieceWidth) / 2 - minX; 2393 | 2394 | // Move nextTetrimino1 to nextTetrimino 2395 | nextTetrimino = nextTetrimino1; 2396 | 2397 | // Move nextTetrimino2 to nextTetrimino1 2398 | nextTetrimino1 = nextTetrimino2; 2399 | 2400 | // Generate a new random piece for nextTetrimino2 2401 | nextTetrimino2 = Tetrimino(rand() % 7); 2402 | 2403 | // Calculate the bottommost row with a block 2404 | int bottommostRow = -1; 2405 | for (int i = 3; i >= 0; --i) { // Start from bottom and go up 2406 | for (int j = 0; j < 4; ++j) { 2407 | rotatedIndex = getRotatedIndex(currentTetrimino.type, i, j, currentTetrimino.rotation); 2408 | if (tetriminoShapes[currentTetrimino.type][rotatedIndex] != 0) { 2409 | bottommostRow = i; 2410 | break; 2411 | } 2412 | } 2413 | if (bottommostRow != -1) break; // Found the bottommost row 2414 | } 2415 | 2416 | // Set the Y position so the bottommost blocks are at row 0 (1 block into the board) 2417 | currentTetrimino.y = -bottommostRow; 2418 | 2419 | // Check if the new Tetrimino is in a valid position 2420 | if (!isPositionValid(currentTetrimino, board)) { 2421 | // Game over: the new Tetrimino can't be placed 2422 | tetrisElement->gameOver = true; 2423 | } 2424 | } 2425 | 2426 | 2427 | 2428 | }; 2429 | 2430 | class Overlay : public tsl::Overlay { 2431 | public: 2432 | 2433 | virtual void initServices() override { 2434 | tsl::overrideBackButton = true; // for properly overriding the always go back functionality of KEY_B 2435 | ult::createDirectory("sdmc:/config/tetris/"); 2436 | } 2437 | 2438 | virtual void exitServices() override {} 2439 | 2440 | virtual void onShow() override {} 2441 | virtual void onHide() override { TetrisElement::paused = true; } 2442 | 2443 | virtual std::unique_ptr loadInitialGui() override { 2444 | firstLoad = true; 2445 | auto r = initially(); 2446 | gameGui = (TetrisGui*)r.get(); 2447 | return r; 2448 | } 2449 | 2450 | private: 2451 | std::string savedGameData; 2452 | TetrisGui* gameGui; 2453 | }; 2454 | 2455 | /** 2456 | * @brief The entry point of the application. 2457 | * 2458 | * This function serves as the entry point for the application. It takes command-line arguments, 2459 | * initializes necessary services, and starts the main loop of the overlay. The `argc` parameter 2460 | * represents the number of command-line arguments, and `argv` is an array of C-style strings 2461 | * containing the actual arguments. 2462 | * 2463 | * @param argc The number of command-line arguments. 2464 | * @param argv An array of C-style strings representing command-line arguments. 2465 | * @return The application's exit code. 2466 | */ 2467 | int main(int argc, char* argv[]) { 2468 | return tsl::loop(argc, argv); 2469 | } 2470 | --------------------------------------------------------------------------------