Skip to content

Commit

Permalink
Closed #183: Ported Visualizer from to gfx-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ozkriff committed Jul 29, 2016
1 parent c8b11f4 commit 6bdb2c2
Show file tree
Hide file tree
Showing 58 changed files with 988 additions and 1,801 deletions.
276 changes: 187 additions & 89 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Expand Up @@ -6,14 +6,14 @@ authors = ["ozkriff <ozkriffgmail.com>"]
description = "ZoC is turn-based hexagonal strategy game written in Rust"
readme = "README.md"
license = "MIT OR Apache-2.0"
keywords = ["opengl", "3D", "game"]
keywords = ["opengl", "3D", "game", "gfx"]
repository = "https://github.com/ozkriff/zoc"

[[bin]]
name = "zoc"

[target.arm-linux-androideabi.dependencies]
android_glue = "0.1.3"

[dependencies.visualizer]
path = "./src/visualizer/"

[package.metadata.android]
assets = "assets"
11 changes: 5 additions & 6 deletions Makefile
Expand Up @@ -14,16 +14,15 @@ run: assets
assets:
git clone --depth=1 https://github.com/ozkriff/zoc_assets assets

ANDROID_APP_NAME = com.example.native_activity/android.app.NativeActivity
APK = ./target/android-artifacts/build/bin/zoc-debug.apk

android: assets
cargo build --target arm-linux-androideabi --release
cargo apk

android_run: android
cp target/arm-linux-androideabi/release/zoc target/arm-linux-androideabi/release/zoc.apk
adb install -r target/arm-linux-androideabi/release/zoc.apk
adb install -r $(APK)
adb logcat -c
adb shell am start -n $(ANDROID_APP_NAME)
adb logcat -v time | grep 'RustAndroidGlue\|native-activity'
adb shell am start -n rust.zoc/rust.zoc.MainActivity
adb logcat -v time | grep 'Rust\|DEBUG'

.PHONY: zoc run android android_run test
10 changes: 1 addition & 9 deletions README.rst
Expand Up @@ -52,15 +52,7 @@ Android
For instructions on setting up your environment see
https://github.com/tomaka/android-rs-glue#setting-up-your-environment.

Build apk-builder with ``assets_hack`` feature:

``cargo build --features assets_hack``

Make sure that path in `.cargo/config` is correct.

Then just: ``make android_run`` (rust-nightly is required).

(Tested on nexus7/android6)
Then just: ``make android_run`` - this will build .apk, install and run it.


Contribute
Expand Down
14 changes: 0 additions & 14 deletions src/common/Cargo.toml

This file was deleted.

12 changes: 0 additions & 12 deletions src/common/src/lib.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/core/Cargo.toml
Expand Up @@ -8,10 +8,6 @@ authors = ["ozkriff <ozkriffgmail.com>"]
name = "core"
doctest = false

[dependencies.common]
path = "../common"

[dependencies]
cgmath = "*"
rand = "*"
num = "*"
2 changes: 1 addition & 1 deletion src/core/src/ai.rs
@@ -1,6 +1,6 @@
// See LICENSE file for copyright and license details.

use common::types::{Size2};
use types::{Size2};
use game_state::{GameState, GameStateMut};
use partial_state::{PartialState};
use map::{distance};
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/db.rs
@@ -1,6 +1,6 @@
// See LICENSE file for copyright and license details.

use common::types::{ZInt};
use types::{ZInt};
use unit::{UnitType, WeaponType, UnitClass, UnitTypeId, WeaponTypeId};
use ::{MovePoints, AttackPoints};

Expand Down
2 changes: 1 addition & 1 deletion src/core/src/dir.rs
@@ -1,7 +1,7 @@
// See LICENSE file for copyright and license details.

use cgmath::{Vector2};
use common::types::{ZInt};
use types::{ZInt};
use ::{MapPos};

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
5 changes: 2 additions & 3 deletions src/core/src/fov.rs
Expand Up @@ -3,9 +3,8 @@
/// Fielf of View

