| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2018 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | https://blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #include "HelpHowtoState.hpp" |
| 25 | #include "global.hpp" |
| 26 | #include "common.h" |
| 27 | #include "MenuSystem.h" |
| 28 | #include <cmath> |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Draws bricks with a string like: |
| 33 | * "aab" for two identical one and another |
| 34 | * "aaB" the third one will have a bomb |
| 35 | * The any char not in 'a' to 'g' or 'A' to 'G' the behavior is undefined. |
| 36 | * @param target Target to draw to |
| 37 | * @param bricks description on what to draw as a string |
| 38 | * @param x |
| 39 | * @param y |
| 40 | */ |
| 41 | static void RenderRowOfBricks(SDL_Renderer* target, const std::string& brickStr, int x, int y) { |
| 42 | Uint32 tick = SDL_GetTicks(); |
| 43 | for (size_t i = 0; i < brickStr.size(); ++i) { |
| 44 | bool bomb = false; |
| 45 | char brickChar = brickStr[i]; |
| 46 | if (brickChar >= 'A' && brickChar <= 'G') { |
| 47 | bomb = true; |
| 48 | brickChar = brickChar + 'a' - 'A'; |
| 49 | } |
| 50 | if (brickChar >= 'a' && brickChar <= 'g') { |
| 51 | globalData.bricks[brickChar - 'a'].Draw(target, tick, x+i*50, y, &globalData.logicalResize); |
| 52 | if (bomb) { |
| 53 | globalData.spriteHolder->GetSprite("block_bomb").Draw(target, tick, x+i*50, y, &globalData.logicalResize); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | class HorizontalSwitchAnimation { |
| 60 | public: |
| 61 | std::string brickStr = "abc"; |
| 62 | int cursorPos = 0; |
| 63 | int state = 0; //0=move left, 1 = switch, 2 = move right, 3 = switch |
| 64 | Uint32 lastTick = 0; |
| 65 | Uint32 animationSpeed = 2000; |
| 66 | |
| 67 | void Update (Uint32 tick) { |
| 68 | if (tick > lastTick + animationSpeed) { |
| 69 | lastTick = tick; |
| 70 | switch (state) { |
| 71 | case 0: |
| 72 | cursorPos = 1; |
| 73 | break; |
| 74 | case 1: //fallthough |
| 75 | case 3: |
| 76 | std::swap(brickStr[cursorPos], brickStr[cursorPos + 1]); |
| 77 | break; |
| 78 | case 2: |
| 79 | cursorPos = 0; |
| 80 | break; |
| 81 | } |
| 82 | ++state; |
| 83 | if (state > 3) { |
| 84 | state = 0; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | class MultiLineBlocks { |
| 91 | private: |
| 92 | std::vector<std::string> lines; |
| 93 | public: |
| 94 | MultiLineBlocks& addLine(const std::string& line) { |
| 95 | lines.push_back(line); |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | void Render(SDL_Renderer* target, int x, int y) { |
| 100 | for (size_t i = 0; i < lines.size(); ++i) { |
| 101 | RenderRowOfBricks(target, lines[i], x, y+i*50); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | }; |
| 106 | |
| 107 | HorizontalSwitchAnimation switchAnimation; |
| 108 | sago::SagoTextField switchAnimationField; |
| 109 | sago::SagoTextField clearRowfield; |
| 110 | sago::SagoTextField comboField; |
| 111 | sago::SagoTextField dropField; |
| 112 | sago::SagoTextField chainField; |
| 113 | |
| 114 | static void InitTextField(sago::SagoTextField& field, const char* text) { |
| 115 | field.SetHolder(&globalData.spriteHolder->GetDataHolder()); |
| 116 | field.SetFontSize(30); |
| 117 | field.SetOutline(2, {0,0,0,255}); |
| 118 | field.SetText(text); |
| 119 | } |
| 120 | |
| 121 | HelpHowtoState::HelpHowtoState() { |
| 122 | InitTextField(switchAnimationField, _("Switch block horizontally")); |
| 123 | InitTextField(clearRowfield, _("Match 3 to clear")); |
| 124 | InitTextField(comboField, _("Create combos!")); |
| 125 | InitTextField(dropField, _("Drop blocks!")); |
| 126 | InitTextField(chainField, _("Create a chain effect")); |
| 127 | } |
| 128 | |
| 129 | HelpHowtoState::~HelpHowtoState() { |
| 130 | } |
| 131 | |
| 132 | const double PI =3.141592653589793238463; |
| 133 | |
| 134 | static void RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2) { |
| 135 | globalData.logicalResize.LogicalToPhysical(&x1, &y1); |
| 136 | globalData.logicalResize.LogicalToPhysical(&x2, &y2); |
| 137 | SDL_RenderDrawLine(renderer, x1, y1, x2, y2); |
| 138 | } |
| 139 | |
| 140 | static void DrawArrow(SDL_Renderer* target, int x1, int y1, int x2, int y2) { |
| 141 | double dx = x1-x2; |
| 142 | double dy = y1-y2; |
| 143 | double distance = std::sqrt((dx*dx)+(dy*dy)); |
| 144 | dx = dx * 10.0 / distance; |
| 145 | dy = dy * 10.0 / distance; |
| 146 | double angle= PI/4.0; |
| 147 | double nx1 = dx * std::cos(angle) - dy * std::sin(angle) + x2; |
| 148 | double ny1 = dx * std::sin(angle) + dy * std::cos(angle) + y2; |
| 149 | RenderDrawLine(target, x1, y1, x2, y2); |
| 150 | RenderDrawLine(target, nx1, ny1, x2, y2); |
| 151 | nx1 = dx * std::cos(-angle) - dy * std::sin(-angle) + x2; |
| 152 | ny1 = dx * std::sin(-angle) + dy * std::cos(-angle) + y2; |
| 153 | RenderDrawLine(target, nx1, ny1, x2, y2); |
| 154 | } |
| 155 | |
| 156 | void HelpHowtoState::Draw(SDL_Renderer* target) { |
| 157 | DrawBackground(target); |
| 158 | SDL_SetRenderDrawColor(target, 0, 0, 0, SDL_ALPHA_OPAQUE); |
| 159 | RenderRowOfBricks(target, switchAnimation.brickStr, 50, 50); |
| 160 | globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+switchAnimation.cursorPos*50, 50, &globalData.logicalResize); |
| 161 | switchAnimationField.Draw(target, 50 +150+30, 50+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center, &globalData.logicalResize); |
| 162 | RenderRowOfBricks(target, "adaa", 50, 150); |
| 163 | globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 150, &globalData.logicalResize); |
| 164 | RenderRowOfBricks(target, "dAAA", 50+300, 150); |
| 165 | DrawArrow(target, 50+200+25, 150+25, 50+300-25, 150+25); |
| 166 | clearRowfield.Draw(target, 600, 150+25, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center, &globalData.logicalResize); |
| 167 | comboField.Draw(target, 50+175, 410, sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize); |
| 168 | MultiLineBlocks().addLine("ab").addLine("ba").addLine("ab").Render(target, 50, 250); |
| 169 | globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 250+50, &globalData.logicalResize); |
| 170 | MultiLineBlocks().addLine("AB").addLine("AB").addLine("AB").Render(target, 50+200, 250); |
| 171 | DrawArrow(target, 175, 325, 225, 325); |
| 172 | MultiLineBlocks().addLine("a").addLine("b").addLine("e").Render(target, 50+400, 250); |
| 173 | globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50+400, 250, &globalData.logicalResize); |
| 174 | MultiLineBlocks().addLine(" ").addLine("b").addLine("ea").Render(target, 50+400+200, 250); |
| 175 | dropField.Draw(target, 50+400+150, 410, sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize); |
| 176 | DrawArrow(target, 575, 325, 625, 325); |
| 177 | DrawArrow(target, 475, 275, 525, 275); |
| 178 | DrawArrow(target, 525, 275, 525, 375); |
| 179 | DrawArrow(target, 675, 275, 725, 275); |
| 180 | DrawArrow(target, 725, 275, 725, 375); |
| 181 | MultiLineBlocks().addLine(" d").addLine(" f").addLine(" f").addLine("fdd").Render(target, 50, 500); |
| 182 | MultiLineBlocks().addLine(" d").addLine(" F").addLine(" F").addLine("dFd").Render(target, 50+200, 500); |
| 183 | MultiLineBlocks().addLine(" d").addLine(" ").addLine(" ").addLine("d d").Render(target, 50+200*2, 500); |
| 184 | MultiLineBlocks().addLine(" ").addLine(" ").addLine(" ").addLine("DDD").Render(target, 50+200*3, 500); |
| 185 | globalData.spriteHolder->GetSprite("cursor").Draw(target, SDL_GetTicks(), 50, 650, &globalData.logicalResize); |
| 186 | DrawArrow(target, 200, 600, 250, 600); |
| 187 | DrawArrow(target, 400, 600, 450, 600); |
| 188 | DrawArrow(target, 600, 600, 650, 600); |
| 189 | DrawArrow(target, 525, 525, 525, 675); |
| 190 | chainField.Draw(target, 400, 710, sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize); |
| 191 | HelpCommonState::Draw(target); |
| 192 | } |
| 193 | |
| 194 | void HelpHowtoState::Update() { |
| 195 | switchAnimation.Update(SDL_GetTicks()); |
| 196 | HelpCommonState::Update(); |
| 197 | } |