Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public abstract class FlixelGame implements ApplicationListener {
/** Where all the global viewports are stored. */
protected SnapshotArray<FlixelViewport> viewports;

/** Is the game currently closing? */
private boolean isClosing = false;

/**
* Creates a new game instance with the specified title, window width/height, and initial screen. This configures
* the game's core parts, such as the viewport, stage, etc.
Expand Down Expand Up @@ -257,6 +260,12 @@ public void toggleFullscreen() {

@Override
public void dispose() {
if (isClosing) {
Flixel.warn("Game is already closing. Skipping dispose...");
return;
}
isClosing = true;

Flixel.warn("SHUTTING DOWN GAME AND DISPOSING ALL RESOURCES.");

Flixel.Signals.preGameClose.dispatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void update(float delta) {
}

/**
* Draws its members onto the screen.
* Draws {@code this} state's members onto the screen.
*
* @param batch The batch that's used to draw {@code this} state's members.
*/
Expand All @@ -71,8 +71,26 @@ public void resume() {}
@Override
public void hide() {}

/**
* Disposes {@code this} state and all of its members. Called automatically when {@link
* me.stringdotjar.flixelgdx.Flixel#switchState(FlixelState)} is used, so that sprites and other
* {@link FlixelObject}s release its resources.
*/
@Override
public void dispose() {}
public void dispose() {
if (members == null) {
return;
}
Object[] items = members.begin();
for (int i = 0, n = members.size; i < n; i++) {
FlixelObject obj = (FlixelObject) items[i];
if (obj != null) {
obj.destroy();
}
}
members.end();
members.clear();
}

/**
* Adds a new sprite to {@code this} screen. If it is {@code null}, it will not be added and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;
Expand Down Expand Up @@ -147,7 +148,7 @@ public FlixelSprite loadSparrowFrames(FileHandle texture, FileHandle xmlFile) {
public FlixelSprite loadSparrowFrames(Texture texture, XmlReader.Element xmlFile) {
// We store regions in a list so we can filter them by prefix later.
// TextureAtlas.AtlasRegion is used because it supports offsets.
atlasRegions = new Array<>();
atlasRegions = new Array<>(AtlasRegion.class);

for (XmlReader.Element subTexture : xmlFile.getChildrenByName("SubTexture")) {
String name = subTexture.getAttribute("name");
Expand Down Expand Up @@ -325,23 +326,37 @@ public void reset() {
stateTime = 0;
currentAnim = null;
looping = true;
texture.dispose();
texture = null;
currentFrame.getTexture().dispose();
currentFrame = null;
for (int i = atlasRegions.size; i >= 0; i--) {
var region = atlasRegions.items[i];
region.getTexture().dispose();
if (texture != null) {
texture.dispose();
texture = null;
}
atlasRegions.setSize(0);
atlasRegions = null;
for (int i = frames.length - 1; i >= 0; i--) {
var frame = frames[i];
for (TextureRegion region : frame) {
region.getTexture().dispose();
if (currentFrame != null) {
currentFrame.getTexture().dispose();
currentFrame = null;
}
if (atlasRegions != null) {
for (int i = atlasRegions.size - 1; i >= 0; i--) {
var region = atlasRegions.items[i];
if (region != null) {
region.getTexture().dispose();
}
}
atlasRegions.setSize(0);
atlasRegions = null;
}
if (frames != null) {
for (int i = frames.length - 1; i >= 0; i--) {
var frame = frames[i];
if (frame != null) {
for (TextureRegion region : frame) {
if (region != null) {
region.getTexture().dispose();
}
}
}
}
frames = null;
}
frames = null;
}

public void changeX(float x) {
Expand All @@ -352,6 +367,10 @@ public void changeY(float y) {
setY(getY() + y);
}

public void changeRotation(float rotation) {
setRotation(getRotation() + rotation);
}

public Map<String, Animation<TextureRegion>> getAnimations() {
return animations;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,97 @@
package me.stringdotjar.flixelgdx.group;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.utils.SnapshotArray;

import me.stringdotjar.flixelgdx.graphics.FlixelObject;

import java.util.function.Consumer;

/**
* Base class for creating groups with a list of members inside of it.
*/
public abstract class FlixelGroup<T> implements FlixelGroupable<T> {
public abstract class FlixelGroup<T extends FlixelObject> implements FlixelGroupable<T>, FlixelObject {

/**
* The list of members that {@code this} group contains.
*/
protected SnapshotArray<T> members;
protected SnapshotArray<FlixelObject> members;

public FlixelGroup() {
members = new SnapshotArray<>();
members = new SnapshotArray<>(FlixelObject.class);
}

@Override
public void add(T member) {
members.add(member);
}

@Override
public void update(float elapsed) {
FlixelObject[] items = members.begin();
for (int i = 0, n = members.size; i < n; i++) {
FlixelObject member = items[i];
if (member == null) {
continue;
}
member.update(elapsed);
}
members.end();
}

@Override
public void draw(Batch batch) {
FlixelObject[] items = members.begin();
for (int i = 0, n = members.size; i < n; i++) {
FlixelObject member = items[i];
if (member == null) {
continue;
}
member.draw(batch);
}
members.end();
}

@Override
public void remove(T member) {
members.removeValue(member, true);
}

@Override
public void clear() {
public void destroy() {
members.forEach(FlixelObject::destroy);
members.clear();
}

@Override
public void forEachMember(Consumer<T> callback) {
for (T member : members.begin()) {
public void clear() {
members.clear();
}

public void forEachMember(Consumer<FlixelObject> callback) {
FlixelObject[] items = members.begin();
for (int i = 0, n = members.size; i < n; i++) {
FlixelObject member = items[i];
if (member == null) {
continue;
}
callback.accept(member);
}
members.end();
}

public <C> void forEachMemberType(Class<C> type, Consumer<C> callback) {
T[] items = members.begin();
FlixelObject[] items = members.begin();
for (int i = 0, n = members.size; i < n; i++) {
T member = items[i];
FlixelObject member = items[i];
if (type.isInstance(member)) {
callback.accept(type.cast(member));
}
}
members.end();
}

public SnapshotArray<T> getMembers() {
public SnapshotArray<FlixelObject> getMembers() {
return members;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package me.stringdotjar.flixelgdx.group;

import java.util.function.Consumer;

/**
* Interface for creating new groups with members inside of them.
*/
public interface FlixelGroupable<T> {
void add(T member);
void remove(T member);
public interface FlixelGroupable<FlixelObject> {
void add(FlixelObject member);
void remove(FlixelObject member);
void clear();
void forEachMember(Consumer<T> callback);
}
Loading