More porting of Camera to C.

This commit is contained in:
UnknownShadow200 2017-08-25 18:58:58 +10:00
parent b9eb5f976c
commit 2281dffdbb
7 changed files with 47 additions and 47 deletions

View File

@ -170,27 +170,19 @@ namespace ClassicalSharp.GraphicsAPI {
protected override int CreateTexture(int width, int height, IntPtr scan0, bool managedPool, bool mipmaps) {
D3D.Texture texture = null;
Usage usage = (mipmaps && AutoMipmaps) ? Usage.None : Usage.None;
int levels = (mipmaps && AutoMipmaps) ? 5 : 1;
int levels = mipmaps ? 5 : 1;
if (managedPool) {
texture = device.CreateTexture(width, height, levels, usage, Format.A8R8G8B8, Pool.Managed);
texture = device.CreateTexture(width, height, levels, Usage.None, Format.A8R8G8B8, Pool.Managed);
texture.SetData(0, LockFlags.None, scan0, width * height * 4);
// TODO: FIX THIS MEMORY LEAK!!!!!
if (mipmaps) DoMipmaps(texture, width, height, scan0);
/*if (mipmaps && AutoMipmaps) {
DoMipmaps(texture, 1, width / 2, height / 2, width, height, scan0);
DoMipmaps(texture, 2, width / 4, height / 4, width, height, scan0);
DoMipmaps(texture, 3, width / 8, height / 8, width, height, scan0);
DoMipmaps(texture, 4, width / 16, height / 16, width, height, scan0);
}*/
} else {
D3D.Texture sys = device.CreateTexture(width, height, levels, usage, Format.A8R8G8B8, Pool.SystemMemory);
D3D.Texture sys = device.CreateTexture(width, height, levels, Usage.None, Format.A8R8G8B8, Pool.SystemMemory);
sys.SetData(0, LockFlags.None, scan0, width * height * 4);
texture = device.CreateTexture(width, height, levels, usage, Format.A8R8G8B8, Pool.Default);
texture = device.CreateTexture(width, height, levels, Usage.None, Format.A8R8G8B8, Pool.Default);
device.UpdateTexture(sys, texture);
sys.Dispose();
}

View File

@ -147,16 +147,17 @@ namespace ClassicalSharp {
if (!game.ViewBobbing) { tiltM = Matrix4.Identity; return; }
LocalPlayer p = game.LocalPlayer;
Matrix4 tiltY, velX;
Matrix4.RotateZ(out tiltM, -p.tilt.tiltX * p.anim.bobStrength);
Matrix4.RotateX(out tiltY, Math.Abs(p.tilt.tiltY) * 3 * p.anim.bobStrength);
tiltM *= tiltY;
Matrix4.Mult(out tiltM, ref tiltM, ref tiltY);
bobbingHor = (p.anim.bobbingHor * 0.3f) * p.anim.bobStrength;
bobbingVer = (p.anim.bobbingVer * 0.6f) * p.anim.bobStrength;
float vel = Utils.Lerp(p.OldVelocity.Y + 0.08f, p.Velocity.Y + 0.08f, t);
Matrix4.RotateX(out velX, -vel * 0.05f * p.tilt.velTiltStrength / velTiltScale);
tiltM *= velX;
Matrix4.Mult(out tiltM, ref tiltM, ref velX);
}
}

View File

@ -103,23 +103,28 @@ void PerspectiveCamera_UpdateMouse(void) {
}
void PerspectiveCamera_CalcViewBobbing(Real32 t, Real32 velTiltScale) {
if (!Game_ViewBobbing) { Camera_TiltMatrix = Matrix_Identity; return; }
LocalPlayer p = game.LocalPlayer;
Matrix tiltY, velX;
Matrix_RotateZ(out tiltM, -p.tilt.tiltX * p.anim.bobStrength);
Matrix_RotateX(out tiltY, Math.Abs(p.tilt.tiltY) * 3 * p.anim.bobStrength);
tiltM *= tiltY;
if (!Game_ViewBobbing) { Camera_TiltM = Matrix_Identity; return; }
LocalPlayer* p = &LocalPlayer_Instance;
Entity* e = &p->Base.Base;
Matrix Camera_tiltY, Camera_velX;
bobbingHor = (p.anim.bobbingHor * 0.3f) * p.anim.bobStrength;
bobbingVer = (p.anim.bobbingVer * 0.6f) * p.anim.bobStrength;
Matrix_RotateZ(&Camera_TiltM, -p->Tilt.TiltX * e->Anim.BobStrength);
Matrix_RotateX(&Camera_tiltY, Math_AbsF(p->Tilt.TiltY) * 3.0f * e->Anim.BobStrength);
Matrix_MulBy(&Camera_TiltM, &Camera_tiltY);
float vel = Utils.Lerp(p.OldVelocity.Y + 0.08f, p.Velocity.Y + 0.08f, t);
Matrix4.RotateX(out velX, -vel * 0.05f * p.tilt.velTiltStrength / velTiltScale);
tiltM *= velX;
Camera_BobbingHor = (e->Anim.BobbingHor * 0.3f) * e->Anim.BobStrength;
Camera_BobbingVer = (e->Anim.BobbingVer * 0.6f) * e->Anim.BobStrength;
Real32 vel = Math_Lerp(e->OldVelocity.Y + 0.08f, e->Velocity.Y + 0.08f, t);
Matrix_RotateX(&Camera_velX, -vel * 0.05f * p->Tilt.VelTiltStrength / velTiltScale);
Matrix_MulBy(&Camera_TiltM, &Camera_velX);
}
void PerspectiveCamera_Init(Camera* cam) {
cam->GetProjection = PerspectiveCamera_GetProjection;
cam->UpdateMouse = PerspectiveCamera_UpdateMouse;
cam->RegrabMouse = PerspectiveCamera_RegrabMouse;
cam->GetPickedBlock = PerspectiveCamera_GetPickedBlock;
}
public class ThirdPersonCamera : PerspectiveCamera {

View File

@ -9,7 +9,7 @@
*/
/* Tilt matrix applied to the active camera. */
Matrix Camera_TiltMatrix;
Matrix Camera_TiltM;
/* Bobbing applied to the active camera. */
Real32 Camera_BobbingVer, Camera_BobbingHor;

View File

@ -129,15 +129,15 @@ void TiltComp_GetCurrent(TiltComp* anim, Real32 t) {
anim->TiltY = Math_Sin(pAnim->WalkTime) * pAnim->Swing * (0.15f * MATH_DEG2RAD);
}
void HacksComponent_SetAll(HacksComponent* hacks, bool allowed) {
void HacksComponent_SetAll(HacksComp* hacks, bool allowed) {
hacks->CanAnyHacks = allowed; hacks->CanFly = allowed;
hacks->CanNoclip = allowed; hacks->CanRespawn = allowed;
hacks->CanSpeed = allowed; hacks->CanPushbackBlocks = allowed;
hacks->CanUseThirdPersonCamera = allowed;
}
void HacksComponent_Init(HacksComponent* hacks) {
Platform_MemSet(hacks, 0, sizeof(HacksComponent));
void HacksComponent_Init(HacksComp* hacks) {
Platform_MemSet(hacks, 0, sizeof(HacksComp));
HacksComponent_SetAll(hacks, true);
hacks->SpeedMultiplier = 10.0f;
hacks->Enabled = true;
@ -148,15 +148,15 @@ void HacksComponent_Init(HacksComponent* hacks) {
hacks->HacksFlags = String_FromRawBuffer(&hacks->HacksFlagsBuffer[0], 128);
}
bool HacksComponent_CanJumpHigher(HacksComponent* hacks) {
bool HacksComponent_CanJumpHigher(HacksComp* hacks) {
return hacks->Enabled && hacks->CanAnyHacks && hacks->CanSpeed;
}
bool HacksComponent_Floating(HacksComponent* hacks) {
bool HacksComponent_Floating(HacksComp* hacks) {
return hacks->Noclip || hacks->Flying;
}
void HacksComponent_ParseHorizontalSpeed(HacksComponent* hacks) {
void HacksComponent_ParseHorizontalSpeed(HacksComp* hacks) {
String* joined = &hacks->HacksFlags;
String horSpeed = String_FromConstant("horspeed=");
Int32 start = String_IndexOfString(joined, &horSpeed);
@ -172,7 +172,7 @@ void HacksComponent_ParseHorizontalSpeed(HacksComponent* hacks) {
hacks->MaxSpeedMultiplier = speed;
}
void HacksComponent_ParseFlag(HacksComponent* hacks, const UInt8* incFlag, const UInt8* excFlag, bool* target) {
void HacksComponent_ParseFlag(HacksComp* hacks, const UInt8* incFlag, const UInt8* excFlag, bool* target) {
String include = String_FromReadonly(incFlag);
String exclude = String_FromReadonly(excFlag);
String* joined = &hacks->HacksFlags;
@ -184,7 +184,7 @@ void HacksComponent_ParseFlag(HacksComponent* hacks, const UInt8* incFlag, const
}
}
void HacksComponent_ParseAllFlag(HacksComponent* hacks, const UInt8* incFlag, const UInt8* excFlag) {
void HacksComponent_ParseAllFlag(HacksComp* hacks, const UInt8* incFlag, const UInt8* excFlag) {
String include = String_FromReadonly(incFlag);
String exclude = String_FromReadonly(excFlag);
String* joined = &hacks->HacksFlags;
@ -198,7 +198,7 @@ void HacksComponent_ParseAllFlag(HacksComponent* hacks, const UInt8* incFlag, co
/* Sets the user type of this user. This is used to control permissions for grass,
bedrock, water and lava blocks on servers that don't support CPE block permissions. */
void HacksComponent_SetUserType(HacksComponent* hacks, UInt8 value) {
void HacksComponent_SetUserType(HacksComp* hacks, UInt8 value) {
bool isOp = value >= 100 && value <= 127;
hacks->UserType = value;
Block_CanPlace[BlockID_Bedrock] = isOp;
@ -212,7 +212,7 @@ void HacksComponent_SetUserType(HacksComponent* hacks, UInt8 value) {
}
/* Disables any hacks if their respective CanHackX value is set to false. */
void HacksComponent_CheckConsistency(HacksComponent* hacks) {
void HacksComponent_CheckConsistency(HacksComp* hacks) {
if (!hacks->CanFly || !hacks->Enabled) {
hacks->Flying = false; hacks->FlyingDown = false; hacks->FlyingUp = false;
}
@ -235,7 +235,7 @@ void HacksComponent_CheckConsistency(HacksComponent* hacks) {
/* Updates ability to use hacks, and raises HackPermissionsChanged event.
Parses hack flags specified in the motd and/or name of the server.
Recognises +/-hax, +/-fly, +/-noclip, +/-speed, +/-respawn, +/-ophax, and horspeed=xyz */
void HacksComponent_UpdateState(HacksComponent* hacks) {
void HacksComponent_UpdateState(HacksComp* hacks) {
HacksComponent_SetAll(hacks, true);
if (hacks->HacksFlags.length == 0) return;

View File

@ -96,26 +96,26 @@ typedef struct HacksComponent_ {
UInt8 HacksFlagsBuffer[String_BufferSize(128)];
/* The actual hack flags usage string.*/
String HacksFlags;
} HacksComponent;
} HacksComp;
/* Initalises the state of this HacksComponent. */
void HacksComponent_Init(HacksComponent* hacks);
void HacksComponent_Init(HacksComp* hacks);
/* Returns whether hacks flags allow for jumping higher usage. */
bool HacksComponent_CanJumpHigher(HacksComponent* hacks);
bool HacksComponent_CanJumpHigher(HacksComp* hacks);
/* Returns whether noclip or flying modes are on.*/
bool HacksComponent_Floating(HacksComponent* hacks);
bool HacksComponent_Floating(HacksComp* hacks);
/* Sets the user type of this user. This is used to control permissions for grass,
bedrock, water and lava blocks on servers that don't support CPE block permissions. */
void HacksComponent_SetUserType(HacksComponent* hacks, UInt8 value);
void HacksComponent_SetUserType(HacksComp* hacks, UInt8 value);
/* Disables any hacks if their respective CanHackX value is set to false. */
void HacksComponent_CheckConsistency(HacksComponent* hacks);
void HacksComponent_CheckConsistency(HacksComp* hacks);
/* Updates ability to use hacks, and raises UserEvents_HackPermissionsChanged event.
Parses hack flags specified in the motd and/or name of the server.
Recognises +/-hax, +/-fly, +/-noclip, +/-speed, +/-respawn, +/-ophax, and horspeed=xyz */
void HacksComponent_UpdateState(HacksComponent* hacks);
void HacksComponent_UpdateState(HacksComp* hacks);
#endif

View File

@ -19,10 +19,12 @@ typedef struct LocalPlayer_ {
Vector3 Spawn;
/* Orientation set to when player respawns.*/
Real32 SpawnRotY, SpawnHeadX;
/* Hacks state of this player. */
HacksComponent Hacks;
/* Hacks state of the player. */
HacksComp Hacks;
/* Distance (in blocks) that players are allowed to reach to and interact/modify blocks in. */
Real32 ReachDistance;
/* Tilt animation state of the player. */
TiltComp Tilt;
} LocalPlayer;