Skip to content

Commit

Permalink
empty die +
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Feb 28, 2021
1 parent 7b0c1e9 commit d6badb8
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 9 deletions.
12 changes: 12 additions & 0 deletions Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pixel(Pixel)> 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));
Expand Down
8 changes: 5 additions & 3 deletions Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ struct Image
Image(const Image &copy);
Image(Image && a) noexcept;

Image &operator=(const Image &other)
/*Image &operator=(const Image &other)
{
error("oh no...");
return *this;
}
}*/

Image &operator=(Image &&other) noexcept
{
Expand Down Expand Up @@ -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<Pixel(Pixel)> PixFunc) const;

/// <summary>chnage image : image[x,y] = PixFunc(image[x,y])</summary>
/// <param name="with_save_pixel">if we know that dif color on that image few then we can hash PixFunc result value</param>
void PixelsChange(std::function<Pixel(Pixel)> PixFunc, bool with_hash_pixel);
Expand Down
3 changes: 3 additions & 0 deletions LiveObjSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
9 changes: 9 additions & 0 deletions Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
10 changes: 9 additions & 1 deletion Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;

Expand All @@ -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
2 changes: 2 additions & 0 deletions Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
65 changes: 60 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d6badb8

Please sign in to comment.