Fixing camera and viewport bugs.

This commit is contained in:
Kapendev 2024-11-17 14:58:52 +02:00
parent 0c67a8d832
commit 09dcb062e5

View file

@ -557,7 +557,6 @@ struct Viewport {
rl.RenderTexture2D data; rl.RenderTexture2D data;
Color color; Color color;
Blend blend; Blend blend;
bool isAttached;
@safe @nogc nothrow: @safe @nogc nothrow:
@ -600,9 +599,12 @@ struct Viewport {
// NOTE: The engine viewport should not use this function. // NOTE: The engine viewport should not use this function.
@trusted @trusted
void attach() { void attach() {
if (isEmpty || isAttached) return; if (isEmpty) return;
if (engineState.currentViewport != null) {
assert(0, "Cannot attach viewport because another viewport is already attached.");
}
engineState.currentViewport = &this;
if (isResolutionLocked) rl.EndTextureMode(); if (isResolutionLocked) rl.EndTextureMode();
isAttached = true;
rl.BeginTextureMode(data); rl.BeginTextureMode(data);
rl.ClearBackground(color.toRl()); rl.ClearBackground(color.toRl());
rl.BeginBlendMode(blend); rl.BeginBlendMode(blend);
@ -612,8 +614,11 @@ struct Viewport {
// NOTE: The engine viewport should not use this function. // NOTE: The engine viewport should not use this function.
@trusted @trusted
void detach() { void detach() {
if (isEmpty || !isAttached) return; if (isEmpty) return;
isAttached = false; if (engineState.currentViewport != &this) {
assert(0, "Cannot detach viewport because it is not the attached viewport.");
}
engineState.currentViewport = null;
rl.EndBlendMode(); rl.EndBlendMode();
rl.EndTextureMode(); rl.EndTextureMode();
if (isResolutionLocked) rl.BeginTextureMode(engineState.viewport.toRl()); if (isResolutionLocked) rl.BeginTextureMode(engineState.viewport.toRl());
@ -640,7 +645,6 @@ struct Camera {
Vec2 position; /// The position of the cammera. Vec2 position; /// The position of the cammera.
float rotation = 0.0f; /// The rotation angle of the camera, in degrees. float rotation = 0.0f; /// The rotation angle of the camera, in degrees.
float scale = 1.0f; /// The zoom level of the camera. float scale = 1.0f; /// The zoom level of the camera.
bool isAttached; /// Indicates whether the camera is currently in use.
bool isCentered; /// Determines if the camera's origin is at the center instead of the top left. bool isCentered; /// Determines if the camera's origin is at the center instead of the top left.
@safe @nogc nothrow: @safe @nogc nothrow:
@ -659,12 +663,20 @@ struct Camera {
/// Returns the origin of the camera. /// Returns the origin of the camera.
Vec2 origin() { Vec2 origin() {
return Rect(position, resolution / Vec2(scale)).origin(hook); if (engineState.currentViewport == null || engineState.currentViewport.isEmpty) {
return Rect(resolution / Vec2(scale)).origin(hook);
} else {
return Rect(engineState.currentViewport.size / Vec2(scale)).origin(hook);
}
} }
/// Returns the area covered by the camera. /// Returns the area covered by the camera.
Rect area() { Rect area() {
return Rect(position, resolution / Vec2(scale)).area(hook); if (engineState.currentViewport == null || engineState.currentViewport.isEmpty) {
return Rect(position, resolution / Vec2(scale)).area(hook);
} else {
return Rect(position, engineState.currentViewport.size / Vec2(scale)).area(hook);
}
} }
/// Returns the top left point of the camera. /// Returns the top left point of the camera.
@ -735,8 +747,10 @@ struct Camera {
/// Attaches the camera, making it active. /// Attaches the camera, making it active.
@trusted @trusted
void attach() { void attach() {
if (isAttached) return; if (engineState.currentCamera != null) {
isAttached = true; assert(0, "Cannot attach camera because another camera is already attached.");
}
engineState.currentCamera = &this;
auto temp = this.toRl(); auto temp = this.toRl();
if (isPixelSnapped || isPixelPerfect) { if (isPixelSnapped || isPixelPerfect) {
temp.target.x = floor(temp.target.x); temp.target.x = floor(temp.target.x);
@ -750,8 +764,10 @@ struct Camera {
/// Detaches the camera, making it inactive. /// Detaches the camera, making it inactive.
@trusted @trusted
void detach() { void detach() {
if (!isAttached) return; if (engineState.currentCamera != &this) {
isAttached = false; assert(0, "Cannot detach camera because it is not the attached camera.");
}
engineState.currentCamera = null;
rl.EndMode2D(); rl.EndMode2D();
} }
} }
@ -884,6 +900,9 @@ struct EngineState {
LStr assetsPath; LStr assetsPath;
LStr tempText; LStr tempText;
Camera* currentCamera;
Viewport* currentViewport;
Color borderColor; Color borderColor;
Filter defaultFilter; Filter defaultFilter;
Sz tickCount; Sz tickCount;
@ -999,8 +1018,9 @@ int toRl(Filter filter) {
/// Converts a Parin type to a raylib type. /// Converts a Parin type to a raylib type.
rl.Camera2D toRl(Camera camera) { rl.Camera2D toRl(Camera camera) {
auto area = Rect((engineState.currentViewport == null || engineState.currentViewport.isEmpty) ? resolution : engineState.currentViewport.size);
return rl.Camera2D( return rl.Camera2D(
Rect(resolution).origin(camera.isCentered ? Hook.center : Hook.topLeft).toRl(), area.origin(camera.isCentered ? Hook.center : Hook.topLeft).toRl(),
camera.position.toRl(), camera.position.toRl(),
camera.rotation, camera.rotation,
camera.scale, camera.scale,