~sircmpwn/tetrominoes

a3235b016b04dbe7adf3983d5decab2f04d0f330 — Drew DeVault 2 years ago bd76fb4
Play background music
5 files changed, 148 insertions(+), 27 deletions(-)

M Makefile
M assets/rhapsody-in-blue.ogg
M main.ha
M notes.txt
M tetrominoes.ha
M Makefile => Makefile +1 -1
@@ 1,7 1,7 @@
.POSIX:
.SUFFIXES:

LIBS=-lc -lSDL2_image -lSDL2
LIBS=-lc -lSDL2_image -lSDL2_mixer -lSDL2

tetrominoes:
	hare build -o $@ $(LIBS)

M assets/rhapsody-in-blue.ogg => assets/rhapsody-in-blue.ogg +0 -0
M main.ha => main.ha +25 -3
@@ 1,8 1,10 @@
use fmt;
use fs;
use math::random;
use sdl2;
use sdl2::{renderer_flags, window_flags};
use sdl2::image;
use sdl2::mixer;
use time;
use types;



@@ 23,9 25,13 @@ type state = struct {
	run: bool,
	window: *sdl2::window,
	render: *sdl2::renderer,

	piece: texture,
	scene: texture,
	clear: texture,

	music: *mixer::chunk,

	input: input,
	board: board,



@@ 43,15 49,27 @@ export fn main() void = {
	match (run()) {
	case let err: sdl2::error =>
		fmt::fatal("SDL error: {}", sdl2::strerror(err));
	case let err: fs::error =>
		fmt::fatal("Error: {}", fs::strerror(err));
	case void => void;
	};
};

fn run() (void | sdl2::error) = {
	sdl2::init(sdl2::init_flags::VIDEO | sdl2::init_flags::GAMECONTROLLER);
fn run() (void | fs::error | sdl2::error) = {
	sdl2::init(sdl2::init_flags::VIDEO
		| sdl2::init_flags::AUDIO
		| sdl2::init_flags::GAMECONTROLLER)?;
	defer sdl2::quit();
	image::init(image::init_flags::PNG | image::init_flags::JPG);
	image::init(image::init_flags::PNG | image::init_flags::JPG)?;
	defer image::quit();
	mixer::init(mixer::init_flags::FLAC
		| mixer::init_flags::MP3
		| mixer::init_flags::OGG
		| mixer::init_flags::OPUS)?; // That should be plenty
	defer mixer::quit();
	mixer::open_audio(mixer::DEFAULT_FREQUENCY,
		mixer::DEFAULT_FORMAT, mixer::DEFAULT_CHANNELS, 1024)?;
	defer mixer::close_audio();

	const win = sdl2::create_window("Tetrominoes",
		sdl2::WINDOWPOS_UNDEFINED, sdl2::WINDOWPOS_UNDEFINED,


@@ 70,6 88,7 @@ fn run() (void | sdl2::error) = {
		piece = load_texture(render, "assets/piece.png")?,
		scene = load_texture(render, "assets/concert.png")?,
		clear = load_texture(render, "assets/clear.png")?,
		music = mixer::load_file("assets/rhapsody-in-blue.ogg")?,
		board = board {
			offs_x = 88,
			offs_y = 74,


@@ 82,6 101,9 @@ fn run() (void | sdl2::error) = {
	defer sdl2::destroy_texture(state.piece.tex);
	defer sdl2::destroy_texture(state.scene.tex);
	defer sdl2::destroy_texture(state.clear.tex);
	defer mixer::free_chunk(state.music);

	mixer::play_channel(0, state.music, 0)?;

	sdl2::set_texture_blend_mode(state.piece.tex, sdl2::blend_mode::BLEND)?;


M notes.txt => notes.txt +8 -4
@@ 1,16 1,17 @@
- tetromino swapping
- tetromino shuffling
- music
- sound effects
- level files & timings
- fix thumbsticks
- scorekeeping/display
- piece swapping
- keyboard input
- victory (+animation)
- music
- sound effects
- alternate clear animations

later:
- menu system
- key/controller re-bindings
- level files
- custom piece designs
- local multiplayer
- online multiplayer


@@ 21,3 22,6 @@ later:
	- fixed seed
- high scores (local, online)
- CRT shader?

https://tetris.fandom.com/wiki/Tetris_Guideline
https://tetris.fandom.com/wiki/SRS

M tetrominoes.ha => tetrominoes.ha +114 -19
@@ 2,42 2,137 @@
const tetrominoes: [_][][TETROMINO_LEN]color = [
	[
		// L
		[1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
		[0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
		[1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[
			0, 0, 1, 0,
			1, 1, 1, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 1, 0, 0,
			0, 1, 0, 0,
			0, 1, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 1, 1, 0,
			1, 0, 0, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 0, 0, 0,
			1, 0, 0, 0,
			1, 1, 0, 0,
			0, 0, 0, 0
		],
	],
	[
		// Reverse L
		[0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
		[1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
		[1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[
			1, 0, 0, 0,
			1, 1, 1, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 1, 0, 0,
			1, 0, 0, 0,
			1, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 1, 1, 0,
			0, 0, 1, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			0, 1, 0, 0,
			0, 1, 0, 0,
			1, 1, 0, 0,
			0, 0, 0, 0,
		],
	],
	[
		// Square
		[1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[
			1, 1, 0, 0,
			1, 1, 0, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
	],
	[
		// Zig-zag
		[1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
		[0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[
			0, 1, 1, 0,
			1, 1, 0, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 0, 0, 0,
			1, 1, 0, 0,
			0, 1, 0, 0,
			0, 0, 0, 0,
		],
	],
	[
		// Reverse zig-zag
		[0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
		[1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[
			1, 1, 0, 0,
			0, 1, 1, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			0, 1, 0, 0,
			1, 1, 0, 0,
			1, 0, 0, 0,
			0, 0, 0, 0,
		],
	],
	[
		// |-
		[1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
		[1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
		[0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[
			0, 1, 0, 0,
			1, 1, 1, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 0, 0, 0,
			1, 1, 0, 0,
			1, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 1, 1, 0,
			0, 1, 0, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			0, 1, 0, 0,
			1, 1, 0, 0,
			0, 1, 0, 0,
			0, 0, 0, 0,
		],
	],
	[
		// |
		[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
		[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[
			1, 1, 1, 1,
			0, 0, 0, 0,
			0, 0, 0, 0,
			0, 0, 0, 0,
		],
		[
			1, 0, 0, 0,
			1, 0, 0, 0,
			1, 0, 0, 0,
			1, 0, 0, 0,
		],
	],
];