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