don't know if you're allowed to ask that here, but I wanted to know if my code is inefficient and leaves much work to the garbage collector, because I'm trying to find a source of lag in my game.
Heres the class, which loads the world through json files.
Worldloader.java:
package com.Sidescroll.game.Helpers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.Sidescroll.game.GameObjects.Background;
import com.Sidescroll.game.GameObjects.Platform;
import com.Sidescroll.game.GameObjects.Player;
import com.Sidescroll.game.GameWorld.GameWorld;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
public final class WorldLoader {
private WorldLoader() {}
public static HashMap<String, GameWorld> worlds = new HashMap<String, GameWorld>();
private static JsonValue jsonPlatforms, jsonSpawn, jsonBackgrounds;
public static GameWorld loadWorld(String levelName) {
if(worlds.get(levelName) != null) return worlds.get(levelName);
//load json file
FileHandle handle = Gdx.files.internal("levels/" + levelName + ".json");
String fileContent = handle.readString();
JsonValue json = new JsonReader().parse(fileContent);
jsonPlatforms = json.get("platforms");
jsonBackgrounds = json.get("backgrounds");
jsonSpawn = json.get("spawn");
//loads platforms
List<Platform> platforms = new ArrayList<Platform>();
for(JsonValue pValue : jsonPlatforms.iterator()) {
Platform platform = new Platform(pValue.getInt("x"),
pValue.getInt("y"),
pValue.getInt("width"),
pValue.getInt("height"),
Platform.Type.valueOf(Platform.Type.class, pValue.getString("type")),
pValue.getString("variant"));
platforms.add(platform);
}
//create player
Vector2 vecSpawn = new Vector2(jsonSpawn.getInt("x"), jsonSpawn.getInt("y"));
Player player = new Player(vecSpawn, platforms);
//loads backgrounds
List<Background> backgrounds = new ArrayList<Background>();
for(JsonValue bgValue : jsonBackgrounds.iterator()) {
Background bg = new Background("bin/sprites/" + bgValue.getString("name") + ".png",
bgValue.getInt("x"),
bgValue.getInt("y"),
bgValue.getInt("width"),
bgValue.getInt("height"),
bgValue.getBoolean("front"));
backgrounds.add(bg);
}
worlds.put(levelName, new GameWorld(player, platforms, backgrounds));
return worlds.get(levelName);
}
}
Related
I have created hungryherogame (recreate) in adobe animate using starling framework..
download from this address below :
( http://www.test.farsvila.ir/animate_starling_Error.rar )
package {
import flash.display3D.textures.Texture;
import flash.utils.Dictionary;
import flash.display.Bitmap;
import starling.textures.Texture;
import starling.display.Sprite;
import starling.events.Event;
import starling.display.Image;
import starling.display.Button;
import starling.core.Starling;
import starling.events.Event;
import screens.Welcome;
public class Assets {
[Embed(source = "media/graphics/bgWelcome.jpg")]
public static const BgWelcome: Class;
[Embed(source = "media/graphics/welcome_hero.png")]
public static const WelcomeHero: Class;
[Embed(source = "media/graphics/welcome_title.png")]
public static const WelcomeTitle: Class;
[Embed(source = "media/graphics/welcome_playButton.png")]
public static const WelcomePlayBtn: Class;
[Embed(source = "media/graphics/welcome_aboutButton.png")]
public static const WelcomeAboutBtn: Class;
private static var gameTextures: Dictionary = new Dictionary();
public static function getTexture(name: String): Texture {
if (gameTextures[name] == undefined) {
var bitmap: Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap(bitmap);
}
return gameTextures[name];
}
}
}
Its made an error --> Call to a possibly undefined method fromBitmap through a reference with static type Class.
in first look, every thing is OK! according DOC fromBitmap is a public static member of starling.textures.Texture
but problem is because of import flash.display3D.textures.Texture which is out of code block in your post and make me hanged some minutes !
so in this case, we have same class names, two Textures. compiler get mixed up too (Ambiguous reference Error).
try it
Edited
public static function getTexture(name: String): starling.textures.Texture {
if (gameTextures[name] == undefined) {
var bitmap: Bitmap = new Assets[name]();
gameTextures[name] = starling.textures.Texture.fromBitmap(bitmap);
}
return gameTextures[name];
}
to make it clear for compiler, which Texture is your mean
Suggestion
i guess you don't really need import flash.display3D.textures.Texture;
so remove it from your default code (problem solved without changing Texture to starling.textures.Texture)
I'm currently loading my level with the Json classes. Problem is, the application lags at the beginning which is probably due to the GarbageCollector removing all my unused JsonValue objects. I don't currently know, how to fix this, any advice would be appreciated,
the WorldLoader class with the json stuff, code looks like much, but is pretty straight forward:
package com.Sidescroll.game.Helpers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.Sidescroll.game.GameObjects.Background;
import com.Sidescroll.game.GameObjects.Platform;
import com.Sidescroll.game.GameObjects.Player;
import com.Sidescroll.game.GameWorld.GameWorld;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
public final class WorldLoader {
private WorldLoader() {}
public static HashMap<String, GameWorld> worlds = new HashMap<String, GameWorld>();
private static JsonValue jsonPlatforms, jsonSpawn, jsonBackgrounds;
public static GameWorld loadWorld(String levelName) {
if(worlds.get(levelName) != null) return worlds.get(levelName);
//load json file
FileHandle handle = Gdx.files.internal("levels/" + levelName + ".json");
String fileContent = handle.readString();
JsonValue json = new JsonReader().parse(fileContent);
jsonPlatforms = json.get("platforms");
jsonBackgrounds = json.get("backgrounds");
jsonSpawn = json.get("spawn");
//loads platforms
List<Platform> platforms = new ArrayList<Platform>();
for(JsonValue pValue : jsonPlatforms.iterator()) {
Platform platform = new Platform(pValue.getInt("x"),
pValue.getInt("y"),
pValue.getInt("width"),
pValue.getInt("height"),
Platform.Type.valueOf(Platform.Type.class, pValue.getString("type")),
pValue.getString("variant"));
platforms.add(platform);
}
//create player
Vector2 vecSpawn = new Vector2(jsonSpawn.getInt("x"), jsonSpawn.getInt("y"));
Player player = new Player(vecSpawn, platforms);
//loads backgrounds
List<Background> backgrounds = new ArrayList<Background>();
for(JsonValue bgValue : jsonBackgrounds.iterator()) {
Background bg = new Background("bin/sprites/" + bgValue.getString("name") + ".png",
bgValue.getInt("x"),
bgValue.getInt("y"),
bgValue.getInt("width"),
bgValue.getInt("height"),
bgValue.getBoolean("front"));
backgrounds.add(bg);
}
worlds.put(levelName, new GameWorld(player, platforms, backgrounds));
return worlds.get(levelName);
}
}
Hey any help appreciated i've been mind boggled by the asset manager for a couple of days
Here is my Hawaii class that I'm trying to reference assets from my asset manager class (MapAssetLoadQeue). and I'm getting the Cannot access a property or method of a null object reference. Error, the error is thrown in my Hawaii class if I don't (Null) the Asset Manager reference, and it is thrown in my Loader Class haw.showHawaii()
Thanks!!!
package Maps
{
import flash.filesystem.File;
import Enqueue.MapAssetLoadQeue;
import starling.animation.IAnimatable;
import starling.animation.Juggler;
import starling.core.Starling;
import starling.display.Image;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.utils.AssetManager;
public class Hawaii extends Sprite
{
public var mal:MapAssetLoadQeue = new MapAssetLoadQeue;
public var Ocean:Image;
public function Hawaii():void{
super();
this.addEventListener
(starling.events.Event.ADDED_TO_STAGE, showHawaii);
}
public function showHawaii(e:Event):void{
if(mal.mapAssets != null) {
Ocean = new Image(mal.mapAssets.getTexture("blue"));
addChild(Ocean);
Ocean.x = 0;
Ocean.y = 0;
}
}
Here is my loader class where I load my assets, and here is where I get my error. haw.showHawaii(null) function. Cannot access a property or method of a null object reference.
package Assets {
import flash.filesystem.File;
import Enqueue.MapAssetLoadQeue;
import GUI.Wave;
import Maps.Hawaii;
import starling.animation.IAnimatable;
import starling.animation.Juggler;
import starling.core.Starling;
import starling.core.starling_internal;
import starling.display.Image;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.utils.AssetManager;
public class Loader extends Sprite {
var loadText: Texture = Texture.fromEmbeddedAsset(loadMCTexture);
var loadXml: XML = XML(new loadMC0Xml());
var loaderAtlas: TextureAtlas = new TextureAtlas(loadText, loadXml);
public var loaderMovie: MovieClip = new MovieClip(loaderAtlas.getTextures("loading_"));
public var mal: MapAssetLoadQeue = new MapAssetLoadQeue;
public var wave:Wave = new Wave;
public var haw:Hawaii;
//Loader MovieClip size 720X233;
[Embed(source = "/../Wavehunter/loadScreenAssets/wm/MovieClips/loader/loader_0.xml", mimeType = "application/octet-stream")]
public static const loadMC0Xml: Class;
[Embed(source = "/../Wavehunter/loadScreenAssets/wm/MovieClips/loader/loader_0.png")]
public static const loadMCTexture: Class;
// public var juggler:Juggler = Starling.juggler;
/*
======================================================================
Every Movieclip asset for the worldMap
> Load WorldMap Assets on stage as Png and Make all alpha to 30%
> and within each function make that png invible to false or remove
> add MoviClip over the invisible png after load is processed
=======================================================================
*/
// Load WorldMap asset function
public function mapLoader(): void {
mal.requestWM();
addChild(loaderMovie);
loaderMovie.x = 347;
loaderMovie.y = 277;
loaderMovie.play();
Starling.juggler.add(loaderMovie);
}
public function mapLdr(){
loaderMovie.visible = true;
addChild(loaderMovie);
loaderMovie.x = 347;
loaderMovie.y = 277;
loaderMovie.play();
Starling.juggler.add(loaderMovie);
}
//this function resizes the loader to original size and places at the bottom corner
public function resizeLdr(){
loaderMovie.width = 72;
loaderMovie.height = 22;
loaderMovie.x =1237;
loaderMovie.y = 712;
}
public function callLoad(): void {
public function callHawaii(): void {
trace(" Hawaii is being runned");
//mal.mapAssets.verbose = true;
mal.mapAssets.loadQueue(function onGo(ratio: Number): void {
trace("Hawaii percentage is", ratio);
if (ratio == 1.0) {
Starling.juggler.delayCall(function (): void {
loaderMovie.visible = false;
//addChild(haw.Ocean);
// addChild(haw);
trace("this is haw value",haw);
//trace("this is the function", haw.showHawaii(null));
haw.showHawaii(null);
}, 0.30);
}
});
}
public function showMap() {
// get texture by name
addChild(mal);
mal.accesMap();
mal.hawaii.addEventListener(Event.TRIGGERED,removeMap);
}
// this code removes the HawaiiMap from the screen and goes directly to the wave
public function removeMap(){
trace("function initated");
mapLdr();
resizeLdr();
mal.mapAssets.purge();
mal.requestHawaii();
callHawaii();
mal.hawaii.removeEventListener(Event.TRIGGERED,removeMap);
}
public function Loader() {
super();
}
}
}
And here is my Asset Manager class
package Enqueue {
import flash.filesystem.File;
import Assets.MapAnimate;
import Assets.MapAssets;
import Maps.Hawaii;
import starling.animation.IAnimatable;
import starling.animation.Juggler;
import starling.animation.Transitions;
import starling.animation.Tween;
import starling.core.Starling;
import starling.core.starling_internal;
import starling.display.Button;
import starling.display.Image;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.events.EnterFrameEvent;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.utils.AssetManager;
public class MapAssetLoadQeue extends Sprite {
public var mapAssets: AssetManager;
public function MapAssetLoadQeue() {
super();
}
public function requestHawaii():void {
// mapAssets.verbose =false;
// mapAssets = new AssetManager;
var appDir:File = File.applicationDirectory;
mapAssets.enqueue(appDir.resolvePath("IslandMaps/Hawaii"));
//mapAssets.enqueue(Hawaii);
mapAssets.verbose = true;
}
If this is all of your code, and if I'm not missing something obvious, I don't see you creating "haw" anywhere. That is, you don't appear to have anything such as...
haw = new Hawaii();
...so, it would be null!
I have got xades XML as InputStream. I do not care if certyficates are valid, check sign, etc. I can't provide any CA or any other type of certificate storage/validation. What I need is just get documents embedded in xades file as streams or temporary files on disk so I can process them as they were plain files from disk. Could someone provide snippet that extracts embedded documents? TIA
To extract Base64-encoded signed content from XAdES signed file i use code like below. It doesn't use xades4j at all.
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.bouncycastle.util.encoders.Base64;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Utils {
/**
* extract ds:Object from .xades file
*
* #param xadesIn .xades file input stream
* #return base64 decoded bytes
* #throws Exception
*/
public static byte[] extractContentFromXadesSignedFile(InputStream xadesIn) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(xadesIn);
xadesIn.close();
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
xpath.setNamespaceContext(new SimpleNamespaceContext(new HashMap<String, String>() {{
put("ds", "http://www.w3.org/2000/09/xmldsig#");
}}));
XPathExpression expr = xpath.compile("//ds:SignedInfo/ds:Reference");
NodeList referenceNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
/**
* loop over all Reference nodes
* i need to find Object node with Id that fits URI value of Reference
*/
for(int i=0;i<referenceNodes.getLength();i++){
Node referenceNode = referenceNodes.item(i);
NamedNodeMap attributes = referenceNode.getAttributes();
if(attributes != null) {
Node uri = attributes.getNamedItem("URI");
if(uri != null) {
String objectId = uri.getNodeValue();
XPathExpression expr2 = xpath.compile("//ds:Object[#Id='"+objectId.substring(1)+"']");
Node contentNode = (Node) expr2.evaluate(doc, XPathConstants.NODE);
if(contentNode != null) {
String base64 = contentNode.getFirstChild().getNodeValue();
return Base64.decode(base64);
}
}
}
}
return null;
}
/**
* http://stackoverflow.com/a/6392700/404395
*/
private static class SimpleNamespaceContext implements NamespaceContext {
private final Map<String, String> PREF_MAP = new HashMap<String, String>();
public SimpleNamespaceContext(final Map<String, String> prefMap) {
PREF_MAP.putAll(prefMap);
}
#Override
public String getNamespaceURI(String prefix) {
return PREF_MAP.get(prefix);
}
#Override
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
#Override
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
}
}
Sample usage of that:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
public class XadesExtractTest {
#Test
public void extract() throws Exception {
InputStream in = XadesExtractTest.class.getClassLoader().getResourceAsStream("test.xades");
byte[] bytes = Utils.extractContentFromXadesSignedFile(in);
Assert.assertNotNull(bytes);
in.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
File f = File.createTempFile("test", ".zip");
System.out.println(f.getAbsolutePath());
FileOutputStream fout = new FileOutputStream(f);
IOUtils.copy(bin, fout);
bin.close();
fout.close();
}
}
I found this code on StackOverflow
import java.io.File;
import java.io.FileOutputStream;
import java.awt.image.RenderedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
public class Main {
public static void main(String args[]) {
File file = new File("input.tif");
try {
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = new NullOpImage(dec.decodeAsRenderedImage(0),
null,
OpImage.OP_IO_BOUND,
null);
FileOutputStream fos = new FileOutputStream("output.jpg");
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(op.getData());
fos.close();
}
catch (java.io.IOException ioe) {
System.out.println(ioe);
}
}
}
I have been trying to reverse this process - convert jpg to uncompressed tiff but but I cant seem to get it to work.
Any ideas?