From 490efb8b0b7d60211c1367b9c4dbb0bfc1c27327 Mon Sep 17 00:00:00 2001 From: Claudio Hoffmann <hoffmanncl72341@th-nuernberg.de> Date: Sat, 16 Jan 2021 03:21:30 +0100 Subject: [PATCH] Add a diagonal fade out effect to the Game Over screen --- Entity.cpp | 5 +++++ Entity.hpp | 1 + Level.cpp | 3 ++- PixelShaders.cpp | 25 +++++++++++++++++++++++++ PixelShaders.hpp | 11 +++++++++++ main.cpp | 17 +++++++++++++---- 6 files changed, 57 insertions(+), 5 deletions(-) diff --git a/Entity.cpp b/Entity.cpp index df138d4..82ad6c3 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -39,6 +39,11 @@ float Entity::getTop() const return topleft.Y; } +int Entity::getHitPoints() const +{ + return hitpoints; +} + float Entity::getHitTime() const { return hittime; diff --git a/Entity.hpp b/Entity.hpp index 64b1a72..8fb4f49 100644 --- a/Entity.hpp +++ b/Entity.hpp @@ -39,6 +39,7 @@ public: float getLeft() const; float getTop() const; + int getHitPoints() const; float getHitTime() const; /// ---------------- diff --git a/Level.cpp b/Level.cpp index 91893ea..cec0902 100644 --- a/Level.cpp +++ b/Level.cpp @@ -145,7 +145,8 @@ void Level::enemyVector_PushBack(std::unique_ptr<Enemy> enemy_) bool Level::doDoSteps(FloatSeconds const &frame_time) { - if(!player.step(player.getTopLeft(), frame_time, *this)) { + player.step(player.getTopLeft(), frame_time, *this); + if(player.getHitPoints() == 0) { return false; } diff --git a/PixelShaders.cpp b/PixelShaders.cpp index 99000fa..376ec5e 100644 --- a/PixelShaders.cpp +++ b/PixelShaders.cpp @@ -24,3 +24,28 @@ Color HitShader::peekUnchecked(PixelPoint const &pos) const } return (dist(rng.rng) > 0.95f) ? color : (is_final ? _t : src); } + +Color DiagonalFadeoutShader::peekUnchecked(PixelPoint const &pos) const +{ + // (percent * 1) would only correspond to the top-left triangle. + auto src = input.peek(pos); + auto edge_right = (input.w() * (percent * 2)); + auto edge_bottom = (input.h() * (percent * 2)); + // y = mx + t + + // Top right point: 0 = m·[edge_right] + t + // Bottom left point: [edge_bottom] = m·0 + t + + // 0 = m·[edge_right] + [edge_bottom] + // -[edge_bottom] = m·[edge_right] + // m = -[edge_bottom]/[edge_right] + auto t = edge_bottom; + auto m = (-edge_bottom / edge_right); + + auto x_end = ((pos.y - t) / m); + + if((pos.x & 0x1) ^ ((pos.y & 0x1) == 1)) { + return src; + } + return (pos.x <= x_end) ? _0 : src; +} diff --git a/PixelShaders.hpp b/PixelShaders.hpp index 7dd2d05..bb20dc2 100644 --- a/PixelShaders.hpp +++ b/PixelShaders.hpp @@ -21,4 +21,15 @@ public: virtual Color peekUnchecked(PixelPoint const &pos) const; }; +class DiagonalFadeoutShader : public ShadedPixelBuffer { +public: + float percent; + + DiagonalFadeoutShader(PixelBuffer const &input, float percent) : + ShadedPixelBuffer(input), percent(percent) { + } + + virtual Color peekUnchecked(PixelPoint const &pos) const; +}; + #endif /* PIXELSHADERS_HPP */ diff --git a/main.cpp b/main.cpp index ae981af..d9f5f85 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include <sstream> #include "PixelBuffer.hpp" // hoffmanncl72341 +#include "PixelShaders.hpp" // hoffmanncl72341 #include "Platform_Win32Console.hpp" // hoffmanncl72341 #include "Level.hpp" // kruegerzo72182 #include "Enemy.hpp" // hauerch71498 @@ -36,6 +37,7 @@ class GameLoop { int frame = 0; FloatSeconds time_elapsed{ 0.0f }; bool GameOverFlag = false; //hauerch71498 + float GameOverTime = 0.0f; DynamicPixelBuffer framebuffer = {screenWidth, screenHeigth}; @@ -112,22 +114,29 @@ public: // <kruegerzo72182 // player movement player_.move(platform, frame_time, level); + } else { + GameOverTime += frame_time.count(); + } // enemy movement GameOverFlag = (level.doDoSteps(frame_time) == false); - } - // Output to the console, Gamelogic should happen before this - framebuffer.blit_topleft({ 0, 0 }, level.draw()); - // kruegerzo72182> + // Output to the console, Gamelogic should happen before this + auto const &levelBuffer = level.draw(); if(GameOverFlag) { + DiagonalFadeoutShader dfs = {levelBuffer, (GameOverTime / 2.0f)}; + framebuffer.blit_topleft({ 0, 0 }, dfs); + Score Highscore; Highscore.SetScoreSprite(level.getScore()); framebuffer.blit_topleft(Highscore.GameOverPos,Highscore.getGameOverSprite()); for(auto & i : Highscore.ScoreBoard){ framebuffer.blit_topleft(i.ScorePos,i.getScoreSprite()); } + } else { + framebuffer.blit_topleft({ 0, 0 }, levelBuffer); } + // kruegerzo72182> // <hoffmanncl72341 platform.render(framebuffer); -- GitLab