int main(int argc, char **argv) { unsigned int frameLimit = SDL_GetTicks() + MIN_TICK_DELAY; int i, active_colors; char *filename = (char *)malloc(100); char *graph_title = (char *)malloc(100); char temp[100]; int step = 0; int max_no_of_dots = 0, winner_no_of_dots; unsigned int seconds_of_gameplay = 0; FILE *game_scores; char **gnuplot_commands; FILE *gnuplotPipe; init_level_names(); if(argc < 2) { give_user_info(); return 0; } sprintf(graph_title, "Game Results: "); filename = get_timestamp(); strcat(filename, "Auralux.dat"); game_scores = fopen(filename, "w"); if(!game_scores) { perror("game_scores pointer"); } /* Start up SDL */ init("Auralux 1"); /* Call the cleanup function when the program exits */ atexit(cleanup); clearInput(); initLevel(argv[1]); while (1) { active_colors = 0; for(i = 0; i < NO_OF_COLORS / 2; i++) { if(active_sun_colors[i]) { active_colors++; } } if(active_colors == 1 || !active_sun_colors[BLUE / 2]) { break; } /* Get the input */ getInput(); /* Update player and entities */ doEntities(step); doPlayer(); /* CPU opponents decide strategy */ if(step % (1000 / MIN_TICK_DELAY) == 0) { cpuThink(); /* add data to game scores */ fprintf(game_scores, "%d\t", seconds_of_gameplay++); for(i = 0; i < NO_OF_COLORS / 2; i++) { fprintf(game_scores, "%d\t", no_of_dots[i]); if(no_of_dots[i] > max_no_of_dots) { max_no_of_dots = no_of_dots[i]; } } fprintf(game_scores, "\n"); } /* Draw everything */ draw(); /* Sleep briefly to stop sucking up all the CPU time */ delay(frameLimit); frameLimit = SDL_GetTicks() + MIN_TICK_DELAY; step--; } fclose(game_scores); game_scores = fopen(filename, "r"); if(!game_scores) { perror("game_scores pointer"); } fscanf(game_scores, "%d", &step); /* time column value skipped */ if(active_sun_colors[BLUE / 2]) { strcat(graph_title, "You won!"); } else { strcat(graph_title, "You lost!"); } /* gnuplot commands begin here */ gnuplot_commands = (char **)malloc(10 * sizeof(char *)); for(i = 0; i < 10; i++) { /* assuming that 10 gnuplot commands are required */ gnuplot_commands[i] = (char *)malloc(1000); } /* set title of the graph */ strcpy(gnuplot_commands[0], "set title font \", 20\" \""); strcat(gnuplot_commands[0], graph_title); strcat(gnuplot_commands[0], "\""); /* set xrange*/ strcpy(gnuplot_commands[1], "set xrange[0:"); sprintf(temp, "%d", seconds_of_gameplay); strcat(gnuplot_commands[1], temp); strcat(gnuplot_commands[1], "]"); /* set yrange */ strcpy(gnuplot_commands[2], "set yrange[0:"); sprintf(temp, "%d", (int)(1.3 * max_no_of_dots)); /* extra space at the top of the graph */ strcat(gnuplot_commands[2], temp); strcat(gnuplot_commands[2], "]"); /* set xlabel */ strcpy(gnuplot_commands[3], "set xlabel 'time (in seconds)'"); /* set ylabel */ strcpy(gnuplot_commands[4], "set ylabel 'dot population'"); /* set line styles */ strcpy(gnuplot_commands[5], "set style line 1 lt 1 lw 2 pt 3 lc rgb \"blue\""); strcpy(gnuplot_commands[6], "set style line 2 lt 1 lw 2 pt 3 lc rgb \"orange\""); strcpy(gnuplot_commands[7], "set style line 3 lt 1 lw 2 pt 3 lc rgb \"green\""); strcpy(gnuplot_commands[8], "set style line 4 lt 1 lw 2 pt 3 lc rgb \"purple\""); /* decide which colors to plot on graph */ strcpy(gnuplot_commands[9], "plot "); for(i = 0; i < NO_OF_COLORS / 2; i++) { fscanf(game_scores, "%d", &winner_no_of_dots); /* time column value skipped */ if(!winner_no_of_dots) { continue; } strcat(gnuplot_commands[9], "'"); strcat(gnuplot_commands[9], filename); strcat(gnuplot_commands[9], "'"); strcat(gnuplot_commands[9], " using 1:"); sprintf(temp, "%d ", 2 + i); /* dot counts are from column 2 onwards */ strcat(gnuplot_commands[9], temp); strcat(gnuplot_commands[9], "title \""); switch(i * 2 + 1) { case BLUE: strcpy(temp, "You"); break; case ORANGE: strcpy(temp, "CPU1"); break; case GREEN: strcpy(temp, "CPU2"); break; case PURPLE: strcpy(temp, "CPU3"); break; } strcat(gnuplot_commands[9], temp); strcat(gnuplot_commands[9], "\" "); strcat(gnuplot_commands[9], "smooth cspline "); strcat(gnuplot_commands[9], "w l ls "); sprintf(temp, "%d, ", 1 + i); /* line styles are from 1 onwards */ strcat(gnuplot_commands[9], temp); } gnuplotPipe = popen("gnuplot -persistent", "w"); if(!gnuplotPipe) { perror("gnuplotPipe pointer"); } for (i = 0; i < 10; i++) { printf("%s\n", gnuplot_commands[i]); fprintf(gnuplotPipe, "%s\n", gnuplot_commands[i]); /* send commands to gnuplot one by one */ } /* Exit the program */ exit(0); }
int main(int argc, char *argv[]) { unsigned int frameLimit; int go, i, mapID, loadSlot, recordingID, replayingID; int joystick, showCredits, languageID; go = TRUE; loadSlot = -1; game.fps = 1000 / 60; languageID = mapID = recordingID = replayingID = -1; joystick = 0; showCredits = FALSE; /* Load the resources */ for (i=1;i<argc;i++) { if (strcmpignorecase("-record", argv[i]) == 0) { if (i + 1 >= argc) { printf("You must specify a file to record to\n"); printf("Type %s -h for help\n", argv[0]); exit(1); } if (recordingID == -1) { recordingID = i + 1; } i++; } else if (strcmpignorecase("-playback", argv[i]) == 0) { if (i + 1 >= argc) { printf("You must specify a file to playback from\n"); printf("Type %s -h for help\n", argv[0]); exit(1); } if (replayingID == -1) { replayingID = i + 1; } i++; } else if (strcmpignorecase("-load", argv[i]) == 0) { if (i + 1 >= argc) { printf("You must specify a slot to load from\n"); printf("Type %s -h for help\n", argv[0]); exit(1); } loadSlot = atoi(argv[i + 1]); i++; } else if (strcmpignorecase("-nojoystick", argv[i]) == 0) { game.disableJoystick = TRUE; } else if (strcmpignorecase("-joystick", argv[i]) == 0) { if (i + 1 >= argc) { printf("You must specify a joystick slot to use\n"); printf("Type %s -h for help\n", argv[0]); exit(1); } joystick = atoi(argv[i + 1]); i++; } else if (strcmpignorecase("-showcredits", argv[i]) == 0) { showCredits = TRUE; } else if (strstr(argv[i], "-lang") != NULL) { if (i + 1 >= argc) { printf("You must specify a language to use\n"); printf("Type %s -h for help\n", argv[0]); exit(1); } languageID = i + 1; i++; } else if (strstr(argv[i], "-h") != NULL || strstr(argv[i], "-help") != NULL) { printf("The Legend of Edgar options\n\n"); printf("\t-record <filename>: Captures keyboard input\n"); printf("\t-playback <filename>: Replays keyboard input\n"); printf("\t-load <save_slot>: Loads the game in slot <save_slot>. Slots start at 0\n"); printf("\t-nojoystick: Disables the joystick\n"); printf("\t-joystick <joystick_slot>: Use joystick <joystick_slot>. Slots start at 0\n"); printf("\t-showcredits: Shows the end credits\n"); printf("\t-language <language_code>: Use language <language_code>. e.g. en_US, es, pl\n\n"); exit(0); } #if DEV == 1 else if (strcmpignorecase("-saveonexit", argv[i]) == 0) { game.saveOnExit = TRUE; } else if (strcmpignorecase("-bmpwrite", argv[i]) == 0) { setScreenshotDir(argv[i + 1]); i++; } else { mapID = i; } #endif } setLanguage("edgar", languageID == -1 ? NULL : argv[languageID]); printf("Numeric is %s\n", setlocale(LC_NUMERIC, "C")); printf("atof(2.75) is %f\n", atof("2.75")); /* Call the cleanup function when the program exits */ atexit(cleanup); /* Start up SDL */ init(_("The Legend of Edgar"), joystick); loadRequiredResources(); if (replayingID != -1 && recordingID != -1) { showErrorAndExit("Cannot record and replay at the same time"); } #if DEV == 0 verifyVersion(); #endif /* Initialise the game variables */ freeGameResources(); initGame(); if (loadSlot == -1) { if (recordingID != -1) { setRecordData(argv[recordingID]); setMapFile(mapID == -1 ? "map01" : argv[mapID]); } else if (replayingID != -1) { setReplayData(argv[replayingID], TRUE); setMapFile(mapID == -1 ? "map01" : argv[mapID]); } if (mapID != -1) { startOnMap(argv[mapID]); } else if (game.firstRun == TRUE) { tutorial(); } else { game.status = IN_TITLE; } } else { if (recordingID != -1) { game.gameType = RECORDING; } else if (replayingID != -1) { game.gameType = REPLAYING; } if (loadGame(loadSlot) == FALSE) { showErrorAndExit("No saved game in slot %d", loadSlot); } if (recordingID != -1) { setRecordData(argv[recordingID]); setMapFile(getMapFilename()); } else if (replayingID != -1) { setReplayData(argv[replayingID], TRUE); } } /* Loop indefinitely for messages */ game.startTicks = SDL_GetTicks(); #if DEV == 1 printf("DEV Version\n"); #else printf("Production Version\n"); #endif frameLimit = SDL_GetTicks() + game.fps; if (showCredits == TRUE) { game.status = IN_CREDITS; } while (go == TRUE) { getInput(game.gameType); switch (game.status) { case IN_TITLE: doTitle(); break; case IN_GAME: freeCollisionGrid(); clearDrawLayers(); doGame(); doPlayer(); doInventory(); doMap(); doEntities(); doDecorations(); doCollisions(); doHud(); doDialogBox(); processMedals(); break; case IN_INVENTORY: doInventoryMenu(); break; case IN_MENU: doMenu(); break; case IN_CREDITS: freeCollisionGrid(); clearDrawLayers(); doGame(); doCredits(); doDecorations(); doCollisions(); break; default: doMenu(); break; } draw(); /* Sleep briefly to stop sucking up all the CPU time */ delay(frameLimit); frameLimit = SDL_GetTicks() + game.fps; game.frames++; } /* Exit the program */ exit(0); }