Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <emscripten/emscripten.h>
- #include <SDL.h>
- #include <stdio.h>
- #include <emscripten/websocket.h>
- #include <iostream>
- #include <string>
- #include <sstream>
- struct GameData
- {
- int BallX;
- int BallY;
- int LeftPaddleY;
- int RightPaddleY;
- int LeftScore;
- int RightScore;
- public:
- void UpdateFromMessage(std::string *message)
- {
- size_t ballXIndex = message->find("X:") + 2;
- if (ballXIndex == std::string::npos)
- return;
- size_t ballYIndex = message->find("Y:") + 2;
- size_t leftPaddleYIndex = message->find("LPY:") + 4;
- size_t rightPaddleYIndex = message->find("RPY:") + 4;
- size_t leftScoreIndex = message->find("L:") + 2;
- size_t rightScoreIndex = message->find("R:") + 2;
- std::istringstream(message->substr(ballXIndex)) >> BallX;
- std::istringstream(message->substr(ballYIndex)) >> BallY;
- std::istringstream(message->substr(leftPaddleYIndex)) >> LeftPaddleY;
- std::istringstream(message->substr(rightPaddleYIndex)) >> RightPaddleY;
- std::istringstream(message->substr(leftScoreIndex)) >> LeftScore;
- std::istringstream(message->substr(rightScoreIndex)) >> RightScore;
- }
- };
- class Context
- {
- public:
- int *Ws;
- SDL_Renderer *Renderer;
- const int SCREEN_WIDTH;
- const int SCREEN_HEIGHT;
- int LeftScore;
- int RightScore;
- public:
- Context(int *ws, SDL_Renderer *renderer, int width, int height)
- : SCREEN_WIDTH(width), SCREEN_HEIGHT(height)
- {
- Ws = ws;
- Renderer = renderer;
- }
- };
- Context *g_context;
- EM_BOOL onopen(int eventType, const EmscriptenWebSocketOpenEvent *websocketEvent, void *userData)
- {
- puts("onopen");
- return EM_TRUE;
- }
- EM_BOOL onerror(int eventType, const EmscriptenWebSocketErrorEvent *websocketEvent, void *userData)
- {
- puts("onerror");
- return EM_TRUE;
- }
- EM_BOOL onclose(int eventType, const EmscriptenWebSocketCloseEvent *websocketEvent, void *userData)
- {
- puts("onclose");
- emscripten_websocket_delete(websocketEvent->socket);
- return EM_TRUE;
- }
- void drawBackground()
- {
- SDL_SetRenderDrawColor(g_context->Renderer, 0, 20, 20, 20);
- SDL_Rect bgRect = {0, 0, 800, 400};
- SDL_RenderFillRect(g_context->Renderer, &bgRect);
- }
- void drawBall(int x, int y)
- {
- SDL_SetRenderDrawColor(g_context->Renderer, 0, 155, 155, 255);
- SDL_Rect ballRect = {x - 10, y - 10, 20, 20};
- SDL_RenderFillRect(g_context->Renderer, &ballRect);
- }
- void drawPaddles(int leftY, int rightY)
- {
- int width = 20;
- int height = 100;
- SDL_SetRenderDrawColor(g_context->Renderer, 0, 155, 255, 255);
- SDL_Rect leftPaddleRect = {0, leftY, width, height};
- SDL_RenderFillRect(g_context->Renderer, &leftPaddleRect);
- SDL_SetRenderDrawColor(g_context->Renderer, 0, 155, 255, 255);
- SDL_Rect rightPaddleRect = {g_context->SCREEN_WIDTH - width, rightY, width, height};
- SDL_RenderFillRect(g_context->Renderer, &rightPaddleRect);
- }
- void draw(GameData gameData)
- {
- drawBackground();
- drawBall(gameData.BallX, gameData.BallY);
- drawPaddles(gameData.LeftPaddleY, gameData.RightPaddleY);
- SDL_RenderPresent(g_context->Renderer);
- }
- EM_BOOL onmessage(int eventType, const EmscriptenWebSocketMessageEvent *websocketEvent, void *userData)
- {
- //puts("onmessage");
- if (websocketEvent->isText)
- {
- const int MESSAGE_LENGTH = 41;
- std::string message = std::string(websocketEvent->data, websocketEvent->data + MESSAGE_LENGTH);
- GameData gameData;
- gameData.UpdateFromMessage(&message);
- draw(gameData);
- if(gameData.LeftScore != g_context->LeftScore ||
- gameData.RightScore!= g_context->RightScore)
- {
- g_context->LeftScore = gameData.LeftScore;
- g_context->RightScore = gameData.RightScore;
- printf("%d:%d\n", g_context->LeftScore, g_context->RightScore);
- }
- // printf("BallX: %d BallY: %d\n", gameData.BallX, gameData.BallY);
- // printf("LPY: %d RPY: %d\n", gameData.LeftPaddleY, gameData.RightPaddleY);
- // printf("L: %d R: %d\n", gameData.LeftScore, gameData.RightScore);
- }
- return EM_TRUE;
- }
- void loop(void *arg)
- {
- Context *context = static_cast<Context *>(arg);
- SDL_Event event;
- //send input to server
- EMSCRIPTEN_RESULT result;
- while (SDL_PollEvent(&event) != 0)
- {
- if (event.type == SDL_KEYDOWN)
- {
- if (event.key.keysym.sym == SDLK_UP)
- {
- puts("Key 'UP' pressed");
- result = emscripten_websocket_send_utf8_text(*(context->Ws), "Up");
- if (result)
- {
- printf("Failed to emscripten_websocket_send_utf8_text(): %d\n", result);
- }
- }
- else if (event.key.keysym.sym == SDLK_DOWN)
- {
- puts("Key 'DOWN' pressed");
- result = emscripten_websocket_send_utf8_text(*(context->Ws), "Down");
- if (result)
- {
- printf("Failed to emscripten_websocket_send_utf8_text(): %d\n", result);
- }
- }
- if (event.key.keysym.sym == SDLK_SPACE)
- {
- puts("Key 'SPACE' pressed");
- emscripten_websocket_close(*(context->Ws), 1000, "Bye");
- }
- }
- }
- }
- int main()
- {
- const int SCREEN_WIDTH = 800;
- const int SCREEN_HEIGHT = 400;
- const char *SERVER_URL = "ws://192.168.1.100:8000";
- SDL_Init(SDL_INIT_VIDEO);
- auto Window = SDL_CreateWindow("Pong!", SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- auto Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
- EmscriptenWebSocketCreateAttributes ws_attrs = {
- SERVER_URL,
- NULL,
- EM_TRUE};
- EMSCRIPTEN_WEBSOCKET_T ws = emscripten_websocket_new(&ws_attrs);
- emscripten_websocket_set_onopen_callback(ws, NULL, onopen);
- emscripten_websocket_set_onclose_callback(ws, NULL, onclose);
- emscripten_websocket_set_onmessage_callback(ws, NULL, onmessage);
- emscripten_websocket_set_onerror_callback(ws, NULL, onerror);
- Context context(&ws, Renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
- g_context = &context;
- emscripten_set_main_loop_arg(loop, &context, 1, -1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement