Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <iostream>
- #include <algorithm>
- #include <winsock2.h>
- #include <Ws2tcpip.h>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- struct playerData
- {
- int playerID;
- double playerX;
- double playerY;
- } playerA;
- int main(int arc, char ** argv)
- {
- SDL_Window* pWindow = NULL;
- SDL_Event event;
- SDL_Renderer *pRenderer = NULL;
- SDL_Texture* pTexture = NULL;
- SDL_Texture* pTextureB = NULL;
- SDL_Surface* pSprite = NULL;
- SDL_Surface* pSpriteB = NULL;
- SDL_Rect dest;
- SDL_Rect destB;
- playerA.playerID = 1;
- string talk = "";
- char responce[1024];
- WSADATA WSAData;
- SOCKET server;
- SOCKADDR_IN addr;
- WSAStartup(MAKEWORD(2, 0), &WSAData);
- server = socket(AF_INET, SOCK_STREAM, 0);
- InetPton(AF_INET, "192.168.1.16", &addr.sin_addr.s_addr);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(5555);
- connect(server, (SOCKADDR *)&addr, sizeof(addr));
- //if (connect(server, (SOCKADDR *)&addr, sizeof(addr))>0)
- {
- 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
- {
- pWindow = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
- }
- pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
- if (pRenderer)
- {
- pSprite = SDL_LoadBMP("data/deck/background.bmp");
- if (pSprite)
- {
- pTexture = SDL_CreateTextureFromSurface(pRenderer, pSprite);
- if (pTexture)
- {
- dest = { 0, 0, 800, 600 };
- SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
- SDL_RenderPresent(pRenderer);
- }
- else
- {
- cout << "Couldn't display image : " << SDL_GetError() << endl;
- }
- }
- else
- {
- cout << "Sprite loading error : " << SDL_GetError() << endl;
- }
- pSpriteB = SDL_LoadBMP("data/deck/player.bmp");
- if (pSpriteB)
- {
- pTextureB = SDL_CreateTextureFromSurface(pRenderer, pSpriteB);
- if (pTextureB)
- {
- dest = { 0, 0, 100, 100 };
- SDL_RenderCopy(pRenderer, pTextureB, NULL, &dest);
- SDL_RenderPresent(pRenderer);
- }
- else
- {
- cout << "Couldn't display image : " << SDL_GetError() << endl;
- }
- }
- else
- {
- cout << "Sprite loading error : " << SDL_GetError() << endl;
- }
- }
- else
- {
- cout << "Couldn't create renderer : " << SDL_GetError() << endl;
- }
- while (cont != 0)
- {
- while (SDL_PollEvent(&event))
- {
- CurrentTime = SDL_GetTicks();
- 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_1)
- {
- talk = "1";
- cout << "1" << endl;
- }
- else if (event.key.keysym.scancode == SDL_SCANCODE_2)
- {
- talk = "2";
- cout << "2" << endl;
- }
- break;
- case SDL_QUIT:
- cont = false;
- break;
- default:
- break;
- }
- PassedTime = CurrentTime;
- }
- }
- send(server, talk.c_str(), sizeof(talk), 0);
- SDL_RenderClear(pRenderer);
- SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
- SDL_RenderPresent(pRenderer);
- CurrentTime = SDL_GetTicks();
- }
- }
- //delse
- {
- cout << "Counldn't connect to server." << endl;
- system("pause");
- }
- SDL_DestroyTexture(pTexture);
- SDL_DestroyRenderer(pRenderer);
- SDL_FreeSurface(pSprite);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement