From 775b0148cec4329a6abb19d03220dc1d8a8b68c3 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 16 Jul 2024 23:38:46 +0200 Subject: rename pixel client --- pixel-client/src/main.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 pixel-client/src/main.rs (limited to 'pixel-client/src/main.rs') diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs new file mode 100644 index 00000000..e3aaa5cc --- /dev/null +++ b/pixel-client/src/main.rs @@ -0,0 +1,105 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +use game::Game; +use hurrycurry_protocol::glam::Vec2; +use network::Network; +use render::SpriteRenderer; +use sdl2::{ + event::Event, + keyboard::{KeyboardState, Keycode}, + pixels::Color, +}; +use std::time::{Duration, Instant}; + +pub mod game; +pub mod helper; +pub mod network; +pub mod render; +pub mod tilemap; + +fn main() { + env_logger::init_from_env("LOG"); + + let sdl_context = sdl2::init().unwrap(); + + let video_subsystem = sdl_context.video().unwrap(); + let window = video_subsystem + .window("Hurry Curry! Light Client", 1280, 720) + .position_centered() + .resizable() + .build() + .map_err(|e| e.to_string()) + .unwrap(); + + let mut canvas = window + .into_canvas() + .accelerated() + .build() + .map_err(|e| e.to_string()) + .unwrap(); + let texture_creator = canvas.texture_creator(); + + let mut net = Network::connect("ws://127.0.0.1/").unwrap(); + let mut renderer = SpriteRenderer::init(&texture_creator); + let mut game = Game::new(&renderer.atlas_layout()); + + net.queue_out.push_back(hurrycurry_protocol::PacketS::Join { + name: "light".to_string(), + character: 0, + }); + + let mut events = sdl_context.event_pump().unwrap(); + + let mut last_tick = Instant::now(); + + canvas.set_logical_size(320, 240).unwrap(); + + 'mainloop: loop { + net.poll(); + + let (width, height) = canvas.logical_size(); + renderer.size = Vec2::new(width as f32, height as f32); + + for packet in net.queue_in.drain(..) { + game.packet_in(packet, &mut renderer); + } + + let keyboard = KeyboardState::new(&events); + let dt = last_tick.elapsed().min(Duration::from_secs_f32(1. / 30.)); + game.tick(dt.as_secs_f32(), &keyboard, &mut net.queue_out); + last_tick += dt; + + game.draw(&mut renderer); + + canvas.set_draw_color(Color::BLACK); + canvas.clear(); + renderer.submit(&mut canvas); + canvas.present(); + + for event in events.poll_iter() { + match event { + Event::Quit { .. } + | Event::KeyDown { + keycode: Option::Some(Keycode::Escape), + .. + } => break 'mainloop, + _ => {} + } + } + } +} -- cgit v1.2.3-70-g09d2