]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
more transform caching
[blank.git] / src / client / client.cpp
index 8b532633ab0cd85dc2926f2dbda72eb3ed7762e3..8bd0ae3db1a6c76d7546cd568d823e2ef616f709 100644 (file)
@@ -59,9 +59,12 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , chunk_receiver(world.Chunks(), save)
 , chunk_renderer(player.GetChunks())
 , loop_timer(16)
+, stat_timer(1000)
 , sky(master.GetEnv().loader.LoadCubeMap("skybox"))
 , update_status()
-, chat(master.GetEnv(), *this, *this) {
+, chat(master.GetEnv(), *this, *this)
+, time_skipped(0)
+, packets_skipped(0) {
        if (!save.Exists()) {
                save.Write(master.GetWorldConf());
        }
@@ -75,13 +78,30 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index);
        chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
        loop_timer.Start();
+       stat_timer.Start();
        if (save.Exists(player)) {
                save.Read(player);
        }
 }
 
-void InteractiveState::OnEnter() {
-       master.GetEnv().window.GrabMouse();
+void InteractiveState::OnResume() {
+       OnFocus();
+}
+
+void InteractiveState::OnPause() {
+       OnBlur();
+}
+
+void InteractiveState::OnFocus() {
+       if (master.GetConfig().input.mouse) {
+               master.GetEnv().window.GrabMouse();
+       }
+       interface.Unlock();
+}
+
+void InteractiveState::OnBlur() {
+       master.GetEnv().window.ReleaseMouse();
+       interface.Lock();
 }
 
 void InteractiveState::Handle(const SDL_Event &event) {
@@ -89,6 +109,11 @@ void InteractiveState::Handle(const SDL_Event &event) {
                case SDL_KEYDOWN:
                        // TODO: move to interface
                        if (event.key.keysym.sym == SDLK_RETURN) {
+                               chat.Clear();
+                               master.GetEnv().state.Push(&chat);
+                               hud.KeepMessages(true);
+                       } else if (event.key.keysym.sym == SDLK_SLASH) {
+                               chat.Preset("/");
                                master.GetEnv().state.Push(&chat);
                                hud.KeepMessages(true);
                        } else {
@@ -119,20 +144,11 @@ void InteractiveState::Handle(const SDL_Event &event) {
 }
 
 void InteractiveState::Update(int dt) {
-       input.Update(dt);
-       if (input.BlockFocus()) {
-               hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
-       } else if (input.EntityFocus()) {
-               hud.FocusEntity(*input.EntityFocus().entity);
-       } else {
-               hud.FocusNone();
-       }
-       hud.Display(res.block_types[player.GetInventorySlot() + 1]);
        loop_timer.Update(dt);
+       stat_timer.Update(dt);
        master.Update(dt);
        chunk_receiver.Update(dt);
 
-       hud.Update(dt);
        int world_dt = 0;
        while (loop_timer.HitOnce()) {
                world.Update(loop_timer.Interval());
@@ -141,9 +157,28 @@ void InteractiveState::Update(int dt) {
        }
        chunk_renderer.Update(dt);
 
+       if (input.BlockFocus()) {
+               hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
+       } else if (input.EntityFocus()) {
+               hud.FocusEntity(*input.EntityFocus().entity);
+       } else {
+               hud.FocusNone();
+       }
        if (world_dt > 0) {
-               input.PushPlayerUpdate(world_dt);
+               if (input.UpdateImportant() || packets_skipped >= master.NetStat().SuggestedPacketSkip()) {
+                       input.PushPlayerUpdate(time_skipped + world_dt);
+                       time_skipped = 0;
+                       packets_skipped = 0;
+               } else {
+                       time_skipped += world_dt;
+                       ++packets_skipped;
+               }
        }
+       hud.Display(res.block_types[player.GetInventorySlot() + 1]);
+       if (stat_timer.Hit()) {
+               hud.UpdateNetStats(master.NetStat());
+       }
+       hud.Update(dt);
 
        glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
        glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
@@ -154,9 +189,7 @@ void InteractiveState::Update(int dt) {
 }
 
 void InteractiveState::Render(Viewport &viewport) {
-       viewport.WorldPosition(
-               player.GetEntity().Transform(player.GetEntity().ChunkCoords())
-               * player.GetEntity().GetModel().EyesTransform());
+       viewport.WorldPosition(player.GetEntity().ViewTransform(player.GetEntity().ChunkCoords()));
        if (master.GetConfig().video.world) {
                chunk_renderer.Render(viewport);
                world.Render(viewport);
@@ -176,8 +209,6 @@ void InteractiveState::Handle(const Packet::SpawnEntity &pack) {
        if (model_id > 0 && model_id <= res.models.size()) {
                res.models.Get(model_id).Instantiate(entity.GetModel());
        }
-       cout << "spawned entity #" << entity_id << "  (" << entity.Name()
-               << ") at " << entity.AbsolutePosition() << endl;
 }
 
 void InteractiveState::Handle(const Packet::DespawnEntity &pack) {
@@ -187,7 +218,6 @@ void InteractiveState::Handle(const Packet::DespawnEntity &pack) {
        for (Entity &entity : world.Entities()) {
                if (entity.ID() == entity_id) {
                        entity.Kill();
-                       cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
                        return;
                }
        }