Skip to content

Commit

Permalink
add not_mix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Feb 18, 2021
1 parent d38ce7d commit 97d2b34
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 59 deletions.
8 changes: 7 additions & 1 deletion General.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ enum class E_Dir
UP,
DOWN,
LEFT,
RIGHT
RIGHT,
};

enum class E_X_Dir{Not, Left, Right};
enum class E_Y_Dir{Not, Down, Up};

inline bool Is_X_Dir(E_Dir dir) { return (dir == E_Dir::LEFT) || (dir == E_Dir::RIGHT); }
inline bool Is_Y_Dir(E_Dir dir) { return (dir == E_Dir::UP) || (dir == E_Dir::DOWN); }

#endif
39 changes: 26 additions & 13 deletions Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ int Image::Save(const std::string &a_path)
return 0;
}

void Image::Draw(Image &canvas, Point p, bool flip_OX, bool flip_OY) const
void Image::Draw(Image &canvas, Point p, bool flip_OX, bool flip_OY, bool not_mix) const
{
int c_w;
int c_h;
Expand All @@ -124,18 +124,31 @@ void Image::Draw(Image &canvas, Point p, bool flip_OX, bool flip_OY) const
//for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++)
//for(int x = start_x; x < end_x; x++, this_x++)
// canvas.MixPixel(x, y, GetPixel(this_x, this_y));

#define Z(th_x, th_y) \
for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++) \
for (int x = start_x; x < end_x; x++, this_x++) \
canvas.MixPixel(x, y, GetPixel(th_x, th_y))

if (!flip_OX && !flip_OY)Z(this_x, this_y);
else if (flip_OX && !flip_OY)Z(this_x, height - 1 - this_y);
else if (!flip_OX && flip_OY)Z(width - 1 - this_x, this_y);
else /*if (flip_OY && flip_OX)*/Z(width - 1 - this_x, height - 1 - this_y);

#undef Z
if (not_mix) {
#define Z(th_x, th_y) \
for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++) \
for (int x = start_x; x < end_x; x++, this_x++) \
canvas.SetPixel(x, y, GetPixel(th_x, th_y))

if (!flip_OX && !flip_OY)Z(this_x, this_y);
else if (flip_OX && !flip_OY)Z(this_x, height - 1 - this_y);
else if (!flip_OX && flip_OY)Z(width - 1 - this_x, this_y);
else Z(width - 1 - this_x, height - 1 - this_y);

#undef Z
} else {
#define Z(th_x, th_y) \
for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++) \
for (int x = start_x; x < end_x; x++, this_x++) \
canvas.MixPixel(x, y, GetPixel(th_x, th_y))

if (!flip_OX && !flip_OY)Z(this_x, this_y);
else if (flip_OX && !flip_OY)Z(this_x, height - 1 - this_y);
else if (!flip_OX && flip_OY)Z(width - 1 - this_x, this_y);
else Z(width - 1 - this_x, height - 1 - this_y);

#undef Z
}
}

void Image::PixelsChange(std::function<Pixel(Pixel)> PixFunc, bool with_hash_pixel)
Expand Down
2 changes: 1 addition & 1 deletion Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ 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) const;
void Draw(Image &canvas, Point p, bool flip_OX = false, bool flip_OY = false, bool not_mix = false) 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
76 changes: 54 additions & 22 deletions Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,67 @@ bool Player::Moved() const
return true;
}

void Player::ProcessInput(E_Dir dir)
void Player::RefreshMoveState(E_Dir dir)
{
int move_dist = move_speed * 1;
switch(dir)
{
int move_dist = move_speed * 1;
switch (dir) {
case E_Dir::UP:
old_coords.y = coords.y;
coords.y += move_dist;
break;
old_coords.y = coords.y;
coords.y += move_dist;
break;
case E_Dir::DOWN:
old_coords.y = coords.y;
coords.y -= move_dist;
break;
old_coords.y = coords.y;
coords.y -= move_dist;
break;
case E_Dir::LEFT:
old_coords.x = coords.x;
coords.x -= move_dist;
break;
old_coords.x = coords.x;
coords.x -= move_dist;
break;
case E_Dir::RIGHT:
old_coords.x = coords.x;
coords.x += move_dist;
break;
default:
break;
}

spr.SetDir(dir);
old_coords.x = coords.x;
coords.x += move_dist;
break;
default:break;
}
}

