Skip to content

Commit

Permalink
hemh, ~~ kawaii - win
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Mar 4, 2021
1 parent cbbf490 commit 1e4fb02
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 102 deletions.
17 changes: 17 additions & 0 deletions GameMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ bool GameMap::CanThrowItem(Point pos)
return E_TileType::Floor == now_room->gri.TileType(pos.x, pos.y);
}

bool GameMap::CheckWin()
{
auto wp = now_room->gri.win_point;
if (wp == GameRoomInfo::NOT_WIN_POINT)return false;
auto cp = Player::Get().GetCenter();

if ((cp.x < 0) && ((cp + Point {TILE_SZ, 0}) / TILE_SZ == wp))return true;
if ((cp.y < 0) && ((cp + Point {0, TILE_SZ}) / TILE_SZ == wp))return true;
if ((cp.x / TILE_SZ >= now_room->gri.map_width) && ((cp + Point {-TILE_SZ, 0}) / TILE_SZ == wp))return true;
if ((cp.y / TILE_SZ >= now_room->gri.map_height) && ((cp + Point {0, -TILE_SZ}) / TILE_SZ == wp))return true;
return false;
}

bool GameMap::CheckChangeMap()
{
auto center_pos = Player::Get().GetCenter();
Expand Down Expand Up @@ -226,6 +239,9 @@ GameMap::GameRoomInfo::GameRoomInfo(char room_type)
else if (c == 'K') {
tt = GameMap::E_TileType::Floor;
keys_pos.emplace_back(Point {.x = i, .y = map_height});
} else if (c == 'W') {
tt = GameMap::E_TileType::Floor;
win_point = {.x = i, .y = map_height};
}
else error("unknown type of tile '" + line.substr(0, 1) + "'");

Expand Down Expand Up @@ -286,4 +302,5 @@ GameMap::GameRoomInfo::GameRoomInfo(char room_type)
for (int i = 0; i < enemies.size(); i++)enemies[i].first.y = map_height - 1 - enemies[i].first.y;
for (int i = 0; i < door_pos.size(); i++)door_pos[i].y = map_height - 1 - door_pos[i].y;
for (int i = 0; i < point_in.size(); i++)point_in[i].y = map_height - 1 - point_in[i].y;
win_point.y = map_height - 1 - win_point.y;
}
4 changes: 4 additions & 0 deletions GameMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ private: static GameMap *cur_map;

GameMap();

bool CheckWin();
bool CheckChangeMap();

void EnemiesMove(Point player_pos) { now_room->map_objects.EnemiesMove(player_pos); }

bool CanStay(E_CanStayType stay_type, Point pos, Size obj_sz, bool empty_can, int enemy_id)
Expand Down Expand Up @@ -166,12 +168,14 @@ private: static GameMap *cur_map;
static constexpr int R = 2;
static constexpr int D = 3;
static constexpr Point BAD_IN = Point {-1, -1};
static constexpr Point NOT_WIN_POINT = Point {-32, -128};
GameRoomInfo(char room_type);
std::vector<std::vector<GameMap::E_TileType>> tile_type{};
std::vector<std::pair<Point, int>> enemies{}; // point - pos, int - type
std::vector<Point> door_pos{}; // point - pos
std::vector< std::pair<Point, int>> items{}; // point - pos, int - rarity
std::vector<Point> keys_pos{}; // point - pos
Point win_point = NOT_WIN_POINT;
int map_width = -1;
int map_height = -1;

Expand Down
4 changes: 4 additions & 0 deletions General.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ static constexpr int TILE_SZ = 32;

static constexpr int W_WIDTH = 1024;
static constexpr int W_HEIGHT = 720;
static constexpr int W2_WIDTH = W_WIDTH / 2;
static constexpr int W2_HEIGHT = W_HEIGHT / 2;
static constexpr int W_DIAG = W_WIDTH * W_WIDTH + W_HEIGHT * W_HEIGHT;
static constexpr int W2_DIAG = W2_WIDTH * W2_WIDTH + W2_HEIGHT * W2_HEIGHT;

struct Point
{
Expand Down
11 changes: 11 additions & 0 deletions Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ void Image::Draw(Image &canvas, std::function<Pixel(Pixel)> PixFunc) const
canvas.SetPixel(x, y, PixFunc(GetPixel(x, y)));
}

