com.kauridev.lunarfever.Lunar.java Source code

Java tutorial

Introduction

Here is the source code for com.kauridev.lunarfever.Lunar.java

Source

/*
 * This file is part of the lunar-fever package.
 *
 * Copyright (c) 2014 Eric Fritz
 *
 * 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 com.kauridev.lunarfever;

import com.kauridev.lunarbase.Game;
import com.kauridev.lunarbase.GameViewManager;
import com.kauridev.lunarfever.input.KeyboardState;
import com.kauridev.lunarfever.input.MouseState;
import com.kauridev.lunarfever.menu.MainMenu;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

/**
 * @author Eric Fritz
 */
public class Lunar extends Game {
    public static final int TARGET_WIDTH = 1024;
    public static final int TARGET_HEIGHT = 576;

    private GameViewManager viewManager;

    @Override
    public void init() {
        this.setFixedTimeStep(false);

        initOpenGL();
        viewManager = new GameViewManager();
        viewManager.addView(new MainMenu(this, viewManager));
        viewManager.init();
    }

    @Override
    public void exit() {
        viewManager.exit();

        if (Display.isCreated()) {
            Display.destroy();
        }
    }

    private void initOpenGL() {
        try {
            Display.setDisplayMode(new DisplayMode(TARGET_WIDTH, TARGET_HEIGHT));
            Display.setTitle("Lunar Fever");
            Display.setFullscreen(false);
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, TARGET_WIDTH, 0, TARGET_HEIGHT, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    @Override
    public void update(double elapsed) {
        if (Display.isCloseRequested()) {
            this.stop();
            return;
        }

        KeyboardState.update();
        MouseState.update();

        viewManager.update(elapsed);
    }

    @Override
    public void render(double elapsed) {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();

        viewManager.render(elapsed);

        Display.update();
    }

    public static void main(String[] args) {
        new Lunar().start();
    }
}