/* Common function and variables */
/* P.K. */


/* ---------- Libraries ---------- */
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_audio.h>
#include <SDL/SDL_mixer.h>

#include "macros.h"
#include "types.h"
#include "common.h"

//? #include "main.h"
#include "evenements.h"

/* Various languages localization */
#include <locale.h>
#include <libintl.h>

/* ---------- Variables ---------- */
int gamemode;  // are we playing or are we in the menu or nided?

int numperso=NPERSO;        // number of players

float bonus_freq;           // P.K. enable even float numbers

//xPersonnage perso[MAX_NPERSO]; // personages in the game

int compteur;               // counter

/* Four directions */
const int dx[4]={ 0, 0,-1, 1};
const int dy[4]={-1, 1, 0, 0};

/* Music variables */
Mix_Music *musintro, *muschoix;
Mix_Chunk *s_expl, *s_fontaine, *s_rire, *s_mort;
Mix_Chunk *s_debut, *s_bouton, *s_teleport, *s_citrouille;
Mix_Chunk *s_bonus, *s_grosrire, *s_ecrantit, *s_pouvoir;

/* ---------- Functions ---------- */
void errormessage(char *message, char *param) {
  printf(gettext("\nErreur :\n%s"), message);
  if (param) printf(" : %s", param);
  printf(".\n%s\n", SDL_GetError());
  exit(0);
}

/* Initialization of the resolution structure */
void init_resolution(Resolution * p_resolution) {
  p_resolution->window_width = INITIAL_WINDOW_WIDTH;    // Size of the window
  p_resolution->window_height = INITIAL_WINDOW_HEIGHT;
  p_resolution->plan_width = INITIAL_PLAN_WIDTH;        // Size of the playground
  p_resolution->plan_height = INITIAL_PLAN_HEIGHT;
  p_resolution->x_offset = INITIAL_X_OFFSET;            // Offset of the playground
  p_resolution->y_offset = INITIAL_Y_OFFSET;
  return;
}

/* allocate game plan = whole playing area */
void allocplan(Plan *p_plan) {
  int i;

#ifdef DEBUG
  printf("Allocating memory of the size (%ix%i) ...", p_plan->nivewidth, p_plan->niveheight);
#endif // DEBUG
  // pointers to columns
  if ((p_plan->element = (PlateauElement **) malloc(sizeof(PlateauElement)*(p_plan->nivewidth))) == NULL) {
    errormessage(gettext("Not enough memory to allocate gameplan columns"), NULL); // error detection
  }

  for (i=0; i < p_plan->nivewidth; i++) {
     // pointers to rows
    if ((p_plan->element[i] = (PlateauElement *) malloc(sizeof(PlateauElement)*(p_plan->niveheight))) == NULL) {
      errormessage(gettext("Not enough memory to allocate gameplan rows"), NULL); // error detection
    }
    // may be alloc one param for each element? ??

  }
#ifdef DEBUG
  printf(" O.K.\n");
#endif // DEBUG
  return;
}

/* init game plan */
void initplan(Plan *p_plan) {
#ifdef DEBUG
  printf("Initializing the gameplan ...");
#endif // DEBUG
  p_plan->nivewidth = 0;
  p_plan->niveheight = 0;
  p_plan->element = NULL; // no memory allocated
#ifdef DEBUG
  printf(" O.K.\n");
#endif // DEBUG

  return;
}

/* free game plan = whole playing area */
void freeplan(Plan *p_plan) {
  int i, j;

#ifdef DEBUG
  printf("Freeing memory of the size (%ix%i) ...", p_plan->nivewidth, p_plan->niveheight);
#endif // DEBUG
  for (i=0; i < p_plan->nivewidth; i++) {
    for (j=0; j < p_plan->niveheight; j++) {
      // free the element parametres
      free(p_plan->element[i][j].params);
    }
    free(p_plan->element[i]);
  }

  if (p_plan->element != NULL) {
    free(p_plan->element);
  }

#ifdef DEBUG
  printf(" O.K.\n");
#endif // DEBUG
  return;
}

/* Structure containing data about a "perso" */
Personnage perso[MAX_NPERSO];

