Advertisement
Combreal

SDL2Client.cpp

Nov 21st, 2020
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <winsock2.h>
  3. #include <ws2tcpip.h>
  4. #pragma comment(lib, "ws2_32.lib")
  5. #include <windows.h>
  6. #include <string>
  7. #include <tchar.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <thread>
  14. #pragma warning( disable : 4996)
  15. using namespace std;
  16.  
  17. struct t_PLAYERDATA
  18. {
  19.     int playerID = 0;
  20.     double playerX = 0;
  21.     double playerY = 0;
  22. } PLAYERDATA;
  23.  
  24. void *receiveMessage(void *sock_desc);
  25. void *sendMessage(void *sock_desc);
  26. bool abortGame = false;
  27. thread sendThread;
  28. thread receiveThread;
  29.  
  30. int main(int arc, char ** argv)
  31. {
  32.     void* data = &PLAYERDATA;
  33.     //PLAYERDATA *myData = (struct t_PLAYERDATA *)data;
  34.     SDL_Window* window = NULL;
  35.     SDL_Event event;
  36.     SDL_Renderer *renderer = NULL;
  37.     SDL_Texture* backgroundTexture = NULL;
  38.     SDL_Texture* playerTexture = NULL;
  39.     SDL_Surface* backgroundSurface = NULL;
  40.     SDL_Surface* playerSurface = NULL;
  41.     SDL_Rect backgroundDest;
  42.     SDL_Rect playerDest;
  43.     PLAYERDATA.playerID = 1;
  44.     char responce[1024];
  45.    
  46.     WSADATA WSAData;
  47.     SOCKADDR_IN addr;
  48.     WSAStartup(MAKEWORD(2, 0), &WSAData);
  49.     SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
  50.     InetPton(AF_INET, "192.168.1.133", &addr.sin_addr.s_addr);
  51.     addr.sin_family = AF_INET;
  52.     addr.sin_port = htons(5555);
  53.     int sock = server;
  54.     int * newSocket = static_cast<int*>(malloc(1));
  55.     *newSocket = sock;
  56.     if (connect(server, (SOCKADDR *)&addr, sizeof(addr)) == 0)
  57.     {
  58.         cout << "\nConnected to server!\n" << endl;
  59.         sendThread = thread(sendMessage, (void *)newSocket);
  60.         receiveThread = thread(receiveMessage, (void *)newSocket);
  61.     }
  62.     else
  63.     {
  64.         cout << "\nCouldn't connect to server.\n" << endl;
  65.     }
  66.  
  67.     bool cont = true;
  68.     int CurrentTime = 0, seconds = 0, PassedTime = 0;
  69.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  70.     {
  71.         cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
  72.     }
  73.     else
  74.     {
  75.         window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
  76.     }
  77.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  78.     if (renderer)
  79.     {
  80.         backgroundDest = { 0, 0, 800, 600 };
  81.         backgroundSurface = SDL_LoadBMP("data/deck/background.bmp");
  82.         backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
  83.         playerDest = { 400, 300, 100, 100 };
  84.         playerSurface = SDL_LoadBMP("data/deck/player.bmp");
  85.         playerTexture = SDL_CreateTextureFromSurface(renderer, playerSurface);
  86.         if (!playerSurface || !backgroundSurface)
  87.         {
  88.             cout << "Sprite loading error : " << SDL_GetError() << endl;
  89.         }
  90.         if (!backgroundTexture || !playerTexture)
  91.         {
  92.             cout << "Couldn't display image : " << SDL_GetError() << endl;
  93.         }
  94.     }
  95.     else
  96.     {
  97.         cout << "Couldn't create renderer : " << SDL_GetError() << endl;
  98.     }
  99.     while (!abortGame)
  100.     {
  101.         while (SDL_PollEvent(&event))
  102.         {
  103.             CurrentTime = SDL_GetTicks();
  104.             SDL_RenderClear(renderer);
  105.             SDL_RenderCopy(renderer, backgroundTexture, NULL, &backgroundDest);
  106.             SDL_RenderCopy(renderer, playerTexture, NULL, &playerDest);
  107.             SDL_RenderPresent(renderer);
  108.             //if (CurrentTime - PassedTime > 200)
  109.             //{
  110.             switch (event.type)
  111.             {
  112.             case SDL_KEYDOWN:
  113.                 if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
  114.                 {
  115.                     cout << "Quit" << endl;
  116.                     cont = 0;
  117.                 }
  118.                 else if (event.key.keysym.scancode == SDL_SCANCODE_UP)
  119.                 {
  120.                     cout << "z" << endl;
  121.                     playerDest.y = playerDest.y - 5;
  122.                 }
  123.                 else if (event.key.keysym.scancode == SDL_SCANCODE_DOWN)
  124.                 {
  125.                     cout << "s" << endl;
  126.                     playerDest.y = playerDest.y + 5;
  127.                 }
  128.                 else if (event.key.keysym.scancode == SDL_SCANCODE_LEFT)
  129.                 {
  130.                     cout << "q" << endl;
  131.                     playerDest.x= playerDest.x - 5;
  132.                 }
  133.                 else if (event.key.keysym.scancode == SDL_SCANCODE_RIGHT)
  134.                 {
  135.                     cout << "d" << endl;
  136.                     playerDest.x = playerDest.x + 5;
  137.                 }
  138.                 break;
  139.             case SDL_QUIT:
  140.                 cont = false;
  141.                 break;
  142.             default:
  143.                 break;
  144.             //}
  145.             //PassedTime = CurrentTime;
  146.             }
  147.             PLAYERDATA.playerX = playerDest.x;
  148.             PLAYERDATA.playerY = playerDest.y;
  149.         }
  150.     }
  151.  
  152.     SDL_DestroyTexture(backgroundTexture);
  153.     SDL_FreeSurface(backgroundSurface);
  154.     SDL_DestroyTexture(playerTexture);
  155.     SDL_FreeSurface(playerSurface);
  156.     SDL_DestroyRenderer(renderer);
  157.     SDL_DestroyWindow(window);
  158.     SDL_Quit();
  159.     if (sendThread.joinable())
  160.     {
  161.         sendThread.detach();
  162.     }
  163.     if (receiveThread.joinable())
  164.     {
  165.         receiveThread.detach();
  166.     }
  167.     closesocket(server);
  168.     WSACleanup();
  169.     cout << "\nServer has closed.\n" << endl;
  170.     return 0;
  171. }
  172.  
  173. void *sendMessage(void *sock_desc)
  174. {
  175.     while (TRUE)
  176.     {
  177.         Sleep(300);
  178.         {
  179.             if (send(*((int *)sock_desc), reinterpret_cast<const char*>(&PLAYERDATA), sizeof(PLAYERDATA) + 1, 0) < 0)
  180.             {
  181.                 cout << "\nSend fail.\n" << endl;
  182.                 abortGame = true;
  183.             }
  184.         }
  185.     }
  186. }
  187.  
  188. void *receiveMessage(void *sock_desc)
  189. {
  190.     char responce[2000];
  191.     while (TRUE)
  192.     {
  193.         memset(responce, 0, sizeof responce);
  194.         if (recv(*((int *)sock_desc), responce, sizeof(responce), 0) < 0)
  195.         {
  196.             cout << "\nRCV fail.\n" << endl;
  197.             abortGame = true;
  198.         }
  199.         //cout << responce << endl;
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement