6 Commits

Author SHA1 Message Date
Priec
4ed4452fae some renames, nothing much 2025-11-14 19:39:19 +01:00
Priec
b9ca8df59b small syntax change 2025-11-14 19:25:25 +01:00
Priec
8fa8bf392b usage of enums for frames 2025-11-14 19:20:14 +01:00
Priec
9170524d33 proper positioning works now 2025-11-14 18:50:57 +01:00
Priec
cda395d6b9 calculations 2025-11-14 17:31:37 +01:00
Priec
d8da84cffc added positioning 2025-11-14 16:42:16 +01:00
8 changed files with 99 additions and 55 deletions

View File

@@ -1,7 +1,7 @@
// background_dark_inverted.h // background_dark_inverted.h
#pragma once #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@@@$..", "...,,,,,,,,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@@@@@..", "..,:''``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 = static const int VIEW_HEIGHT = sizeof(BACKGROUND_MASK) / sizeof(BACKGROUND_MASK[0]);
sizeof(BACKGROUND_DARK_INVERTED) / sizeof(BACKGROUND_DARK_INVERTED[0]);

View File

@@ -11,8 +11,8 @@ void draw_mask(const char *unused_filename, int shift, const char *text) {
// Terminal clear + home // Terminal clear + home
printf("\033[2J\033[H"); printf("\033[2J\033[H");
for (int i = 0; i < view_height && i < BACKGROUND_DARK_INVERTED_LINES; i++) { for (int i = 0; i < view_height && i < VIEW_HEIGHT; i++) {
const char *row = BACKGROUND_DARK_INVERTED[i]; const char *row = BACKGROUND_MASK[i];
int width = strlen(row); int width = strlen(row);
if (width == 0) { if (width == 0) {
printf("\r\n"); printf("\r\n");

View File

@@ -1,6 +1,7 @@
// src/render/loop.cpp // src/render/loop.cpp
#include "loop.h" #include "loop.h"
#include "../background_dark_inverted.h" #include "../background_dark_inverted.h"
#include "player_positioning.h"
#include "background.h" #include "background.h"
#include "mbed.h" #include "mbed.h"
#include "player.h" #include "player.h"
@@ -15,9 +16,10 @@ constexpr size_t BUFFER_SIZE = 64;
constexpr auto CRAWL_DURATION = 300ms; constexpr auto CRAWL_DURATION = 300ms;
constexpr auto ANIMATION_TICK = 170ms; constexpr auto ANIMATION_TICK = 170ms;
constexpr auto MESSAGE_DISPLAY_DURATION = 1s; constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
constexpr int PLAYER_X = 18; constexpr int PLAYER_X = 9;
constexpr int PLAYER_Y = 13; constexpr int PLAYER_Y = 6;
// PlayerState is what user pressed. We are mapping CharacterState to it
enum class PlayerState { Walk1, Walk2, Crawl1 }; enum class PlayerState { Walk1, Walk2, Crawl1 };
struct WalkingState { struct WalkingState {
@@ -85,16 +87,16 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) {
return false; return false;
} }
static int get_player_frame(PlayerState state) { static CharacterFrame get_character_frame(PlayerState state) {
switch (state) { switch (state) {
case PlayerState::Walk1: case PlayerState::Walk1:
return 0; return CharacterFrame::Walk1;
case PlayerState::Walk2: case PlayerState::Walk2:
return 1; return CharacterFrame::Walk2;
case PlayerState::Crawl1: case PlayerState::Crawl1:
return 2; return CharacterFrame::Crawl1;
default: default:
return 0; return CharacterFrame::Walk1;
} }
} }
@@ -128,6 +130,8 @@ void render_loop(int speed) {
anim_timer.start(); anim_timer.start();
int shift = 0; int shift = 0;
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
const char *bg_file = "background_dark_inverted.txt"; const char *bg_file = "background_dark_inverted.txt";
bool need_redraw = false; bool need_redraw = false;
@@ -154,8 +158,9 @@ void render_loop(int speed) {
if (need_redraw) { if (need_redraw) {
draw_mask(bg_file, shift, message_active ? message : nullptr); draw_mask(bg_file, shift, message_active ? message : nullptr);
int player_frame = get_player_frame(player_state.current_state); CharacterFrame player_frame = get_character_frame(player_state.current_state);
draw_player(PLAYER_X, PLAYER_Y, player_frame); 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 // alternate between frame 0 and 1 when not crawling
if (player_state.current_state != PlayerState::Crawl1) { if (player_state.current_state != PlayerState::Crawl1) {

View File

@@ -1,35 +1,33 @@
// src/render/player.cpp // src/render/player.cpp
#include "player.h" #include "player.h"
#include "player_mask.h" #include "player_mask.h"
#include <cstdio> #include <cstdio>
void draw_player(int view_width, int view_height, int frame_index) { void draw_character(int x, int y, CharacterFrame frame) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT) int frame_index = static_cast<int>(frame);
frame_index = 0; // fallback if (frame_index < 0 || frame_index >= CHARACTER_FRAME_COUNT)
frame_index = 0; // fallback safety
const char **sprite = PLAYER_FRAMES[frame_index]; const char **character = 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;
// Width from first line int character_height = 0;
int sprite_width = 0; switch (frame) {
if (sprite_height > 0) { case CharacterFrame::Walk1:
const char *first_line = sprite[0]; character_height = CHARACTER_HEIGHT_1;
while (first_line[sprite_width] != '\0') break;
sprite_width++; 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; for (int i = 0; i < character_height; i++) {
int center_y = view_height / 2 - sprite_height / 2; printf("\033[%d;%dH%s", y + i + 1, x + 1, character[i]);
for (int i = 0; i < sprite_height; i++) {
printf("\033[%d;%dH%s", center_y + i + 1, center_x + 1, sprite[i]);
} }
fflush(stdout); fflush(stdout);
} }

View File

@@ -1,5 +1,6 @@
// src/render/player.h // src/render/player.h
#pragma once #pragma once
#include "player_mask.h"
// Draw the player object centered over the background // Draw the player object starting at given (x, y)
void draw_player(int view_width, int view_height, int frame_index); void draw_character(int x, int y, CharacterFrame frame);

View File

@@ -3,7 +3,7 @@
#pragma once #pragma once
// Player frame 1 // Player frame 1
static const char *PLAYER_MASK_FRAME_1[] = { static const char *CHARACTER_MASK_FRAME_1[] = {
".....a@$..", ".....a@$..",
".....7PF..", ".....7PF..",
"..yaM@@__y", "..yaM@@__y",
@@ -14,7 +14,7 @@ static const char *PLAYER_MASK_FRAME_1[] = {
}; };
// Player frame 2 // Player frame 2
static const char *PLAYER_MASK_FRAME_2[] = { static const char *CHARACTER_MASK_FRAME_2[] = {
".g@$", ".g@$",
".7PF", ".7PF",
"y@$.", "y@$.",
@@ -25,26 +25,30 @@ static const char *PLAYER_MASK_FRAME_2[] = {
}; };
// Player frame 3 (triggered) // Player frame 3 (triggered)
static const char *PLAYER_MASK_FRAME_3[] = { static const char *CHARACTER_MASK_FRAME_3[] = {
".._$@.", ".._$@.",
".$By`.", ".$By`.",
"4@$@@a", "4@$@@a",
"aa@W@." "aa@W@."
}; };
// Combine frames for easy access enum class CharacterFrame {
static const char **PLAYER_FRAMES[] = { Walk1 = 0,
PLAYER_MASK_FRAME_1, // 0 Walk2,
PLAYER_MASK_FRAME_2, // 1 Crawl1,
PLAYER_MASK_FRAME_3 // 2 (triggered) 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 CHARACTER_FRAME_COUNT = static_cast<int>(CharacterFrame::COUNT);
static const int PLAYER_MASK_LINES_FRAME_1 =
sizeof(PLAYER_MASK_FRAME_1) / sizeof(PLAYER_MASK_FRAME_1[0]); // Height per frame
static const int PLAYER_MASK_LINES_FRAME_2 = static const int CHARACTER_HEIGHT_1 = sizeof(CHARACTER_MASK_FRAME_1) / sizeof(CHARACTER_MASK_FRAME_1[0]);
sizeof(PLAYER_MASK_FRAME_2) / sizeof(PLAYER_MASK_FRAME_2[0]); static const int CHARACTER_HEIGHT_2 = sizeof(CHARACTER_MASK_FRAME_2) / sizeof(CHARACTER_MASK_FRAME_2[0]);
static const int PLAYER_MASK_LINES_FRAME_3 = static const int CHARACTER_HEIGHT_3 = sizeof(CHARACTER_MASK_FRAME_3) / sizeof(CHARACTER_MASK_FRAME_3[0]);
sizeof(PLAYER_MASK_FRAME_3) / sizeof(PLAYER_MASK_FRAME_3[0]);

View 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;
}

View 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);