tiles
This commit is contained in:
parent
0ed63eefa2
commit
a5336349b8
1 changed files with 40 additions and 17 deletions
57
src/main.rs
57
src/main.rs
|
@ -16,7 +16,7 @@ enum Message {
|
|||
pub const MESSAGETYPE_RESIZE: u32 = 0;
|
||||
pub const MESSAGETYPE_DATA: u32 = 1;
|
||||
|
||||
fn read_message(stream: &mut TcpStream, argb_buffer: &mut Vec<u32>) -> Option<Message> {
|
||||
fn read_message(stream: &mut TcpStream, argb_buffer: &mut Vec<u32>, width: u32) -> Option<Message> {
|
||||
let message_type = stream.read_u32::<LittleEndian>().expect("fuck");
|
||||
let message_len = stream.read_u32::<LittleEndian>().expect("fuck");
|
||||
|
||||
|
@ -35,19 +35,37 @@ fn read_message(stream: &mut TcpStream, argb_buffer: &mut Vec<u32>) -> Option<Me
|
|||
}
|
||||
|
||||
MESSAGETYPE_DATA => {
|
||||
argb_buffer.resize((message_len / 4) as usize, 0);
|
||||
let tile_count = stream.read_u32::<LittleEndian>().expect("fuck");
|
||||
|
||||
unsafe {
|
||||
let slice = std::slice::from_raw_parts_mut(
|
||||
argb_buffer.as_mut_ptr() as *mut u8,
|
||||
argb_buffer.len() * core::mem::size_of::<u32>(),
|
||||
);
|
||||
stream.read_exact(&mut slice[..]).expect("FUCK");
|
||||
println!("{tile_count} tiles");
|
||||
|
||||
for i in 0..tile_count {
|
||||
// tile rect
|
||||
let tile_x = stream.read_u32::<LittleEndian>().expect("fuck");
|
||||
let tile_y = stream.read_u32::<LittleEndian>().expect("fuck");
|
||||
let tile_width = stream.read_u32::<LittleEndian>().expect("fuck");
|
||||
let tile_height = stream.read_u32::<LittleEndian>().expect("fuck");
|
||||
|
||||
//println!("tile{i}: {tile_x} {tile_y} {tile_width}x{tile_height}");
|
||||
|
||||
// tile data, painted directly onto the argb buffer. It's stupid
|
||||
let argb_slice = unsafe {
|
||||
std::slice::from_raw_parts_mut(
|
||||
argb_buffer.as_mut_ptr() as *mut u8,
|
||||
argb_buffer.len() * core::mem::size_of::<u32>(),
|
||||
)
|
||||
};
|
||||
|
||||
for y in tile_y..tile_y + tile_height {
|
||||
//println!("{y} {tile_y} {tile_height}");
|
||||
//for x in tile_
|
||||
let dest_slice = &mut argb_slice[((y * width + tile_x) * 4) as usize
|
||||
..((y * width + (tile_x + tile_width)) * 4) as usize];
|
||||
|
||||
stream.read_exact(&mut dest_slice[..]).expect("FUCK");
|
||||
}
|
||||
}
|
||||
|
||||
//let mut data = Vec::new();
|
||||
//data.resize(message_len as usize, 0);
|
||||
//stream.read_exact(&mut data[..]).expect("FUCK");
|
||||
|
||||
Some(Message::Data {})
|
||||
}
|
||||
|
@ -70,12 +88,9 @@ fn main() {
|
|||
let mut screen_height: u32 = 200;
|
||||
|
||||
loop {
|
||||
if let Some(message) = read_message(&mut socket, &mut argb_buffer) {
|
||||
if let Some(message) = read_message(&mut socket, &mut argb_buffer, screen_width) {
|
||||
match message {
|
||||
Message::Resize {
|
||||
width,
|
||||
height,
|
||||
} => {
|
||||
Message::Resize { width, height } => {
|
||||
println!("read message {:?}", message);
|
||||
screen_width = width;
|
||||
screen_height = height;
|
||||
|
@ -87,10 +102,18 @@ fn main() {
|
|||
WindowOptions::default(),
|
||||
)
|
||||
.expect("you banned forever: rules do not");
|
||||
|
||||
argb_buffer.resize((screen_width * screen_height) as usize, 0);
|
||||
}
|
||||
Message::Data {} => {
|
||||
//println!("read DATA");
|
||||
window.update_with_buffer(&argb_buffer[..], screen_width as usize, screen_height as usize).expect("Failed to update screen");
|
||||
window
|
||||
.update_with_buffer(
|
||||
&argb_buffer[..],
|
||||
screen_width as usize,
|
||||
screen_height as usize,
|
||||
)
|
||||
.expect("Failed to update screen");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue