Create image layer
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
package com.bartlomiejpluta.base.core.image;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||||
|
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
|
||||||
|
import com.bartlomiejpluta.base.core.util.math.MathUtil;
|
||||||
|
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
|
public class DefaultImageManager implements ImageManager {
|
||||||
|
private final MeshManager meshManager;
|
||||||
|
private final TextureManager textureManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Image createImage(String imageFileName) {
|
||||||
|
var texture = textureManager.loadTexture(imageFileName);
|
||||||
|
var width = texture.getWidth();
|
||||||
|
var height = texture.getHeight();
|
||||||
|
var gcd = MathUtil.gcdEuclidean(width, height);
|
||||||
|
var initialWidth = width / gcd;
|
||||||
|
var initialHeight = height / gcd;
|
||||||
|
var mesh = meshManager.createQuad(initialWidth, initialHeight, 0, 0);
|
||||||
|
|
||||||
|
var image = new Image(mesh, Material.textured(texture), initialWidth, initialHeight);
|
||||||
|
image.setScale(gcd);
|
||||||
|
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanUp() {
|
||||||
|
log.info("There is nothing to clean up here");
|
||||||
|
}
|
||||||
|
}
|
||||||
19
engine/src/main/java/com/bartlomiejpluta/base/core/image/Image.java
Executable file
19
engine/src/main/java/com/bartlomiejpluta/base/core/image/Image.java
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.bartlomiejpluta.base.core.image;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||||
|
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
||||||
|
import com.bartlomiejpluta.base.core.world.object.RenderableObject;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class Image extends RenderableObject {
|
||||||
|
private final int initialWidth;
|
||||||
|
private final int initialHeight;
|
||||||
|
|
||||||
|
Image(Mesh mesh, Material texture, int initialWidth, int initialHeight) {
|
||||||
|
super(mesh);
|
||||||
|
this.initialWidth = initialWidth;
|
||||||
|
this.initialHeight = initialHeight;
|
||||||
|
setMaterial(texture);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.bartlomiejpluta.base.core.image;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.gc.Cleanable;
|
||||||
|
|
||||||
|
public interface ImageManager extends Cleanable {
|
||||||
|
Image createImage(String imageFileName);
|
||||||
|
}
|
||||||
17
engine/src/main/java/com/bartlomiejpluta/base/core/util/math/MathUtil.java
Executable file
17
engine/src/main/java/com/bartlomiejpluta/base/core/util/math/MathUtil.java
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.bartlomiejpluta.base.core.util.math;
|
||||||
|
|
||||||
|
public class MathUtil {
|
||||||
|
public static int gcdEuclidean(int a, int b) {
|
||||||
|
int x = a;
|
||||||
|
int y = b;
|
||||||
|
int z;
|
||||||
|
|
||||||
|
while(y != 0) {
|
||||||
|
z = x % y;
|
||||||
|
x = y;
|
||||||
|
y = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.core.world.map;
|
|||||||
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
||||||
import com.bartlomiejpluta.base.core.gl.shader.constant.UniformName;
|
import com.bartlomiejpluta.base.core.gl.shader.constant.UniformName;
|
||||||
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||||
|
import com.bartlomiejpluta.base.core.image.Image;
|
||||||
import com.bartlomiejpluta.base.core.logic.Updatable;
|
import com.bartlomiejpluta.base.core.logic.Updatable;
|
||||||
import com.bartlomiejpluta.base.core.ui.Window;
|
import com.bartlomiejpluta.base.core.ui.Window;
|
||||||
import com.bartlomiejpluta.base.core.world.animation.Animator;
|
import com.bartlomiejpluta.base.core.world.animation.Animator;
|
||||||
@@ -80,6 +81,12 @@ public class GameMap implements Renderable, Updatable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GameMap createImageLayer(Image image, ImageLayer.Mode imageDisplayMode) {
|
||||||
|
layers.add(new ImageLayer(this, image, imageDisplayMode));
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public GameMap addObject(int layerIndex, MovableObject object) {
|
public GameMap addObject(int layerIndex, MovableObject object) {
|
||||||
((ObjectLayer) layers.get(layerIndex)).addObject(object);
|
((ObjectLayer) layers.get(layerIndex)).addObject(object);
|
||||||
|
|
||||||
@@ -103,6 +110,12 @@ public class GameMap implements Renderable, Updatable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GameMap setImage(int layerIndex, Image image) {
|
||||||
|
((ImageLayer) layers.get(layerIndex)).setImage(image);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isMovementPossible(int layerIndex, Movement movement) {
|
public boolean isMovementPossible(int layerIndex, Movement movement) {
|
||||||
var target = movement.getTargetCoordinate();
|
var target = movement.getTargetCoordinate();
|
||||||
|
|
||||||
|
|||||||
58
engine/src/main/java/com/bartlomiejpluta/base/core/world/map/ImageLayer.java
Executable file
58
engine/src/main/java/com/bartlomiejpluta/base/core/world/map/ImageLayer.java
Executable file
@@ -0,0 +1,58 @@
|
|||||||
|
package com.bartlomiejpluta.base.core.world.map;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.gl.shader.constant.UniformName;
|
||||||
|
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||||
|
import com.bartlomiejpluta.base.core.image.Image;
|
||||||
|
import com.bartlomiejpluta.base.core.ui.Window;
|
||||||
|
|
||||||
|
public class ImageLayer implements Layer {
|
||||||
|
|
||||||
|
public enum Mode {
|
||||||
|
NORMAL,
|
||||||
|
FIT_SCREEN,
|
||||||
|
FIT_MAP;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final float mapWidth;
|
||||||
|
private final float mapHeight;
|
||||||
|
|
||||||
|
private Image image;
|
||||||
|
private float imageInitialWidth;
|
||||||
|
private float imageInitialHeight;
|
||||||
|
private final Mode mode;
|
||||||
|
|
||||||
|
public ImageLayer(GameMap map, Image image, Mode mode) {
|
||||||
|
var stepSize = map.getStepSize();
|
||||||
|
this.mapWidth = map.getColumns() * stepSize.x;
|
||||||
|
this.mapHeight = map.getRows() * stepSize.y;
|
||||||
|
this.mode = mode;
|
||||||
|
setImage(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImage(Image image) {
|
||||||
|
this.image = image;
|
||||||
|
this.imageInitialWidth = image.getInitialWidth();
|
||||||
|
this.imageInitialHeight = image.getInitialHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render(Window window, ShaderManager shaderManager) {
|
||||||
|
if (image == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
shaderManager.setUniform(UniformName.UNI_MODEL_MATRIX, image.getModelMatrix());
|
||||||
|
|
||||||
|
switch (mode) {
|
||||||
|
case FIT_SCREEN -> image.setScale(window.getWidth() / imageInitialWidth, window.getHeight() / imageInitialHeight);
|
||||||
|
case FIT_MAP -> image.setScale(mapWidth / imageInitialWidth, mapHeight / imageInitialHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
image.render(window, shaderManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(float dt) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,7 +21,11 @@ public abstract class PositionableObject {
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
protected float scale = 1.0f;
|
protected float scaleX = 1.0f;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
protected float scaleY = 1.0f;
|
||||||
|
|
||||||
public PositionableObject setPosition(float x, float y) {
|
public PositionableObject setPosition(float x, float y) {
|
||||||
position.x = x;
|
position.x = x;
|
||||||
@@ -51,12 +55,24 @@ public abstract class PositionableObject {
|
|||||||
this.rotation += rotation;
|
this.rotation += rotation;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PositionableObject setScale(float scale) {
|
||||||
|
this.scaleX = scale;
|
||||||
|
this.scaleY = scale;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PositionableObject setScale(float scaleX, float scaleY) {
|
||||||
|
this.scaleX = scaleX;
|
||||||
|
this.scaleY = scaleY;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Matrix4f getModelMatrix() {
|
public Matrix4f getModelMatrix() {
|
||||||
return modelMatrix
|
return modelMatrix
|
||||||
.identity()
|
.identity()
|
||||||
.translate(position.x, position.y, 0)
|
.translate(position.x, position.y, 0)
|
||||||
.rotateZ((float) toRadians(-rotation))
|
.rotateZ((float) toRadians(-rotation))
|
||||||
.scale(scale);
|
.scaleXY(scaleX, scaleY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,6 @@ public class DefaultEntityManager implements EntityManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanUp() {
|
public void cleanUp() {
|
||||||
log.info("There is nothing to clean up");
|
log.info("There is nothing to clean up here");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user