More socket API changes
This commit is contained in:
parent
4c9057cec2
commit
97d01fe36d
4 changed files with 864 additions and 696 deletions
|
|
@ -1,62 +1,72 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - Basic window
|
||||
*
|
||||
* Welcome to raylib!
|
||||
*
|
||||
* To test examples, just press F6 and execute raylib_compile_execute script
|
||||
* Note that compiled executable is placed in the same folder as .c file
|
||||
*
|
||||
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
||||
* raylib official webpage: www.raylib.com
|
||||
*
|
||||
* Enjoy using raylib. :)
|
||||
*
|
||||
* This example has been created using raylib 1.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
|
||||
#include <iostream>
|
||||
#include "raylib.h"
|
||||
#include <vector>
|
||||
#include <time.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct room
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int width = 10;
|
||||
int height = 10;
|
||||
};
|
||||
|
||||
|
||||
room Temp_Rect_Creat()
|
||||
{
|
||||
room in_creation;
|
||||
|
||||
in_creation.x = int(rand() % 700);
|
||||
in_creation.y = int(rand() % 400);
|
||||
|
||||
in_creation.width = int(rand() % 50);
|
||||
if (in_creation.width < 10) in_creation.width = 10;
|
||||
in_creation.height = int(rand() % 70);
|
||||
if (in_creation.height < 10) in_creation.height = 10;
|
||||
|
||||
return in_creation;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
vector<room> rooms;
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input");
|
||||
|
||||
Vector2 ballPosition = { (float)screenWidth / 2, (float)screenHeight / 2 };
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
||||
int i = 0;
|
||||
while (i < 20)
|
||||
{
|
||||
room temp = Temp_Rect_Creat();
|
||||
rooms.push_back(temp);
|
||||
i++;
|
||||
}
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
for (int i = 0; i < rooms.size(); i++)
|
||||
{
|
||||
DrawRectangle(rooms[i].x, rooms[i].y, rooms[i].width, rooms[i].height, BLACK);
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -32,7 +32,7 @@ int main()
|
|||
SetTargetFPS(60);
|
||||
|
||||
// Networking
|
||||
InitNetwork();
|
||||
InitNetwork();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@
|
|||
|
||||
#include "raylib.h"
|
||||
|
||||
#define MYPORT "4950"
|
||||
#define MAXBUFLEN 100
|
||||
|
||||
int main()
|
||||
{
|
||||
// Setup
|
||||
|
|
@ -32,7 +35,7 @@ int main()
|
|||
SetTargetFPS(60);
|
||||
|
||||
// Networking
|
||||
InitNetwork();
|
||||
InitNetwork();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
|
|
@ -47,7 +50,7 @@ int main()
|
|||
EndDrawing();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
// Cleanup
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -23,10 +23,9 @@
|
|||
#include "raylib.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
{
|
||||
// Setup
|
||||
int screenWidth = 800;
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
|
|
@ -37,29 +36,45 @@ int main()
|
|||
// Networking
|
||||
InitNetwork();
|
||||
|
||||
// Server socket and address
|
||||
AddressInformation serveraddr;
|
||||
Socket server;
|
||||
server.blocking = false;
|
||||
ResolveHost(&serveraddr, "localhost", "3490", SOCKET_TCP);
|
||||
// Create the server
|
||||
SocketConfig server_cfg = {
|
||||
.host = "127.0.0.1",
|
||||
.port = 8080,
|
||||
.server = true,
|
||||
.nonblocking = true,
|
||||
};
|
||||
|
||||
CreateSocket(&server, serveraddr);
|
||||
BindSocket(server, serveraddr);
|
||||
ListenSocket(server);
|
||||
SocketResult server_res;
|
||||
memset(&server_res, 0, sizeof(SocketResult));
|
||||
{
|
||||
bool ok = SocketOpen(&server_cfg, &server_res);
|
||||
if (!ok) { return false; }
|
||||
}
|
||||
|
||||
// Client socket and address
|
||||
AddressInformation clientaddr;
|
||||
Socket client;
|
||||
client.blocking = false;
|
||||
ResolveHost(&clientaddr, "localhost", "3490", SOCKET_TCP);
|
||||
CreateSocket(&client, clientaddr);
|
||||
ConnectSocket(client, clientaddr);
|
||||
// Create the client
|
||||
SocketConfig client_cfg = {
|
||||
.host = "127.0.0.1",
|
||||
.port = 8080,
|
||||
};
|
||||
|
||||
Socket connection; // The socket connection between server->client
|
||||
float elapsed = 0.0f, delay = 1.0f; // ms
|
||||
bool ping = false, pong = false;
|
||||
char recvBuffer[512];
|
||||
bool connected = false;
|
||||
SocketResult client_res;
|
||||
memset(&client_res, 0, sizeof(SocketResult));
|
||||
{
|
||||
bool ok = SocketOpen(&client_cfg, &client_res);
|
||||
if (!ok)
|
||||
{
|
||||
printf("failed to open: status %d, errno %d\n",
|
||||
client_res.status, client_res.saved_errno);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SocketResult connection;
|
||||
memset(&connection, 0, sizeof(SocketResult));
|
||||
float elapsed = 0.0f, delay = 1.0f; // ms
|
||||
bool ping = false, pong = false;
|
||||
char recvBuffer[512];
|
||||
bool connected = false;
|
||||
memset(&recvBuffer, 0, 8);
|
||||
|
||||
// Main game loop
|
||||
|
|
@ -74,15 +89,17 @@ int main()
|
|||
// A valid connection will != -1
|
||||
if (!connected)
|
||||
{
|
||||
AcceptSocket(server, &connection);
|
||||
ping = true;
|
||||
connected = true;
|
||||
if (SocketAccept(server_res.socket.handle, &connection))
|
||||
{
|
||||
ping = true;
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Connected
|
||||
if (connected)
|
||||
{
|
||||
int bytesRecv = ReceiveTCP(connection.handle, recvBuffer, 5);
|
||||
int bytesRecv = SocketReceive(&connection.socket, recvBuffer, 5);
|
||||
if (bytesRecv > 0)
|
||||
{
|
||||
if (strcmp(recvBuffer, "Ping!") == 0)
|
||||
|
|
@ -103,12 +120,12 @@ int main()
|
|||
if (ping)
|
||||
{
|
||||
ping = false;
|
||||
SendTCP(client.handle, "Ping!", 5);
|
||||
SocketSend(&client_res.socket, "Ping!", 5);
|
||||
}
|
||||
else if (pong)
|
||||
{
|
||||
pong = false;
|
||||
SendTCP(client.handle, "Pong!", 5);
|
||||
SocketSend(&client_res.socket, "Pong!", 5);
|
||||
}
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue