git repos / blockattack-game

blame: source/code/HelpHowtoState.cpp

normal view · raw

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