Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Code you wrote [Part II]

Name: Anonymous 2014-09-18 3:37

All I've worked on tonight is this dumb raycaster: http://hastebin.com/iyerililov.rs

I screwed up the projection something awful and I have no idea how, but otherwise it sort of works.

To compile: Install a recent Rust+Cargo nightly, save this file http://hastebin.com/huyobaripa.toml as "Cargo.toml," put the Rust source in "src/ray.rs," and type "cargo run." It will automatically download and build the dependencies.

Name: Anonymous 2014-09-18 3:42

>>1

#![feature(globs)]

extern crate sdl2;
extern crate native;
extern crate nalgebra;

use sdl2::video::{Window, PosCentered, OpenGL};
use sdl2::event::{QuitEvent, NoEvent, KeyDownEvent, KeyUpEvent, poll_event};

use sdl2::keycode::*;
use sdl2::pixels::{RGB};
use sdl2::rect::{Rect};
use sdl2::surface::{Surface};

use nalgebra::na::{Vec1, Vec2, Rot2};

Name: Anonymous 2014-09-18 10:57

>>1
anonymous gist please

Name: Anonymous 2014-09-18 18:00

>>3
All right. I fixed the projection and added some stuff so it needed updating anyway: https://gist.github.com/anonymous/8e76062cded0fe2001a6

Name: Anonymous 2014-09-19 1:42

let dest = self.player.pos + if self.keys[0] {
self.player.dir * walk_speed
} else if self.keys[1] {
self.player.dir * walk_speed * -0.5f64
} else if self.keys[2] {
self.right * walk_speed
} else if self.keys[3] {
self.right * walk_speed * -1.0f64
} else {
Vec2 { x: 0.0f64, y: 0.0 }
};


This could do with a match statement:

let dest = match self.keys {
[true,_,_,_, _,_,_,_] => self.player.dir * walk_speed,
[_,true,_,_, _,_,_,_] => ...,
...
}


Something similar in the playpen to prove it works:http://play.rust-lang.org/?code=%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20keys%20%3D%20%5Bfalse%2C%20true%2C%20false%2C%20true%5D%3B%0A%20%20%20%20let%20state%20%3D%2010f32%3B%0A%20%20%20%20let%20add%20%3D%200.5f32%3B%0A%20%20%20%20%0A%20%20%20%20let%20newstate%20%3D%20match%20keys%20%7B%0A%20%20%20%20%20%20%20%20%5Btrue%2C_%2C_%2C_%5D%20%3D%3E%20state%20%2B%20add%2C%0A%20%20%20%20%20%20%20%20%5B_%2Ctrue%2C_%2C_%5D%20%3D%3E%20state%20-%20add%2C%0A%20%20%20%20%20%20%20%20_%20%3D%3E%200f32%2C%20%2F%2F%20you%20get%20the%20idea%0A%20%20%20%20%7D%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20newstate)%3B%0A%7D

Name: Anonymous 2014-09-19 1:57

>>5

Symta:
Dest = case Self.keys
[1 @_] Self.player.dir * walk_speed
[_ 1 @_] ...


Rust:
let dest = match self.keys {
[true,_,_,_, _,_,_,_] => self.player.dir * walk_speed,
[_,true,_,_, _,_,_,_] => ...,
...
}

Name: Anonymous 2014-09-19 2:36

>>6
There are better ways, but that would involve a deeper changes. You might even be able to match on something like [true, ..], I haven't checked. I'm not fond of the drift it would create.

A fairer comparison:
$ rustc >/dev/null 2>&1 && echo yep || echo nope
yep
$ symta >/dev/null 2>&1 && echo yep || echo nope
nope

Name: Anonymous 2014-09-19 4:23

>>7
Last login: Thu Sep 18 12:45:17 on ttys002
$ rustc >/dev/null 2>&1 && echo yep || echo nope
nope
$ uname -a
Darwin snv-mbp.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64
$

Name: Anonymous 2014-09-19 4:29

>>7

$ symta >/dev/null 2>&1 && echo yep || echo nope
Symta is now self-hosting, but I'm too lazy to produce a compiler that works outside of my box.

Anyone who cares can bootstrap the interactive compiler, using CL and GCC (clang fails for some reason)
https://github.com/saniv/symta/blob/master/utils/sint/src/main.s

Name: Anonymous 2014-09-19 12:57

>>8
Have fun with that!

>>9
Thanks, I was just goading you into providing a link this time.

Name: Anonymous 2014-09-19 14:07

>>5
I like this change, but I haven't decided whether I'll add more keybindings later, and it's not terribly semantic anyway, so I'll probably break up that big array into several smaller ones containing only the mutually exclusive sets (ie. one array for W and S, and others for A/D, left/right turning, jump/crouch, etc.) and pattern match on each of them separately.

Not gonna lie, I wrote some pretty awful code there. The whole thing is in need of major refactoring and optimization. You could probably halve the line count and quadruple the frame rate if you knew what you were doing — as opposed to me, who had never written anything in Rust, or used SDL directly in any way, before slamming this together in a day and a half.

Name: Anonymous 2014-09-19 17:31

>>11
It's not too bad really. There are a lot of things I would do different, but I don't have a whole lot to actually complain about.

I noticed some other things:
let mut surface = match window.get_surface() {
Ok(s) => s,
Err(err) => fail!("Could not get surface for window: {}", err)
};


Consider:
let mut surface = window.get_surface()
.unwrap_or_else(|err| fail!("Could not get surface for window: {}", err));


This is a matter of taste. There was a macro for making monadic wrapping of Result easier, but I think it's gone now.

See: http://play.rust-lang.org/?code=%0Afn%20main()%20%7B%0A%20%20%20%20let%20foo%3A%20Result%3Cint%2C%26str%3E%20%3D%20Err(%22shit%22)%3B%0A%20%20%20%20foo.unwrap_or_else(%7Ce%7C%20fail!(%22fuck%3A%20%7B%7D%22%2C%20e))%3B%0A%7D

Same goes for Option, but if you just want to fail (common with Option), just call .unwrap(). Note: try not to use .unwrap() if you can handle the error.

This is kind of ugly:
for x in range(0, 16) {
for y in range(0, 16) {


but there's nothing you can do. If it showed up more than a few times in your code, I'd suggest writing a .cartesean() iterator method, but that's tricky to write (and should be in the standard library for that reason.)

I do recommend paying special attention to iterators. It's more readable when you have a named method that does something to an entire iterator, than a for-loop that needs digesting. You don't need to use for-loops at all if you are just transforming data, let bar = foo.map(|| …).collect() or whatever.

You can iterate Options and Results too: http://play.rust-lang.org/?code=%0Afn%20main()%20%7B%0A%20%20%20%20let%20foo%3A%20Result%3Cint%2C%26str%3E%20%3D%20Ok(1)%3B%20%2F%2F%20Err(%22shit%22)%3B%0A%20%20%20%20for%20x%20in%20foo.iter()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%20%20%20%20%7D%0A%7D

(Or you can just call .map()/map_err() without the .iter() because monads.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List