use std::f32::consts::{PI};
use num::{Float};
use cgmath::{EuclideanVector};
use common::types::{ZInt, ZFloat};
use types::{ZInt, ZFloat};
use map::{Map, Terrain, distance, spiral_iter};
use geom;
use ::{MapPos};
Expand Down Expand Up @@ -49,7 +48,7 @@ pub fn fov(
let pos3d = geom::map_pos_to_world_pos(&pos);
let diff = pos3d - origin3d;
let distance = diff.magnitude();
let angle = Float::atan2(diff.x, diff.y); // TODO: optimize
let angle = diff.x.atan2(diff.y); // TODO: optimize
if is_tile_visible(angle, &shadows) {
callback(&pos);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/fow.rs
@@ -1,7 +1,7 @@
// See LICENSE file for copyright and license details.

use std::default::{Default};
use common::types::{Size2, ZInt};
use types::{Size2, ZInt};
use internal_state::{InternalState};
use game_state::{GameState};
use map::{Map, Terrain, distance};
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/geom.rs
@@ -1,7 +1,7 @@
// See LICENSE file for copyright and license details.

use cgmath::{Vector2};
use common::types::{ZFloat};
use types::{ZFloat};
use ::{MapPos};

pub const HEX_EX_RADIUS: ZFloat = 1.4;
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/internal_state.rs
Expand Up @@ -2,7 +2,7 @@

use std::collections::{HashMap};
use cgmath::{Vector2};
use common::types::{ZInt, Size2};
use types::{ZInt, Size2};
use unit::{Unit};
use db::{Db};
use map::{Map, Terrain};
Expand Down
8 changes: 4 additions & 4 deletions src/core/src/lib.rs
@@ -1,9 +1,7 @@
// See LICENSE file for copyright and license details.

extern crate num;
extern crate cgmath;
extern crate rand;
extern crate common;

pub mod geom;
pub mod map;
Expand All @@ -13,6 +11,8 @@ pub mod dir;
pub mod partial_state;
pub mod game_state;
pub mod pathfinder;
pub mod misc;
pub mod types;

mod ai;
mod fov;
Expand All @@ -24,8 +24,8 @@ use rand::{thread_rng, Rng};
use std::{cmp, fmt};
use std::collections::{HashMap, HashSet, LinkedList};
use cgmath::{Vector2};
use common::types::{Size2, ZInt};
use common::misc::{clamp};
use types::{Size2, ZInt};
use misc::{clamp};
use internal_state::{InternalState};
use game_state::{GameState, GameStateMut};
use partial_state::{PartialState};
Expand Down
5 changes: 2 additions & 3 deletions src/core/src/map.rs
Expand Up @@ -2,9 +2,8 @@

use std::default::{Default};
use std::iter::{repeat};
use num::{Float};
use cgmath::{Vector2, Array, Vector};
use common::types::{Size2, ZInt};
use cgmath::{Vector2, Array};
use types::{Size2, ZInt};
use dir::{Dir, DirIter, dirs};
use ::{MapPos};

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/core/src/partial_state.rs
@@ -1,7 +1,7 @@
// See LICENSE file for copyright and license details.

use std::collections::{HashMap};
use common::types::{Size2};
use types::{Size2};
use unit::{Unit};
use db::{Db};
use map::{Map, Terrain};
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/pathfinder.rs
@@ -1,7 +1,7 @@
// See LICENSE file for copyright and license details.

use std::default::{Default};
use common::types::{ZInt, Size2};
use types::{ZInt, Size2};
use db::{Db};
use unit::{Unit, UnitClass};
use map::{Map, Terrain};
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/core/src/unit.rs
@@ -1,6 +1,6 @@
// See LICENSE file for copyright and license details.

use common::types::{ZInt};
use types::{ZInt};
use ::{ReactionFireMode, MovePoints, AttackPoints, UnitId, PlayerId, ExactPos};

#[derive(PartialOrd, Ord, PartialEq, Eq, Hash, Clone)]
Expand Down
7 changes: 0 additions & 7 deletions src/main.rs
@@ -1,16 +1,9 @@
// See LICENSE file for copyright and license details.

#[cfg(target_os = "android")]
#[macro_use]
extern crate android_glue;

extern crate visualizer;

use visualizer::{Visualizer};

#[cfg(target_os = "android")]
android_start!(main);

pub fn main() {
let mut visualizer = Visualizer::new();
while visualizer.is_running() {
Expand Down
17 changes: 10 additions & 7 deletions src/visualizer/Cargo.toml
Expand Up @@ -11,16 +11,19 @@ doctest = false
[dependencies.core]
path = "../core/"

[dependencies.common]
path = "../common/"

[dependencies.zgl]
path = "../zgl/"

[dependencies]
gfx_core = "*"
gfx_device_gl = "*"
gfx_window_glutin = "*"
gfx = "*"
glutin = "*"
collision = "*"
cgmath = "*"
time = "*"
rand = "*"
num = "*"
rusttype = "*"

[dependencies.image]
version = "*"
default-features = false
features = ["png_codec"]
36 changes: 16 additions & 20 deletions src/zgl/src/camera.rs → src/visualizer/src/camera.rs
@@ -1,11 +1,9 @@
// See LICENSE file for copyright and license details.

use std::f32::consts::{PI};
use cgmath::{perspective, rad, Matrix4, Vector3, Rad, Array};
use common::types::{Size2, ZFloat};
use common::misc::{clamp};
use types::{WorldPos};
use ::{Zgl};
use cgmath::{perspective, rad, Matrix4, Matrix3, Vector3, Rad, Array};
use core::misc::{clamp};
use types::{ZFloat, WorldPos, Size2};

pub struct Camera {
x_angle: Rad<ZFloat>,
Expand All @@ -21,29 +19,27 @@ fn get_projection_mat(win_size: &Size2) -> Matrix4<ZFloat> {
let ratio = win_size.w as ZFloat / win_size.h as ZFloat;
let display_range_min = 0.1;
let display_range_max = 100.0;
perspective(
fov, ratio, display_range_min, display_range_max)
perspective(fov, ratio, display_range_min, display_range_max)
}

impl Camera {
pub fn new(win_size: &Size2) -> Camera {
Camera {
x_angle: rad(PI / 4.0),
z_angle: rad(0.0),
z_angle: rad(PI / 4.0),
pos: WorldPos{v: Vector3::from_value(0.0)},
max_pos: WorldPos{v: Vector3::from_value(0.0)},
zoom: 20.0,
zoom: 15.0,
projection_mat: get_projection_mat(win_size),
}
}

pub fn mat(&self, zgl: &Zgl) -> Matrix4<ZFloat> {
let m = self.projection_mat;
let m = zgl.tr(m, &Vector3{x: 0.0, y: 0.0, z: -self.zoom});
let m = zgl.rot_x(m, &-self.x_angle);
let m = zgl.rot_z(m, &-self.z_angle);
let m = zgl.tr(m, &self.pos.v);
m
pub fn mat(&self) -> Matrix4<ZFloat> {
let zoom_m = Matrix4::from_translation(Vector3{x: 0.0, y: 0.0, z: -self.zoom});
let x_angle_m = Matrix4::from(Matrix3::from_angle_x(-self.x_angle));
let z_angle_m = Matrix4::from(Matrix3::from_angle_z(-self.z_angle));
let tr_m = Matrix4::from_translation(self.pos.v);
self.projection_mat * zoom_m * x_angle_m * z_angle_m * tr_m
}

pub fn add_horizontal_angle(&mut self, angle: Rad<ZFloat>) {
Expand Down Expand Up @@ -82,12 +78,12 @@ impl Camera {
self.zoom = clamp(self.zoom, 10.0, 40.0);
}

pub fn get_z_angle(&self) -> &Rad<ZFloat> {
&self.z_angle
pub fn get_z_angle(&self) -> Rad<ZFloat> {
self.z_angle
}

pub fn get_x_angle(&self) -> &Rad<ZFloat> {
&self.x_angle
pub fn get_x_angle(&self) -> Rad<ZFloat> {
self.x_angle
}

// TODO: rename to 'move'
Expand Down

0 comments on commit 6bdb2c2

Please sign in to comment.