Skip to content
This repository was archived by the owner on Mar 11, 2023. It is now read-only.

Commit

Permalink
0.4.0 released yesterday
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 27, 2021
1 parent 59cd4a8 commit a333403
Show file tree
Hide file tree
Showing 56 changed files with 2,323 additions and 366 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ repositories {
project.ext.lwjglVersion = '3.2.3'

dependencies {
implementation 'com.google.guava:guava:30.1-jre'
implementation 'it.unimi.dsi:fastutil:8.4.4'
implementation 'org.apache.logging.log4j:log4j-core:2.14.0'
compileOnly 'org.jetbrains:annotations:20.1.0'
Expand Down
152 changes: 65 additions & 87 deletions src/main/java/io/github/overrun/mc2d/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,120 +24,100 @@

package io.github.overrun.mc2d;

import io.github.overrun.mc2d.block.Blocks;
import io.github.overrun.mc2d.level.World;
import io.github.overrun.mc2d.client.Mc2dClient;
import io.github.overrun.mc2d.client.WindowEventHandler;
import io.github.overrun.mc2d.event.KeyCallback;
import io.github.overrun.mc2d.option.Options;
import io.github.overrun.mc2d.screen.CreativeTabScreen;
import io.github.overrun.mc2d.util.GlfwUtils;
import io.github.overrun.mc2d.util.ImageReader;
import io.github.overrun.mc2d.util.TextureDrawer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryStack;

import java.io.Closeable;
import java.nio.IntBuffer;
import java.util.Objects;
import java.util.ArrayList;
import java.util.List;

import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
import static org.lwjgl.system.MemoryUtil.memUTF8;

/**
* @author squid233
* @since 2021/01/07
*/
public final class Main {
public final class Main implements Runnable, Closeable {
public static final String VERSION = "0.4.0";
public static boolean openingGroup;
private static final Logger logger = LogManager.getLogger();
private final Player player = new Player();
private World world;
private static final List<WindowEventHandler> WINDOW_EVENT_HANDLERS = new ArrayList<>(1);
private final Mc2dClient client = registerWindowEventHandler(Mc2dClient.getInstance());
private long window;
private int width = 896;
private int height = 512;
private int mouseX;
private int mouseY;

private void run() {
public <T extends WindowEventHandler> T registerWindowEventHandler(T handler) {
WINDOW_EVENT_HANDLERS.add(handler);
return handler;
}

@Override
public void run() {
logger.info("Loading for game Minecraft2D {}", VERSION);
init();
resize(width, height);
glClearColor(.4f, .6f, .9f, .1f);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
if (!openingGroup) {
player.move();
}
client.render();
client.tick();
glfwSwapBuffers(window);
glfwPollEvents();
}
logger.info("Stopping!");
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
glfwTerminate();
Objects.requireNonNull(glfwSetErrorCallback(null)).free();
}

private void init() {
GLFWErrorCallback.createPrint(System.err).set();
GLFWErrorCallback.create((error, description) -> {
String desc = memUTF8(description);
logger.error("########## GL ERROR ##########");
logger.error("{}: {}", error, desc);
});
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window = glfwCreateWindow(width, height, "Minecraft2D " + VERSION, NULL, NULL);
window = glfwCreateWindow(896, 512, "Minecraft2D " + VERSION, NULL, NULL);
if (window == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
ImageReader.withGlfwImg("icon.png",
ImageReader.withGlfwImg("assets/mc2d/icon.png",
imgs -> glfwSetWindowIcon(window, imgs));
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
KeyCallback.post(new KeyCallback.Context(window, key, scancode, action, mods));
if (action == GLFW_PRESS) {
client.screen.keyPressed(key, scancode, mods);
}
});
glfwSetMouseButtonCallback(window, (window, button, action, mods) -> {
if (action == GLFW_PRESS) {
if (key == GLFW_KEY_ESCAPE) {
if (openingGroup) {
openingGroup = false;
} else {
world.save();
glfwSetWindowShouldClose(window, true);
}
}
if (key == (int) Options.get(Options.KEY_CREATIVE_TAB,
"E").toUpperCase().toCharArray()[0]) {
openingGroup = !openingGroup;
if (!openingGroup) {
GlfwUtils.setDefaultCursor();
}
}
if (key == GLFW_KEY_ENTER) {
world.save();
}
if (key == GLFW_KEY_1) {
player.handledBlock = 1;
}
if (key == GLFW_KEY_2) {
player.handledBlock = 2;
}
if (key == GLFW_KEY_3) {
player.handledBlock = 3;
}
if (key == GLFW_KEY_4) {
player.handledBlock = 4;
}
client.screen.mouseClicked(client.mouseX, client.mouseY, button);
}
});
glfwSetWindowCloseCallback(window, window -> {
if (client.world != null) {
client.world.save();
}
});
glfwSetWindowCloseCallback(window, window -> world.save());
glfwSetWindowSizeCallback(window, (window, width, height) -> resize(width, height));
glfwSetCursorPosCallback(window, (window, x, y) -> {
mouseX = (int) Math.floor(x);
mouseY = (int) Math.floor(y);
for (WindowEventHandler handler : WINDOW_EVENT_HANDLERS) {
handler.onMouseMove((int) Math.floor(x), (int) Math.floor(y));
}
});
try (MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer pWidth = stack.mallocInt(1);
Expand All @@ -154,46 +134,44 @@ private void init() {
}
glfwMakeContextCurrent(window);
GL.createCapabilities();
world = new World(player, 64, 64);
client.player = new Player();
glfwSwapInterval(1);
glfwShowWindow(window);
GlfwUtils.setDefaultCursor();
}

private void render() {
world.render(mouseX, mouseY, width, height);
if (openingGroup) {
CreativeTabScreen.render(mouseX, mouseY, height, player);
} else {
glPushMatrix();
glTranslatef(width, height, 0);
TextureDrawer.begin(ImageReader.loadTexture(
Blocks.RAW_ID_BLOCKS.get(player.handledBlock).toString() + ".png"))
.color4f(1, 1, 1, 1)
.tex2dVertex2d(0, 0, -64, 0)
.tex2dVertex2d(0, 1, -64, -64)
.tex2dVertex2d(1, 1, 0, -64)
.tex2dVertex2d(1, 0, 0, 0)
.end();
glPopMatrix();
}
}

private void resize(int width, int height) {
int w = Math.max(width, 1);
int h = Math.max(height, 1);
CreativeTabScreen.init(w, h);
this.width = w;
this.height = h;
if (client.screen == null) {
client.openScreen(null);
}
for (WindowEventHandler handler : WINDOW_EVENT_HANDLERS) {
handler.onResize(w, h);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, 1, -1);
glOrtho(0, w, h, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
}

@Override
public void close() {
client.close();
logger.info("Stopping!");
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
glfwTerminate();
GLFWErrorCallback gec = glfwSetErrorCallback(null);
if (gec != null) {
gec.free();
}
}

public static void main(String[] args) {
Options.init();
new Main().run();
try (Main main = new Main()) {
main.run();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/github/overrun/mc2d/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public boolean equals(Object o) {

@Override
public String toString() {
return Blocks.BLOCKS.inverse().get(this);
return Blocks.BLOCK2ID.get(this);
}
}
13 changes: 8 additions & 5 deletions src/main/java/io/github/overrun/mc2d/block/Blocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@

package io.github.overrun.mc2d.block;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectArrayMap;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* @author squid233
* @since 2021/01/09
*/
public final class Blocks {
public static final BiMap<String, Block> BLOCKS = HashBiMap.create(5);
public static final Byte2ObjectMap<Block> RAW_ID_BLOCKS = new Byte2ObjectLinkedOpenHashMap<>(5);
public static final Map<String, Block> BLOCKS = new LinkedHashMap<>(5);
public static final Map<Block, String> BLOCK2ID = new LinkedHashMap<>(5);
public static final Byte2ObjectMap<Block> RAW_ID_BLOCKS = new Byte2ObjectArrayMap<>(5);
public static final int BLOCK_SIZE = 32;
public static final Block AIR = register("air", 0);
public static final Block GRASS_BLOCK = register("grass_block", 1);
Expand All @@ -46,6 +48,7 @@ public final class Blocks {
public static Block register(String id, int rawId) {
Block b = new Block(rawId);
BLOCKS.putIfAbsent(id, b);
BLOCK2ID.putIfAbsent(b, id);
RAW_ID_BLOCKS.putIfAbsent(b.getRawId(), b);
return b;
}
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/io/github/overrun/mc2d/client/Mc2dClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* MIT License
*
* Copyright (c) 2020-2021 Over-Run
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.overrun.mc2d.client;

import io.github.overrun.mc2d.Player;
import io.github.overrun.mc2d.client.font.TextRenderer;
import io.github.overrun.mc2d.client.gui.screen.Screen;
import io.github.overrun.mc2d.client.gui.screen.TitleScreen;
import io.github.overrun.mc2d.client.texture.TextureManager;
import io.github.overrun.mc2d.level.World;
import org.jetbrains.annotations.Nullable;

import java.io.Closeable;

/**
* @author squid233
* @since 2021/01/25
*/
public final class Mc2dClient implements Closeable, WindowEventHandler {
private static final Mc2dClient INSTANCE = new Mc2dClient();
public final TextRenderer textRenderer;
private final TextureManager textureManager;
public Screen screen;
public @Nullable World world;
public Player player;
private int width, height;
public int mouseX, mouseY;

private Mc2dClient() {
textRenderer = new TextRenderer(this);
textureManager = new TextureManager();
}

public void openScreen(@Nullable Screen screen) {
if (this.screen != null) {
this.screen.removed(screen);
}
if (screen == null && world == null) {
screen = new TitleScreen();
}
this.screen = screen;
if (screen != null) {
screen.init(this, width, height);
}
}

public void render() {
screen.render(mouseX, mouseY);
}

public void tick() {
screen.tick();
}

public TextureManager getTextureManager() {
return textureManager;
}

public int width() {
return width;
}

public int height() {
return height;
}

public static Mc2dClient getInstance() {
return INSTANCE;
}

@Override
public void close() {
textureManager.close();
}

@Override
public void onResize(int newWidth, int newHeight) {
width = newWidth;
height = newHeight;
screen.init(this, width, height);
}

@Override
public void onMouseMove(int newX, int newY) {
mouseX = newX;
mouseY = newY;
}
}
Loading

0 comments on commit a333403

Please sign in to comment.