selected_block: Vector3i;
player_shader: Shader;
-world_fog_block: Shader_Block;
+@Relocate
+world_fog_block: Shader_Block;
World_Fog :: struct {
u_fog_color: Vector3;
u_fog_start: f32;
shader_link_world_matrix_block(world_shader);
shader_link_world_matrix_block(player_shader);
- shader_block_link(^world_fog_block, world_shader, #cstr "u_game_fog_block");
- shader_block_link(^world_fog_block, player_shader, #cstr "u_game_fog_block");
world_fog_block = shader_block_create(World_Fog);
shader_block_update(^world_fog_block, "u_fog_start", 50.0f);
shader_block_update(^world_fog_block, "u_fog_range", 10.0f);
+ shader_block_update(^world_fog_block, "u_fog_color", fog_color);
+ shader_block_link(^world_fog_block, world_shader, #cstr "u_game_fog_block");
+ shader_block_link(^world_fog_block, player_shader, #cstr "u_game_fog_block");
font = font_lookup(.{"./assets/fonts/calibri.ttf", 32});
chat_font = font_lookup(.{"./assets/fonts/calibri.ttf", 16});
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader_use(world_shader);
- shader_block_update(^world_fog_block, "u_fog_color", fog_color);
shader_set_uniform(world_shader, #cstr "u_texture", 0);
glEnable(GL_DEPTH_TEST);
world_draw(world);
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_FALSE);
}
}
-}
\ No newline at end of file
+}
+net_register_handles :: () {
+ use type_info;
+
+ for_all_types() {
+ if struct_inherits(type_idx, Packet_Handler) {
+ func := get_struct_method(type_idx, "handle");
+ if func == null do continue;
+
+ struct_type_info := cast(^Type_Info_Struct) type_info;
+ packet_type := *cast(^packets.Type) struct_type_info.members[0].default;
+ packet_handles[packet_type] = *cast(^(rawptr) -> void) func.data;
+ }
+ }
+
+ println(packet_handles);
+}
+
net_connect :: (ip_addr: [] u8, port: u16) {
addr: net.Socket_Address;
addr.addr = net.str_to_ipv4(ip_addr);
}
}
-net_handle_packet :: (packet_data: [] u8) {
- packet := cast(^packets.Packet_Base) packet_data.data;
- switch packet.type {
- case .Verify_Connect {
- vc_packet := cast(^packets.Verify_Connect) packet;
- player_id = vc_packet.player_id;
+#local packet_handles: Map(packets.Type, (rawptr) -> void);
- push_game_state(Game_State, null);
- }
+Packet_Handler :: struct {
+ type: packets.Type;
+}
- case .Player_Joined {
- joined_packet := cast(^packets.Player_Joined) packet;
+#local Verify_Connect_Handle :: struct {
+ use base := Packet_Handler.{ .Verify_Connect };
- name := string.alloc_copy(.{
- ~~ ^joined_packet.name_data,
- ~~ joined_packet.name_length
- });
+ handle :: (packet: ^packets.Verify_Connect) {
+ player_id = packet.player_id;
- other_players[~~ joined_packet.player_id] = .{
- name,
- .{0, 0, 0}, .{0, 0, 0}, .{0, 0, 0},
- false
- };
+ push_game_state(Game_State, null);
+ }
+}
- if joined_packet.player_id != player_id {
- buf: [1024] u8;
- chat_messages << (conv.format(buf, "{} joined the server!", name) |> string.alloc_copy());
- }
- }
+#local Player_Joined_Handle :: struct {
+ use base := Packet_Handler.{ .Player_Joined };
- case .Connection_Rejected {
- cr_packet := cast(^packets.Connection_Rejected) packet;
- printf("Connection refused: {}\n", cr_packet.reason);
+ handle :: (packet: ^packets.Player_Joined) {
+ name := string.alloc_copy(packet->name());
- pop_game_state();
- push_game_state(Connect_Menu, null);
+ other_players[~~ packet.player_id] = .{
+ name,
+ .{0, 0, 0}, .{0, 0, 0}, .{0, 0, 0},
+ false
+ };
+
+ if packet.player_id != player_id {
+ buf: [1024] u8;
+ chat_messages << (conv.format(buf, "{} joined the server!", name) |> string.alloc_copy());
}
+ }
+}
- case .Chat_Message {
- chat_packet := cast(^packets.Chat_Message) packet;
+#local Connection_Rejected_Handle :: struct {
+ use base := Packet_Handler.{ .Connection_Rejected };
- buf: [1024] u8;
- msg: str;
-
- sender := ^other_players[~~ chat_packet.sender_id];
- if sender != null {
- msg = conv.format(buf, "[{}] {}\n", sender.name, str.{
- ~~ ^chat_packet.message_data,
- ~~ chat_packet.message_length,
- });
- } else {
- msg = .{
- ~~ ^chat_packet.message_data,
- ~~ chat_packet.message_length,
- };
- }
+ handle :: (packet: ^packets.Connection_Rejected) {
+ @NetworkErrors // Handle this case.
+ printf("Connection refused: {}\n", packet.reason);
- chat_messages << string.alloc_copy(msg);
- }
+ pop_game_state();
+ push_game_state(Connect_Menu, null);
+ }
+}
+
+#local Chat_Message_Handle :: struct {
+ use base := Packet_Handler.{ .Chat_Message };
- case .Player_Moved {
- moved := cast(^packets.Player_Moved) packet;
- if moved.player_id == player_id do return;
+ handle :: (packet: ^packets.Chat_Message) {
+ buf: [1024] u8;
+ msg: str;
- player := ^other_players[~~ moved.player_id];
- player.position = moved.position;
- player.velocity = moved.velocity;
- player.facing = moved.facing;
- player.on_ground = moved.on_ground;
+ sender := map.get_ptr(^other_players, ~~ packet.sender_id);
+ if sender != null {
+ msg = conv.format(buf, "[{}] {}\n", sender.name, packet->message());
+ } else {
+ msg = packet->message();
}
- case .Block_Updates {
- block_updates := cast(^packets.Block_Updates) packet;
+ chat_messages << string.alloc_copy(msg);
+ }
+}
- update_count := block_updates.update_count;
- updates := ([] packets.Block_Updates.Update).{ ~~^block_updates.updates, ~~update_count };
- for updates {
- pos := it.position;
- world_set_block(world, pos.x, pos.y, pos.z, it.new_block);
- }
+#local Player_Moved_Handle :: struct {
+ use base := Packet_Handler.{ .Player_Moved };
+
+ handle :: (packet: ^packets.Player_Moved) {
+ if packet.player_id == player_id do return;
+
+ player := map.get_ptr(^other_players, ~~ packet.player_id);
+ player.position = packet.position;
+ player.velocity = packet.velocity;
+ player.facing = packet.facing;
+ player.on_ground = packet.on_ground;
+ }
+}
+
+#local Block_Updates_Handle :: struct {
+ use base := Packet_Handler.{ .Block_Updates };
+
+ handle :: (packet: ^packets.Block_Updates) {
+ updates := packet->updates();
+ for updates {
+ pos := it.position;
+ world_set_block(world, pos.x, pos.y, pos.z, it.new_block);
}
}
}
+net_handle_packet :: (packet_data: [] u8) {
+ packet := cast(^packets.Packet_Base) packet_data.data;
+ if packet_handles[~~ packet.type] != null_proc {
+ packet_handles[~~ packet.type](packet);
+ }
+}
+
net_send_connect :: (name: str) {
msg := cast(^packets.Connect) calloc(sizeof packets.Connect + name.count);
msg.type = .Connect;
block_updates.type = .Block_Updates;
block_updates.update_count = ~~updates.count;
- block_update := cast(^packets.Block_Updates.Update) ^block_updates.updates;
+ block_update := cast(^packets.Block_Updates.Update) ^block_updates.update_data;
for updates.count {
block_update[it].position = updates[it].position;
block_update[it].new_block = updates[it].new_block;