Browse Source

getting simpler version working

Inderjit Gill 4 months ago
parent
commit
5a5b2775d9
10 changed files with 97 additions and 51 deletions
  1. 20
    3
      Cargo.toml
  2. 6
    6
      src/colour.rs
  3. 1
    9
      src/error.rs
  4. 12
    11
      src/game.rs
  5. 4
    4
      src/geometry.rs
  6. 13
    6
      src/lib.rs
  7. 1
    1
      src/text.rs
  8. 1
    1
      src/units.rs
  9. 32
    0
      src/utils.rs
  10. 7
    10
      www/index.js

+ 20
- 3
Cargo.toml View File

@@ -2,13 +2,30 @@
2 2
 name = "wasm_tetris"
3 3
 version = "0.1.0"
4 4
 authors = ["Inderjit Gill <inderjit.gill@gmail.com>"]
5
+edition = "2018"
5 6
 
6 7
 [lib]
7
-crate-type = ["cdylib"]
8
+crate-type = ["cdylib", "rlib"]
9
+
10
+[features]
11
+default = ["console_error_panic_hook"]
8 12
 
9 13
 [dependencies]
10
-wasm-bindgen = "0.2.17"
11
-failure = "0.1.2"
14
+cfg-if = "0.1.2"
15
+wasm-bindgen = "0.2"
16
+
17
+# The `console_error_panic_hook` crate provides better debugging of panics by
18
+# logging them with `console.error`. This is great for development, but requires
19
+# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
20
+# code size when deploying.
21
+console_error_panic_hook = { version = "0.1.1", optional = true }
22
+
23
+# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
24
+# compared to the default allocator's ~10K. It is slower than the default
25
+# allocator, however.
26
+#
27
+# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
28
+wee_alloc = { version = "0.4.2", optional = true }
12 29
 
13 30
 [profile.release]
14 31
 # Include function names in the `.wasm` for better debugging and

+ 6
- 6
src/colour.rs View File

@@ -7,14 +7,14 @@
7 7
 // | LAB    | L 0..100  | A -128..128 | B -128..128 |
8 8
 // |--------+-----------+-------------+-------------|
9 9
 
10
-use error;
10
+use crate::error;
11 11
 use std;
12 12
 
13
-use atan2;
14
-use cos;
15
-use pow;
16
-use sin;
17
-use sqrt;
13
+use crate::atan2;
14
+use crate::cos;
15
+use crate::pow;
16
+use crate::sin;
17
+use crate::sqrt;
18 18
 
19 19
 const REF_U: f64 = 0.197_830_006_642_836_807_64;
20 20
 const REF_V: f64 = 0.468_319_994_938_791_003_70;

+ 1
- 9
src/error.rs View File

@@ -1,29 +1,21 @@
1 1
 /// A specialized `Result` type for the `skeleton` crate.
2 2
 pub type Result<T> = ::std::result::Result<T, TetrisError>;
3 3
 
4
-#[derive(Debug, Fail)]
4
+#[derive(Debug)]
5 5
 pub enum TetrisError {
6
-    #[fail(display = "Line Removal")]
7 6
     LineRemoval,
8 7
 
9
-    #[fail(display = "Invalid Random Colour Index")]
10 8
     InvalidRandomColourIndex,
11 9
 
12
-    #[fail(display = "Invalid Random Shape Index")]
13 10
     InvalidRandomShapeIndex,
14 11
 
15
-    #[fail(display = "Invalid Colour Format")]
16 12
     InvalidColourFormat,
17 13
 
18
-    #[fail(display = "General")]
19 14
     GeneralError,
20 15
 
21
-    #[fail(display = "IncorrectFormat")]
22 16
     IncorrectFormat,
23 17
 
24
-    #[fail(display = "InvalidHue")]
25 18
     InvalidHue,
26 19
 
27
-    #[fail(display = "InvalidChannel")]
28 20
     InvalidChannel,
29 21
 }

