i broke a bunch of stuff, but lets commit it anyway

This commit is contained in:
Jak Barnes 2019-03-03 00:34:11 +00:00
commit 082a0c85ce

View file

@ -21,6 +21,44 @@
#include "raylib.h"
#define MAX_BUFFER_SIZE 512
char buffer[MAX_BUFFER_SIZE];
float elapsed = 0.0f;
float delay = 1.0f;
bool connected = false;
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .nonblocking = true};
SocketResult* client_res = NULL;
SocketSet* socket_set = NULL;
void NetworkConnect()
{
if (!SocketConnect(&client_cfg, client_res)) {
TraceLog(LOG_WARNING,
"Failed to connect socket to server: status %d, errno %d",
client_res->status, client_res->socket->status);
} else {
connected = true;
}
}
void NetworkUpdate()
{
int active = CheckSockets(socket_set, 0);
if (active != 0) {
TraceLog(LOG_DEBUG,
"There are currently %d socket(s) with data to be processed.", active);
}
if (active > 0) {
int bytesRecv = 0;
if (IsSocketReady(client_res->socket)) {
bytesRecv = SocketReceive(client_res->socket, buffer, MAX_BUFFER_SIZE, 0);
}
if (bytesRecv > 0) { TraceLog(LOG_INFO, "%s", buffer); }
}
}
int main()
{
int screenWidth = 800;
@ -29,12 +67,35 @@ int main()
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
// Set log type
SetTraceLogLevel(LOG_DEBUG);
// Init the network layer
InitNetwork();
// Create the server
client_res = AllocSocketResult();
socket_set = AllocSocketSet(1);
if (!SocketCreate(&client_cfg, client_res)) {
TraceLog(LOG_WARNING, "Failed to create socket: status %d, errno %d",
client_res->status, client_res->socket->status);
} else {
AddSocket(socket_set, client_res->socket);
}
// Main game loop
while (!WindowShouldClose())
{
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
ClearBackground(RAYWHITE);
if (connected) {
NetworkUpdate();
} else {
elapsed += GetFrameTime();
if (elapsed > delay) {
NetworkConnect();
elapsed = 0.0f;
}
}
EndDrawing();
}
CloseWindow();