view main.c @ 44:2c85322348cf

add print log.
author koba <koba@cr.ie.u-ryukyu.ac.jp>
date Fri, 14 Jan 2011 23:49:23 +0900
parents 56ef94618a0e
children fce767bb5e25
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <SDL.h>
#include "SDL_opengl.h"
#include "object.h"
#include "tree_controll.h"
#include "xml.h"
#include "profile.h"
#include "syokika.h"
#include "sankaku.h"
#include "sgoex.h"
#include "sound.h"
#include "bool.h"
#include "Character.h"
#include "Character_state.h"
#include "bom.h"
#include "count2.h"
#include "tokuten.h"
#include "schedule.h"
#include "tama.h"
#include "debug.h"
#include "trace.h"
#include "LoadSprite.h"
#include <SDL_mixer.h>

#include "error.h"

static int runmode = 0;
static int gamef = 0;
static int use_keybord = 0;
static int i;

static int rswait = 0;
static int cf = 0;
static int cc = 0;

static Uint32 background;
static TraceBuffPtr t_buff;
static char *pad_trace_file;

extern void schedule();
extern void tokuten();
extern void keybord();
//extern void PutSpriteEx(int number, int x, int y, GLfloat scalex, GLfloat scaley, GLfloat angle);
//GLuint texture[250] = {0};
static void gamesyokika(SDL_Joystick *joy);
static void opening(SDL_Joystick *joy);
static void pause(SDL_Joystick *joy);
static void KeyAssign(int runmode, TraceBuffPtr buff, SDL_Joystick *joy, SGO_PAD *pad);

extern void SDL_GL_Enter2DMode();
extern void SDL_GL_Leave2DMode();

/**
 * runmode:
 * 0 - normal game                                                         
 * 1 - capture mode                                                        
 *     program will capture all pad traces on a play and               
 *     dump it into a file                                             
 * 2 - trace mode                                                          
 *     program will run with a trace file which is generated           
 *     by capture-mode
 */


#define SCMP_SIZ 8


static void
print_usage()
{
  printf("usage: ./exe_file [-h|-help|--help][-capture filename][-trace filename]\n");
}


static int
get_option(int argc, char *argv[])
{
    int i;
    if (argc < 2) {
      runmode = 0;
      return 0;
    }
    for (i = 0; i < argc; i++) {
      if (!strncmp(argv[i], "-capture", SCMP_SIZ)) {
	if (++i >= argc) {
	  return 1;
	}
	printf("Start Capture mode.\n");
	t_buff = (TraceBuffPtr)malloc(sizeof(TraceBuff));
	t_buff->next = NULL;
	pad_trace_file = argv[i];
	runmode = 1;
	return 0;
      } else if (!strncmp(argv[i], "-trace", SCMP_SIZ)) {
	if (++i >= argc) {
	  return 1;
	}
	printf("Start Trace mode.\n");
	t_buff = (TraceBuffPtr)malloc(sizeof(TraceBuff));
	t_buff->next = NULL;
	pad_trace_file = argv[i];
	runmode = 2;
	return 0;
      } else if (!strncmp(argv[i], "--help", SCMP_SIZ) ||
		 !strncmp(argv[i], "-help", SCMP_SIZ) ||
		 !strncmp(argv[i], "-h", SCMP_SIZ)) {
	return 1;
      }
    }

    return 1;
}


