Bug fixes and added wrap enum.

This commit is contained in:
Kapendev 2024-12-05 18:00:40 +02:00
parent d54610d430
commit 37d3901936
2 changed files with 57 additions and 15 deletions

View file

@ -42,6 +42,12 @@ enum Filter : ubyte {
linear = rl.TEXTURE_FILTER_BILINEAR, /// Bilinear filtering (smooth).
}
/// A type representing texture wrapping modes.
enum Wrap : ubyte {
repeat = rl.TEXTURE_WRAP_REPEAT, // Repeats texture.
clamp = rl.TEXTURE_WRAP_CLAMP, // Clamps texture.
}
/// A type representing blending modes.
enum Blend : ubyte {
alpha = rl.BLEND_CUSTOM_SEPARATE, /// Standard alpha blending.
@ -52,7 +58,7 @@ enum Blend : ubyte {
}
/// A type representing a limited set of keyboard keys.
enum Keyboard {
enum Keyboard : ushort {
a = rl.KEY_A, /// The A key.
b = rl.KEY_B, /// The B key.
c = rl.KEY_C, /// The C key.
@ -133,14 +139,14 @@ enum Keyboard {
}
/// A type representing a limited set of mouse keys.
enum Mouse {
enum Mouse : ubyte {
left = rl.MOUSE_BUTTON_LEFT, /// The left mouse button.
right = rl.MOUSE_BUTTON_RIGHT, /// The right mouse button.
middle = rl.MOUSE_BUTTON_MIDDLE, /// The middle mouse button.
}
/// A type representing a limited set of gamepad buttons.
enum Gamepad {
enum Gamepad : ubyte {
left = rl.GAMEPAD_BUTTON_LEFT_FACE_LEFT, /// The left button.
right = rl.GAMEPAD_BUTTON_LEFT_FACE_RIGHT, /// The right button.
up = rl.GAMEPAD_BUTTON_LEFT_FACE_UP, /// The up button.
@ -268,6 +274,13 @@ struct Texture {
rl.SetTextureFilter(data, value);
}
/// Sets the wrap mode of the texture.
@trusted
void setWrap(Wrap value) {
if (isEmpty) return;
rl.SetTextureWrap(data, value);
}
/// Frees the loaded texture.
@trusted
void free() {
@ -352,6 +365,13 @@ struct Font {
rl.SetTextureFilter(data.texture, value);
}
/// Sets the wrap mode of the font.
@trusted
void setWrap(Wrap value) {
if (isEmpty) return;
rl.SetTextureWrap(data.texture, value);
}
/// Frees the loaded font.
@trusted
void free() {
@ -594,6 +614,7 @@ struct Viewport {
if (!isEmpty) rl.UnloadRenderTexture(data);
data = rl.LoadRenderTexture(width, height);
setFilter(engineState.defaultFilter);
setWrap(engineState.defaultWrap);
}
/// Attaches the viewport, making it active.
@ -634,6 +655,13 @@ struct Viewport {
rl.SetTextureFilter(data.texture, value);
}
/// Sets the wrap mode of the viewport.
@trusted
void setWrap(Wrap value) {
if (isEmpty) return;
rl.SetTextureWrap(data.texture, value);
}
/// Frees the loaded viewport.
@trusted
void free() {
@ -911,6 +939,7 @@ struct EngineState {
Color borderColor;
Filter defaultFilter;
Wrap defaultWrap;
Sz tickCount;
@safe @nogc nothrow:
@ -1130,6 +1159,7 @@ TextId loadText(IStr path, Sz tag = 0) {
Result!Texture loadRawTexture(IStr path) {
auto value = rl.LoadTexture(path.toAssetsPath().toCStr().getOr()).toParin();
value.setFilter(engineState.defaultFilter);
value.setWrap(engineState.defaultWrap);
return Result!Texture(value, value.isEmpty.toFault(Fault.cantFind));
}
@ -1168,6 +1198,7 @@ Result!Font loadRawFont(IStr path, int size, int runeSpacing, int lineSpacing, I
value.runeSpacing = runeSpacing;
value.lineSpacing = lineSpacing;
value.setFilter(engineState.defaultFilter);
value.setWrap(engineState.defaultWrap);
return Result!Font(value, value.isEmpty.toFault(Fault.cantFind));
}
@ -1498,16 +1529,31 @@ Filter defaultFilter() {
return engineState.defaultFilter;
}
/// Returns the default wrap mode for textures.
Wrap defaultWrap() {
return engineState.defaultWrap;
}
/// Sets the default filter mode for textures to the specified value.
void setDefaultFilter(Filter value) {
engineState.defaultFilter = value;
}
/// Sets the default wrap mode for textures to the specified value.
void setDefaultWrap(Wrap value) {
engineState.defaultWrap = value;
}
/// Sets the filter mode used by the engine viewport to the specified value.
void setEngineViewportFilter(Filter value) {
engineState.viewport.setFilter(value);
}
/// Sets the wrap mode used by the engine viewport to the specified value.
void setEngineViewportWrap(Wrap value) {
engineState.viewport.setWrap(value);
}
/// Returns the current master volume level.
@trusted
float masterVolume() {

View file

@ -22,14 +22,14 @@ public import joka.types;
@safe @nogc nothrow:
struct Tile {
int width;
int height;
Sz id;
int width = 16;
int height = 16;
short id;
Vec2 position;
@safe @nogc nothrow:
this(int width, int height, Sz id, Vec2 position = Vec2()) {
this(int width, int height, short id, Vec2 position = Vec2()) {
this.width = width;
this.height = height;
this.id = id;
@ -70,6 +70,7 @@ struct TileMap {
int tileWidth = 16;
int tileHeight = 16;
Vec2 position;
alias data this;
@safe @nogc nothrow:
@ -83,11 +84,6 @@ struct TileMap {
this.data.fill(-1);
}
/// Returns true if the tile map has not been loaded.
bool isEmpty() {
return data.length == 0;
}
int width() {
return cast(int) (softMaxColCount * tileWidth);
}
@ -231,7 +227,7 @@ Result!TileMap loadRawTileMap(IStr path, int tileWidth, int tileHeight) {
}
void drawTile(Texture texture, Tile tile, DrawOptions options = DrawOptions()) {
if (texture.isEmpty || tile.width == 0 || tile.height == 0) return;
if (texture.isEmpty || tile.id < 0 || tile.width <= 0 || tile.height <= 0) return;
drawTextureArea(texture, tile.textureArea(texture.width / tile.width), tile.position, options);
}
@ -240,7 +236,7 @@ void drawTile(TextureId texture, Tile tile, DrawOptions options = DrawOptions())
}
void drawTileMap(Texture texture, TileMap map, Camera camera, DrawOptions options = DrawOptions()) {
if (texture.isEmpty || map.tileWidth == 0 || map.tileHeight == 0) return;
if (texture.isEmpty || map.tileWidth <= 0 || map.tileHeight <= 0) return;
auto textureColCount = texture.width / map.tileWidth;
auto targetTileWidth = cast(int) (map.tileWidth * options.scale.x);
@ -253,7 +249,7 @@ void drawTileMap(Texture texture, TileMap map, Camera camera, DrawOptions option
foreach (row; colRow1.y .. colRow2.y) {
foreach (col; colRow1.x .. colRow2.x) {
auto id = map[row, col];
if (id == -1) continue;
if (id < 0) continue;
textureArea.position.x = (id % textureColCount) * map.tileWidth;
textureArea.position.y = (id / textureColCount) * map.tileHeight;
drawTextureArea(