3 Commits

Author SHA1 Message Date
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
7 changed files with 52 additions and 24 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,8 +16,8 @@ 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;
enum class PlayerState { Walk1, Walk2, Crawl1 }; enum class PlayerState { Walk1, Walk2, Crawl1 };
@@ -128,6 +129,8 @@ void render_loop(int speed) {
anim_timer.start(); anim_timer.start();
int shift = 0; int shift = 0;
PlayerPosition 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;
@@ -155,7 +158,8 @@ 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); int player_frame = get_player_frame(player_state.current_state);
draw_player(PLAYER_X, PLAYER_Y, player_frame); PlayerPosition draw_pos = get_aligned_frame_position(pos, player_frame);
draw_player(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

@@ -4,9 +4,9 @@
#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_player(int x, int y, int frame_index) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT) if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
frame_index = 0; // fallback frame_index = 0; // fallback safety
const char **sprite = PLAYER_FRAMES[frame_index]; const char **sprite = PLAYER_FRAMES[frame_index];
int sprite_height = 0; int sprite_height = 0;
@@ -17,19 +17,9 @@ void draw_player(int view_width, int view_height, int frame_index) {
else else
sprite_height = PLAYER_MASK_LINES_FRAME_3; sprite_height = PLAYER_MASK_LINES_FRAME_3;
// Width from first line // Draw the sprite starting from (x, y)
int sprite_width = 0;
if (sprite_height > 0) {
const char *first_line = sprite[0];
while (first_line[sprite_width] != '\0')
sprite_width++;
}
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++) { for (int i = 0; i < sprite_height; i++) {
printf("\033[%d;%dH%s", center_y + i + 1, center_x + 1, sprite[i]); printf("\033[%d;%dH%s", y + i + 1, x + 1, sprite[i]);
} }
fflush(stdout); fflush(stdout);
} }

View File

@@ -1,5 +1,5 @@
// src/render/player.h // src/render/player.h
#pragma once #pragma once
// 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_player(int x, int y, int frame_index);

View File

@@ -0,0 +1,24 @@
// 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
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
frame_index = 0;
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;
PlayerPosition draw_pos;
draw_pos.x = base.x;
draw_pos.y = (VIEW_HEIGHT - base.y) - sprite_height;
return draw_pos;
}

View File

@@ -0,0 +1,11 @@
// src/render/player_positioning.h
#pragma once
#include "player_mask.h"
struct PlayerPosition {
int x;
int y;
};
// Calculates drawing position relative to bottom-left pivot
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index);