int main(int argc, char *argv[])
{
    Timeprof timeprof_move;
    timeprof_move = timeprof_new();
    dbg_init("/dev/stdout");

    /**
     * timeprof があるんだけどね
     */
    int frames = 0;
    int start_time = 0, stop_time = 0;

    if (get_option(argc, argv)) {
      print_usage();
      return 0;
    }

    TraceBuffPtr first;
    if ((runmode == 1)||(runmode ==2)) {
      if (!PadfileOpen(pad_trace_file)) {
	runmode = 0;
      } else if(runmode == 2) {
	PadfileRead(t_buff);
      }
      first = t_buff;
    }
    
    /*
      if(myrand_open_file("rand_trace")) {
      perror("main");
      retun 1;
    }
    */

    //SDL_Joystick *joy=SDL_JoystickOpen(0);
  
    syokika(argc, argv);
    __debug("syokikaが呼びだされました\n");
    SDL_Joystick *joy=SDL_JoystickOpen(0);
    if(!joy) {
	fprintf(stderr,"failed to open joystick 0\n");
	use_keybord = 1;
    }
    background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
    SDL_FillRect(screen, NULL, background);
    SDL_GL_Enter2DMode();

    opening(joy);

    __debug("openingがよびだされました\n");

    /**
     * sound.c に移しました
     * 代わりに InitSdlMixer(void); を使います。
     * まあ中身一緒なんだけどね
     */
    /**
      if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY,MIX_DEFAULT_FORMAT,2,1024) < 0) {
        fprintf(stderr,"failed to initialize SDL_mixer.\n");
        SDL_Quit();
        exit(-1);
      }
    */

    // SDL_mixer とかいろいろ初期化
    // もし Init に失敗したら
    // 以後の Mixer 関連の関数を呼び出しても
    // 中では何も行われないとかいう小細工を入れました。
    InitSdlMixer();

    // 音楽ファイルの読み込み
    LoadSdlMixer();

    // ボリューム調整
    InitVolume();
	
    // 音出しテスト
    PlaySdlMixer(-1, BGM);

    if(!(init_chara_list(1024))){
	__debug("failed to init_chara_list\n");
    }

    __debug("finished init_chara_list\n");

    start_time = SDL_GetTicks();

    while (gamef) {
      if ((pad[0].l1 != 0) && (pad[0].r1 != 0) &&
	    (pad[0].l2 != 0) && (pad[0].r2 != 0)) {
	    glFlush();
	    SDL_GL_SwapBuffers( );
	    gamesyokika(joy);
	}

	if (pad[0].quit != 0) {
	    gamef = 0;
	}

	//SDL_FillRect(screen, NULL, background);
	glClear( GL_COLOR_BUFFER_BIT);		
    
	schedule();
	enemyfaste = count;
    
	timeprof_begin(timeprof_move);
	//Move(); //enemy move
	collision_detect();
	outofwindow();
	timeprof_end(timeprof_move);
	state_update();
	asteroidi = 0;
	//charpatern();
	//bosguage();

	if (jiki.bf == TRUE) {
	    Player(0);
	    count++;
	    PutSprite(count, jiki.x, jiki.y, jiki.ch);
	}

	Putbom();
	tokuten();
    
	count++;
	//PutSprite(count, 0, 960 - 96, 10);
	/*インフレゲージ */
	count++;
	//PutSprite(count, 0, 0, 48);
	obj_draw();
	pause(joy);

	KeyAssign(runmode, t_buff, joy, pad);

	glFlush();
	SDL_GL_SwapBuffers( );
	filpcount++;
	SDL_Delay(10);
	count = 0;


	frames++;
    }

    stop_time = SDL_GetTicks();
    
    {
	char buf[128];
	timeprof_sprint(buf, "move", timeprof_move);
	dbg_printf("%s\n", buf);
    }

    printf("%f FPS\n", ((float)frames)/(stop_time-start_time)*1000.0);

    SDL_GL_Leave2DMode();
    SDL_FreeSurface(sprite);

    SDL_JoystickClose(joy);

    //_______________________________________________
    // SDL_mixerの後始末
    Mix_CloseAudio();
    Mix_HaltMusic();
    FreeSdlMixer() ;
    //_______________________________________________

    if (runmode == 1) {
      PadfileWrite(first);
      PadfileClose(first);
    } else if (runmode == 2) {
      PadfileClose(first);
    }
  
    SDL_Quit();
    return(0);
}


static void
gamesyokika(SDL_Joystick *joy)
{
    laser_lv3[0].r = 62;
    laser_lv3[0].r = 62;
    for (i = 0; i < 3; i++) {
	tlv3[i].y = -1;
    }
    filpcount = 0;
    stage = 1;
    //for (i = 0; i < 300; i++)
    //enemy[i].f = FALSE;
    jiki.zanki = 3;
    jiki.x = 60;
    jiki.y = 200;
    jiki.ch = 3;
    jiki.point = 0;
    jiki.bf = FALSE;
    jiki.muteki = 120;
    enemycount = 0;
    lg.stg = 4096;
    pg.stg = 4096;
    infg.stg = 0;
    infg_level = 0;
    //kyeenemyno = -1;
    fastebos = 0;
    jiki.ccount = 99;
    p_extend = 200000;
    pad[0].up=0;
    pad[0].down=0;
    pad[0].right=0;
    pad[0].left=0;
    pad[0].st=0;
    pad[0].se=0;
    opening(joy);
}


