// =============================================================================
#include <cgl/cgl.h>
#include <core/corefile.h>
#include <cgl/core.h>
#include <coresound/coresound.h>
#include <stdlib.h>
#include "c_player.h"
#include "c_enemy.h"

#define MAXENEMIES   250

// ======================== variables ===============================
		
// objects
c_player player;
c_enemy enemy[MAXENEMIES];
int currentframe;
int gameactive;

// graphics
s_bitmap bmp_background;
s_sprite spr_player;
s_sprite spr_enemy;
s_font gamefont;

// ======================== functions ===============================
void InitEnemies()

{
    for (int i=0;i<MAXENEMIES;i++) {
        enemy[i].Init(rand()%1024,rand()%768);
    }

}

void DrawEnemies()

{
     for (int i=0;i<MAXENEMIES;i++) {
         enemy[i].Draw();
     }
}

void HandleEnemies()

{
     for (int i=0;i<MAXENEMIES;i++) {
          enemy[i].Handle(&player);
     }
}


void LoadGraphics()

{

	int numtiles,x,y;

    // Load background image, sprites and font
    CGL_LoadBitmap("background.tga",&bmp_background);
    CGL_LoadSprite("player.tga",&spr_player);
    CGL_LoadSprite("ball.tga",&spr_enemy);
    CGL_InitFont("font_heat.tga",&gamefont);

}

void DrawBackground()
{
     CGL_DrawBitmap(0,0,bmp_background); 
}

void DrawHUD()

{
      CGL_DrawText(10,30,gamefont,"SCORE: %d",player.score);
}

// ===================== Main - Program entry =================
void coremain()
        
{
	// Init Video & Resources
	corefile_mountimage("res",MOUNT_DIR);
    CGL_InitVideo(1024,768,CGL_VIDEO_NONE);
    CGL_SetTitle("CGL - Minimal zombie game");

    LoadGraphics();
    
    InitEnemies();
    player.Init();
    
	// update loop
    gameactive=1;
    do {
      CGL_WaitRefresh();

      // Draw stuff
	  DrawBackground();
      DrawEnemies();
      player.Draw();
 	  DrawHUD();
      
	  // Handle stuff
      HandleEnemies();          
      player.HandleInput();