Skip to content

Commit e9b8457

Browse files
committed
Generate Died and Defeated messages dynamically
1 parent 9b2fbf5 commit e9b8457

6 files changed

Lines changed: 67 additions & 16 deletions

File tree

assets/fonts/FreeSerif.ttf

3.15 MB
Binary file not shown.

assets/fonts/FreeSerifItalic.ttf

900 KB
Binary file not shown.

include/Draw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class Draw {
2525

2626
private:
2727
SDL_Renderer * renderer;
28-
TTF_Font * font;
28+
TTF_Font * font_custom;
29+
TTF_Font * font_text;
30+
TTF_Font * font_title;
2931
};
3032

3133
#endif // DRAW_H

include/Tile.h

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,20 +1045,15 @@ class EnemyNuke : public Nuke
10451045

10461046
};
10471047

1048-
class Message : public Static
1048+
class MessageDied : public Static
10491049
{
10501050
public:
1051-
Message(int x, int y, Draw * draw, Textures * textures, Sound * sound, Board * board, int r=0) : Static(x,y,draw,textures,sound,board,r) {
1052-
SetRotation(r);
1051+
MessageDied(int x, int y, Draw * draw, Textures * textures, Sound * sound, Board * board, int r=0) : Static(x,y,draw,textures,sound,board,r) {
10531052

10541053
}
10551054
virtual BlockType GetBlockType() {
10561055
return BlockType::NONE;
10571056
}
1058-
virtual bool SetRotation(int r) {
1059-
rotation=r;
1060-
return true;
1061-
}
10621057

10631058
protected: //functions
10641059
virtual void Display() {
@@ -1076,6 +1071,56 @@ class Message : public Static
10761071

10771072
};
10781073

1074+
class MessageDefeated : public Static
1075+
{
1076+
public:
1077+
MessageDefeated(int x, int y, Draw * draw, Textures * textures, Sound * sound, Board * board, int r=0) : Static(x,y,draw,textures,sound,board,r) {
1078+
1079+
}
1080+
virtual BlockType GetBlockType() {
1081+
return BlockType::NONE;
1082+
}
1083+
1084+
protected: //functions
1085+
virtual void Display() {
1086+
const char * messages[] = {
1087+
"Press Enter to continue",
1088+
};
1089+
draw->BlitMessage("You Defeated Your Enemies!", messages, 1);
1090+
}
1091+
1092+
protected: //members
1093+
private: // functions
1094+
private: //members
1095+
1096+
};
1097+
1098+
class MessageCredits : public Static
1099+
{
1100+
public:
1101+
MessageCredits(int x, int y, Draw * draw, Textures * textures, Sound * sound, Board * board, int r=0) : Static(x,y,draw,textures,sound,board,r) {
1102+
SetRotation(r);
1103+
1104+
}
1105+
virtual BlockType GetBlockType() {
1106+
return BlockType::NONE;
1107+
}
1108+
virtual bool SetRotation(int r) {
1109+
rotation=r;
1110+
return true;
1111+
}
1112+
1113+
protected: //functions
1114+
virtual void Display() {
1115+
draw->BlitSquare(textures->getMessageSprite(), rotation%14 ,rotation/14, x_pos, y_pos);
1116+
}
1117+
1118+
protected: //members
1119+
private: // functions
1120+
private: //members
1121+
1122+
};
1123+
10791124
class BarsVert : public Static
10801125
{
10811126
public:

src/Board.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void Board::YouDied() {
193193
for (int y=ROWS/2-2, yy=0; y<ROWS/2+1; y++, yy++) {
194194
for (int x=COLUMNS/2-3, xx=0; x<COLUMNS/2+3; x++, xx++) {
195195
Tile* temp=array[x][y]->over;
196-
array[x][y]->over=new Message(x,y,draw,textures,sound,this,yy*14+xx);
196+
array[x][y]->over=new MessageDied(x,y,draw,textures,sound,this,yy*14+xx);
197197
if (temp) array[x][y]->ground->AddDead(temp);
198198
}
199199
}
@@ -208,7 +208,7 @@ void Board::YouDefeated() {
208208
for (int y=ROWS/2-2, yy=0; y<ROWS/2+1; y++, yy++) {
209209
for (int x=COLUMNS/2-4, xx=6; x<COLUMNS/2+4; x++, xx++) {
210210
Tile* temp=array[x][y]->over;
211-
array[x][y]->over=new Message(x,y,draw,textures,sound,this, yy*14+xx);
211+
array[x][y]->over=new MessageDefeated(x,y,draw,textures,sound,this, yy*14+xx);
212212
if (temp) array[x][y]->ground->AddDead(temp);
213213
}
214214
}
@@ -219,7 +219,7 @@ void Board::Credits() {
219219
for (int y=ROWS/2-5, yy=3; y<ROWS/2+6; y++, yy++) {
220220
for (int x=COLUMNS/2-7, xx=0; x<COLUMNS/2+7; x++, xx++) {
221221
Tile* temp=array[x][y]->over;
222-
array[x][y]->over=new Message(x,y,draw,textures,sound,this,yy*14+xx);
222+
array[x][y]->over=new MessageCredits(x,y,draw,textures,sound,this,yy*14+xx);
223223
if (temp) array[x][y]->ground->AddDead(temp);
224224
}
225225
}

src/Draw.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66
Draw::Draw(SDL_Renderer * renderer) : renderer(renderer) {
77
animation = 0;
8-
font = TTF_OpenFont(getAssetPath("fonts", "FreeSans.ttf").c_str(), 14);
8+
font_custom = TTF_OpenFont(getAssetPath("fonts", "FreeSans.ttf").c_str(), 14);
9+
font_text = TTF_OpenFont(getAssetPath("fonts", "FreeSerifItalic.ttf").c_str(), 24);
10+
font_title = TTF_OpenFont(getAssetPath("fonts", "FreeSerifItalic.ttf").c_str(), 28);
911
}
1012

1113
Draw::~Draw() {
12-
TTF_CloseFont(font);
14+
TTF_CloseFont(font_custom);
15+
TTF_CloseFont(font_text);
16+
TTF_CloseFont(font_title);
1317
}
1418

1519
void Draw::BlitSquare(SDL_Texture * texture, int x, int y, int dx, int dy) {
@@ -93,7 +97,7 @@ void Draw::BlitBeam(int rotation, int x, int y) {
9397
void Draw::BlitMessage(const char *title, const char **lines, int line_count) {
9498
int widest_line = 0;
9599
int text_height = 0;
96-
SDL_Surface * title_surface = TTF_RenderText_Solid(font, title, {0, 0, 0, 255});
100+
SDL_Surface * title_surface = TTF_RenderText_Solid(font_title, title, {0, 0, 0, 255});
97101
if (title_surface == NULL) {
98102
SDL_Log("Couldn't create surface for text %s: %s", title, TTF_GetError());
99103
return;
@@ -112,7 +116,7 @@ void Draw::BlitMessage(const char *title, const char **lines, int line_count) {
112116
// Draw each line of text
113117
SDL_Texture ** line_textures = (SDL_Texture **) SDL_calloc(line_count, sizeof(SDL_Texture *));
114118
for (int i = 0; i < line_count; i++) {
115-
SDL_Surface * surface = TTF_RenderText_Solid(font, lines[i], {0, 0, 0, 255});
119+
SDL_Surface * surface = TTF_RenderText_Solid(font_text, lines[i], {0, 0, 0, 255});
116120
if (surface == NULL) {
117121
SDL_Log("Couldn't create surface for line %s: %s", lines[i], TTF_GetError());
118122
return;
@@ -208,7 +212,7 @@ void Draw::BlitMessageBox(SDL_Rect *box) {
208212
}
209213

210214
void Draw::BlitText(char * text, int x, int y) {
211-
SDL_Surface * surface = TTF_RenderText_Solid(font, text, {0, 255, 0, 255});
215+
SDL_Surface * surface = TTF_RenderText_Solid(font_custom, text, {0, 255, 0, 255});
212216
if (surface == NULL) {
213217
SDL_Log("Couldn't create surface for text %s: %s", text, TTF_GetError());
214218
return;

0 commit comments

Comments
 (0)