123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #include "client/Main.h"
- #include "client/Chat.h"
- #include "client/Controls.h"
- #include "client/FontRenderer.h"
- #include "client/Network.h"
- #include "client/Player.h"
- #include "client/World.h"
- #include "common/NetworkPackets.h"
- #include "math/View.h"
- #include "network/Client.h"
- #include "rendering/VertexBuffer.h"
- #include "rendering/Window.h"
- #include "utils/Buffer.h"
- static VertexBuffer markVertexBuffer;
- HashMap<int, OtherPlayer> players;
- static bool isRunning() {
- return !Window::shouldClose();
- }
- static void addTriangle(Buffer& buffer, const Vector3& a, const Vector3& b,
- const Vector3& c) {
- Vector3 normal = (b - a).cross(c - a);
- buffer.add(a).add(normal).add(b).add(normal).add(c).add(normal);
- }
- static bool init() {
- if(World::init() || FontRenderer::init()) {
- return true;
- }
- markVertexBuffer.init(VertexBuffer::Attributes().addFloat(3).addFloat(3));
- Vector3 v000(0, 0, 0);
- Vector3 v001(0, 0, 1);
- Vector3 v010(0, 1, 0);
- Vector3 v011(0, 1, 1);
- Vector3 v100(1, 0, 0);
- Vector3 v101(1, 0, 1);
- Vector3 v110(1, 1, 0);
- Vector3 v111(1, 1, 1);
- Buffer buffer(100);
- addTriangle(buffer, v000, v100, v001);
- addTriangle(buffer, v100, v101, v001);
- addTriangle(buffer, v010, v011, v110);
- addTriangle(buffer, v110, v011, v111);
- addTriangle(buffer, v000, v001, v010);
- addTriangle(buffer, v001, v011, v010);
- addTriangle(buffer, v100, v110, v101);
- addTriangle(buffer, v101, v110, v111);
- addTriangle(buffer, v001, v101, v011);
- addTriangle(buffer, v111, v011, v101);
- addTriangle(buffer, v000, v010, v100);
- addTriangle(buffer, v110, v100, v010);
- markVertexBuffer.setData(buffer, GL::STATIC_DRAW);
- Controls::init();
- return false;
- }
- static void tick() {
- if(!Chat::isActive()) {
- if(Controls::wasReleased(Controls::primaryClick)) {
- Window::trapCursor();
- }
- if(Controls::wasReleased(Controls::escape)) {
- Window::freeCursor();
- }
- }
- Chat::tick();
- Player::tick();
- for(OtherPlayer& p : players.values()) {
- p.position.update();
- }
- Network::tick();
- if(Controls::wasReleased(Controls::primaryClick) && !Chat::isActive()) {
- RayTrace rt =
- World::rayTrace(Player::getPosition() + Vector3(0.0f, 0.8f, 0.0f),
- Player::getLook(), 10.0f);
- if(rt.type == RayTrace::Type::BLOCK) {
- World::set(rt.block[0], rt.block[1], rt.block[2], false);
- OutPacket out(100);
- out.writeU8(static_cast<uint8>(ToServerPacket::SET_BLOCK));
- out.writeVector(rt.block);
- out.writeU8(0);
- Client::send(out, PacketSendMode::RELIABLE);
- }
- }
- OutPacket out(100);
- out.writeU8(static_cast<uint8>(ToServerPacket::PLAYER));
- out.writeVector(Player::getPosition());
- out.writeS32(-1);
- Client::send(out, PacketSendMode::RELIABLE);
- }
- static void render(float lag) {
- GL::clear();
- GL::enableDepthTesting();
- World::render(lag);
- RayTrace rt =
- World::rayTrace(Player::getPosition(lag) + Vector3(0.0f, 0.8f, 0.0f),
- Player::getLook(lag), 10.0f);
- if(rt.type == RayTrace::Type::BLOCK) {
- World::shader.setVector("color", Vector3(0.8f, 0.6f, 0.6f));
- Matrix model;
- model.translate(Vector3(-0.5f, -0.5f, -0.5f));
- model.scale(1.01f);
- model.translate(Vector3(0.5f, 0.5f, 0.5f));
- model.translate(rt.block.toFloat());
- World::shader.setMatrix("model", model);
- markVertexBuffer.draw(36);
- }
- World::shader.setVector("color", Vector3(1.0f, 0.0f, 0.0f));
- for(const OtherPlayer& p : players.values()) {
- Matrix model;
- model.translateTo(Vector3(-0.5f, -0.5f, -0.5f));
- model.scale(0.8f);
- model.translate(p.position.get(lag) + Vector3(0.0f, 0.4f, 0.0f));
- World::shader.setMatrix("model", model);
- markVertexBuffer.draw(36);
- }
- World::shader.setMatrix("proj", Matrix());
- World::shader.setMatrix("view", Matrix());
- World::shader.setVector("color", Vector3(1.0f, 0.0f, 1.0f));
- Matrix model;
- model.translateTo(Vector3(-0.5f, -0.5f, -0.5f));
- model.scale(0.05f);
- World::shader.setMatrix("model", model);
- markVertexBuffer.draw(36);
- Chat::render();
- }
- static void startWindow() {
- Window::Options options(4, 3, IntVector2(1024, 600), false, "test");
- Error e = Window::open(options);
- if(e.has()) {
- e.message.printLine();
- return;
- } else if(!init()) {
- Window::show();
- Window::run<isRunning, tick, render>(50'000'000);
- }
- Window::close();
- }
- int main(int argAmount, const char* const* args) {
- const char* server = "127.0.0.1";
- if(argAmount >= 2) {
- server = args[1];
- }
- if(Network::start(server)) {
- return 0;
- }
- startWindow();
- Network::stop();
- return 0;
- }
|