Advertisement
bekovski

p11v1

Jan 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.95 KB | None | 0 0
  1. #include <Kore/pch.h>
  2.  
  3. #include <Kore/IO/FileReader.h>
  4. #include <Kore/Math/Core.h>
  5. #include <Kore/System.h>
  6. #include <Kore/Input/Keyboard.h>
  7. #include <Kore/Graphics4/Graphics.h>
  8. #include <Kore/Graphics4/PipelineState.h>
  9. #include <Kore/Math/Quaternion.h>
  10. #include <Kore/Threads/Thread.h>
  11. #include <Kore/Threads/Mutex.h>
  12. #include <Kore/Network/Socket.h>
  13. #include <Kore/Log.h>
  14. #include "ObjLoader.h"
  15. #include "Memory.h"
  16.  
  17. #include "MeshObject.h"
  18.  
  19. #ifdef MASTER
  20. #define SRC_PORT 9898
  21. #define DEST_PORT 9897
  22. #define CLIENT_NAME "Master"
  23. #else
  24. #define SRC_PORT 9897
  25. #define DEST_PORT 9898
  26. #define CLIENT_NAME "Slave"
  27. #endif
  28.  
  29. #define DEST_IP1 127
  30. #define DEST_IP2 0
  31. #define DEST_IP3 0
  32. #define DEST_IP4 1
  33.  
  34. using namespace Kore;
  35.  
  36. enum MessageType {
  37.     Hello,
  38.     NpcPos,
  39.     Player1Pos,
  40.     Player2Pos
  41. };
  42.  
  43. class Ball : public MeshObject {
  44. public:
  45.     Ball(float x, float y, float z, const Graphics4::VertexStructure& structure, float scale = 1.0f) : MeshObject("ball.obj", "unshaded.png", structure, scale), x(x), y(y), z(z), dir(0, 0, 0) {
  46.         rotation = Quaternion(vec3(0, 0, 1), 0);
  47.     }
  48.    
  49.     void update(float tdif) override {
  50.         vec3 dir = this->dir;
  51.         if (dir.getLength() != 0) dir.setLength(dir.getLength() * tdif * 60.0f);
  52.         x += dir.x();
  53.         if (x > 1) {
  54.             x = 1;
  55.         }
  56.         if (x < -1) {
  57.             x = -1;
  58.         }
  59.         y += dir.y();
  60.         if (y < -4) {
  61.             y = 4;
  62.         }
  63.         if (y > 4) {
  64.             y = -4;
  65.         }
  66.         z += dir.z();
  67.         if (dir.getLength() != 0) {
  68.             float Horizontal = dir.dot(vec3(1, 0, 0));
  69.             float Vertical = dir.dot(vec3(0, 1, 0));
  70.            
  71.             rotation = rotation.rotated(Quaternion(vec3(-1, 0, 0), Vertical * 3.0f));
  72.             rotation = rotation.rotated(Quaternion(vec3(0, 1, 0), Horizontal * 3.0f));
  73.         }
  74.         M = mat4::Translation(x, y, z) * rotation.matrix();
  75.     }
  76.    
  77.     vec3 dir;
  78.     Quaternion rotation;
  79.     float x, y, z;
  80. };
  81.  
  82. namespace {
  83.     void updateBall();
  84.    
  85.     const int width = 512;
  86.     const int height = 512;
  87.    
  88.     double startTime;
  89.     Graphics4::Shader* vertexShader;
  90.     Graphics4::Shader* fragmentShader;
  91.     Graphics4::PipelineState* pipeline;
  92.    
  93.     // null terminated array of MeshObject pointers
  94.     MeshObject* objects[] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  95.     Ball* balls[] = { nullptr, nullptr, nullptr };
  96.    
  97.     // uniform locations - add more as you see fit
  98.     Graphics4::TextureUnit tex;
  99.     Graphics4::ConstantLocation pvLocation;
  100.     Graphics4::ConstantLocation mLocation;
  101.    
  102.     mat4 PV;
  103.    
  104.     float lastTime = 0.0;
  105.    
  106.     Socket socket;
  107.     vec3 position(0, 0, -2.25); // of map?
  108.    
  109.     const int port = SRC_PORT;
  110.     const int destPort = DEST_PORT;
  111.     const char* destination = "localhost";
  112.    
  113.     // Send a packet to the other client
  114.     // length is the length of the packet in bytes
  115.     void sendPacket(const unsigned char data[], int length) {
  116.         socket.send(destination, destPort, data, length);
  117.     }
  118.    
  119.     // Movement data for clients
  120.     bool left = false, right = false, up = false, down = false;
  121.     bool left2 = false, right2 = false, up2 = false, down2 = false;
  122.    
  123.     void update() {
  124.         // receive packets
  125.         while (true) {
  126.             // Read buffer
  127.             u8 buffer[256];
  128.             unsigned fromAddress;
  129.             unsigned fromPort;
  130.             int read = socket.receive(buffer, sizeof(buffer), fromAddress, fromPort);   // sets fromAddress and fromPort
  131.            
  132.             // break if there was no new packet
  133.             if (read <= 0) {
  134.                 break;
  135.             }
  136.  
  137.             /************************************************************************/
  138.             /* Practical Task: Read the packets with the movement data you sent  and
  139.             /* apply them by setting the boolean values for movement control.
  140.             /************************************************************************/
  141. #ifdef MASTER
  142.             // Set the values for left2, right2, up2, down2 here
  143.             if (buffer[0] == Player2Pos) {
  144.                 bool* bools = (bool*)&buffer[1];
  145.                 left2   = bools[0];
  146.                 right2  = bools[1];
  147.                 up2     = bools[2];
  148.                 down2   = bools[3];
  149.             }
  150. #else
  151.             // Set the values for left, right, up, down here
  152.             if (buffer[0] == Player1Pos) {
  153.                 bool* bools = (bool*)&buffer[1];
  154.                 left    = bools[0];
  155.                 right   = bools[1];
  156.                 up      = bools[2];
  157.                 down    = bools[3];
  158.             }
  159.            
  160.             // receive position updates of the npc ball
  161.             if (buffer[0] == NpcPos) {
  162.                 float* floats = (float*)&buffer[1];
  163.                 balls[2]->x = floats[0];
  164.                 balls[2]->y = floats[1];
  165.                 balls[2]->z = floats[2];
  166.             }
  167. #endif // MASTER
  168.            
  169.             updateBall();
  170.         }
  171.        
  172.         float t = (float)(System::time() - startTime);
  173.         float tdif = t - lastTime;
  174.         lastTime = t;
  175.        
  176.         updateBall();
  177.        
  178.         Graphics4::begin();
  179.         Graphics4::clear(Graphics4::ClearColorFlag | Graphics4::ClearDepthFlag, 0xff9999FF, 1.0f);
  180.        
  181.         Graphics4::setPipeline(pipeline);
  182.        
  183.         PV = mat4::Perspective(90, (float)width / (float)height, 0.1f, 100) * mat4::lookAt(position, vec3(0.0, 0.0, 0.0), vec3(0, 1, 0));
  184.         Graphics4::setMatrix(pvLocation, PV);
  185.        
  186.         MeshObject** current = &objects[0];
  187.         while (*current != nullptr) {
  188.             (*current)->update(tdif);
  189.             Graphics4::setMatrix(mLocation, (*current)->M);
  190.             (*current)->render(tex);
  191.             ++current;
  192.         }
  193.        
  194. #ifdef MASTER
  195.         // send position of the npc ball
  196.         u8 data[1 + sizeof(float) * 3];     // first entry: type of data, the rest (3 entries) are the position coordinates
  197.         float* fdata = (float*)&data[1];    // fdata is a pointer -> changing it affects 'data' as well
  198.        
  199.         data[0] = (u8)NpcPos;               // indicates that the following data is about NPC's position (hence +1 in array allocation)
  200.         fdata[0] = balls[2]->x;
  201.         fdata[1] = balls[2]->y;
  202.         fdata[2] = balls[2]->z;
  203.         sendPacket(data, sizeof(data));     // data = [NpcPos, balls[2].x, balls[2].y, balls[2].z]
  204. #endif
  205.        
  206.         Graphics4::end();
  207.         Graphics4::swapBuffers();
  208.     }
  209.    
  210.     void updateBall() {
  211.         // user controlled balls
  212.         float speed = 0.05f;
  213.         if (left) {
  214.             balls[0]->dir.x() = -speed;
  215.         }
  216.         else if (right) {
  217.             balls[0]->dir.x() = speed;
  218.         }
  219.         else {
  220.             balls[0]->dir.x() = 0;
  221.         }
  222.         if (up) {
  223.             balls[0]->dir.y() = speed;
  224.         }
  225.         else if (down) {
  226.             balls[0]->dir.y() = -speed;
  227.         }
  228.         else {
  229.             balls[0]->dir.y() = 0;
  230.         }
  231.         if (left2) {
  232.             balls[1]->dir.x() = -speed;
  233.         }
  234.         else if (right2) {
  235.             balls[1]->dir.x() = speed;
  236.         }
  237.         else {
  238.             balls[1]->dir.x() = 0;
  239.         }
  240.         if (up2) {
  241.             balls[1]->dir.y() = speed;
  242.         }
  243.         else if (down2) {
  244.             balls[1]->dir.y() = -speed;
  245.         }
  246.         else {
  247.             balls[1]->dir.y() = 0;
  248.         }
  249.         // NPC ball
  250.         balls[2]->dir.y() = -0.04f;
  251. #ifdef MASTER
  252.         if (balls[2]->y == 4) {
  253.             balls[2]->x = ((float)rand() / RAND_MAX)*2-1;
  254.         }
  255. #endif
  256.     }
  257.    
  258.     /************************************************************************/
  259.     /* Practical Task: Send packets with information about the input controls
  260.     /* of the local player - keyDown
  261.     /************************************************************************/
  262.     void keyDown(KeyCode code) {
  263. #ifdef MASTER
  264.         if (code == KeyLeft) {
  265.             left = true;
  266.         }
  267.         else if (code == KeyRight) {
  268.             right = true;
  269.         }
  270.         else if (code == KeyUp) {
  271.             up = true;
  272.         }
  273.         else if (code == KeyDown) {
  274.             down = true;
  275.         }
  276.  
  277.         u8 data[1 + sizeof(bool) * 4];
  278.         bool* bdata = (bool*)&data[1];
  279.  
  280.         data[0] = (u8)Player1Pos;
  281.         bdata[0] = left;
  282.         bdata[1] = right;
  283.         bdata[2] = up;
  284.         bdata[3] = down;
  285.         sendPacket(data, sizeof(data));
  286. #else
  287.         if (code == KeyA) {
  288.             left2 = true;
  289.         }
  290.         else if (code == KeyD) {
  291.             right2 = true;
  292.         }
  293.         if (code == KeyW) {
  294.             up2 = true;
  295.         }
  296.         else if (code == KeyS) {
  297.             down2 = true;
  298.         }
  299.  
  300.         u8 data[1 + sizeof(bool) * 4];
  301.         bool* bdata = (bool*)&data[1];
  302.  
  303.         data[0] = (u8)Player2Pos;
  304.         bdata[0] = left2;
  305.         bdata[1] = right2;
  306.         bdata[2] = up2;
  307.         bdata[3] = down2;
  308.         sendPacket(data, sizeof(data));
  309.  
  310.         // sendPacket()
  311. #endif // MASTER
  312.     }
  313.    
  314.     /************************************************************************/
  315.     /* Practical Task: Send packets with information about the input controls
  316.     /* of the local player - keyUp
  317.     /************************************************************************/
  318.     void keyUp(KeyCode code) {
  319. #ifdef MASTER
  320.         if (code == KeyLeft) {
  321.             left = false;
  322.         }
  323.         else if (code == KeyRight) {
  324.             right = false;
  325.         }
  326.         else if (code == KeyUp) {
  327.             up = false;
  328.         }
  329.         else if (code == KeyDown) {
  330.             down = false;
  331.         }
  332.  
  333.         u8 data[1 + sizeof(bool) * 4];
  334.         bool* bdata = (bool*)&data[1];
  335.  
  336.         data[0] = (u8)Player1Pos;
  337.         bdata[0] = left;
  338.         bdata[1] = right;
  339.         bdata[2] = up;
  340.         bdata[3] = down;
  341.         sendPacket(data, sizeof(data));
  342. #else
  343.         if (code == KeyA) {
  344.             left2 = false;
  345.         }
  346.         else if (code == KeyD) {
  347.             right2 = false;
  348.         }
  349.         else if (code == KeyW) {
  350.             up2 = false;
  351.         }
  352.         else if (code == KeyS) {
  353.             down2 = false;
  354.         }
  355.  
  356.         u8 data[1 + sizeof(bool) * 4];
  357.         bool* bdata = (bool*)&data[1];
  358.  
  359.         data[0] = (u8)Player2Pos;
  360.         bdata[0] = left2;
  361.         bdata[1] = right2;
  362.         bdata[2] = up2;
  363.         bdata[3] = down2;
  364.         sendPacket(data, sizeof(data));
  365. #endif // MASTER
  366.     }
  367.    
  368.     void init() {
  369.         srand(42);
  370.         socket.init();
  371.         socket.open(port);
  372.        
  373.         // send "hello" when joining to tell other player you are there
  374.         u8 hello = Hello;
  375.         sendPacket(&hello, 1);
  376.        
  377. #ifdef MASTER
  378.         log(Info, "Waiting for another player (the SLAVE) to join my game...");
  379. #else
  380.         log(Info, "Waiting for another player (the MASTER) in control of the game...");
  381. #endif // MASTER
  382.        
  383.         // wait for other player
  384.         while (true) {
  385.             // read buffer
  386.             unsigned char buffer[256];
  387.             unsigned fromAddress;
  388.             unsigned fromPort;
  389.             int read = socket.receive(buffer, sizeof(buffer), fromAddress, fromPort);
  390.             if (read <= 0) {
  391.                 continue;
  392.             }
  393.  
  394.             // break if player is there
  395.             if (buffer[0] == Hello) {
  396.                 break;
  397.             }
  398.         }
  399.        
  400. #ifdef MASTER
  401.         log(Info, "Another player (the SLAVE) has joined my game!");
  402. #else
  403.         log(Info, "I have joined another players (the MASTER) game!");
  404. #endif // MASTER
  405.        
  406.         // resend hello for newly connected player
  407.         sendPacket(&hello, 1);
  408.        
  409.         FileReader vs("shader.vert");
  410.         FileReader fs("shader.frag");
  411.         vertexShader = new Graphics4::Shader(vs.readAll(), vs.size(), Graphics4::VertexShader);
  412.         fragmentShader = new Graphics4::Shader(fs.readAll(), fs.size(), Graphics4::FragmentShader);
  413.        
  414.         // This defines the structure of your Vertex Buffer
  415.         Graphics4::VertexStructure structure;
  416.         structure.add("pos", Graphics4::Float3VertexData);
  417.         structure.add("tex", Graphics4::Float2VertexData);
  418.         structure.add("nor", Graphics4::Float3VertexData);
  419.        
  420.         pipeline = new Graphics4::PipelineState;
  421.         pipeline->inputLayout[0] = &structure;
  422.         pipeline->inputLayout[1] = nullptr;
  423.         pipeline->vertexShader = vertexShader;
  424.         pipeline->fragmentShader = fragmentShader;
  425.         pipeline->depthMode = Graphics4::ZCompareLess;
  426.         pipeline->depthWrite = true;
  427.         pipeline->compile();
  428.        
  429.         tex = pipeline->getTextureUnit("tex");
  430.         pvLocation = pipeline->getConstantLocation("PV");
  431.         mLocation = pipeline->getConstantLocation("M");
  432.        
  433.         objects[0] = balls[0] = new Ball(0.5f, -2.0f, 0.0f, structure, 0.25f);
  434.         objects[1] = balls[1] = new Ball(-0.5f, -2.0f, 0.0f, structure, 0.25f);
  435.        
  436.         objects[2] = balls[2] = new Ball(((float)rand() / RAND_MAX)*2-1, 4.0f, 0.0f, structure, 0.25f);
  437.         objects[3] = new MeshObject("base.obj", "floor.png", structure);
  438.         objects[3]->M = mat4::RotationX(Kore::pi / 2.0f)*mat4::Scale(0.15f, 1, 1);
  439.         objects[4] = new MeshObject("base.obj", "StarMap.png", structure);
  440.         objects[4]->M = mat4::RotationX(Kore::pi / 2.0f)*mat4::Scale(1, 1, 1)*mat4::Translation(0, 0, 0.5f);
  441.        
  442.         Graphics4::setTextureAddressing(tex, Graphics4::U, Graphics4::Repeat);
  443.         Graphics4::setTextureAddressing(tex, Graphics4::V, Graphics4::Repeat);
  444.     }
  445. }
  446.  
  447. int kore(int argc, char** argv) {
  448. #ifdef MASTER
  449.     log(Info, "I am the MASTER, I am in control of the game.");
  450. #else
  451.     log(Info, "I am the SLAVE, I want to join another game.");
  452. #endif // MASTER
  453.    
  454.     log(Info, "I am listening on port %i", port);
  455.     log(Info, "and want to connect to %s:%i\n", destination, destPort);
  456.    
  457.     Kore::System::init("Exercise 11 - "  CLIENT_NAME, width, height);
  458.    
  459.     Memory::init();
  460.     init();
  461.    
  462.     Kore::System::setCallback(update);
  463.    
  464.     startTime = System::time();
  465.    
  466.     Keyboard::the()->KeyDown = keyDown;
  467.     Keyboard::the()->KeyUp = keyUp;
  468.    
  469.     Kore::System::start();
  470.    
  471.     return 0;
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement