Compare commits
6 Commits
f6a13309f7
...
v1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ed4452fae | ||
|
|
b9ca8df59b | ||
|
|
8fa8bf392b | ||
|
|
9170524d33 | ||
|
|
cda395d6b9 | ||
|
|
d8da84cffc |
@@ -1,7 +1,7 @@
|
||||
// background_dark_inverted.h
|
||||
#pragma once
|
||||
|
||||
static const char *BACKGROUND_DARK_INVERTED[] = {
|
||||
static const char *BACKGROUND_MASK[] = {
|
||||
"~`````````````````````````````````````````````````````````````````````````````````````\"````````````````````````````````~```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````+````````````````````~````````````````````````````````~```````````````````````````````````````````````````````",
|
||||
"...................................................................,.......................:..........................................'.........`..:.....................................................................................................+...................................:...........:..,................................................",
|
||||
"...........................\"............................................._................................+...........................................................................................................,..............................................`..............................................................!........................",
|
||||
@@ -19,5 +19,4 @@ static const char *BACKGROUND_DARK_INVERTED[] = {
|
||||
"...,,,,,,,,4@@@$g@....,7^=sa@@$a@@@@@@@@@@@@@@@@@@~``.`,,,,,,,,:::`,g@R@BZa$TTTTF5PM@@@@^:`'`~T5#@@y_`,u@~$@$@@g_,F,,,,`@@@@'':^na$g@@@=_:@@.`,.4__,,,,,,,,,g@@K:,,,,,,g@@@@w....,,,,,,,,,$@@$g@F...,,^=#aa@$gg@@@@@@@@@@@@@@@@@F``.`,,,,,,,,,::`,:@@P@R5$T~TTFFPP5@@@$:'``~~7P@@y_``,$E4@W@$@y,4::,,`3@@@[`:,raagg@@Fk`@@L.,.`g_,,,,,,,,,,g@@~:,,,,,,g@@@$..",
|
||||
"..,:''``a$gg@$@@ZF...:,,,,,,:`~T@@@@@@@@@@@@@A@@^``...:,,,,,`,,::,.,@@@@@@@@,`.`````@@@[',,,,,,,,,,`TR@@@@@@@$@@@`'''':,A@MP.,,,,,\"~TT@,`3@@*g'.`4@':,,,,,,:EDT,,,,,,,_@@@@@$....:':':H$$g@$@@$@....,,,,,`:`~7R@@@@@@@@@@@@AR@@```..,',,,'``,,::,,d@0@@@@@y`.`````g@@@`',,,,,,,,,:79@$@@@@@@@@@'\"``':,!$FP~:,,,,,~~7@y.~A@mg@'.\"@F::,,,,,,4ER~,,,,,,,g@@@@@..",
|
||||
};
|
||||
static const int BACKGROUND_DARK_INVERTED_LINES =
|
||||
sizeof(BACKGROUND_DARK_INVERTED) / sizeof(BACKGROUND_DARK_INVERTED[0]);
|
||||
static const int VIEW_HEIGHT = sizeof(BACKGROUND_MASK) / sizeof(BACKGROUND_MASK[0]);
|
||||
|
||||
@@ -11,8 +11,8 @@ void draw_mask(const char *unused_filename, int shift, const char *text) {
|
||||
// Terminal clear + home
|
||||
printf("\033[2J\033[H");
|
||||
|
||||
for (int i = 0; i < view_height && i < BACKGROUND_DARK_INVERTED_LINES; i++) {
|
||||
const char *row = BACKGROUND_DARK_INVERTED[i];
|
||||
for (int i = 0; i < view_height && i < VIEW_HEIGHT; i++) {
|
||||
const char *row = BACKGROUND_MASK[i];
|
||||
int width = strlen(row);
|
||||
if (width == 0) {
|
||||
printf("\r\n");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// src/render/loop.cpp
|
||||
#include "loop.h"
|
||||
#include "../background_dark_inverted.h"
|
||||
#include "player_positioning.h"
|
||||
#include "background.h"
|
||||
#include "mbed.h"
|
||||
#include "player.h"
|
||||
@@ -15,9 +16,10 @@ constexpr size_t BUFFER_SIZE = 64;
|
||||
constexpr auto CRAWL_DURATION = 300ms;
|
||||
constexpr auto ANIMATION_TICK = 170ms;
|
||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
||||
constexpr int PLAYER_X = 18;
|
||||
constexpr int PLAYER_Y = 13;
|
||||
constexpr int PLAYER_X = 9;
|
||||
constexpr int PLAYER_Y = 6;
|
||||
|
||||
// PlayerState is what user pressed. We are mapping CharacterState to it
|
||||
enum class PlayerState { Walk1, Walk2, Crawl1 };
|
||||
|
||||
struct WalkingState {
|
||||
@@ -85,16 +87,16 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static int get_player_frame(PlayerState state) {
|
||||
static CharacterFrame get_character_frame(PlayerState state) {
|
||||
switch (state) {
|
||||
case PlayerState::Walk1:
|
||||
return 0;
|
||||
return CharacterFrame::Walk1;
|
||||
case PlayerState::Walk2:
|
||||
return 1;
|
||||
return CharacterFrame::Walk2;
|
||||
case PlayerState::Crawl1:
|
||||
return 2;
|
||||
return CharacterFrame::Crawl1;
|
||||
default:
|
||||
return 0;
|
||||
return CharacterFrame::Walk1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +130,8 @@ void render_loop(int speed) {
|
||||
anim_timer.start();
|
||||
int shift = 0;
|
||||
|
||||
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
||||
|
||||
const char *bg_file = "background_dark_inverted.txt";
|
||||
bool need_redraw = false;
|
||||
|
||||
@@ -154,8 +158,9 @@ void render_loop(int speed) {
|
||||
|
||||
if (need_redraw) {
|
||||
draw_mask(bg_file, shift, message_active ? message : nullptr);
|
||||
int player_frame = get_player_frame(player_state.current_state);
|
||||
draw_player(PLAYER_X, PLAYER_Y, player_frame);
|
||||
CharacterFrame player_frame = get_character_frame(player_state.current_state);
|
||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
||||
draw_character(draw_pos.x, draw_pos.y, player_frame);
|
||||
|
||||
// alternate between frame 0 and 1 when not crawling
|
||||
if (player_state.current_state != PlayerState::Crawl1) {
|
||||
|
||||
@@ -1,35 +1,33 @@
|
||||
// src/render/player.cpp
|
||||
|
||||
#include "player.h"
|
||||
#include "player_mask.h"
|
||||
#include <cstdio>
|
||||
|
||||
void draw_player(int view_width, int view_height, int frame_index) {
|
||||
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
|
||||
frame_index = 0; // fallback
|
||||
void draw_character(int x, int y, CharacterFrame frame) {
|
||||
int frame_index = static_cast<int>(frame);
|
||||
if (frame_index < 0 || frame_index >= CHARACTER_FRAME_COUNT)
|
||||
frame_index = 0; // fallback safety
|
||||
|
||||
const char **sprite = PLAYER_FRAMES[frame_index];
|
||||
int sprite_height = 0;
|
||||
if (frame_index == 0)
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_1;
|
||||
else if (frame_index == 1)
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_2;
|
||||
else
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_3;
|
||||
const char **character = PLAYER_FRAMES[frame_index];
|
||||
|
||||
// Width from first line
|
||||
int sprite_width = 0;
|
||||
if (sprite_height > 0) {
|
||||
const char *first_line = sprite[0];
|
||||
while (first_line[sprite_width] != '\0')
|
||||
sprite_width++;
|
||||
int character_height = 0;
|
||||
switch (frame) {
|
||||
case CharacterFrame::Walk1:
|
||||
character_height = CHARACTER_HEIGHT_1;
|
||||
break;
|
||||
case CharacterFrame::Walk2:
|
||||
character_height = CHARACTER_HEIGHT_2;
|
||||
break;
|
||||
case CharacterFrame::Crawl1:
|
||||
character_height = CHARACTER_HEIGHT_3;
|
||||
break;
|
||||
default:
|
||||
character_height = CHARACTER_HEIGHT_1;
|
||||
break;
|
||||
}
|
||||
|
||||
int center_x = view_width / 2 - sprite_width / 2;
|
||||
int center_y = view_height / 2 - sprite_height / 2;
|
||||
|
||||
for (int i = 0; i < sprite_height; i++) {
|
||||
printf("\033[%d;%dH%s", center_y + i + 1, center_x + 1, sprite[i]);
|
||||
for (int i = 0; i < character_height; i++) {
|
||||
printf("\033[%d;%dH%s", y + i + 1, x + 1, character[i]);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// src/render/player.h
|
||||
#pragma once
|
||||
#include "player_mask.h"
|
||||
|
||||
// Draw the player object centered over the background
|
||||
void draw_player(int view_width, int view_height, int frame_index);
|
||||
// Draw the player object starting at given (x, y)
|
||||
void draw_character(int x, int y, CharacterFrame frame);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#pragma once
|
||||
|
||||
// Player frame 1
|
||||
static const char *PLAYER_MASK_FRAME_1[] = {
|
||||
static const char *CHARACTER_MASK_FRAME_1[] = {
|
||||
".....a@$..",
|
||||
".....7PF..",
|
||||
"..yaM@@__y",
|
||||
@@ -14,7 +14,7 @@ static const char *PLAYER_MASK_FRAME_1[] = {
|
||||
};
|
||||
|
||||
// Player frame 2
|
||||
static const char *PLAYER_MASK_FRAME_2[] = {
|
||||
static const char *CHARACTER_MASK_FRAME_2[] = {
|
||||
".g@$",
|
||||
".7PF",
|
||||
"y@$.",
|
||||
@@ -25,26 +25,30 @@ static const char *PLAYER_MASK_FRAME_2[] = {
|
||||
};
|
||||
|
||||
// Player frame 3 (triggered)
|
||||
static const char *PLAYER_MASK_FRAME_3[] = {
|
||||
static const char *CHARACTER_MASK_FRAME_3[] = {
|
||||
".._$@.",
|
||||
".$By`.",
|
||||
"4@$@@a",
|
||||
"aa@W@."
|
||||
};
|
||||
|
||||
// Combine frames for easy access
|
||||
static const char **PLAYER_FRAMES[] = {
|
||||
PLAYER_MASK_FRAME_1, // 0
|
||||
PLAYER_MASK_FRAME_2, // 1
|
||||
PLAYER_MASK_FRAME_3 // 2 (triggered)
|
||||
enum class CharacterFrame {
|
||||
Walk1 = 0,
|
||||
Walk2,
|
||||
Crawl1,
|
||||
COUNT // number of frames, must stay last
|
||||
};
|
||||
|
||||
static const int PLAYER_FRAME_COUNT = sizeof(PLAYER_FRAMES) / sizeof(PLAYER_FRAMES[0]);
|
||||
// Combine frames for easy access
|
||||
static const char **PLAYER_FRAMES[] = {
|
||||
CHARACTER_MASK_FRAME_1, // Walk1
|
||||
CHARACTER_MASK_FRAME_2, // Walk1
|
||||
CHARACTER_MASK_FRAME_3 // Crawl1
|
||||
};
|
||||
|
||||
// Line counts per frame
|
||||
static const int PLAYER_MASK_LINES_FRAME_1 =
|
||||
sizeof(PLAYER_MASK_FRAME_1) / sizeof(PLAYER_MASK_FRAME_1[0]);
|
||||
static const int PLAYER_MASK_LINES_FRAME_2 =
|
||||
sizeof(PLAYER_MASK_FRAME_2) / sizeof(PLAYER_MASK_FRAME_2[0]);
|
||||
static const int PLAYER_MASK_LINES_FRAME_3 =
|
||||
sizeof(PLAYER_MASK_FRAME_3) / sizeof(PLAYER_MASK_FRAME_3[0]);
|
||||
static const int CHARACTER_FRAME_COUNT = static_cast<int>(CharacterFrame::COUNT);
|
||||
|
||||
// Height per frame
|
||||
static const int CHARACTER_HEIGHT_1 = sizeof(CHARACTER_MASK_FRAME_1) / sizeof(CHARACTER_MASK_FRAME_1[0]);
|
||||
static const int CHARACTER_HEIGHT_2 = sizeof(CHARACTER_MASK_FRAME_2) / sizeof(CHARACTER_MASK_FRAME_2[0]);
|
||||
static const int CHARACTER_HEIGHT_3 = sizeof(CHARACTER_MASK_FRAME_3) / sizeof(CHARACTER_MASK_FRAME_3[0]);
|
||||
|
||||
25
semestralka1/src/render/player_positioning.cpp
Normal file
25
semestralka1/src/render/player_positioning.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// src/render/player_positioning.cpp
|
||||
#include "player_positioning.h"
|
||||
#include "../background_dark_inverted.h"
|
||||
#include <cstdio>
|
||||
|
||||
// Convert a pivot coordinate (bottom-left) to the top-left draw position
|
||||
CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame) {
|
||||
int frame_index = static_cast<int>(frame);
|
||||
if (frame_index < 0 || frame_index >= CHARACTER_FRAME_COUNT)
|
||||
frame_index = 0;
|
||||
|
||||
int character_height = 0;
|
||||
if (frame_index == 0)
|
||||
character_height = CHARACTER_HEIGHT_1;
|
||||
else if (frame_index == 1)
|
||||
character_height = CHARACTER_HEIGHT_2;
|
||||
else
|
||||
character_height = CHARACTER_HEIGHT_3;
|
||||
|
||||
CharacterPosition draw_pos;
|
||||
draw_pos.x = base.x;
|
||||
draw_pos.y = (VIEW_HEIGHT - base.y) - character_height;
|
||||
|
||||
return draw_pos;
|
||||
}
|
||||
12
semestralka1/src/render/player_positioning.h
Normal file
12
semestralka1/src/render/player_positioning.h
Normal file
@@ -0,0 +1,12 @@
|
||||
// src/render/player_positioning.h
|
||||
#pragma once
|
||||
#include "../background_dark_inverted.h"
|
||||
#include "player_mask.h"
|
||||
|
||||
struct CharacterPosition {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
// Calculates drawing position relative to bottom-left pivot
|
||||
CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame);
|
||||
Reference in New Issue
Block a user