commit 2756b35721af0795b11e36c016fe7c81babc96d6 Author: Luan Nico Date: Sun Oct 22 00:58:04 2017 -0200 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..cf75b78a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +.atom/ +.idea +*.iml +.packages +.pub/ +packages +pubspec.lock diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..ad8d8745b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +## [0.1.0] + - First release, basic utilities diff --git a/README.md b/README.md new file mode 100644 index 000000000..e90d5690d --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# flame + +A new flutter package project. + +## Getting Started + +For help getting started with Flutter, view our online [documentation](http://flutter.io/). + +For help on editing package code, view the [documentation](https://flutter.io/developing-packages/). diff --git a/lib/audio.dart b/lib/audio.dart new file mode 100644 index 000000000..a2f2c0acc --- /dev/null +++ b/lib/audio.dart @@ -0,0 +1,30 @@ +import 'package:flutter/services.dart' show rootBundle; + +import 'dart:async'; +import 'dart:typed_data'; +import 'dart:io'; + +import 'package:audioplayers/audioplayer.dart'; +import 'package:path_provider/path_provider.dart'; + +class Audio { + + Map loadedFiles = new Map(); + + Future _loadAsset(String fileName) async { + return await rootBundle.load('assets/audio/' + fileName); + } + + Future load(String fileName) async { + final file = new File('${(await getTemporaryDirectory()).path}/${fileName}'); + return await file.writeAsBytes((await _loadAsset(fileName)).buffer.asUint8List()); + } + + Future play(String fileName) async { + if (!loadedFiles.containsKey(fileName)) { + loadedFiles[fileName] = await load(fileName); + } + AudioPlayer audioPlayer = new AudioPlayer(); + return await audioPlayer.play(loadedFiles[fileName].path, isLocal: true); + } +} \ No newline at end of file diff --git a/lib/component.dart b/lib/component.dart new file mode 100644 index 000000000..2b1b3c1a0 --- /dev/null +++ b/lib/component.dart @@ -0,0 +1,38 @@ +import 'dart:ui'; +import 'dart:math'; + +import 'flame.dart'; + +abstract class Component { + + void update(double t); + void render(Canvas c); + +} + +abstract class SpriteComponent extends Component { + + double x, y, angle; + + double size; + Image image; + + final Paint paint = new Paint()..color = new Color(0xffffffff); + + SpriteComponent(this.size, String imagePath) { + Flame.images.load(imagePath).then((image) { this.image = image; }); + } + + render(Canvas canvas) { + canvas.translate(x, y); + canvas.rotate(PI /2 + angle); + canvas.translate(-size/2, -size/2); + if (image != null) { + Rect src = new Rect.fromLTWH(0.0, 0.0, image.width.toDouble(), image.height.toDouble()); + Rect dst = new Rect.fromLTWH(0.0, 0.0, size, size); + canvas.drawImageRect(image, src, dst, paint); + } + } + + update(double t) {} +} \ No newline at end of file diff --git a/lib/flame.dart b/lib/flame.dart new file mode 100644 index 000000000..6a8151e6b --- /dev/null +++ b/lib/flame.dart @@ -0,0 +1,13 @@ +library flame; + +import 'audio.dart'; +import 'images.dart'; +import 'util.dart'; + +class Flame { + + static Audio audio = new Audio(); + static Images images = new Images(); + static Util util = new Util(); + +} \ No newline at end of file diff --git a/lib/game.dart b/lib/game.dart new file mode 100644 index 000000000..7725fdaae --- /dev/null +++ b/lib/game.dart @@ -0,0 +1,46 @@ +import 'dart:ui'; +import 'dart:typed_data'; + +abstract class Game { + + void update(double t); + + void render(Canvas canvas); + + start() { + var previous = Duration.ZERO; + + window.onBeginFrame = (now) { + var recorder = new PictureRecorder(); + var canvas = new Canvas(recorder, new Rect.fromLTWH( + 0.0, 0.0, window.physicalSize.width, window.physicalSize.height)); + + Duration delta = now - previous; + if (previous == Duration.ZERO) { + delta = Duration.ZERO; + } + previous = now; + + var t = delta.inMicroseconds / Duration.MICROSECONDS_PER_SECOND; + + update(t); + render(canvas); + + var deviceTransform = new Float64List(16) + ..[0] = 1.0 // window.devicePixelRatio + ..[5] = 1.0 // window.devicePixelRatio + ..[10] = 1.0 + ..[15] = 1.0; + + var builder = new SceneBuilder() + ..pushTransform(deviceTransform) + ..addPicture(Offset.zero, recorder.endRecording()) + ..pop(); + + window.render(builder.build()); + window.scheduleFrame(); + }; + + window.scheduleFrame(); + } +} \ No newline at end of file diff --git a/lib/images.dart b/lib/images.dart new file mode 100644 index 000000000..5a69eac6b --- /dev/null +++ b/lib/images.dart @@ -0,0 +1,17 @@ +import 'package:flutter/services.dart' show rootBundle; +import 'dart:typed_data'; +import 'dart:ui'; +import 'dart:async'; + +class Images { + + Future load(String name) async { + ByteData data = await rootBundle.load('assets/images/' + name); + Uint8List bytes = new Uint8List.view(data.buffer); + Completer completer = new Completer(); + decodeImageFromList(bytes, (image) { + completer.complete(image); + }); + return completer.future; + } +} \ No newline at end of file diff --git a/lib/util.dart b/lib/util.dart new file mode 100644 index 000000000..f22b34685 --- /dev/null +++ b/lib/util.dart @@ -0,0 +1,24 @@ +import 'package:flutter/material.dart'; + +import 'dart:async'; +import 'dart:ui'; + +class Util { + + Future initialDimensions() async { + // https://github.com/flutter/flutter/issues/5259 + // "In release mode we start off at 0x0 but we don't in debug mode" + return await new Future(() { + if (window.physicalSize.isEmpty) { + var completer = new Completer(); + window.onMetricsChanged = () { + if (!window.physicalSize.isEmpty) { + completer.complete(window.physicalSize); + } + }; + return completer.future; + } + return window.physicalSize; + }); + } +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 000000000..5a4f9d976 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,13 @@ +name: flame +description: A minimalist Flutter game engine +version: 0.1.0 +author: Luan Nico +homepage: https://github.com/luanpotter/flame + +flutter: + +dependencies: + flutter: + sdk: flutter + audioplayers: + path_provider: \ No newline at end of file