Skip to content

Commit

Permalink
更新:支持了动态水面与动态浪花
Browse files Browse the repository at this point in the history
  • Loading branch information
park671 committed Jun 10, 2023
1 parent 4046f84 commit 16c8f5d
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 88 deletions.
124 changes: 103 additions & 21 deletions app/src/main/cpp/graphic/bitmap_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "bitmap_render.h"
#include "../opt/mem_opt.h"
#include "palette_data.h"
#include "../logic/logic.h"

/**
*
Expand All @@ -21,10 +22,21 @@ byte *renderBitmap(byte *originData,
int x, int y,
byte *result) {
int offset = y * 256 + x;
int realWidth = width;
int startX = 0;
if(x < 0) {
realWidth += x;
startX = -x;
} else if(x > 240) {
realWidth -= (x - 240);
}
for (int i = 0; i < height; i++) {
__memcpy_aarch64_simd(result + i * 256 + offset,
originData + i * width,
width);
if (i + y < 0 || i + y > 240) {
continue;
}
__memcpy_aarch64_simd(result + i * 256 + offset + startX,
originData + i * width + startX,
realWidth);
}
return result;
}
Expand All @@ -41,36 +53,106 @@ byte *renderBitmap(byte *originData,
* @param result
* @return
*/
byte *renderBitmapColorOffset(byte *originData, int colorOffset,
int width, int height,
int x, int y,
byte *result) {
byte *renderBitmapWithTrans(byte *originData,
int width, int height,
int x, int y,
byte *result) {
int realWidth = width;//16
int startX = 0;
if(x < 0) {
realWidth += x;//16 + -3 = 13
startX = -x;//3
} else if(x > 240) {
realWidth -= (x - 240);
}

int offset = y * 256 + x;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(originData[i * width + j] + colorOffset == TRANSPARENT) {
if (i + y < 0 || i + y > 240) {
continue;
}
for (int j = startX; j < startX + realWidth; j++) {
if (originData[i * width + j] == TRANSPARENT) {
//skip trans color!
continue;
} else {
result[i * 256 + offset + j] = originData[i * width + j] + colorOffset;
result[i * 256 + offset + j] = originData[i * width + j];
}
}
}
return result;
}

byte *renderBitmapWithScroll(byte *originData,
int width, int height,
int x, int y,
byte *result) {
int offset = y * 256 + x;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if(originData[i * width + j] == TRANSPARENT) {
//skip trans color!
byte *renderBitmapWithScroll(byte *originData, byte direct,
int width, int height,
int x, int y,
byte *result) {
if (direct & up) {
renderBitmapWithTrans(originData,
width, height,
x, y,
result);
}
if (direct & down) {
int realWidth = width;//16
int startX = 0;
if(x < 0) {
realWidth += x;//16 + -3 = 13
startX = -x;//3
} else if(x > 240) {
realWidth -= (x - 240);
}

int offset = y * 256 + x;
for (int i = 0; i < height; i++) {
if ((15 - i) + y < 0 || (15 - i) + y > 240) {
continue;
} else {
result[i * 256 + offset + j] = originData[j * width + i];
}
for (int j = startX; j < startX + realWidth; j++) {
if (originData[i * width + j] == TRANSPARENT) {
//skip trans color!
continue;
} else {
result[(15 - i) * 256 + offset + j] = originData[i * width + j];
}
}
}
}
if (direct & left) {
int offset = y * 256 + x;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j + y < 0 || j + y > 240) {
continue;
}
if (i + x < 0 || i + x > 255) {
continue;
}
if (originData[i * width + j] == TRANSPARENT) {
//skip trans color!
continue;
} else {
result[j * 256 + offset + i] = originData[i * width + j];
}
}
}
}
if (direct & right) {
int offset = y * 256 + x;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j + y < 0 || j + y > 240) {
continue;
}
if ((width - 1 - i) + x < 0 || (width - 1 - i) + x > 255) {
continue;
}
if (originData[i * width + j] == TRANSPARENT) {
//skip trans color!
continue;
} else {
result[j * 256 + offset + (width - 1 - i)] = originData[i * width + j];
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/cpp/graphic/bitmap_render.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ byte *renderBitmap(byte *originData,
int width, int height,
int x, int y,
byte *result);
byte *renderBitmapColorOffset(byte *originData, int colorOffset,
int width, int height,
int x, int y,
byte *result);
byte *renderBitmapWithScroll(byte *originData,
byte *renderBitmapWithTrans(byte *originData,
int width, int height,
int x, int y,
byte *result);
byte *renderBitmapWithScroll(byte *originData, byte direct,
int width, int height,
int x, int y,
byte *result);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/cpp/logic/render_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ void DebugRender::renderDebugInfo(byte *screenBuffer) const {
getDefaultPlayer()->godMode?"T":"F");
renderAsciText(screenBuffer, info, 10, 36);
int mapId = getCurrentMap();
int map_width = map_size[mapId * 2 + 1];
sprintf(info, "POS X=%d, Y=%d, TILE=%d ",
getDefaultPlayer()->x,
getDefaultPlayer()->y,
short_map_data[mapId][getDefaultPlayer()->y * map_width + getDefaultPlayer()->x]);
getTileIdx(getDefaultPlayer()->x,
getDefaultPlayer()->y));
renderAsciText(screenBuffer, info, 10, 44);
}

Expand Down
81 changes: 36 additions & 45 deletions app/src/main/cpp/logic/render_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ byte *MapRender::render(byte *screenBuffer) {
}
pthread_mutex_lock(&changeMapMutex);
Character *player = getDefaultPlayer();
posX = player->renderX - 127;
posY = player->renderY - 127;
posX = player->renderX - 128;
posY = player->renderY - 128;
screenBuffer = renderMap(posX, posY, screenBuffer);
// renderWater(screenBuffer);
renderWater(screenBuffer);
renderDoor(screenBuffer);
renderSprite(screenBuffer);
renderPlayers(screenBuffer, player);
Expand All @@ -78,54 +78,45 @@ byte *MapRender::render(byte *screenBuffer) {
}

void MapRender::renderPlayers(byte *screenBuffer, Character *player) const {
renderBitmapColorOffset(player->currentBitmap, 0,
16, 16,
127, 127, screenBuffer);
renderBitmapWithTrans(player->currentBitmap,
16, 16,
128, 128, screenBuffer);
}

int water_status_map[4] = {1,2,3,2};
int water_status_map[4] = {1, 2, 3, 2};

byte water_status = 0;
int water_status_clk = 0;

bool isTileWater(unsigned short tileId) {
for(int i = 0;i<WATER_BLOCK_COUNT;i++) {
if (tileId == water_block_id[i]) {
return true;
}
}
return false;
}

void MapRender::renderWater(byte *screenBuffer) const {
int map_width = map_size[mapId * 2 + 1];
int startX = posX / 16;
int startY = posY / 16;
for (int x = startX; x < startX + 16; x++) {
for (int y = startY; y < startY + 16; y++) {
if(isTileWater(short_map_data[mapId][y * map_width + x])) {
for (int x = startX - 1; x < startX + 17; x++) {
for (int y = startY - 1; y < startY + 17; y++) {
if (isPureWater(getTileIdx(x, y))) {
int renderY = y * 16 - posY;
int renderX = x * 16 - posX;
if (renderX > 0 && renderX < 240 && renderY > 0 && renderY < 224) {
renderBitmapColorOffset(water[water_status_map[water_status] - 1],0,
16, 16,
renderX, renderY,
screenBuffer);
}
byte direct = 0;
if (!isTileWater(short_map_data[mapId][(y - 1) * map_width + (x)])) {
direct |= up;
}
if (!isTileWater(short_map_data[mapId][(y + 1) * map_width + (x)])) {
direct |= down;
}
if (!isTileWater(short_map_data[mapId][(y) * map_width + (x - 1)])) {
direct |= left;
}
if (!isTileWater(short_map_data[mapId][(y) * map_width + (x + 1)])) {
direct |= right;
if (renderX > -16 && renderX < 272 && renderY > -16 && renderY < 256) {
renderBitmap(water[water_status_map[water_status] - 1],
16, 16,
renderX, renderY,
screenBuffer);
byte direct = 0;
if (!isWater(getTileIdx(x, y - 1))) {
direct |= up;
}
if (!isWater(getTileIdx(x, y + 1))) {
direct |= down;
}
if (!isWater(getTileIdx(x - 1, y))) {
direct |= left;
}
if (!isWater(getTileIdx(x + 1, y))) {
direct |= right;
}
renderWave(direct, water_status_map[water_status] - 1, screenBuffer, renderX,
renderY);
}
renderWave(direct, water_status_map[water_status] - 1, screenBuffer, renderX, renderY);
}
}
}
Expand All @@ -138,12 +129,12 @@ void MapRender::renderSprite(byte *screenBuffer) const {
byte y = map_sprite[mapId][i * 3 + 1] - 1;
int renderY = y * 16 - posY;
int renderX = x * 16 - posX;
if (renderX > 0 && renderX < 240 && renderY > 0 && renderY < 224) {
if (renderX > -16 && renderX < 272 && renderY > -16 && renderY < 256) {
byte spriteBmpId = map_sprite[mapId][i * 3 + 2];
renderBitmapColorOffset(sprites[spriteBmpId], 0,
16, 16,
renderX, renderY,
screenBuffer);
renderBitmapWithTrans(sprites[spriteBmpId],
16, 16,
renderX, renderY,
screenBuffer);
}
}
}
Expand Down Expand Up @@ -249,7 +240,7 @@ void MapRender::tikLogic() {
entranceAnimation--;
}
water_status_clk++;
if(water_status_clk > 60) {
if (water_status_clk > 60) {
water_status_clk = 0;
water_status++;
water_status %= 4;
Expand Down
38 changes: 23 additions & 15 deletions app/src/main/cpp/maps/active_tile_bmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../logic/status/character.h"
#include "../graphic/bitmap_render.h"
#include "../logic/logic.h"
#include "../graphic/palette_data.h"

unsigned short water_block_id[WATER_BLOCK_COUNT] = {
13,15,31,35,39,47,85,112,154,190,
Expand All @@ -22,26 +23,33 @@ unsigned short pure_water_block_id[32] = {
1864,1881,
};

bool isWater(unsigned short tileId) {
for(int i = 0;i<WATER_BLOCK_COUNT;i++) {
if (tileId == water_block_id[i]) {
return true;
}
}
return false;
}

bool isPureWater(unsigned short tileId) {
for(int i = 0;i<32;i++) {
if (tileId == pure_water_block_id[i]) {
return true;
}
}
return false;
}

/**
* @param direct 复用一下方向键的方向吧
* @param screenBuffer
*/
void renderWave(byte direct, byte status, byte *screenBuffer, int x, int y) {
if (direct & up) {
renderBitmapColorOffset(wave[status],0,
16, 4,
x, y,
screenBuffer);
}
if(direct & down) {

}
if(direct & left) {

}
if(direct & right) {

}
renderBitmapWithScroll(wave[status], direct,
16, 4,
x, y,
screenBuffer);
}

//upper side, need to scroll & use
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/maps/active_tile_bmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ extern byte water[3][16 * 16];

extern void renderWave(byte direct, byte status, byte *screenBuffer, int x, int y);

bool isWater(unsigned short tileId);

bool isPureWater(unsigned short tileId);

#endif //METALMAX_ACTIVE_TILE_BMP_H
6 changes: 6 additions & 0 deletions app/src/main/cpp/maps/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ void refreshCurrentMap(int mapId) {
}

unsigned short getTileIdx(int x, int y) {
if (y >= map_height
|| y < 0
|| x >= map_width
|| x < 0) {
return current_fill;
}
return short_current_map[y * (map_width) + x];
}

Expand Down

0 comments on commit 16c8f5d

Please sign in to comment.