Main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "client/Main.h"
  2. #include "client/Chat.h"
  3. #include "client/Controls.h"
  4. #include "client/FontRenderer.h"
  5. #include "client/Network.h"
  6. #include "client/Player.h"
  7. #include "client/World.h"
  8. #include "common/NetworkPackets.h"
  9. #include "math/View.h"
  10. #include "network/Client.h"
  11. #include "rendering/VertexBuffer.h"
  12. #include "rendering/Window.h"
  13. #include "utils/Buffer.h"
  14. static VertexBuffer markVertexBuffer;
  15. HashMap<int, OtherPlayer> players;
  16. static bool isRunning() {
  17. return !Window::shouldClose();
  18. }
  19. static void addTriangle(Buffer& buffer, const Vector3& a, const Vector3& b,
  20. const Vector3& c) {
  21. Vector3 normal = (b - a).cross(c - a);
  22. buffer.add(a).add(normal).add(b).add(normal).add(c).add(normal);
  23. }
  24. static bool init() {
  25. if(World::init() || FontRenderer::init()) {
  26. return true;
  27. }
  28. markVertexBuffer.init(VertexBuffer::Attributes().addFloat(3).addFloat(3));
  29. Vector3 v000(0, 0, 0);
  30. Vector3 v001(0, 0, 1);
  31. Vector3 v010(0, 1, 0);
  32. Vector3 v011(0, 1, 1);
  33. Vector3 v100(1, 0, 0);
  34. Vector3 v101(1, 0, 1);
  35. Vector3 v110(1, 1, 0);
  36. Vector3 v111(1, 1, 1);
  37. Buffer buffer(100);
  38. addTriangle(buffer, v000, v100, v001);
  39. addTriangle(buffer, v100, v101, v001);
  40. addTriangle(buffer, v010, v011, v110);
  41. addTriangle(buffer, v110, v011, v111);
  42. addTriangle(buffer, v000, v001, v010);
  43. addTriangle(buffer, v001, v011, v010);
  44. addTriangle(buffer, v100, v110, v101);
  45. addTriangle(buffer, v101, v110, v111);
  46. addTriangle(buffer, v001, v101, v011);
  47. addTriangle(buffer, v111, v011, v101);
  48. addTriangle(buffer, v000, v010, v100);
  49. addTriangle(buffer, v110, v100, v010);
  50. markVertexBuffer.setData(buffer, GL::STATIC_DRAW);
  51. Controls::init();
  52. return false;
  53. }
  54. static void tick() {
  55. if(!Chat::isActive()) {
  56. if(Controls::wasReleased(Controls::primaryClick)) {
  57. Window::trapCursor();
  58. }
  59. if(Controls::wasReleased(Controls::escape)) {
  60. Window::freeCursor();
  61. }
  62. }
  63. Chat::tick();
  64. Player::tick();
  65. for(OtherPlayer& p : players.values()) {
  66. p.position.update();
  67. }
  68. Network::tick();
  69. if(Controls::wasReleased(Controls::primaryClick) && !Chat::isActive()) {
  70. RayTrace rt =
  71. World::rayTrace(Player::getPosition() + Vector3(0.0f, 0.8f, 0.0f),
  72. Player::getLook(), 10.0f);
  73. if(rt.type == RayTrace::Type::BLOCK) {
  74. World::set(rt.block[0], rt.block[1], rt.block[2], false);
  75. OutPacket out(100);
  76. out.writeU8(static_cast<uint8>(ToServerPacket::SET_BLOCK));
  77. out.writeVector(rt.block);
  78. out.writeU8(0);
  79. Client::send(out, PacketSendMode::RELIABLE);
  80. }
  81. }
  82. OutPacket out(100);
  83. out.writeU8(static_cast<uint8>(ToServerPacket::PLAYER));
  84. out.writeVector(Player::getPosition());
  85. out.writeS32(-1);
  86. Client::send(out, PacketSendMode::RELIABLE);
  87. }
  88. static void render(float lag) {
  89. GL::clear();
  90. GL::enableDepthTesting();
  91. World::render(lag);
  92. RayTrace rt =
  93. World::rayTrace(Player::getPosition(lag) + Vector3(0.0f, 0.8f, 0.0f),
  94. Player::getLook(lag), 10.0f);
  95. if(rt.type == RayTrace::Type::BLOCK) {
  96. World::shader.setVector("color", Vector3(0.8f, 0.6f, 0.6f));
  97. Matrix model;
  98. model.translate(Vector3(-0.5f, -0.5f, -0.5f));
  99. model.scale(1.01f);
  100. model.translate(Vector3(0.5f, 0.5f, 0.5f));
  101. model.translate(rt.block.toFloat());
  102. World::shader.setMatrix("model", model);
  103. markVertexBuffer.draw(36);
  104. }
  105. World::shader.setVector("color", Vector3(1.0f, 0.0f, 0.0f));
  106. for(const OtherPlayer& p : players.values()) {
  107. Matrix model;
  108. model.translateTo(Vector3(-0.5f, -0.5f, -0.5f));
  109. model.scale(0.8f);
  110. model.translate(p.position.get(lag) + Vector3(0.0f, 0.4f, 0.0f));
  111. World::shader.setMatrix("model", model);
  112. markVertexBuffer.draw(36);
  113. }
  114. World::shader.setMatrix("proj", Matrix());
  115. World::shader.setMatrix("view", Matrix());
  116. World::shader.setVector("color", Vector3(1.0f, 0.0f, 1.0f));
  117. Matrix model;
  118. model.translateTo(Vector3(-0.5f, -0.5f, -0.5f));
  119. model.scale(0.05f);
  120. World::shader.setMatrix("model", model);
  121. markVertexBuffer.draw(36);
  122. Chat::render();
  123. }
  124. static void startWindow() {
  125. Window::Options options(4, 3, IntVector2(1024, 600), false, "test");
  126. Error e = Window::open(options);
  127. if(e.has()) {
  128. e.message.printLine();
  129. return;
  130. } else if(!init()) {
  131. Window::show();
  132. Window::run<isRunning, tick, render>(50'000'000);
  133. }
  134. Window::close();
  135. }
  136. int main(int argAmount, const char* const* args) {
  137. const char* server = "127.0.0.1";
  138. if(argAmount >= 2) {
  139. server = args[1];
  140. }
  141. if(Network::start(server)) {
  142. return 0;
  143. }
  144. startWindow();
  145. Network::stop();
  146. return 0;
  147. }