From d6badb880d675fa9e74c221519fb91960730d0c0 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 28 Feb 2021 21:39:00 +0300 Subject: [PATCH] empty die + --- Image.cpp | 12 +++++++++ Image.h | 8 +++--- LiveObjSprite.h | 3 +++ Player.cpp | 9 +++++++ Player.h | 10 +++++++- Sprite.h | 2 ++ main.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++---- 7 files changed, 100 insertions(+), 9 deletions(-) diff --git a/Image.cpp b/Image.cpp index dca8354..e943c58 100644 --- a/Image.cpp +++ b/Image.cpp @@ -159,6 +159,18 @@ void Image::Draw(Image &canvas, Point p, bool flip_OX, bool flip_OY, bool not_mi } } + +void Image::Draw(Image &canvas, std::function PixFunc) const +{ + int c_w = Width(); + int c_h = Height(); + + #pragma omp parallel for + for (int y = 0; y < c_h; y++) + for (int x = 0; x < c_w; x++) + canvas.SetPixel(x, y, PixFunc(GetPixel(x, y))); +} + void Image::FastDraw(Image &canvas, int lines, int from_line) const { std::memcpy(canvas.data + (from_line * canvas.width), data, canvas.width * lines * sizeof(Pixel)); diff --git a/Image.h b/Image.h index 714f309..16a9a19 100644 --- a/Image.h +++ b/Image.h @@ -65,11 +65,11 @@ struct Image Image(const Image ©); Image(Image && a) noexcept; - Image &operator=(const Image &other) + /*Image &operator=(const Image &other) { error("oh no..."); return *this; - } + }*/ Image &operator=(Image &&other) noexcept { @@ -101,9 +101,11 @@ struct Image inline void SetPixel(int x, int y, const Pixel &pix) { data[width * y + x] = pix; } void Draw(Image &canvas, Point p, bool flip_OX = false, bool flip_OY = false, bool not_mix = false) const; - + void FastDraw(Image &canvas, int lines, int from_line = 0) const; + void Draw(Image &canvas, std::function PixFunc) const; + /// chnage image : image[x,y] = PixFunc(image[x,y]) /// if we know that dif color on that image few then we can hash PixFunc result value void PixelsChange(std::function PixFunc, bool with_hash_pixel); diff --git a/LiveObjSprite.h b/LiveObjSprite.h index 9c8da92..69b6327 100644 --- a/LiveObjSprite.h +++ b/LiveObjSprite.h @@ -63,6 +63,9 @@ struct LiveObjSprite cur_spr->Draw(canvas, p, flip_OX, flip_OY); } + const Image& GetImage(E_LiveObjState state, E_Dir dir, int frame) const {return spr_states.find(state)->second.find(dir)->second.GetFrame(frame);} + const Image& GetImage(E_LiveObjState state, int frame) const {return spr_states.find(state)->second.find(cur_dir)->second.GetFrame(frame);} + private: void SpritePrepare() { diff --git a/Player.cpp b/Player.cpp index f88150e..a79f347 100644 --- a/Player.cpp +++ b/Player.cpp @@ -50,11 +50,20 @@ void Player::RefreshMoveState(E_X_Dir x, E_Y_Dir y, bool ctrl) //printf(Moved() ? "move\n" : "not move\n"); } +void Player::DieDraw(Image &canvas, double proc) +{ + if (!died)return; + SetPosY(-30 + -50 + (50 + die_pos.y) * (1 - proc)); + spr.GetImage(E_LiveObjState::Idle, 0).Draw(canvas, coords, false); +} + void Player::Draw(Image &screen) { + if (died)return; if (now_attack && GameTime::Now().TimeCome(blocked_to_time)) { now_attack = false; spr.SetState(E_LiveObjState::Idle); } spr.Draw(screen, coords, true); + } \ No newline at end of file diff --git a/Player.h b/Player.h index 8afc7d1..a9da2ba 100644 --- a/Player.h +++ b/Player.h @@ -23,6 +23,7 @@ private: static Player *player_getter; bool Moved(); void Draw(Image &screen); + void DieDraw(Image &canvas, double proc); inline void RefreshMoveState(bool up, bool down, bool left, bool right, bool ctrl) { @@ -39,11 +40,15 @@ private: static Player *player_getter; auto empty = GameMap::E_TileType::Empty; if ((GameMap::GetCur()->PointType(Point {.x = cur_pos.x + 12, .y = cur_pos.y + 1}) == empty) && (GameMap::GetCur()->PointType(Point {.x = cur_pos.x + 19, .y = cur_pos.y + 1}) == empty)) { - std::cout << "you stay on empty" << std::endl; + die_pos = coords; + died = true; } } Point GetPos()const { return coords; } + bool GetIsDied()const { return died; } + + void SetPosY(int y) { coords.y = y; } private: E_Dir back_dir = E_Dir::DOWN; @@ -61,6 +66,9 @@ private: static Player *player_getter; double blocked_to_time = -0.0; int keys = 0; + + bool died = false; + Point die_pos; }; #endif //MAIN_PLAYER_H diff --git a/Sprite.h b/Sprite.h index d18ee36..3b9301e 100644 --- a/Sprite.h +++ b/Sprite.h @@ -25,6 +25,8 @@ struct Sprite int Width()const { return imgs[frame_now].Width(); } int Height()const { return imgs[frame_now].Height(); } + + const Image &GetFrame(int frame) const { return imgs.at(frame); } private: void SpriteFromImg(Image &img, int p_frames, int ms_on_frame = 125, int scale = 1); diff --git a/main.cpp b/main.cpp index 4feef37..f268ed5 100644 --- a/main.cpp +++ b/main.cpp @@ -122,6 +122,32 @@ int initGL() return 0; } +Pixel lin_interp(Pixel from, Pixel to, double proc) +{ + uint8_t r = (uint8_t)(from.r + (to.r - from.r) * proc); + uint8_t g = (uint8_t)(from.g + (to.g - from.g) * proc); + uint8_t b = (uint8_t)(from.b + (to.b - from.b) * proc); + uint8_t a = 255;// (uint8_t)(from.a + (to.a - from.a) * proc); + return Pixel {r,g,b,a}; +} + +void Image_Draw_SpeedUp_Die(Image& screen_save, Image &canvas, double proc) +{ + int c_w = screen_save.Width(); + int c_h = screen_save.Height(); + + #pragma omp parallel for + for (int y = 0; y < c_h; y++) + for (int x = 0; x < c_w; x++) { + auto from = screen_save.GetPixel(x, y); + uint8_t r = (uint8_t)(from.r + (127 - from.r) * proc); + uint8_t g = (uint8_t)(from.g + (20 - from.g) * proc); + uint8_t b = (uint8_t)(from.b + (27 - from.b) * proc); + uint8_t a = 255; + canvas.SetPixel(x, y, Pixel {r,g,b,a}); + } +} + int main(int argc, char** argv) { if (!glfwInit())return -1; @@ -176,22 +202,51 @@ int main(int argc, char** argv) double sec = GameTime::Now().GetTime(); int frames = 0; + bool is_alive = true; + Image for_die = Image::Image(0, 0, 0); + + double die_time = 0; + double die_duration = 3; + while (!glfwWindowShouldClose(window)) { GameTime::Now().SetCur(glfwGetTime()); if (GameTime::Now().GetTime() - sec > 1) { sec = GameTime::Now().GetTime(); - std::cout <<"fps : " << frames << std::endl; + std::cout << "fps : " << frames << std::endl; frames = 0; } frames++; - + glfwPollEvents(); - gmap.Draw(screenBuffer); - processPlayerMovement(player); - player.Draw(screenBuffer); + if (is_alive) { + + gmap.Draw(screenBuffer); + + processPlayerMovement(player); + is_alive = !player.GetIsDied(); + + if (!is_alive) { // die + for_die = Image {screenBuffer}; + die_time = GameTime::Now().GetTime(); + } + else player.Draw(screenBuffer); + } + if(!is_alive) { + double proc = GameTime::Now().GetSecAfter(die_time) / die_duration; + if (proc > 1) { + for_die.Draw(screenBuffer, [&](auto x) {return lin_interp(x, Pixel {100, 20, 27, 255}, 1); }); + player.DieDraw(screenBuffer, 1); + break; + } + for_die.Draw(screenBuffer, [&](auto x) {return lin_interp(x, Pixel {100, 20, 27, 255}, proc); }); + //Image_Draw_SpeedUp_Die(for_die, screenBuffer, proc); + + player.DieDraw(screenBuffer, proc); + // + } /*TODO:DEL +++*/ //auto pos = player.GetPos();