-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing ChatBalloon #13 [ci skip]
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. | ||
* If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* This Source Code Form is "Incompatible With Secondary Licenses", | ||
* as defined by the Mozilla Public License, v. 2.0. | ||
*/ | ||
|
||
#include <SDL2/SDL_ttf.h> | ||
#include <SDL2/SDL2_gfxPrimitives.h> | ||
#include "chatballoon.hpp" | ||
using namespace std; | ||
|
||
/** | ||
* Constructor | ||
* \param text message to display | ||
* \param x x coord of the tip (bottom) of the balloon | ||
* \param y y coord of the tip (bottom) of the balloon | ||
*/ | ||
ChatBalloon::ChatBalloon(const string &text, const int x, const int y) : message(text), | ||
_x(x), _y(y), nbCharsDisplayed(1), creationTime(SDL_GetTicks()) { | ||
if(message.size() == 0) { | ||
message = " "; | ||
} | ||
} | ||
|
||
ChatBalloon::~ChatBalloon() { | ||
|
||
} | ||
|
||
void ChatBalloon::draw(SDL_Renderer *renderer) { | ||
if(nbCharsDisplayed < message.size()) { | ||
Uint32 delta = SDL_GetTicks() - creationTime; | ||
nbCharsDisplayed = 1 + delta / (1000 / CHARACTERS_PER_SECOND); | ||
} | ||
|
||
int textWidth = nbCharsDisplayed >= BALLOON_WIDTH ? BALLOON_WIDTH : nbCharsDisplayed; | ||
int nbLines = 1 + nbCharsDisplayed / BALLOON_WIDTH; | ||
if(nbCharsDisplayed % BALLOON_WIDTH == 0) { | ||
nbLines -= 1; | ||
} | ||
|
||
// Draw tip | ||
filledTrigonRGBA(renderer, _x, _y, _x + 10, _y - 10, _x - 10, _y - 10, 0xff, 0xff, 0xff, 0xff); | ||
|
||
// Draw balloon | ||
|
||
// Draw text, line by line | ||
|
||
} | ||
|
||
bool ChatBalloon::isVisible() const { | ||
return SDL_Ticks() <= creationTime + FULL_DELAY + (message.size() - 1) * 1000 / CHARACTERS_PER_SECOND; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. | ||
* If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* This Source Code Form is "Incompatible With Secondary Licenses", | ||
* as defined by the Mozilla Public License, v. 2.0. | ||
*/ | ||
|
||
#include <SDL2/SDL.h> | ||
#include <string> | ||
|
||
#ifndef _H_CHATBALLOON_ | ||
#define _H_CHATBALLOON_ | ||
|
||
class ChatBalloon final { | ||
public: | ||
const int BALLOON_WIDTH = 20; //! in characters | ||
const int CHARACTERS_PER_SECOND = 10; //! used when deploying the balloon | ||
const Uint32 FULL_DELAY = 5000; //! time to leave the full balloon visible | ||
|
||
ChatBalloon(const std::string &text, const int x, const int y); | ||
~ChatBalloon(); | ||
|
||
void draw(SDL_Renderer *renderer); | ||
bool isVisible() const; | ||
|
||
private: | ||
std::string message; | ||
int _x; | ||
int _y; | ||
int nbCharsDisplayed; | ||
Uint32 creationTime; | ||
|
||
const int PADDING = 5; //! in pixels | ||
}; | ||
|
||
#endif // _H_CHATBALLOON_ |