void Player::RefreshMoveState(E_X_Dir x, E_Y_Dir y)
{
bool x_move = x != E_X_Dir::Not;
bool y_move = y != E_Y_Dir::Not;

if (x_move && y_move) {
bool change_dir = false;

E_Dir e_dir_x = (x == E_X_Dir::Left) ? E_Dir::LEFT : E_Dir::RIGHT;
E_Dir e_dir_y = (y == E_Y_Dir::Up) ? E_Dir::UP : E_Dir::DOWN;
RefreshMoveState(e_dir_x);
RefreshMoveState(e_dir_y);

if(e_dir_x != back_dir && e_dir_y != back_dir)
spr.SetDir(Is_X_Dir(back_dir) ? e_dir_y : e_dir_x);
}
else if (x_move) {
E_Dir e_dir_x = (x == E_X_Dir::Left) ? E_Dir::LEFT : E_Dir::RIGHT;
RefreshMoveState(e_dir_x);
spr.SetDir(e_dir_x);
back_dir = e_dir_x;
}
else if (y_move) {
E_Dir e_dir_y = (y == E_Y_Dir::Up) ? E_Dir::UP : E_Dir::DOWN;
RefreshMoveState(e_dir_y);
spr.SetDir(e_dir_y);
back_dir = e_dir_y;
}

//TODO : if (NOT ATTACKED)
spr.SetState(Moved() ? E_LiveObjState::Walk : E_LiveObjState::Idle);
//printf(Moved() ? "move\n" : "not move\n");

}

void Player::Draw(Image &screen)
{
spr.Draw(screen, coords, true);
old_coords = coords;
spr.Draw(screen, coords, true);
}
28 changes: 18 additions & 10 deletions Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@

struct Player
{
//explicit Player(Point pos = {.x = 10, .y = 10}) : coords(pos), old_coords(coords) {};
//explicit Player(Point pos = {.x = 10, .y = 10}) : coords(pos), old_coords(coords) {};

explicit Player(Point pos, LiveObjSprite&sprite) : coords(pos), old_coords(coords), spr(sprite) {};
explicit Player(Point pos, LiveObjSprite &sprite) : coords(pos), old_coords(coords), spr(sprite) {};

bool Moved() const;
void ProcessInput(E_Dir dir);
void Draw(Image &screen);
bool Moved() const;
void Draw(Image &screen);

inline void RefreshMoveState(bool up, bool down, bool left, bool right)
{
RefreshMoveState((left == right) ? E_X_Dir::Not : (left ? E_X_Dir::Left : E_X_Dir::Right),
(up == down) ? E_Y_Dir::Not : (up ? E_Y_Dir::Up : E_Y_Dir::Down));
}
void RefreshMoveState(E_X_Dir x, E_Y_Dir y);
private:
Point coords {.x = 10, .y = 10};
Point old_coords {.x = 10, .y = 10};
Pixel color {.r = 255, .g = 255, .b = 0, .a = 255};
int move_speed = 4;
void RefreshMoveState(E_Dir dir);
E_Dir back_dir = E_Dir::DOWN;

LiveObjSprite &spr;
Point coords {.x = 10, .y = 10};
Point old_coords {.x = 10, .y = 10};
Pixel color {.r = 255, .g = 255, .b = 0, .a = 255};
int move_speed = 4;

LiveObjSprite &spr;

};

Expand Down
2 changes: 1 addition & 1 deletion Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void Sprite::SpriteFromImg(Image &img0, int p_frames, int ms_on_frame, int scale

for (int frame = 0; frame < frames; frame++, temp_x_now -= sz_w) {
imgs.emplace_back(Image {sz_w, sz_h, chans});
img0.Draw(imgs[frame], {.x = temp_x_now, .y = 0});
img0.Draw(imgs[frame], {.x = temp_x_now, .y = 0}, false, false, true);
}
}

Expand Down
Binary file modified bin/Debug/main.exe
Binary file not shown.
Binary file modified bin/Debug/main.ilk
Binary file not shown.
Binary file modified bin/Debug/main.pdb
Binary file not shown.
19 changes: 8 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,11 @@ void OnKeyboardPressed(GLFWwindow* window, int key, int scancode, int action, in

void processPlayerMovement(Player &player)
{
if (Input.keys[GLFW_KEY_W])
player.ProcessInput(E_Dir::UP);
else if (Input.keys[GLFW_KEY_S])
player.ProcessInput(E_Dir::DOWN);
if (Input.keys[GLFW_KEY_A])
player.ProcessInput(E_Dir::LEFT);
else if (Input.keys[GLFW_KEY_D])
player.ProcessInput(E_Dir::RIGHT);
bool U = Input.keys[GLFW_KEY_W];
bool D = Input.keys[GLFW_KEY_S];
bool L = Input.keys[GLFW_KEY_A];
bool R = Input.keys[GLFW_KEY_D];
player.RefreshMoveState(U, D, L, R);
}

void OnMouseButtonClicked(GLFWwindow* window, int button, int action, int mods)
Expand Down Expand Up @@ -173,14 +170,14 @@ int main(int argc, char** argv)

glfwPollEvents();

processPlayerMovement(player);

player.Draw(screenBuffer);
img.Draw(screenBuffer, {64 * 0,0});
img.Draw(screenBuffer, {64 * 1,0}, true);
img.Draw(screenBuffer, {64 * 2,0}, false, true);
img.Draw(screenBuffer, {64 * 3,0}, true, true);

processPlayerMovement(player);
player.Draw(screenBuffer);

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;

Expand Down

0 comments on commit 97d2b34

Please sign in to comment.