tilo.Tilo.java Source code

Java tutorial

Introduction

Here is the source code for tilo.Tilo.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2015 copyright Muresan Vlad 
    
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 tilo;

import java.util.ArrayList;
import java.util.List;

import tilo.languages.Language;
import tilo.languages.lua;
import tilo.modules.audio;
import tilo.modules.graphics;
import tilo.modules.input;
import tilo.modules.math;
import tilo.modules.timer;
import tilo.modules.window;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.FileTextureData;

public class Tilo implements InputProcessor, ApplicationListener {
    public static final String TAG = "Tilo";
    public static final String E_RESOURCE = "Resource not found - ";
    public static final String E_ARGUMENT = "Argument not found - ";
    public static final String E_PLUGIN = "Plugin not found - ";
    public static final String E_LANGUAGE = "Incorrect scripting language - ";

    public static boolean ready = false;
    public static Language script;
    public static AssetManager assets;

    public static int width, height;

    // plugins
    graphics g = new graphics();
    timer t = new timer();
    window w = new window();
    math m = new math();
    input i = new input();
    audio a = new audio();

    public static String getPlatform() {
        ApplicationType type = Gdx.app.getType();
        if (type == ApplicationType.Desktop)
            return "desktop";
        if (type == ApplicationType.Android)
            return "android";
        if (type == ApplicationType.iOS)
            return "ios";
        if (type == ApplicationType.Applet)
            return "applet";
        if (type == ApplicationType.WebGL)
            return "web";
        return "desktop";
    }

    public static void error(String type, String msg) {
        Gdx.app.error(type, msg);
    }

    public static void log(String type, String msg) {
        Gdx.app.log(type, msg);
    }

    public static void debug(String type, String msg) {
        Gdx.app.debug(type, msg);
    }

    public static void print(Object o) {
        System.out.println(o);
    }

    public static FileHandle file(String path) {
        return Gdx.files.internal(path);
    }

    public static void quit() {
        Gdx.app.exit();
    }

    public void create() {
        int width = 800;
        int height = 600;
        Tilo.width = width;
        Tilo.height = height;

        script = new lua();

        // LOAD PLUGINS
        g.plugin_load();
        script.put("graphics", g);
        script.put("timer", t);
        script.put("window", w);
        script.put("math", m);
        script.put("input", i);
        script.put("audio", a);

        if (getPlatform().equalsIgnoreCase("desktop")) {
            width = 800;
            height = 600;

            Tilo.width = width;
            Tilo.height = height;

            boolean fullscreen = false;
            Gdx.graphics.setDisplayMode(width, height, fullscreen);
        }
        assets = new AssetManager();
        Gdx.input.setInputProcessor(this);

        // LOAD SCRIPT
        FileHandle main = file("main.lua");

        if (main.exists()) {
            script.eval(main.readString());
        } else {
            error(TAG, E_RESOURCE + main.name());
            quit();
        }

        // Enable non power of two textures.
        FileTextureData.copyToPOT = true;

        // Set OpenGL features
        Gdx.gl.glDisable(GL20.GL_CULL_FACE);
        Gdx.gl.glDisable(GL20.GL_DITHER);
        Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
        Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST);

        script.invoke("tilo", "load", assets);
        ready = true;
    }

    public void render() {
        Gdx.gl.glScissor(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClearColor(0, 0, 0, 1.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        script.invoke("tilo", "update", Gdx.graphics.getDeltaTime());

        g.getShape().setProjectionMatrix(g.getCamera().combined);
        g.getBatch().setProjectionMatrix(g.getCamera().combined);
        g.getBatch().begin();
        script.invoke("tilo", "draw");
        for (int i = 0; i < g.addTextures.size(); i++)
            g.getBatch().draw(g.addTextures.get(i), g.addTextures.get(i).getX(), g.addTextures.get(i).getY());
        g.getBatch().end();

    }

    public void resize(int width, int height) {

    }

    public void pause() {
        if (ready)
            script.invoke("tilo", "pause");
    }

    public void resume() {
        if (ready)
            script.invoke("tilo", "resume");
    }

    public void dispose() {

        assets.dispose();
    }

    String key = "";

    @Override
    public boolean keyDown(int keycode) {

        key = String.valueOf(keycode);

        for (int i = 0; i < input.keycodeNames.size(); i++) {
            if (input.keycodeNames.containsKey(keycode)) {
                key = input.keycodeNames.get(keycode);
                script.invoke("tilo", "keyDown", key);
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        key = String.valueOf(keycode);

        for (int i = 0; i < input.keycodeNames.size(); i++) {
            if (input.keycodeNames.containsKey(keycode)) {
                key = input.keycodeNames.get(keycode);
                script.invoke("tilo", "keyUp", key);
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}