Use text renderer

This commit is contained in:
zvon 2020-08-23 09:49:04 +02:00
parent bb0f7d4e75
commit 097fab4329
2 changed files with 21 additions and 26 deletions

View File

@ -145,8 +145,8 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
z->setId(DEATH);
z->setColiderColor("FF00FF");
scene.addObject(z);
auto y = std::make_shared<SDLPP::RectangleRender>(0, 0, 0.2, 0.1, r);
y->setTexture(*font, "DEMO", "#FFFFFF", "#000000", 5);
auto y = std::make_shared<SDLPP::TextRenderer>(0, 0, 0.2, 0.1, r);
y->setText(*font, "DEMO", "#FFFFFF", "#000000", 5);
y->setPermanent(true);
y->setId(123);
scene.addObject(y);
@ -157,19 +157,19 @@ void addPause(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
bg->setId(123);
bg->setPermanent(true);
scene.addObject(bg);
auto y = std::make_shared<SDLPP::RectangleRender>(0.25, 0.1, 0.5, 0.3, r);
y->setTexture(*font, "PAUSED", "#FFFFFF", "#000000", 5);
auto y = std::make_shared<SDLPP::TextRenderer>(0.25, 0.1, 0.5, 0.3, r);
y->setText(*font, "PAUSED", "#FFFFFF", "#000000", 5);
y->setId(0);
y->centerX();
scene.addObject(y);
auto resume = std::make_shared<SDLPP::RectangleRender>(0.4, 0.5, 0.2, 0.1, r);
resume->setTexture(*font, "Resume", "#FFFFFF", "#000000", 5);
auto resume = std::make_shared<SDLPP::TextRenderer>(0.4, 0.5, 0.2, 0.1, r);
resume->setText(*font, "Resume", "#FFFFFF", "#000000", 5);
resume->setColor("#FFFFFF40");
resume->centerX();
scene.addObject(resume);
pause_options.push_back(resume);
auto quit = std::make_shared<SDLPP::RectangleRender>(0.4, 0.7, 0.2, 0.1, r);
quit->setTexture(*font, "Quit Game", "#FFFFFF", "#000000", 5);
auto quit = std::make_shared<SDLPP::TextRenderer>(0.4, 0.7, 0.2, 0.1, r);
quit->setText(*font, "Quit Game", "#FFFFFF", "#000000", 5);
quit->centerX();
scene.addObject(quit);
pause_options.push_back(quit);

View File

@ -15,7 +15,7 @@ int ticks_till_next = 1500;
int ticks_till_fall = 500;
int ticks_till_descend = 50;
std::vector<std::shared_ptr<SDLPP::RectangleRender>> pause_options;
std::shared_ptr<SDLPP::RectangleRender> score_texture;
std::shared_ptr<SDLPP::TextRenderer> score_texture;
std::shared_ptr<SDLPP::Renderer> active_renderer;
int score = 0;
bool update_score = false;
@ -225,12 +225,10 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
auto bottom_barrier = std::make_shared<SDLPP::RectangleRender>(0.28,1,0.44,0.02,r,"#FF000080", true);
bottom_barrier->centerX();
scene.addObject(bottom_barrier);
auto tetris = std::make_shared<SDLPP::RectangleRender>(0.4, 0, 0.2, 0.1, r);
tetris->setTexture(*font, "TETRIS", "#FFFFFF", "#000000", 5);
auto tetris = std::make_shared<SDLPP::TextRenderer>(0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5);
tetris->centerX();
scene.addObject(tetris);
auto next = std::make_shared<SDLPP::RectangleRender>(0.8, 0.35, 0.2, 0.1, r);
next->setTexture(*font, "NEXT", "#FFFFFF", "#000000", 5);
auto next = std::make_shared<SDLPP::TextRenderer>(0.8, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF", "000000", 5, SDLPP_TEXT_CENTER);
next->centerX();
scene.addObject(next);
double posy = 1;
@ -241,15 +239,12 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
gameover->setId(GAME_OVER);
gameover->setColiderColor("FF0000");
scene.addObject(gameover);
auto score_text = std::make_shared<SDLPP::RectangleRender>(0.8, 0.1, 0.2, 0.1, r);
score_text->setTexture(*font, "SCORE", "#FFFFFF", "#000000", 5);
auto score_text = std::make_shared<SDLPP::TextRenderer>(0.8, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF", "#000000", 5, SDLPP_TEXT_CENTER);
score_text->centerX();
scene.addObject(score_text);
score_texture = std::make_shared<SDLPP::RectangleRender>(0.8, 0.2, 0.2, 0.1, r);
score_texture->setTexture(*font, std::to_string(score), "#FFFFFF", "#000000", 5);
score_texture = std::make_shared<SDLPP::TextRenderer>(0.8, 0.2, 0.2, 0.1, r, *font, std::to_string(score), "FFFFFF", "000000", 5, SDLPP_TEXT_TOP);
score_texture->centerX();
score_texture->setId(123);
scene.addObject(score_texture);
for(int i = 0; i < 20; i++) {
posy -= 0.04;
@ -264,7 +259,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
}
void updateScore() {
score_texture->setTexture(*font, std::to_string(score), "#FFFFFF", "#000000", 5);
score_texture->setText(*font, std::to_string(score), "#FFFFFF", "#000000", 5);
}
void addPause(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
@ -272,19 +267,19 @@ void addPause(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
bg->setId(123);
bg->setPermanent(true);
scene.addObject(bg);
auto y = std::make_shared<SDLPP::RectangleRender>(0.25, 0.1, 0.5, 0.3, r);
y->setTexture(*font, "PAUSED", "#FFFFFF", "#000000", 5);
auto y = std::make_shared<SDLPP::TextRenderer>(0.25, 0.1, 0.5, 0.3, r);
y->setText(*font, "PAUSED", "#FFFFFF", "#000000", 5);
y->setId(0);
y->centerX();
scene.addObject(y);
auto resume = std::make_shared<SDLPP::RectangleRender>(0.4, 0.5, 0.2, 0.1, r);
resume->setTexture(*font, "Resume", "#FFFFFF", "#000000", 5);
auto resume = std::make_shared<SDLPP::TextRenderer>(0.4, 0.5, 0.2, 0.1, r);
resume->setText(*font, "Resume", "#FFFFFF", "#000000", 5);
resume->setColor("#FFFFFF40");
resume->centerX();
scene.addObject(resume);
pause_options.push_back(resume);
auto quit = std::make_shared<SDLPP::RectangleRender>(0.4, 0.7, 0.2, 0.1, r);
quit->setTexture(*font, "Quit Game", "#FFFFFF", "#000000", 5);
auto quit = std::make_shared<SDLPP::TextRenderer>(0.4, 0.7, 0.2, 0.1, r);
quit->setText(*font, "Quit Game", "#FFFFFF", "#000000", 5);
quit->centerX();
scene.addObject(quit);
pause_options.push_back(quit);
@ -613,7 +608,7 @@ int main() {
frames++;
if(SDL_GetTicks() - base >= 1000) {
base = SDL_GetTicks();
printf("FPS: %d\n", frames);
std::cout << "FPS: " << frames << std::endl;
frames = 0;
}
}