Skip to content

Commit

Permalink
allow skipping sort of objects by zorder for Overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Apr 17, 2022
1 parent 91c368a commit 318cd13
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
35 changes: 25 additions & 10 deletions mode7_demo/Mode7.asc
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ int Mode7World::GetAngleObjectAndCamera(Mode7Object* m7obj)
return _NormalizeAngle(FloatToInt(angle_target - this._camera_angle_y, eRoundNearest));
}

void Mode7World::UpdateObjects()
void Mode7World::UpdateObjects(bool do_sort)
{
int fnd_objects = 0;

Expand Down Expand Up @@ -469,7 +469,8 @@ void Mode7World::UpdateObjects()
if (fnd_objects == 0) return;

// sort found objects
if (fnd_objects > 1) {

if (do_sort && fnd_objects > 1) {
// bubble sort
for(int i = 0; i < fnd_objects - 1; i++) {
for (int j = i; j < fnd_objects; j++) {
Expand All @@ -481,7 +482,7 @@ void Mode7World::UpdateObjects()
}
}
}

int in, obin, slot, w, h; // in: fnd_list index, obin: all objects list index
int x2d, y2d;

Expand Down Expand Up @@ -510,7 +511,11 @@ void Mode7World::UpdateObjects()
this.Objects[obin].ScreenWidth = w;
this.Objects[obin].ScreenHeight = h;
this.Objects[obin].ScreenVisible = this.Objects[obin].Visible;
this.Objects[obin].ScreenZOrder = i;
if(do_sort) {
this.Objects[obin].ScreenZOrder = i;
} else {
this.Objects[obin].ScreenZOrder = FloatToInt(10000.0 / _zd[in]);
}
}
}

Expand Down Expand Up @@ -538,9 +543,9 @@ void Mode7World::DrawObjectsOverlay()
{
Mode7Object* obj = this.Objects[i];

if(obj.ScreenVisible)
bool is_visible = obj.ScreenVisible;
if(is_visible)
{
this.Overlays[i].Transparency = 0;

if(this.Overlays[i] == null || !this.Overlays[i].Valid) {
this.Overlays[i] = Overlay.CreateGraphical(obj.ScreenX, obj.ScreenY, obj.Graphic, true);
Expand All @@ -551,6 +556,7 @@ void Mode7World::DrawObjectsOverlay()
this.OverlaysGraphic[i] = obj.Graphic;
}

this.Overlays[i].Transparency = 0;
Overlay* ovr = this.Overlays[i];

if(ovr.X != obj.ScreenX) ovr.X = obj.ScreenX;
Expand All @@ -563,6 +569,7 @@ void Mode7World::DrawObjectsOverlay()
{
if(this.Overlays[i] != null) {
this.Overlays[i].Transparency = 100;
this.Overlays[i].X = Screen.Width;
//this.Overlays[i].Remove();
//this.Overlays[i] = null;
}
Expand Down Expand Up @@ -623,6 +630,9 @@ void Mode7World::AddExternalObject(Mode7Object* m7obj)
if (this.ObjectCount == MAX_OBJECTS) return;
this.Objects[this.ObjectCount] = m7obj;

this.OverlaysGraphic[this.ObjectCount] = m7obj.Graphic;
this.Overlays[this.ObjectCount] = Overlay.CreateGraphical(Screen.Width, Screen.Height, m7obj.Graphic, true);

this.ObjectCount++;
}

Expand All @@ -632,12 +642,15 @@ void Mode7World::RemoveObject(int object_id)

// removes object at the top
if(object_id < 0) {
this.ObjectCount--;

this.Objects[this.ObjectCount] = null;
if(this.Overlays[this.ObjectCount] != null && this.Overlays[this.ObjectCount].Valid)
if(this.Overlays[this.ObjectCount] != null && this.Overlays[this.ObjectCount].Valid) {
this.Overlays[this.ObjectCount].Transparency = 100;
this.Overlays[this.ObjectCount].Remove();
}
this.Overlays[this.ObjectCount] = null;

this.ObjectCount--;
return;
}

Expand All @@ -650,8 +663,10 @@ void Mode7World::RemoveObject(int object_id)
this.ObjectCount--;

this.Objects[this.ObjectCount] = null;
if(this.Overlays[this.ObjectCount] != null && this.Overlays[this.ObjectCount].Valid)
if(this.Overlays[this.ObjectCount] != null && this.Overlays[this.ObjectCount].Valid) {
this.Overlays[this.ObjectCount].Transparency = 100;
this.Overlays[this.ObjectCount].Remove();
}
this.Overlays[this.ObjectCount] = null;

}
Expand All @@ -661,4 +676,4 @@ void Mode7World::RemoveAllsObjects()
while(this.ObjectCount > 0) {
this.RemoveObject(-1);
}
}
}
4 changes: 2 additions & 2 deletions mode7_demo/Mode7.ash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// new module header
#define MAX_OBJECTS 512
#define MAX_OBJECTS 2048

enum eCameraTargetType {
eCameraTarget_FollowBehind,
Expand Down Expand Up @@ -144,7 +144,7 @@ struct Mode7World extends Mode7 {
/// Returns the angle in degrees between the camera and whatever angle is set to a specific object. Useful when you want to change the graphic of an object based on their relative angle.
import int GetAngleObjectAndCamera(Mode7Object* m7obj);
/// Update the screen transform of all objects world positions to their screen positions. You must call it before drawing any objects!
import void UpdateObjects();
import void UpdateObjects(bool do_sort = true);
/// Draws only the objects in the screen sprite. You can use when you need to draw additional things between the ground and the objects. Or when you don't need the ground at all.
import void DrawObjects();
/// Draws objects as overlays, without rasterizing at the screen sprite.
Expand Down
2 changes: 1 addition & 1 deletion mode7_demo/room1.asc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function room_RepExec()

m7.TargetCamera(ship.X, ship.Y, ship.Z, ship.Angle);

m7.UpdateObjects();
m7.UpdateObjects(false);

// determine kart sprite
int kart_sprite = m7.GetAngleObjectAndCamera(ship) / 22;
Expand Down

0 comments on commit 318cd13

Please sign in to comment.