Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #pragma comment(lib, "ws2_32.lib")
- #include <windows.h>
- #include <string>
- #include <tchar.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <iostream>
- #include <algorithm>
- #include <thread>
- #pragma warning( disable : 4996)
- using namespace std;
- struct t_PLAYERDATA
- {
- int playerID = 0;
- double playerX = 0;
- double playerY = 0;
- } PLAYERDATA;
- void *receiveMessage(void *sock_desc);
- void *sendMessage(void *sock_desc);
- bool abortGame = false;
- thread sendThread;
- thread receiveThread;
- int main(int arc, char ** argv)
- {
- void* data = &PLAYERDATA;
- //PLAYERDATA *myData = (struct t_PLAYERDATA *)data;
- SDL_Window* window = NULL;
- SDL_Event event;
- SDL_Renderer *renderer = NULL;
- SDL_Texture* backgroundTexture = NULL;
- SDL_Texture* playerTexture = NULL;
- SDL_Surface* backgroundSurface = NULL;
- SDL_Surface* playerSurface = NULL;
- SDL_Rect backgroundDest;
- SDL_Rect playerDest;
- PLAYERDATA.playerID = 1;
- char responce[1024];
- WSADATA WSAData;
- SOCKADDR_IN addr;
- WSAStartup(MAKEWORD(2, 0), &WSAData);
- SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
- InetPton(AF_INET, "192.168.1.133", &addr.sin_addr.s_addr);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(5555);
- int sock = server;
- int * newSocket = static_cast<int*>(malloc(1));
- *newSocket = sock;
- if (connect(server, (SOCKADDR *)&addr, sizeof(addr)) == 0)
- {
- cout << "\nConnected to server!\n" << endl;
- sendThread = thread(sendMessage, (void *)newSocket);
- receiveThread = thread(receiveMessage, (void *)newSocket);
- }
- else
- {
- cout << "\nCouldn't connect to server.\n" << endl;
- }
- bool cont = true;
- int CurrentTime = 0, seconds = 0, PassedTime = 0;
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
- }
- else
- {
- window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
- }
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- if (renderer)
- {
- backgroundDest = { 0, 0, 800, 600 };
- backgroundSurface = SDL_LoadBMP("data/deck/background.bmp");
- backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
- playerDest = { 400, 300, 100, 100 };
- playerSurface = SDL_LoadBMP("data/deck/player.bmp");
- playerTexture = SDL_CreateTextureFromSurface(renderer, playerSurface);
- if (!playerSurface || !backgroundSurface)
- {
- cout << "Sprite loading error : " << SDL_GetError() << endl;
- }
- if (!backgroundTexture || !playerTexture)
- {
- cout << "Couldn't display image : " << SDL_GetError() << endl;
- }
- }
- else
- {
- cout << "Couldn't create renderer : " << SDL_GetError() << endl;
- }
- while (!abortGame)
- {
- while (SDL_PollEvent(&event))
- {
- CurrentTime = SDL_GetTicks();
- SDL_RenderClear(renderer);
- SDL_RenderCopy(renderer, backgroundTexture, NULL, &backgroundDest);
- SDL_RenderCopy(renderer, playerTexture, NULL, &playerDest);
- SDL_RenderPresent(renderer);
- //if (CurrentTime - PassedTime > 200)
- //{
- switch (event.type)
- {
- case SDL_KEYDOWN:
- if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
- {
- cout << "Quit" << endl;
- cont = 0;
- }
- else if (event.key.keysym.scancode == SDL_SCANCODE_UP)
- {
- cout << "z" << endl;
- playerDest.y = playerDest.y - 5;
- }
- else if (event.key.keysym.scancode == SDL_SCANCODE_DOWN)
- {
- cout << "s" << endl;
- playerDest.y = playerDest.y + 5;
- }
- else if (event.key.keysym.scancode == SDL_SCANCODE_LEFT)
- {
- cout << "q" << endl;
- playerDest.x= playerDest.x - 5;
- }
- else if (event.key.keysym.scancode == SDL_SCANCODE_RIGHT)
- {
- cout << "d" << endl;
- playerDest.x = playerDest.x + 5;
- }
- break;
- case SDL_QUIT:
- cont = false;
- break;
- default:
- break;
- //}
- //PassedTime = CurrentTime;
- }
- PLAYERDATA.playerX = playerDest.x;
- PLAYERDATA.playerY = playerDest.y;
- }
- }
- SDL_DestroyTexture(backgroundTexture);
- SDL_FreeSurface(backgroundSurface);
- SDL_DestroyTexture(playerTexture);
- SDL_FreeSurface(playerSurface);
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- if (sendThread.joinable())
- {
- sendThread.detach();
- }
- if (receiveThread.joinable())
- {
- receiveThread.detach();
- }
- closesocket(server);
- WSACleanup();
- cout << "\nServer has closed.\n" << endl;
- return 0;
- }
- void *sendMessage(void *sock_desc)
- {
- while (TRUE)
- {
- Sleep(300);
- {
- if (send(*((int *)sock_desc), reinterpret_cast<const char*>(&PLAYERDATA), sizeof(PLAYERDATA) + 1, 0) < 0)
- {
- cout << "\nSend fail.\n" << endl;
- abortGame = true;
- }
- }
- }
- }
- void *receiveMessage(void *sock_desc)
- {
- char responce[2000];
- while (TRUE)
- {
- memset(responce, 0, sizeof responce);
- if (recv(*((int *)sock_desc), responce, sizeof(responce), 0) < 0)
- {
- cout << "\nRCV fail.\n" << endl;
- abortGame = true;
- }
- //cout << responce << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement