Advertisement
Griff1th

Pong

Jun 4th, 2024
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | None | 0 0
  1. #include <emscripten/emscripten.h>
  2. #include <SDL.h>
  3. #include <stdio.h>
  4. #include <emscripten/websocket.h>
  5. #include <iostream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9.  
  10. struct GameData
  11. {
  12.     int BallX;
  13.     int BallY;
  14.     int LeftPaddleY;
  15.     int RightPaddleY;
  16.     int LeftScore;
  17.     int RightScore;
  18.  
  19. public:
  20.     void UpdateFromMessage(std::string *message)
  21.     {  
  22.         size_t ballXIndex = message->find("X:") + 2;
  23.         if (ballXIndex == std::string::npos)
  24.             return;
  25.  
  26.         size_t ballYIndex = message->find("Y:") + 2;
  27.         size_t leftPaddleYIndex = message->find("LPY:") + 4;
  28.         size_t rightPaddleYIndex = message->find("RPY:") + 4;
  29.         size_t leftScoreIndex = message->find("L:") + 2;
  30.         size_t rightScoreIndex = message->find("R:") + 2;
  31.  
  32.         std::istringstream(message->substr(ballXIndex)) >> BallX;
  33.         std::istringstream(message->substr(ballYIndex)) >> BallY;
  34.         std::istringstream(message->substr(leftPaddleYIndex)) >> LeftPaddleY;
  35.         std::istringstream(message->substr(rightPaddleYIndex)) >> RightPaddleY;
  36.         std::istringstream(message->substr(leftScoreIndex)) >> LeftScore;
  37.         std::istringstream(message->substr(rightScoreIndex)) >> RightScore;
  38.     }
  39. };
  40.  
  41. class Context
  42. {
  43. public:
  44.     int *Ws;
  45.     SDL_Renderer *Renderer;
  46.     const int SCREEN_WIDTH;
  47.     const int SCREEN_HEIGHT;
  48.     int LeftScore;
  49.     int RightScore;
  50.  
  51. public:
  52.     Context(int *ws, SDL_Renderer *renderer, int width, int height)
  53.     : SCREEN_WIDTH(width), SCREEN_HEIGHT(height)
  54.     {
  55.         Ws = ws;
  56.         Renderer = renderer;
  57.     }
  58. };
  59. Context *g_context;
  60.  
  61. EM_BOOL onopen(int eventType, const EmscriptenWebSocketOpenEvent *websocketEvent, void *userData)
  62. {
  63.     puts("onopen");
  64.     return EM_TRUE;
  65. }
  66. EM_BOOL onerror(int eventType, const EmscriptenWebSocketErrorEvent *websocketEvent, void *userData)
  67. {
  68.     puts("onerror");
  69.     return EM_TRUE;
  70. }
  71. EM_BOOL onclose(int eventType, const EmscriptenWebSocketCloseEvent *websocketEvent, void *userData)
  72. {
  73.     puts("onclose");
  74.     emscripten_websocket_delete(websocketEvent->socket);
  75.  
  76.     return EM_TRUE;
  77. }
  78.  
  79. void drawBackground()
  80. {
  81.     SDL_SetRenderDrawColor(g_context->Renderer, 0, 20, 20, 20);
  82.     SDL_Rect bgRect = {0, 0, 800, 400};
  83.     SDL_RenderFillRect(g_context->Renderer, &bgRect);
  84. }
  85. void drawBall(int x, int y)
  86. {
  87.     SDL_SetRenderDrawColor(g_context->Renderer, 0, 155, 155, 255);
  88.     SDL_Rect ballRect = {x - 10, y - 10, 20, 20};
  89.     SDL_RenderFillRect(g_context->Renderer, &ballRect);
  90. }
  91. void drawPaddles(int leftY, int rightY)
  92. {  
  93.     int width = 20;
  94.     int height = 100;
  95.  
  96.     SDL_SetRenderDrawColor(g_context->Renderer, 0, 155, 255, 255);
  97.     SDL_Rect leftPaddleRect = {0, leftY, width, height};
  98.     SDL_RenderFillRect(g_context->Renderer, &leftPaddleRect);
  99.  
  100.     SDL_SetRenderDrawColor(g_context->Renderer, 0, 155, 255, 255);
  101.     SDL_Rect rightPaddleRect = {g_context->SCREEN_WIDTH - width, rightY, width, height};
  102.     SDL_RenderFillRect(g_context->Renderer, &rightPaddleRect);
  103. }
  104. void draw(GameData gameData)
  105. {
  106.     drawBackground();
  107.     drawBall(gameData.BallX, gameData.BallY);
  108.     drawPaddles(gameData.LeftPaddleY, gameData.RightPaddleY);
  109.  
  110.     SDL_RenderPresent(g_context->Renderer);
  111. }
  112.  
  113. EM_BOOL onmessage(int eventType, const EmscriptenWebSocketMessageEvent *websocketEvent, void *userData)
  114. {
  115.     //puts("onmessage");
  116.     if (websocketEvent->isText)
  117.     {      
  118.         const int MESSAGE_LENGTH = 41;        
  119.         std::string message = std::string(websocketEvent->data, websocketEvent->data + MESSAGE_LENGTH);
  120.         GameData gameData;
  121.         gameData.UpdateFromMessage(&message);        
  122.         draw(gameData);
  123.  
  124.         if(gameData.LeftScore != g_context->LeftScore ||
  125.             gameData.RightScore!= g_context->RightScore)
  126.         {
  127.             g_context->LeftScore = gameData.LeftScore;
  128.             g_context->RightScore = gameData.RightScore;
  129.             printf("%d:%d\n", g_context->LeftScore, g_context->RightScore);
  130.         }
  131.        
  132.         // printf("BallX: %d BallY: %d\n", gameData.BallX, gameData.BallY);
  133.         // printf("LPY: %d RPY: %d\n", gameData.LeftPaddleY, gameData.RightPaddleY);
  134.         // printf("L: %d R: %d\n", gameData.LeftScore, gameData.RightScore);
  135.     }
  136.     return EM_TRUE;
  137. }
  138.  
  139. void loop(void *arg)
  140. {
  141.     Context *context = static_cast<Context *>(arg);
  142.     SDL_Event event;
  143.  
  144.     //send input to server
  145.     EMSCRIPTEN_RESULT result;
  146.     while (SDL_PollEvent(&event) != 0)
  147.     {
  148.         if (event.type == SDL_KEYDOWN)
  149.         {
  150.             if (event.key.keysym.sym == SDLK_UP)
  151.             {
  152.                 puts("Key 'UP' pressed");
  153.                 result = emscripten_websocket_send_utf8_text(*(context->Ws), "Up");
  154.                 if (result)
  155.                 {
  156.                     printf("Failed to emscripten_websocket_send_utf8_text(): %d\n", result);
  157.                 }
  158.             }
  159.             else if (event.key.keysym.sym == SDLK_DOWN)
  160.             {
  161.                 puts("Key 'DOWN' pressed");
  162.                 result = emscripten_websocket_send_utf8_text(*(context->Ws), "Down");
  163.                 if (result)
  164.                 {
  165.                     printf("Failed to emscripten_websocket_send_utf8_text(): %d\n", result);
  166.                 }
  167.             }
  168.  
  169.             if (event.key.keysym.sym == SDLK_SPACE)
  170.             {
  171.                 puts("Key 'SPACE' pressed");
  172.                 emscripten_websocket_close(*(context->Ws), 1000, "Bye");
  173.             }
  174.         }
  175.     }
  176. }
  177. int main()
  178. {  
  179.     const int SCREEN_WIDTH = 800;
  180.     const int SCREEN_HEIGHT = 400;
  181.     const  char *SERVER_URL = "ws://192.168.1.100:8000";
  182.  
  183.     SDL_Init(SDL_INIT_VIDEO);
  184.     auto Window = SDL_CreateWindow("Pong!", SDL_WINDOWPOS_UNDEFINED,
  185.         SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  186.     auto Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
  187.  
  188.     EmscriptenWebSocketCreateAttributes ws_attrs = {
  189.         SERVER_URL,
  190.         NULL,
  191.         EM_TRUE};
  192.  
  193.     EMSCRIPTEN_WEBSOCKET_T ws = emscripten_websocket_new(&ws_attrs);
  194.  
  195.     emscripten_websocket_set_onopen_callback(ws, NULL, onopen);
  196.     emscripten_websocket_set_onclose_callback(ws, NULL, onclose);
  197.     emscripten_websocket_set_onmessage_callback(ws, NULL, onmessage);
  198.     emscripten_websocket_set_onerror_callback(ws, NULL, onerror);
  199.  
  200.     Context context(&ws, Renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
  201.     g_context = &context;
  202.     emscripten_set_main_loop_arg(loop, &context, 1, -1);
  203. }
  204.  
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement