medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f0b31ef8b90cca6baca87cae0d12d839aea82d02
parent c128c176f486781e71809fc26f5c39aa1ea597b1
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Dec 24 16:23:47 +0200

Connect to the server and show other players in the terrain renderer

Diffstat:
Makefile | 2+-
hm.cc | 109++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 109 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile @@ -14,7 +14,7 @@ launcher/launcher: launcher/main.o log.o http.o sha2.o strlcpy.o # Module dependencies bsp.so: bsp.o bsp_renderer.o -hm.so: hm.o heightmap.o terrain_manager.o lz4.o btt.o gpubtt.o skybox.o +hm.so: hm.o heightmap.o terrain_manager.o lz4.o btt.o gpubtt.o skybox.o http.o btt.so: mod_btt.o btt.o gpubtt.o heightmap.o skybox.o stb_image.o lz4.o sm.so: shadow_map.o diff --git a/hm.cc b/hm.cc @@ -1,5 +1,6 @@ #include <stdlib.h> #include <math.h> +#include <errno.h> #include "game.h" #include "intrinsics.h" @@ -11,7 +12,11 @@ #include "work_queue.h" #include "text_renderer.h" #include "pool.h" +#include "hashtable.h" +#include "stream.h" +#include "http.h" #include "platform_io.h" +#include "platform_net.h" static const char * vert_src = GLSL( in vec3 position; @@ -80,6 +85,18 @@ static WORK_QUEUE_CALLBACK( testwq ) { static UB ub_sphere; +static int sock; +static u64 sid; +static bool connected = false; +static struct sockaddr_in addr; + +static bool set_nonblocking( int fd ) { + int flags = fcntl( fd, F_GETFL, 0 ); + if( flags == -1 ) return false; + int ok = fcntl( fd, F_SETFL, flags | O_NONBLOCK ); + return ok != -1; +} + extern "C" GAME_INIT( game_init ) { workqueue_init( &game->background_tasks, &mem->persistent_arena, 2 ); u32 nums[ 10 ]; @@ -152,9 +169,16 @@ struct Explosion { double created_at; }; +struct Player { + v3 pos; +}; + static Pool< Fireball, 1024 > fireballs; static Pool< Explosion, 1024 > explosions; +static Pool< Player, 1024 > players; +static HashTable< Player *, 1024 * 2 > sid_to_player; + extern "C" GAME_FRAME( game_frame ) { renderer_begin_frame( CLEARCOLOUR_DONT ); @@ -270,8 +294,91 @@ extern "C" GAME_FRAME( game_frame ) { } { + char buf[ 1400 ]; + sockaddr_storage a; + socklen_t addr_len = sizeof( a ); + ssize_t len = recvfrom( sock, buf, sizeof( buf ), 0, ( sockaddr * ) &a, &addr_len ); + if( len == -1 ) { + if( errno != EAGAIN ) { + FATAL( "recvfrom" ); + } + len = 0; + } + + ReadStream rs( buf, len ); + + if( connected ) { + char write_buf[ 1400 ]; + WriteStream ws( write_buf ); + write( &ws, sid ); + write( &ws, game->pos ); + + int ok = sendto( sock, ws.start, ws.len(), 0, ( sockaddr * ) &addr, sizeof( addr ) ); + if( ok == -1 ) { + FATAL( "sendto" ); + } + + u16 player_count = read_u16( &rs ); + ReadStreamCheckpoint rsc = rs.checkpoint(); + + for( u16 i = 0; i < player_count; i++ ) { + u64 s = read_u64( &rs ); + v3 pos; + read( &rs, &pos ); + } + + if( rs.done() ) { + rs.reset( &rsc ); + for( u16 i = 0; i < player_count; i++ ) { + u64 s = read_u64( &rs ); + v3 pos; + read( &rs, &pos ); + + Player * player; + if( !sid_to_player.get( s, &player ) ) { + player = players.acquire(); + ASSERT( player != NULL ); // TODO + sid_to_player.add( s, player ); + } + + player->pos = pos; + } + + ASSERT( rs.done() ); + } + + ImmediateContext imm; + ImmediateTriangle boxes[ 4096 ]; + immediate_init( &imm, boxes, ARRAY_COUNT( boxes ) ); + + for( const Player * player : players ) { + v3 mins = player->pos - v3( 0.1, 0.1, 1.8 ); + v3 maxs = player->pos + v3( 0.1, 0.1, 0 ); + immediate_aabb( &imm, mins, maxs, v4( 1, 1, 0, 1 ) ); + } + + m4 VP = V * P; + renderer_ub_data( ub_sphere, &VP, sizeof( VP ) ); + + RenderState impact_render_state = { }; + impact_render_state.shader = game->test_outline_shader; + impact_render_state.ubs[ UB_VS_HOT ] = ub_sphere; + immediate_render( &imm, impact_render_state ); + } + else { + sid = read_u64( &rs ); + if( rs.ok ) { + connected = true; + } + } + } + + { char buf[ 128 ]; - snprintf( buf, sizeof( buf ), "Frame time: %.1fms FPS: %.1f", dt * 1000.0f, 1.0f / dt ); + snprintf( buf, sizeof( buf ), "Frame time: %.1fms FPS: %.1f %s", + dt * 1000.0f, + 1.0f / dt, + connected ? "connected!" : "connecting" ); draw_text( buf, 2.0f, 2.0f, 16.0f ); } }