#! /usr/bin/python -O
# -*- python -*-

# Balazar
# Copyright (C) 2003 Jean-Baptiste LAMY
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os, os.path, sys
# If installed in /usr, /usr/local, ... add /usr/share (or ...) to the module path.

HERE = os.path.dirname(sys.argv[0])

if   HERE.endswith("games"): # /usr/{local/}games
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..", "share", "games"))
elif HERE.endswith("bin"): # /usr/{local/}bin
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..", "share"))
else: # Raw source not installed
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))

print "* Balazar * Balazar lives in %s" % APPDIR
sys.path.insert(0, APPDIR)

import balazar.globdef as globdef

i = 1
while i < len(sys.argv):
  arg = sys.argv[i]
  i = i + 1
  
  if   arg == "--fullscreen": globdef.FULLSCREEN = 1
  elif arg == "--windowed"  : globdef.FULLSCREEN = 0
  
  elif arg == "--screensize":
    globdef.SCREEN_WIDTH  = int(sys.argv[i])
    globdef.SCREEN_HEIGHT = int(sys.argv[i + 1])
    i += 2
    
  elif arg == "--no-fps-limit":
    globdef.MIN_FRAME_DURATION = 0.0
    
  elif arg == "--version":
    print "Balazar version", globdef.VERSION
    sys.exit()
    
  elif arg == "--help":
    print """Balazar: A fun game
Synopsis: balazar [options...]

where options are:
--help                     This help
--version                  Shows version number
--fullscreen               Fullscreen mode
--windowed                 Windowed mode
--screensize WIDTH HEIGHT  Sets the screen size (in pixel)
--no-fps-limit             Disable FPS limit, for benchmarking (default is to limit the FPS to 30)
"""
    sys.exit()
    
  else:
    print "Unknown command line arg : '%s' !" % arg
    sys.exit(2)
    

import soya
soya.init(width = globdef.SCREEN_WIDTH, height = globdef.SCREEN_HEIGHT, fullscreen = globdef.FULLSCREEN)
soya.set_quality(globdef.QUALITY)

if globdef.SOUND:
  import soya.openal4soya
  soya.openal4soya.init(None)
  

import soya._soya, soya.widget
# SHADER_DEFAULT_MATERIAL = soya.Material()
# SHADER_DEFAULT_MATERIAL.texture = soya.Image.get("shader.png")
# SHADER_DEFAULT_MATERIAL.filename = "shader"
# SHADER_DEFAULT_MATERIAL.save()
# SHADER_DEFAULT_MATERIAL.filename = "__SHADER_DEFAULT_MATERIAL__"
# SHADER_DEFAULT_MATERIAL.diffuse = (1.0, 1.0, 1.0, 1.0)
SHADER_DEFAULT_MATERIAL = soya.Material.get("shader")
SHADER_DEFAULT_MATERIAL.filename = "__SHADER_DEFAULT_MATERIAL__"
soya._soya._set_shader_default_material(SHADER_DEFAULT_MATERIAL)

soya.widget.big_font     = soya.widget.default_font
soya.widget.default_font = soya.Font(os.path.join(soya.DATADIR, "FreeSans.ttf"), 18, 15)


try:
  import psyco
  psyco.full()
  
except:
  print "* Balazar * (Psyco not found ; if you are using an x86 processor, installing psyco can speed up Balazar a little)"


# Display the splash screen
soya.set_root_widget(soya.widget.Image(None, soya.Material.get("splash"), resize_style = ("maximize",)))
soya.render()
soya.root_widget = None

#import random; random.seed(12282)

if 0:
  import soya.ark2soya
  model = soya.ark2soya.convert_ark("scn-village-house")
  model.scale(0.003, 0.003, 0.003)
  model.shapifier = soya.CellShadingShapifier(outline_width = 0.0, max_face_angle = 20.0)
  model.save()

if 0:
  import soya.ark2soya
  model = soya.ark2soya.convert_ark("scn-tree5")
  #model.scale(0.02, 0.02, 0.02)
  #model.scale(0.04, 0.04, 0.04)
  model.scale(0.05, 0.05, 0.05)
  model.shapifier = soya.CellShadingShapifier(outline_width = 0.0, max_face_angle = 80.0)
  model.save()

if 0:
  import soya.ark2soya
  model = soya.ark2soya.convert_ark("scn-high-house")
  model.scale(4.0, 4.0, 4.0)
  #model.scale(0.04, 0.04, 0.04)
  #model.shapifier = soya.CellShadingShapifier(outline_width = 0.0, max_face_angle = 80.0)
  for face in model: face.smooth_lit = 1
  #model.shapifier = soya.CellShadingShapifier(outline_width = 0.0, max_face_angle = 30.0)
  model.save()
  
import balazar.player
balazar.player.start_single_game()
  

