mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 10:38:17 +08:00
First commit
This commit is contained in:
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
.DS_Store
|
||||
.atom/
|
||||
.idea
|
||||
*.iml
|
||||
.packages
|
||||
.pub/
|
||||
packages
|
||||
pubspec.lock
|
||||
2
CHANGELOG.md
Normal file
2
CHANGELOG.md
Normal file
@ -0,0 +1,2 @@
|
||||
## [0.1.0]
|
||||
- First release, basic utilities
|
||||
9
README.md
Normal file
9
README.md
Normal file
@ -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/).
|
||||
30
lib/audio.dart
Normal file
30
lib/audio.dart
Normal file
@ -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<String, File> loadedFiles = new Map();
|
||||
|
||||
Future<ByteData> _loadAsset(String fileName) async {
|
||||
return await rootBundle.load('assets/audio/' + fileName);
|
||||
}
|
||||
|
||||
Future<File> load(String fileName) async {
|
||||
final file = new File('${(await getTemporaryDirectory()).path}/${fileName}');
|
||||
return await file.writeAsBytes((await _loadAsset(fileName)).buffer.asUint8List());
|
||||
}
|
||||
|
||||
Future<int> 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);
|
||||
}
|
||||
}
|
||||
38
lib/component.dart
Normal file
38
lib/component.dart
Normal file
@ -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) {}
|
||||
}
|
||||
13
lib/flame.dart
Normal file
13
lib/flame.dart
Normal file
@ -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();
|
||||
|
||||
}
|
||||
46
lib/game.dart
Normal file
46
lib/game.dart
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
17
lib/images.dart
Normal file
17
lib/images.dart
Normal file
@ -0,0 +1,17 @@
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui';
|
||||
import 'dart:async';
|
||||
|
||||
class Images {
|
||||
|
||||
Future<Image> load(String name) async {
|
||||
ByteData data = await rootBundle.load('assets/images/' + name);
|
||||
Uint8List bytes = new Uint8List.view(data.buffer);
|
||||
Completer<Image> completer = new Completer();
|
||||
decodeImageFromList(bytes, (image) {
|
||||
completer.complete(image);
|
||||
});
|
||||
return completer.future;
|
||||
}
|
||||
}
|
||||
24
lib/util.dart
Normal file
24
lib/util.dart
Normal file
@ -0,0 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:ui';
|
||||
|
||||
class Util {
|
||||
|
||||
Future<Size> 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<Size>(() {
|
||||
if (window.physicalSize.isEmpty) {
|
||||
var completer = new Completer<Size>();
|
||||
window.onMetricsChanged = () {
|
||||
if (!window.physicalSize.isEmpty) {
|
||||
completer.complete(window.physicalSize);
|
||||
}
|
||||
};
|
||||
return completer.future;
|
||||
}
|
||||
return window.physicalSize;
|
||||
});
|
||||
}
|
||||
}
|
||||
13
pubspec.yaml
Normal file
13
pubspec.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
name: flame
|
||||
description: A minimalist Flutter game engine
|
||||
version: 0.1.0
|
||||
author: Luan Nico <luannico27@gmail.com>
|
||||
homepage: https://github.com/luanpotter/flame
|
||||
|
||||
flutter:
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
audioplayers:
|
||||
path_provider:
|
||||
Reference in New Issue
Block a user