Browse Source

getting simpler version working

master
Inderjit Gill 8 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 @@
name = "wasm_tetris"
version = "0.1.0"
authors = ["Inderjit Gill <inderjit.gill@gmail.com>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.17"
failure = "0.1.2"
cfg-if = "0.1.2"
wasm-bindgen = "0.2"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }

[profile.release]
# Include function names in the `.wasm` for better debugging and

+ 6
- 6
src/colour.rs View File

@@ -7,14 +7,14 @@
// | LAB | L 0..100 | A -128..128 | B -128..128 |
// |--------+-----------+-------------+-------------|

use error;
use crate::error;
use std;

use atan2;
use cos;
use pow;
use sin;
use sqrt;
use crate::atan2;
use crate::cos;
use crate::pow;
use crate::sin;
use crate::sqrt;

const REF_U: f64 = 0.197_830_006_642_836_807_64;
const REF_V: f64 = 0.468_319_994_938_791_003_70;

+ 1
- 9
src/error.rs View File

@@ -1,29 +1,21 @@
/// A specialized `Result` type for the `skeleton` crate.
pub type Result<T> = ::std::result::Result<T, TetrisError>;

#[derive(Debug, Fail)]
#[derive(Debug)]
pub enum TetrisError {
#[fail(display = "Line Removal")]
LineRemoval,

#[fail(display = "Invalid Random Colour Index")]
InvalidRandomColourIndex,

#[fail(display = "Invalid Random Shape Index")]
InvalidRandomShapeIndex,

#[fail(display = "Invalid Colour Format")]
InvalidColourFormat,

#[fail(display = "General")]
GeneralError,

#[fail(display = "IncorrectFormat")]
IncorrectFormat,

#[fail(display = "InvalidHue")]
InvalidHue,

#[fail(display = "InvalidChannel")]
InvalidChannel,
}

+ 12
- 11
src/game.rs View File

@@ -1,14 +1,15 @@
use audio_play;
use audio_volume;
use colour::{Colour, Format};
use controller::{Controller, ControllerButton};
use error;
use geometry::Geometry;
use log;
use sprite::Sprite;
use crate::audio_play;
use crate::audio_volume;
use crate::colour::{Colour, Format};
use crate::controller::{Controller, ControllerButton};
use crate::error;
use crate::geometry::Geometry;
use crate::log;
use crate::sprite::Sprite;
use crate::units::*;
use crate::Config;

use std::collections::HashMap;
use units::*;
use Config;

#[derive(PartialEq)]
pub enum GameMode {
@@ -1101,7 +1102,7 @@ fn get_board_positions(shape: &Shape, pos: &BoardPos, angle: &PieceAngle) -> Vec
}

fn update_block_colours(board: &mut Board) {
for mut row in &mut board.board {
for row in &mut board.board {
for mut block in row {
if !block.occupied {
block.hsl = move_cached_col_closer(

+ 4
- 4
src/geometry.rs View File

@@ -1,7 +1,7 @@
use sprite::{get_sprite_location, Sprite};
use text::sprite_location_from_char;
use units::*;
use Config;
use crate::sprite::{get_sprite_location, Sprite};
use crate::text::sprite_location_from_char;
use crate::units::*;
use crate::Config;

struct UV {
u: f32,

+ 13
- 6
src/lib.rs View File

@@ -1,14 +1,10 @@
#![allow(dead_code)]
#![feature(use_extern_macros)]
#![cfg_attr(
feature = "cargo-clippy",
allow(many_single_char_names, too_many_arguments)
allow(clippy::many_single_char_names, clippy::too_many_arguments)
)]

#[macro_use]
extern crate failure;
extern crate wasm_bindgen;

use cfg_if::cfg_if;
use wasm_bindgen::prelude::*;

mod colour;
@@ -19,11 +15,22 @@ mod geometry;
mod sprite;
mod text;
mod units;
mod utils;

use controller::{Controller, ControllerAction, ControllerButton};
use game::Game;
use geometry::Geometry;

cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = Math)]

+ 1
- 1
src/text.rs View File

@@ -1,4 +1,4 @@
use sprite::SpriteLocation;
use crate::sprite::SpriteLocation;

// note: these spritelocation values are for letters which have half the width of regular sprites


+ 1
- 1
src/units.rs View File

@@ -1,4 +1,4 @@
use log;
use crate::log;

#[derive(Debug, Clone, Copy)]
pub struct Col {

+ 32
- 0
src/utils.rs View File

@@ -0,0 +1,32 @@
// Copyright (C) 2019 Inderjit Gill

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// 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 General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use cfg_if::cfg_if;

cfg_if! {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
if #[cfg(feature = "console_error_panic_hook")] {
extern crate console_error_panic_hook;
pub use self::console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}

+ 7
- 10
www/index.js View File

@@ -1,4 +1,4 @@
const { Bridge, BridgeConfig } = wasm_bindgen;
const { Bridge, Config } = wasm_bindgen;
let memory = undefined;

function matrixCreate() {
@@ -370,8 +370,8 @@ class GLRenderer {
this.glColourBuffer = gl.createBuffer();
this.glTextureBuffer = gl.createBuffer();

this.mvMatrix = Matrix.create();
this.pMatrix = Matrix.create();
this.mvMatrix = matrixCreate();
this.pMatrix = matrixCreate();
// Matrix.ortho(this.pMatrix, 0, config.canvas_width, 0, config.canvas_height, 10, -10);

this.renderTexture = createRenderTexture(gl, config);
@@ -426,7 +426,7 @@ class GLRenderer {


// render the entirety of the scene
Matrix.ortho(this.pMatrix, 0, destTextureWidth, 0, destTextureHeight, 10, -10);
matrixOrtho(this.pMatrix, 0, destTextureWidth, 0, destTextureHeight, 10, -10);

gl.uniformMatrix4fv(shader.pMatrixLocation,
false,
@@ -492,13 +492,10 @@ class GLRenderer {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

// render the entirety of the scene
Matrix.ortho(this.pMatrix, 0, canvasWidth, 0, canvasHeight, 10, -10);

matrixOrtho(this.pMatrix, 0, canvasWidth, 0, canvasHeight, 10, -10);

// add some uniforms for canvas width and height



gl.uniformMatrix4fv(shader.pMatrixLocation,
false,
this.pMatrix);
@@ -605,8 +602,6 @@ class Audio {
let gState = {
canvasId: 'render-canvas',

config: new Config(),

tileset: 'img/tileset.png',

bridge: undefined,
@@ -716,6 +711,8 @@ function main() {
// in mapping the rendertexture onto the canvas
//

gState.config = new Config();

const canvasElement = document.getElementById(gState.canvasId);

resize_canvas_element(canvasElement, gState.config);

Loading…
Cancel
Save