/*
 *   EZ GL2D Platfom Game engine Example
 *   Source is provided "as is" without express or implied warranty of any kind.
 * 
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using EZGL2D;

namespace GL2DexampleJump.Game
{
    class Engine
    {
        GameWindow gameWindow;

        int frame;

        Player player = new Player();

        LevelMap levelMap = new LevelMap();
        
        FontGL fontProto = new FontGL();

        GL2D.SpriteGL[] tileImages;
        GL2D.SpriteGL[] sonicImages;

        public Engine( GameWindow gameWindow )
        {
            this.gameWindow = gameWindow;
        }

        public void load()
        {

            GL2D.Screen2D(gameWindow.Width, gameWindow.Height);  // reset the screen on load
            
            // Load our font
            fontProto.loadFont("data/EZGL2DexampleJump/protofont.png", 32, 32, (int)TextureMinFilter.Nearest);

            tileImages = GL2D.LoadTileSet("data/EZGL2DexampleJump/tiles.png", 32, 32, (int)TextureMinFilter.Nearest);
            sonicImages = GL2D.LoadSpriteSet("data/EZGL2DexampleJump/sonic.png", 
                                             AtlasCoords.sonicTextureCoords.Length / 4, 
                                             AtlasCoords.sonicTextureCoords,
                                             (int)TextureMinFilter.Nearest);
            levelMap.init(Constants.SCREEN_WIDTH / Constants.TILE_SIZE,
                                              Constants.SCREEN_HEIGHT / Constants.TILE_SIZE,
                                              Constants.TILE_SIZE, tileImages);
        }

        public void resize()
        {
            GL2D.Screen2D(gameWindow.Width, gameWindow.Height);  // reset the screen every resize
        }

        public void update()
        {
            frame++;
            player.update( levelMap, gameWindow );
            levelMap.update(0);

        }

        public void render()
        {
            GL2D.ClearScreen();   // clear the screen


            GL2D.Begin2D();         // start 2D drawing mode
                
                levelMap.renderMap();
                player.render( sonicImages );    
                fontProto.print(32, 32, "[Arrow Keys]->Move");   // Wavy font
                fontProto.print(32, 64, "[Space]->Jump");   // Wavy font
            
            GL2D.End2D();

        }


    }
}
