/*
* Copyright 2013 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.terasology.asset.AssetFactory;
import org.terasology.asset.AssetManager;
import org.terasology.asset.AssetType;
import org.terasology.asset.AssetUri;
import org.terasology.config.Config;
import org.terasology.engine.module.ModuleManager;
import org.terasology.engine.subsystem.lwjgl.GLBufferPool;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.entitySystem.prefab.PrefabData;
import org.terasology.entitySystem.prefab.internal.PojoPrefab;
import org.terasology.naming.Name;
import org.terasology.registry.CoreRegistry;
import org.terasology.rendering.ShaderManager;
import org.terasology.rendering.ShaderManagerLwjgl;
import org.terasology.rendering.assets.animation.MeshAnimation;
import org.terasology.rendering.assets.animation.MeshAnimationData;
import org.terasology.rendering.assets.animation.MeshAnimationImpl;
import org.terasology.rendering.assets.material.Material;
import org.terasology.rendering.assets.material.MaterialData;
import org.terasology.rendering.assets.mesh.Mesh;
import org.terasology.rendering.assets.mesh.MeshData;
import org.terasology.rendering.assets.shader.Shader;
import org.terasology.rendering.assets.shader.ShaderData;
import org.terasology.rendering.assets.skeletalmesh.SkeletalMesh;
import org.terasology.rendering.assets.skeletalmesh.SkeletalMeshData;
import org.terasology.rendering.assets.texture.Texture;
import org.terasology.rendering.assets.texture.TextureData;
import org.terasology.rendering.nui.skin.UISkin;
import org.terasology.rendering.nui.skin.UISkinData;
import org.terasology.rendering.opengl.GLSLMaterial;
import org.terasology.rendering.opengl.GLSLShader;
import org.terasology.rendering.opengl.OpenGLMesh;
import org.terasology.rendering.opengl.OpenGLSkeletalMesh;
import org.terasology.rendering.opengl.OpenGLTexture;
import org.terasology.utilities.LWJGLHelper;
import org.terasology.world.block.BlockManager;
import org.terasology.world.block.family.AttachedToSurfaceFamilyFactory;
import org.terasology.world.block.family.DefaultBlockFamilyFactoryRegistry;
import org.terasology.world.block.family.HorizontalBlockFamilyFactory;
import org.terasology.world.block.internal.BlockManagerImpl;
import org.terasology.world.block.loader.WorldAtlasImpl;
import org.terasology.world.block.shapes.BlockShape;
import org.terasology.world.block.shapes.BlockShapeData;
import org.terasology.world.block.shapes.BlockShapeImpl;
/**
* Setup environment with display support
*
* @author Martin Steiger
*/
public class DisplayEnvironment extends HeadlessEnvironment {
private GLBufferPool bufferPool = new GLBufferPool(true);
/**
* @param modules a set of module names that should be loaded (latest version)
*/
public DisplayEnvironment(Name ... modules) {
super(modules);
}
@Override
protected void setupDisplay() {
LWJGLHelper.initNativeLibs();
try {
Display.setDisplayMode(new DisplayMode(1, 1));
Display.create(CoreRegistry.get(Config.class).getRendering().getPixelFormat());
} catch (LWJGLException e) {
throw new IllegalStateException(e);
}
}
@Override
protected void setupAssetManager() {
AssetManager assetManager = new AssetManager(CoreRegistry.get(ModuleManager.class).getEnvironment());
CoreRegistry.put(AssetManager.class, assetManager);
AssetType.registerAssetTypes(assetManager);
assetManager.setAssetFactory(AssetType.PREFAB, new AssetFactory<PrefabData, Prefab>() {
@Override
public Prefab buildAsset(AssetUri uri, PrefabData data) {
return new PojoPrefab(uri, data);
}
});
assetManager.setAssetFactory(AssetType.SHAPE, new AssetFactory<BlockShapeData, BlockShape>() {
@Override
public BlockShape buildAsset(AssetUri uri, BlockShapeData data) {
return new BlockShapeImpl(uri, data);
}
});
assetManager.setAssetFactory(AssetType.TEXTURE, new AssetFactory<TextureData, Texture>() {
@Override
public Texture buildAsset(AssetUri uri, TextureData data) {
return new OpenGLTexture(uri, data);
}
});
assetManager.setAssetFactory(AssetType.SHADER, new AssetFactory<ShaderData, Shader>() {
@Override
public Shader buildAsset(AssetUri uri, ShaderData data) {
return new GLSLShader(uri, data);
}
});
assetManager.setAssetFactory(AssetType.MATERIAL, new AssetFactory<MaterialData, Material>() {
@Override
public Material buildAsset(AssetUri uri, MaterialData data) {
return new GLSLMaterial(uri, data);
}
});
assetManager.setAssetFactory(AssetType.MESH, new AssetFactory<MeshData, Mesh>() {
@Override
public Mesh buildAsset(AssetUri uri, MeshData data) {
return new OpenGLMesh(uri, data, bufferPool);
}
});
assetManager.setAssetFactory(AssetType.SKELETON_MESH, new AssetFactory<SkeletalMeshData, SkeletalMesh>() {
@Override
public SkeletalMesh buildAsset(AssetUri uri, SkeletalMeshData data) {
return new OpenGLSkeletalMesh(uri, data, bufferPool);
}
});
assetManager.setAssetFactory(AssetType.ANIMATION, new AssetFactory<MeshAnimationData, MeshAnimation>() {
@Override
public MeshAnimation buildAsset(AssetUri uri, MeshAnimationData data) {
return new MeshAnimationImpl(uri, data);
}
});
CoreRegistry.get(AssetManager.class).setAssetFactory(AssetType.UI_SKIN, new AssetFactory<UISkinData, UISkin>() {
@Override
public UISkin buildAsset(AssetUri uri, UISkinData data) {
return new UISkin(uri, data);
}
});
// TODO: move somewhere else
CoreRegistry.put(ShaderManager.class, new ShaderManagerLwjgl()).initShaders();
}
@Override
protected void setupBlockManager() {
DefaultBlockFamilyFactoryRegistry blockFamilyFactoryRegistry = new DefaultBlockFamilyFactoryRegistry();
blockFamilyFactoryRegistry.setBlockFamilyFactory("horizontal", new HorizontalBlockFamilyFactory());
blockFamilyFactoryRegistry.setBlockFamilyFactory("alignToSurface", new AttachedToSurfaceFamilyFactory());
BlockManagerImpl blockManager = new BlockManagerImpl(new WorldAtlasImpl(4096), blockFamilyFactoryRegistry);
CoreRegistry.put(BlockManager.class, blockManager);
}
@Override
public void close() throws Exception {
Display.destroy();
super.close();
}
}