static void
opening(SDL_Joystick *joy)
{
    //for (i = 0; i < 300; i++) {
    //};

    SoundStop();
    SoundPlay(7);

    while (1) {
	if(use_keybord == 1) {
	    keybord();
	} else {
	    Pad(joy);
	}

	count = 1;

	PutSprite(count, 700, 480, 53); //  put `push start' string on screen.  
	count++;
	PutSprite(count, 224, 776, 119); //  put `super dandy'                   
	count++;
	//    PutSprite(count, 200, 64, 190);
	//    PutSpriteEx(count, 8192, 8192, 0);
        PutSpriteEx(190, 800, 264, 2, 2, 5);
	count++;
	PutSprite(count, 396, 432, 191);
	//PutSpriteEx(count, 8192, 8192, 0);
	count++;
	PutSprite(count, 640, 640, 192);
	//PutSpriteEx(count, 8192, 8192, 0);

	if (pad[0].st > 1) {
	    jiki.bf = TRUE;
	    
	    //CdPlay(1,&cdp[0],0);                                          
	    SoundStop();
	    SoundPlay(0);

	    gamef = 1;
	    pad[0].st = 1;
	    return;
	}
	if ((pad[0].se > 0) && (pad[0].st > 0)) {
	    gamef = 0;
	    break;
	}


	SDL_GL_SwapBuffers( );
    }
}

static void
pause(SDL_Joystick *joy)
{
    if (jiki.zanki != 0 && jiki.bf != FALSE) {
	if (pad[0].se == 1) {
	    //Mix_Pause(BGM);
	    PauseSdlMixer(BGM);
	    while(1){
		if(use_keybord == 1) {
		    keybord();
		} else {
		    Pad(joy);
		}
		if(pad[0].st == 0)
		    continue;
		pad[0].up=0;
		pad[0].down=0;
		pad[0].right=0;
		pad[0].left=0;
		pad[0].se=0;
		pad[0].st=0;
		//Mix_Resume(BGM);
		ResumeSdlMixer(BGM);
		break;
	    }
	}
    }
    if (jiki.zanki == 0) {
	//     RECT *recp;                                                      
	if ((jiki.ccount > 0) && (cf == 0)) {
	    cf = 1;
	    count++;
	    //cgcg(2);
	    PutSprite(2, 480, 480, 110);
	    count++;
	    //cgcg(1);
	}
	if ((jiki.ccount > 0) && (cf == 1)) {
	    while (1) {
		if (pad[0].st > 0) {
		    cf = 0;
		    jiki.ccount--;
		    jiki.zanki = 3;
		    enemycount++;
		    cc = 0;
		    for (i = 0; i < 4; i++) {
			clear_result[i] = 0;
		    }
		    goto f;
		}
		cc++;
		if (29 - cc / 60 < 20) {
		    cc = 0;
		    cf = 3;
		    break;
		}
		SDL_Delay(100);
		PutSprite(1, 640, 640, 29 - cc / 60);
		//      	PutSpriteEx(1, 409 * ((cc % 60) + 1), 409 * ((cc % 60) + 1), 0);

		Pad(joy);
		SDL_GL_SwapBuffers( );
	    }
	}
	if (rswait > 60 * 6) {
	    cf = 0;
	    rswait = 0;
	    gamesyokika(joy);
	}
	rswait++;
	count++;
	//cgcg(1);
	PutSprite(1, 480, 480, 19);
	return;
    }
    if (jiki.bf == FALSE) {
    f:
	if (rswait < 80) {
	    rswait++;
	    return;
	} else {
	    rswait = 0;
	    jiki.x = 60;
	    jiki.y = 200;
	    jiki.bf = TRUE;

	    pad[0].st = 0;
	    lg.stg = 4096;
	    pg.stg = 4096;
	    laser_lv3[0].r = 62;
	    tlv3[0].r = 0;
	    tlv3[0].y = -1;
	    return;
	    // sb_size = -1;                                                    
	}
    }
}


static void
KeyAssign(int runmode, TraceBuffPtr buff, SDL_Joystick *joy, SGO_PAD *pad)
{
    switch (runmode) {
    case 0:
      if (use_keybord) {
	keybord();
      } else {
	Pad(joy);
      }
      break;
    case 1:
      if (use_keybord) {
	keybord();
      } else {
	Pad(joy);
      }
      t_buff = CapturePad(t_buff, pad);
      break;
    case 2:
      t_buff = TracePad(t_buff, pad);
      break;
    }
}