void Image::Draw(Image &canvas, std::function<Pixel(Point, 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(Point {x, y}, 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
3 changes: 3 additions & 0 deletions Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ inline bool operator< (const Pixel a, const Pixel b)


constexpr Pixel backgroundColor{0, 0, 0, 0};
constexpr Pixel WHITE_COLOR{255, 255, 255, 255};
constexpr Pixel KW_COLOR{210, 255, 255, 255};

enum class E_ImgRotation { Rot_90 };

Expand Down Expand Up @@ -107,6 +109,7 @@ struct Image
void FastDraw(Image &canvas, int lines, int from_line = 0) const;

void Draw(Image &canvas, std::function<Pixel(Pixel)> PixFunc) const;
void Draw(Image &canvas, std::function<Pixel(Point, 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>
Expand Down
226 changes: 133 additions & 93 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ int initGL()
return 0;
}

Pixel lin_interp(Pixel from, Pixel to, double proc)
inline 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);
Expand All @@ -171,6 +171,32 @@ Pixel only_r_color(Pixel from, double proc)
return Pixel {r,g,b,a};
}

Pixel win_fn_backup(Point p, Pixel from, double proc)
{
double white_r = (1 - proc) * W2_DIAG;

int d = 0;
if (p.x < W2_WIDTH) d += (W2_WIDTH - p.x) * (W2_WIDTH - p.x);
if (p.x > W2_WIDTH) d += (p.x - W2_WIDTH) * (p.x - W2_WIDTH);
if (p.y < W2_HEIGHT) d += (W2_HEIGHT - p.y) * (W2_HEIGHT - p.y);
if (p.y > W2_HEIGHT) d += (p.y - W2_HEIGHT) * (p.y - W2_HEIGHT);
if (d > white_r)return KW_COLOR;
return from;
}

Pixel win_fn(Point p, Pixel from, double proc)
{
double white_r = proc * W2_DIAG;

int d = 0;
if (p.x < W2_WIDTH) d += (W2_WIDTH - p.x) * (W2_WIDTH - p.x);
if (p.x > W2_WIDTH) d += (p.x - W2_WIDTH) * (p.x - W2_WIDTH);
if (p.y < W2_HEIGHT) d += (W2_HEIGHT - p.y) * (W2_HEIGHT - p.y);
if (p.y > W2_HEIGHT) d += (p.y - W2_HEIGHT) * (p.y - W2_HEIGHT);
if (d <= white_r)return KW_COLOR;
return lin_interp(from, KW_COLOR, white_r / d);
}

void Image_Draw_SpeedUp_Die(Image& screen_save, Image &canvas, double proc)
{
int c_w = screen_save.Width();
Expand All @@ -188,42 +214,41 @@ void Image_Draw_SpeedUp_Die(Image& screen_save, Image &canvas, double proc)
}
}

int main(int argc, char** argv)
int main(int argc, char **argv)
{
if (!glfwInit())return -1;

// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "task1 base project", nullptr, nullptr);
if (window == nullptr)
{
GLFWwindow *window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "task1 base project", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);

glfwSetKeyCallback (window, OnKeyboardPressed);
glfwSetCursorPosCallback (window, OnMouseMove);
glfwSetMouseButtonCallback(window, OnMouseButtonClicked);
glfwSetScrollCallback (window, OnMouseScroll);
glfwMakeContextCurrent(window);

glfwSetKeyCallback(window, OnKeyboardPressed);
glfwSetCursorPosCallback(window, OnMouseMove);
glfwSetMouseButtonCallback(window, OnMouseButtonClicked);
glfwSetScrollCallback(window, OnMouseScroll);

if(initGL() != 0)
if (initGL() != 0)
return -1;
//Reset any OpenGL errors which could be present for some reason

//Reset any OpenGL errors which could be present for some reason
GLenum gl_error = glGetError();
while (gl_error != GL_NO_ERROR)
gl_error = glGetError();

std::srand(std::time(nullptr));

GameMap gmap{};
GameMap gmap {};

LiveObjSprite player_img {HERO_0, SpritePixSz{16}, 125, 2};
Player player {gmap.GetPos(GameMap::E_MapPos::Center, Size{.w = 30, .h = 32}), player_img};

Expand All @@ -239,80 +264,95 @@ int main(int argc, char** argv)
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); GL_CHECK_ERRORS;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GL_CHECK_ERRORS;

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;
Player::E_DieType die_type;
bool last_die_draw = false;

while (!glfwWindowShouldClose(window)) {
GameTime::Now().SetCur(glfwGetTime());

if (GameTime::Now().GetTime() - sec > 1) {
sec = GameTime::Now().GetTime();
std::cout << "fps : " << frames << std::endl;
frames = 0;
}
frames++;

glfwPollEvents();


if (is_alive) {

gmap.CheckChangeMap();//TODO: add if(...) for effect
gmap.EnemiesMove(player.GetCenter());

gmap.Draw(screenBuffer);

processPlayerMovement(player);
is_alive = !player.GetIsDied();

if (!is_alive) { // die
for_die = Image {screenBuffer};
die_time = GameTime::Now().GetTime();
die_type = player.GetDiedType();
}
else {
player.Draw(screenBuffer);
player.InventoryDraw(screenBuffer);
}
}
if(!is_alive) {
double proc = GameTime::Now().GetSecAfter(die_time) / die_duration;
if (proc >= 1) {
if (last_die_draw) break;
last_die_draw = true;
}

if(die_type == Player::E_DieType::Kill)
for_die.Draw(screenBuffer, [&](auto x) {return only_r_color(x, proc); });
else if (die_type == Player::E_DieType::EmptyStay)
for_die.Draw(screenBuffer, [&](auto x) {return lin_interp(x, Pixel {27, 20, 27, 255}, proc); });
//Image_Draw_SpeedUp_Die(for_die, screenBuffer, proc);

player.DieDraw(screenBuffer, proc);
}
SpriteManager::Get().DrawDoor(screenBuffer, {32, 32}, E_Dir::UP);
SpriteManager::Get().DrawDoor(screenBuffer, {32, 64}, E_Dir::DOWN);
SpriteManager::Get().DrawDoor(screenBuffer, {32, 64+32}, E_Dir::LEFT);
SpriteManager::Get().DrawDoor(screenBuffer, {32, 128}, E_Dir::RIGHT);
/*TODO:DEL +++*/
//auto pos = player.GetPos();
//screenBuffer.SetPixel(pos.x + 12, pos.y + 1, Pixel {.r = 255, .g = 0, .b = 0, .a = 255,});
//screenBuffer.SetPixel(pos.x + 19, pos.y + 1, Pixel {.r = 255, .g = 0, .b = 0, .a = 255,});
/*TODO:DEL ---*/

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GL_CHECK_ERRORS;
glDrawPixels(WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, screenBuffer.Data()); GL_CHECK_ERRORS;

glfwSwapBuffers(window);
}
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;
Player::E_DieType die_type;
bool last_die_draw = false;

bool win = false;
double win_time = 0;
const double win_duration = 3;
Image win_img {PATH + "win.png"};

while (!glfwWindowShouldClose(window)) {
GameTime::Now().SetCur(glfwGetTime());

if (GameTime::Now().GetTime() - sec > 1) {
sec = GameTime::Now().GetTime();
std::cout << "fps : " << frames << std::endl;
frames = 0;
}
frames++;

glfwPollEvents();


if (!win) {
win = gmap.CheckWin();
if (win) {
for_die = Image {screenBuffer};
win_time = GameTime::Now().GetTime();
}
}

if (!win && is_alive) {
gmap.CheckChangeMap();//TODO: add if(...) for effect
gmap.EnemiesMove(player.GetCenter());

gmap.Draw(screenBuffer);

processPlayerMovement(player);
is_alive = !player.GetIsDied();

if (!is_alive) { // die
for_die = Image {screenBuffer};
die_time = GameTime::Now().GetTime();
die_type = player.GetDiedType();
} else {
player.Draw(screenBuffer);
player.InventoryDraw(screenBuffer);
}
}
if (!is_alive) {
double proc = GameTime::Now().GetSecAfter(die_time) / die_duration;
if (proc >= 1) {
if (last_die_draw) break;
last_die_draw = true;
}

if (die_type == Player::E_DieType::Kill)
for_die.Draw(screenBuffer, [&](auto x) {return only_r_color(x, proc); });
else if (die_type == Player::E_DieType::EmptyStay)
for_die.Draw(screenBuffer, [&](auto x) {return lin_interp(x, Pixel {27, 20, 27, 255}, proc); });
//Image_Draw_SpeedUp_Die(for_die, screenBuffer, proc);

player.DieDraw(screenBuffer, proc);
}

if (win) {
double proc = GameTime::Now().GetSecAfter(win_time) / win_duration;
if (proc >= 1)break;
for_die.Draw(screenBuffer, [&](auto pos, auto pix) {return win_fn(pos, pix, proc); });
win_img.Draw(screenBuffer, Point {.x = (W_WIDTH - win_img.Width()) / 2, .y = (W_HEIGHT - win_img.Height()) / 2}, true);

}
/*TODO:DEL +++*/
//auto pos = player.GetPos();
//screenBuffer.SetPixel(pos.x + 12, pos.y + 1, Pixel {.r = 255, .g = 0, .b = 0, .a = 255,});
//screenBuffer.SetPixel(pos.x + 19, pos.y + 1, Pixel {.r = 255, .g = 0, .b = 0, .a = 255,});
/*TODO:DEL ---*/

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GL_CHECK_ERRORS;
glDrawPixels(WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, screenBuffer.Data()); GL_CHECK_ERRORS;

glfwSwapBuffers(window);
}

glfwTerminate();
return 0;
Expand Down
17 changes: 17 additions & 0 deletions maps/C.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
z+
################################
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
#..............................#
################################
z-
Loading

0 comments on commit 1e4fb02

Please sign in to comment.