Skip to content

Commit

Permalink
Less popping in of chunks on fog border (Thanks MrGoober), also can p…
Browse files Browse the repository at this point in the history
…ress F8 to show a shadow above the block you are standing on. (Thanks MrGoober)
  • Loading branch information
UnknownShadow200 committed Feb 4, 2016
1 parent 88613a8 commit bfe9e28
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 8 deletions.
4 changes: 3 additions & 1 deletion ClassicalSharp/Entities/LocalPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class LocalPlayer : Player {

/// <summary> Position the player's position is set to when the 'respawn' key binding is pressed. </summary>
public Vector3 SpawnPoint;

public float SpawnYaw, SpawnPitch;

/// <summary> The distance (in blocks) that players are allowed to
Expand All @@ -21,6 +21,8 @@ public partial class LocalPlayer : Player {
/// when the 'speeding' key binding is held down. </summary>
public float SpeedMultiplier = 10;

public bool ShowShadow = false;

public byte UserType;

/// <summary> Whether blocks that the player places that intersect themselves
Expand Down
57 changes: 56 additions & 1 deletion ClassicalSharp/Entities/Player.Rendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ partial class Player {
public override void Despawn() {
game.Graphics.DeleteTexture( ref PlayerTextureId );
game.Graphics.DeleteTexture( ref nameTex.ID );
if( shadowTex != -1 )
game.Graphics.DeleteTexture( ref shadowTex );
}

protected void InitRenderingData() {
Expand All @@ -38,11 +40,64 @@ protected void DrawName() {
Utils.CalcBillboardPoints( size, pos, ref game.View, out p111, out p121, out p212, out p222 );
api.texVerts[0] = new VertexPos3fTex2fCol4b( p111, nameTex.U1, nameTex.V2, col );
api.texVerts[1] = new VertexPos3fTex2fCol4b( p121, nameTex.U1, nameTex.V1, col );
api.texVerts[2] = new VertexPos3fTex2fCol4b( p222, nameTex.U2, nameTex.V1, col );
api.texVerts[2] = new VertexPos3fTex2fCol4b( p222, nameTex.U2, nameTex.V1, col );
api.texVerts[3] = new VertexPos3fTex2fCol4b( p212, nameTex.U2, nameTex.V2, col );

api.SetBatchFormat( VertexFormat.Pos3fTex2fCol4b );
api.UpdateDynamicIndexedVb( DrawMode.Triangles, api.texVb, api.texVerts, 4, 6 );
}

internal void DrawShadow( bool show ) {
int x = Utils.Floor( Position.X ), z = Utils.Floor( Position.Z );
if( !show || !game.Map.IsValidPos( x, 0, z ) || Position.Y < 0 ) return;
CheckShadowTexture();
BlockInfo info = game.BlockInfo;
game.Graphics.BindTexture( shadowTex );

int y = Math.Min( (int)Position.Y, game.Map.Height - 1 );
float shadowY = 0;
while( y >= 0 ) {
byte block = game.Map.GetBlock( x, y, z );
if( !(info.IsAir[block] || info.IsSprite[block]) ) {
shadowY = y + info.MaxBB[block].Y; break;
}
y--;
}

if( (Position.Y - y) <= 16 ) shadowY += 1/32f;
else if( (Position.Y - y) <= 32 ) shadowY += 1/16f;
else if( (Position.Y - y) <= 96 ) shadowY += 1/8f;
else shadowY += 1/4f;
VertexPos3fTex2fCol4b[] verts = game.Graphics.texVerts;
int vb = game.Graphics.texVb;

FastColour col = FastColour.White;
verts[0] = new VertexPos3fTex2fCol4b( x, shadowY, z, 0, 0, col );
verts[1] = new VertexPos3fTex2fCol4b( x + 1, shadowY, z, 1, 0, col );
verts[2] = new VertexPos3fTex2fCol4b( x + 1, shadowY, z + 1, 1, 1, col );
verts[3] = new VertexPos3fTex2fCol4b( x, shadowY, z + 1, 0, 1, col );
game.Graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, vb, verts, 4, 6 );
}

int shadowTex = -1;
unsafe void CheckShadowTexture() {
if( shadowTex != -1 ) return;
const int size = 128, half = size / 2;
using( Bitmap bmp = new Bitmap( size, size ) )
using( FastBitmap fastBmp = new FastBitmap( bmp, true, false ) )
{
int inPix = new FastColour( 0, 0, 0, 200 ).ToArgb();
int outPix = inPix & 0xFFFFFF;
for( int y = 0; y < fastBmp.Height; y++ ) {
int* row = fastBmp.GetRowPtr( y );
for( int x = 0; x < fastBmp.Width; x++ ) {
double dist = (half - (x + 0.5)) * (half - (x + 0.5)) +
(half - (y + 0.5)) * (half - (y + 0.5));
row[x] = dist < half * half ? inPix : outPix;
}
}
shadowTex = game.Graphics.CreateTexture( fastBmp );
}
}
}
}
2 changes: 2 additions & 0 deletions ClassicalSharp/Game/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ bool HandleBuiltinKey( Key key ) {
game.SetNewScreen( new PauseScreen( game ) );
} else if( key == Keys[KeyBinding.OpenInventory] ) {
game.SetNewScreen( new BlockSelectScreen( game ) );
} else if( key == Key.F8 ) {
game.LocalPlayer.ShowShadow = !game.LocalPlayer.ShowShadow;
} else {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions ClassicalSharp/GraphicsAPI/IGraphicsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ public virtual void Dispose() {
DeleteDynamicVb( texVb );
}

VertexPos3fCol4b[] quadVerts = new VertexPos3fCol4b[4];
int quadVb;
internal VertexPos3fCol4b[] quadVerts = new VertexPos3fCol4b[4];
internal int quadVb;
public virtual void Draw2DQuad( float x, float y, float width, float height, FastColour col ) {
quadVerts[0] = new VertexPos3fCol4b( x, y, 0, col );
quadVerts[1] = new VertexPos3fCol4b( x + width, y, 0, col );
Expand Down
8 changes: 6 additions & 2 deletions ClassicalSharp/Rendering/MapBordersRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public void Render( double deltaTime ) {
Vector3 camPos = game.CurrentCameraPos;
bool underWater = camPos.Y < game.Map.EdgeHeight;
graphics.AlphaBlending = true;
if( underWater )
if( underWater ) {
game.LocalPlayer.DrawShadow( game.LocalPlayer.ShowShadow );
game.WeatherRenderer.Render( deltaTime );
}

graphics.BindTexture( edgeTexId );
graphics.BindVb( edgesVb );
Expand All @@ -65,8 +67,10 @@ public void Render( double deltaTime ) {
if( game.Map.EdgeBlock != Block.Air && camPos.Y >= yVisible )
graphics.DrawIndexedVb_TrisT2fC4b( edgesVertices * 6 / 4, 0 );

if( !underWater )
if( !underWater ) {
game.LocalPlayer.DrawShadow( game.LocalPlayer.ShowShadow );
game.WeatherRenderer.Render( deltaTime );
}
graphics.AlphaBlending = false;
graphics.Texturing = false;
graphics.AlphaTest = false;
Expand Down
2 changes: 1 addition & 1 deletion ClassicalSharp/Rendering/MapRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void Render( double deltaTime ) {
const double targetTime = (1.0 / 30) + 0.01;
void UpdateChunks( double deltaTime ) {
int chunkUpdates = 0;
int viewDist = game.ViewDistance < 16 ? 16 : game.ViewDistance;
int viewDist = Utils.AdjViewDist( game.ViewDistance < 16 ? 16 : game.ViewDistance );
int adjViewDistSqr = (viewDist + 24) * (viewDist + 24);
chunksTarget += deltaTime < targetTime ? 1 : -1; // build more chunks if 30 FPS or over, otherwise slowdown.
Utils.Clamp( ref chunksTarget, 4, 12 );
Expand Down
2 changes: 1 addition & 1 deletion ClassicalSharp/Rendering/WeatherRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public WeatherRenderer( Game game ) {
public void Render( double deltaTime ) {
Weather weather = map.Weather;
if( weather == Weather.Sunny ) return;
if( heightmap == null ) InitHeightmap();
if( heightmap == null ) InitHeightmap();

graphics.BindTexture( weather == Weather.Rainy ? game.RainTexId : game.SnowTexId );
Vector3 camPos = game.CurrentCameraPos;
Expand Down

0 comments on commit bfe9e28

Please sign in to comment.