Make map rendering centered

This commit is contained in:
2024-10-05 10:17:12 -05:00
parent 27ce6526e6
commit bdef59bbe1
8 changed files with 163 additions and 51 deletions

View File

@ -21,38 +21,11 @@ void frameInit() {
}
void frameUpdate() {
// Draw buffer
entity_t *ent;
tile_t tile;
uint32_t i;
map_t *map = GAME.currentMap;
if(map == NULL) return;
// Draw the map area.
i = 0;
for(uint16_t y = 0; y < FRAME_HEIGHT - FRAME_BOTTOM_UI_HEIGHT; y++) {
for(uint16_t x = 0; x < FRAME_WIDTH; x++) {
if(x >= map->width || y >= map->height) {
FRAME_COLOR[i] = COLOR_BLACK;
FRAME_BUFFER[i++] = ' ';
continue;
}
// Entity?
ent = mapEntityGetByPosition(map, x, y);
if(ent != NULL) {
FRAME_COLOR[i] = symbolGetColorByEntity(ent);
FRAME_BUFFER[i++] = symbolGetCharByEntity(ent);
continue;
}
// Tile?
tile = mapTileGetByPosition(map, x, y, 0);
FRAME_COLOR[i] = symbolGetColorByTile(tile);
FRAME_BUFFER[i++] = symbolGetCharByTile(tile);
}
}
frameMapDraw();
}
void frameUIReset() {
@ -90,4 +63,104 @@ void frameUIReset() {
i = y * FRAME_WIDTH + FRAME_WIDTH - 1;
FRAME_BUFFER[i] = '|';
}
}
void frameMapDraw() {
// Draw the map area.
entity_t *ent;
tile_t tile;
uint32_t i;
map_t *map = GAME.currentMap;
if(map == NULL) return;
// Try get player
entity_t *player = mapEntityGetByType(map, ENTITY_TYPE_PLAYER);
uint16_t cameraPositionX, cameraPositionY;
if(player == NULL) {
cameraPositionX = 0;
cameraPositionY = 0;
} else {
cameraPositionX = player->x;
cameraPositionY = player->y;
}
// If the size of the map's smaller than the frame, center it, otherwise use
// the cameraPosition as the center point.
int16_t offsetX = 0, offsetY = 0;
if(map->width < FRAME_MAP_WIDTH) {
offsetX = (FRAME_MAP_WIDTH - map->width) / 2;
} else {
// Clamp to map bounds
if(cameraPositionX < FRAME_MAP_WIDTH / 2) {
offsetX = 0;
} else if(cameraPositionX >= map->width - (FRAME_MAP_WIDTH / 2)) {
offsetX = -(map->width - FRAME_MAP_WIDTH);
} else {
offsetX = -(cameraPositionX - (FRAME_MAP_WIDTH / 2));
}
}
if(map->height < FRAME_MAP_HEIGHT) {
offsetY = (FRAME_MAP_HEIGHT - map->height) / 2;
} else {
// Clamp to map bounds
if(cameraPositionY < FRAME_MAP_HEIGHT / 2) {
offsetY = 0;
} else if(cameraPositionY >= map->height - (FRAME_MAP_HEIGHT / 2)) {
offsetY = -(map->height - FRAME_MAP_HEIGHT);
} else {
offsetY = -(cameraPositionY - (FRAME_MAP_HEIGHT / 2));
}
}
// Draw the map
i = 0;
for(uint16_t y = 0; y < FRAME_MAP_HEIGHT; y++) {
for(uint16_t x = 0; x < FRAME_MAP_WIDTH; x++) {
if(x < offsetX || y < offsetY || x >= map->width + offsetX || y >= map->height + offsetY) {
FRAME_COLOR[i] = COLOR_BLACK;
FRAME_BUFFER[i++] = ' ';
continue;
}
// Entity?
ent = mapEntityGetByPosition(map, x - offsetX, y - offsetY);
if(ent != NULL) {
FRAME_COLOR[i] = symbolGetColorByEntity(ent);
FRAME_BUFFER[i++] = symbolGetCharByEntity(ent);
continue;
}
// Tile?
tile = mapTileGetByPosition(map, x - offsetX, y - offsetY, 0);
FRAME_COLOR[i] = symbolGetColorByTile(tile);
FRAME_BUFFER[i++] = symbolGetCharByTile(tile);
}
}
// Old code:
// i = 0;
// for(uint16_t y = 0; y < FRAME_HEIGHT - FRAME_BOTTOM_UI_HEIGHT; y++) {
// for(uint16_t x = 0; x < FRAME_WIDTH; x++) {
// if(x >= map->width || y >= map->height) {
// FRAME_COLOR[i] = COLOR_BLACK;
// FRAME_BUFFER[i++] = ' ';
// continue;
// }
// // Entity?
// ent = mapEntityGetByPosition(map, x, y);
// if(ent != NULL) {
// FRAME_COLOR[i] = symbolGetColorByEntity(ent);
// FRAME_BUFFER[i++] = symbolGetCharByEntity(ent);
// continue;
// }
// // Tile?
// tile = mapTileGetByPosition(map, x, y, 0);
// FRAME_COLOR[i] = symbolGetColorByTile(tile);
// FRAME_BUFFER[i++] = symbolGetCharByTile(tile);
// }
// }
}

View File

@ -16,6 +16,9 @@
#define FRAME_BOTTOM_UI_HEIGHT 8
#define FRAME_MAP_WIDTH FRAME_WIDTH
#define FRAME_MAP_HEIGHT (FRAME_HEIGHT - FRAME_BOTTOM_UI_HEIGHT)
extern char_t FRAME_BUFFER[FRAME_HEIGHT * FRAME_WIDTH];
extern uint8_t FRAME_COLOR[FRAME_HEIGHT * FRAME_WIDTH];
@ -32,4 +35,9 @@ void frameUpdate();
/**
* Resets the UI area of the frame.
*/
void frameUIReset();
void frameUIReset();
/**
* Redraws the entire map area of the frame.
*/
void frameMapDraw();