+ 12
- 11
src/game.rs View File

@@ -1,14 +1,15 @@
1
-use audio_play;
2
-use audio_volume;
3
-use colour::{Colour, Format};
4
-use controller::{Controller, ControllerButton};
5
-use error;
6
-use geometry::Geometry;
7
-use log;
8
-use sprite::Sprite;
1
+use crate::audio_play;
2
+use crate::audio_volume;
3
+use crate::colour::{Colour, Format};
4
+use crate::controller::{Controller, ControllerButton};
5
+use crate::error;
6
+use crate::geometry::Geometry;
7
+use crate::log;
8
+use crate::sprite::Sprite;
9
+use crate::units::*;
10
+use crate::Config;
11
+
9 12
 use std::collections::HashMap;
10
-use units::*;
11
-use Config;
12 13
 
13 14
 #[derive(PartialEq)]
14 15
 pub enum GameMode {
@@ -1101,7 +1102,7 @@ fn get_board_positions(shape: &Shape, pos: &BoardPos, angle: &PieceAngle) -> Vec
1101 1102
 }
1102 1103
 
1103 1104
 fn update_block_colours(board: &mut Board) {
1104
-    for mut row in &mut board.board {
1105
+    for row in &mut board.board {
1105 1106
         for mut block in row {
1106 1107
             if !block.occupied {
1107 1108
                 block.hsl = move_cached_col_closer(

+ 4
- 4
src/geometry.rs View File

@@ -1,7 +1,7 @@
1
-use sprite::{get_sprite_location, Sprite};
2
-use text::sprite_location_from_char;
3
-use units::*;
4
-use Config;
1
+use crate::sprite::{get_sprite_location, Sprite};
2
+use crate::text::sprite_location_from_char;
3
+use crate::units::*;
4
+use crate::Config;
5 5
 
6 6
 struct UV {
7 7
     u: f32,

+ 13
- 6
src/lib.rs View File

@@ -1,14 +1,10 @@
1 1
 #![allow(dead_code)]
2
-#![feature(use_extern_macros)]
3 2
 #![cfg_attr(
4 3
     feature = "cargo-clippy",
5
-    allow(many_single_char_names, too_many_arguments)
4
+    allow(clippy::many_single_char_names, clippy::too_many_arguments)
6 5
 )]
7 6
 
8
-#[macro_use]
9
-extern crate failure;
10
-extern crate wasm_bindgen;
11
-
7
+use cfg_if::cfg_if;
12 8
 use wasm_bindgen::prelude::*;
13 9
 
14 10
 mod colour;
@@ -19,11 +15,22 @@ mod geometry;
19 15
 mod sprite;
20 16
 mod text;
21 17
 mod units;
18
+mod utils;
22 19
 
23 20
 use controller::{Controller, ControllerAction, ControllerButton};
24 21
 use game::Game;
25 22
 use geometry::Geometry;
26 23
 
24
+cfg_if! {
25
+    // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
26
+    // allocator.
27
+    if #[cfg(feature = "wee_alloc")] {
28
+        extern crate wee_alloc;
29
+        #[global_allocator]
30
+        static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
31
+    }
32
+}
33
+
27 34
 #[wasm_bindgen]
28 35
 extern "C" {
29 36
     #[wasm_bindgen(js_namespace = Math)]

+ 1
- 1
src/text.rs View File

@@ -1,4 +1,4 @@
1
-use sprite::SpriteLocation;
1
+use crate::sprite::SpriteLocation;
2 2
 
3 3
 // note: these spritelocation values are for letters which have half the width of regular sprites
4 4
 

+ 1
- 1
src/units.rs View File

@@ -1,4 +1,4 @@
1
-use log;
1
+use crate::log;
2 2
 
3 3
 #[derive(Debug, Clone, Copy)]
4 4
 pub struct Col {

+ 32
- 0
src/utils.rs View File

@@ -0,0 +1,32 @@
1
+// Copyright (C) 2019 Inderjit Gill
2
+
3
+// This program is free software: you can redistribute it and/or modify
4
+// it under the terms of the GNU General Public License as published by
5
+// the Free Software Foundation, either version 3 of the License, or
6
+// (at your option) any later version.
7
+
8
+// This program is distributed in the hope that it will be useful,
9
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+// GNU General Public License for more details.
12
+
13
+// You should have received a copy of the GNU General Public License
14
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
+
16
+use cfg_if::cfg_if;
17
+
18
+cfg_if! {
19
+    // When the `console_error_panic_hook` feature is enabled, we can call the
20
+    // `set_panic_hook` function at least once during initialization, and then
21
+    // we will get better error messages if our code ever panics.
22
+    //
23
+    // For more details see
24
+    // https://github.com/rustwasm/console_error_panic_hook#readme
25
+    if #[cfg(feature = "console_error_panic_hook")] {
26
+        extern crate console_error_panic_hook;
27
+        pub use self::console_error_panic_hook::set_once as set_panic_hook;
28
+    } else {
29
+        #[inline]
30
+        pub fn set_panic_hook() {}
31
+    }
32
+}

+ 7
- 10
www/index.js View File

@@ -1,4 +1,4 @@
1
-const { Bridge, BridgeConfig } = wasm_bindgen;
1
+const { Bridge, Config } = wasm_bindgen;
2 2
 let memory = undefined;
3 3
 
4 4
 function matrixCreate() {
@@ -370,8 +370,8 @@ class GLRenderer {
370 370
     this.glColourBuffer = gl.createBuffer();
371 371
     this.glTextureBuffer = gl.createBuffer();
372 372
 
373
-    this.mvMatrix = Matrix.create();
374
-    this.pMatrix = Matrix.create();
373
+    this.mvMatrix = matrixCreate();
374
+    this.pMatrix = matrixCreate();
375 375
     // Matrix.ortho(this.pMatrix, 0, config.canvas_width, 0, config.canvas_height, 10, -10);
376 376
 
377 377
     this.renderTexture = createRenderTexture(gl, config);
@@ -426,7 +426,7 @@ class GLRenderer {
426 426
 
427 427
 
428 428
     // render the entirety of the scene
429
-    Matrix.ortho(this.pMatrix, 0, destTextureWidth, 0, destTextureHeight, 10, -10);
429
+    matrixOrtho(this.pMatrix, 0, destTextureWidth, 0, destTextureHeight, 10, -10);
430 430
 
431 431
     gl.uniformMatrix4fv(shader.pMatrixLocation,
432 432
                         false,
@@ -492,13 +492,10 @@ class GLRenderer {
492 492
     gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
493 493
 
494 494
     // render the entirety of the scene
495
-    Matrix.ortho(this.pMatrix, 0, canvasWidth, 0, canvasHeight, 10, -10);
496
-
495
+    matrixOrtho(this.pMatrix, 0, canvasWidth, 0, canvasHeight, 10, -10);
497 496
 
498 497
     // add some uniforms for canvas width and height
499 498
 
500
-
501
-
502 499
     gl.uniformMatrix4fv(shader.pMatrixLocation,
503 500
                         false,
504 501
                         this.pMatrix);
@@ -605,8 +602,6 @@ class Audio {
605 602
 let gState = {
606 603
   canvasId: 'render-canvas',
607 604
 
608
-  config: new Config(),
609
-
610 605
   tileset: 'img/tileset.png',
611 606
 
612 607
   bridge: undefined,
@@ -716,6 +711,8 @@ function main() {
716 711
   //   in mapping the rendertexture onto the canvas
717 712
   //
718 713
 
714
+  gState.config = new Config();
715
+
719 716
   const canvasElement = document.getElementById(gState.canvasId);
720 717
 
721 718
   resize_canvas_element(canvasElement, gState.config);

Loading…
Cancel
Save