TX Library Help – Version: 00173a, Revision: 173
|
//{=========================================================================== //}=========================================================================== #include "proto.h" #define ANOTHER_PEER_IP "localhost" //Меняйте адрес тут :) void NetPaint (TX_SOCKET paint_peer); void CursorDraw (NetCursor_t cursor, COLORREF color); void CursorMove (NetCursor_t* cursor); int main() { txCreateWindow (1024, 768); printf ("Searching for server...\n"); TX_SOCKET paint_peer = txCreateSocket (TX_CLIENT, ANOTHER_PEER_IP); printf ("Start draw meow.\n"); if (txnAssert (paint_peer) == TXN_NOT_CREATED) { printf ("Can't find server on this IP address.\n"); return -1; } NetPaint (paint_peer); return 0; } void NetPaint (TX_SOCKET paint_peer) { NetCursor_t cursor = {100, 100, -1, -1, CUR_NOP}; NetCursor_t peer_cursor = {100, 100, -1, -1, CUR_NOP}; while (!GetAsyncKeyState (VK_SPACE)) { if (peer_cursor.action == CUR_EXIT) break; CursorDraw(cursor, TX_LIGHTGREEN); CursorDraw(peer_cursor, TX_LIGHTRED); CursorMove (&cursor); txRecvFrom (paint_peer, &peer_cursor, sizeof(peer_cursor)); txSendTo (paint_peer, &cursor , sizeof(cursor)); txSleep (10); } cursor.action = CUR_EXIT; txSendTo (paint_peer, &cursor, sizeof(cursor)); } void CursorMove(NetCursor_t* cursor) { cursor->old_x = cursor->x; cursor->old_y = cursor->y; cursor->x = txMouseX(); cursor->y = txMouseY(); if (txMouseButtons() == 1) cursor->action = CUR_DRAW; else cursor->action = CUR_NOP; } void CursorDraw (NetCursor_t cursor, COLORREF color) { txSetFillColor (color); txSetColor (color, 10); if (cursor.action == 1) txLine (cursor.old_x, cursor.old_y, cursor.x, cursor.y); }
//{=========================================================================== //}=========================================================================== #include "proto.h" void NetPaint (TX_SOCKET paint_peer); void CursorDraw (NetCursor_t cursor, COLORREF color); void CursorMove (NetCursor_t* cursor); int main() { txCreateWindow (1024, 768); printf ("Searching for client...\n"); TX_SOCKET paint_peer = txCreateSocket (TX_SERVER, ""); printf ("Start draw meow.\n"); if (txnAssert (paint_peer) == TXN_NOT_CREATED) { printf ("Can't create socket. Maybe, port are busy.\n"); return -1; } NetPaint (paint_peer); return 0; } void NetPaint (TX_SOCKET paint_peer) { NetCursor_t cursor = {100, 100, -1, -1, CUR_NOP}; NetCursor_t peer_cursor = {100, 100, -1, -1, CUR_NOP}; while (!GetAsyncKeyState (VK_ESCAPE)) { if (peer_cursor.action == CUR_EXIT) break; CursorDraw(cursor, TX_LIGHTGREEN); CursorDraw(peer_cursor, TX_LIGHTRED); CursorMove (&cursor); txSendTo (paint_peer, &cursor , sizeof(cursor)); txRecvFrom (paint_peer, &peer_cursor, sizeof(peer_cursor)); txSleep (10); } cursor.action = CUR_EXIT; txSendTo (paint_peer, &cursor, sizeof(cursor)); } void CursorMove(NetCursor_t* cursor) { cursor->old_x = cursor->x; cursor->old_y = cursor->y; cursor->x = txMouseX(); cursor->y = txMouseY(); if (txMouseButtons() == 1) cursor->action = CUR_DRAW; else cursor->action = CUR_NOP; } void CursorDraw (NetCursor_t cursor, COLORREF color) { txSetFillColor (color); txSetColor (color, 10); if (cursor.action == CUR_DRAW) txLine (cursor.old_x, cursor.old_y, cursor.x, cursor.y); }
//{=========================================================================== //}=========================================================================== // proto.h // 1. server -> client: NetCursor_t // 2. server <- client: NetCursor_t #include "TXNetwork.h" #include "TXLib.h" enum CursorActions { CUR_NOP = 0, CUR_DRAW = 1, CUR_EXIT = -1 }; struct NetCursor_t { int x, y; int old_x, old_y; CursorActions action; };
/server.cpp"