Fixed viewport alpha bug.

This commit is contained in:
Kapendev 2024-10-27 04:37:06 +02:00
parent 651850d84a
commit af2b896a37
2 changed files with 41 additions and 30 deletions

View file

@ -592,6 +592,8 @@ struct Viewport {
if (isAttached) return;
isAttached = true;
rl.BeginTextureMode(data);
// NOTE: Maybe change this in the future. It was used to fix an alpha problem.
rl.BeginBlendMode(rl.BLEND_ADDITIVE);
rl.ClearBackground(color.toRl());
}
@ -600,6 +602,7 @@ struct Viewport {
void detach() {
if (!isAttached) return;
isAttached = false;
rl.EndBlendMode();
rl.EndTextureMode();
}
@ -1704,30 +1707,6 @@ Vec2 measureTextSize(FontId font, IStr text, DrawOptions options = DrawOptions()
return measureTextSize(font.getOr(), text, options);
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(char key) {
return rl.IsKeyPressed(toUpper(key));
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(Keyboard key) {
return rl.IsKeyPressed(key);
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(Mouse key) {
return rl.IsMouseButtonPressed(key);
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(Gamepad key, int id = 0) {
return rl.IsGamepadButtonPressed(id, key);
}
/// Returns true if the specified key is currently pressed.
@trusted
bool isDown(char key) {
@ -1752,6 +1731,30 @@ bool isDown(Gamepad key, int id = 0) {
return rl.IsGamepadButtonDown(id, key);
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(char key) {
return rl.IsKeyPressed(toUpper(key));
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(Keyboard key) {
return rl.IsKeyPressed(key);
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(Mouse key) {
return rl.IsMouseButtonPressed(key);
}
/// Returns true if the specified key was pressed.
@trusted
bool isPressed(Gamepad key, int id = 0) {
return rl.IsGamepadButtonPressed(id, key);
}
/// Returns true if the specified key was released.
@trusted
bool isReleased(char key) {

View file

@ -62,8 +62,8 @@ struct TileMap {
Grid!short data;
Sz estimatedMaxRowCount;
Sz estimatedMaxColCount;
int tileWidth;
int tileHeight;
int tileWidth = 16;
int tileHeight = 16;
Vec2 position;
alias data this;
@ -220,6 +220,7 @@ Result!TileMap loadRawTileMap(IStr path, int tileWidth, int tileHeight) {
return toTileMap(temp.get(), tileWidth, tileHeight);
}
// TODO: Change that so it's easier to use.
void drawTile(Texture texture, Tile tile, DrawOptions options = DrawOptions()) {
if (texture.isEmpty) return;
drawTextureArea(texture, tile.textureArea(texture.width / tile.width), tile.position, options);
@ -230,18 +231,25 @@ void drawTile(TextureId texture, Tile tile, DrawOptions options = DrawOptions())
}
void drawTileMap(Texture texture, TileMap map, Camera camera, DrawOptions options = DrawOptions()) {
auto textureColCount = texture.width / map.tileWidth;
auto targetTileWidth = cast(int) (map.tileWidth * options.scale.x);
auto targetTileHeight = cast(int) (map.tileHeight * options.scale.y);
auto colRow1 = map.firstGridPosition(camera.topLeftPoint, options);
auto colRow2 = map.lastGridPosition(camera.bottomRightPoint, options);
if (colRow1.x == colRow2.x || colRow1.y == colRow2.y) return;
if (map.tileWidth == 0 || map.tileHeight == 0) return;
foreach (row; colRow1.y .. colRow2.y) {
foreach (col; colRow1.x .. colRow2.x) {
if (map[row, col] == -1) continue;
auto tile = Tile(map[row, col], map.tileWidth, map.tileHeight);
tile.position = map.position + Vec2(col * targetTileWidth, row * targetTileHeight);
drawTile(texture, tile, options);
auto id = map[row, col];
if (id == -1) continue;
drawTextureArea(
texture,
Rect((id % textureColCount) * map.tileWidth, (id / textureColCount) * map.tileHeight, map.tileWidth, map.tileHeight),
map.position + Vec2(col * targetTileWidth, row * targetTileHeight),
options,
);
}
}
}