Ejemplo n.º 1
0
int main(int argc, char **argv) {

	srand(time(NULL));

	char ** cells = calloc(C_WIDTH, sizeof(char * ));

	for (int i = 0; i < C_WIDTH; ++i) {
		cells[i] = calloc(C_HEIGHT, sizeof(char));
	}

	char ** newCells = calloc(C_WIDTH, sizeof(char * ));

	for (int i = 0; i < C_WIDTH; ++i) {
		newCells[i] = calloc(C_HEIGHT, sizeof(char));
	}

	initCA(cells, C_WIDTH, C_HEIGHT);

	for (int i = 0; i < 1000; ++i) {
		nextGenCA(cells, newCells, C_WIDTH, C_HEIGHT);

		for (int j = 0; j <= C_HEIGHT; ++j) {
			for (int i = 0; i < C_WIDTH; ++i) {
				cells[i][j] = newCells[i][j];
			}
		}
	}

	int durationSeconds = 32;

	short int * audioData = createAudioData(newCells, C_WIDTH, C_HEIGHT, 44100, (44100 * durationSeconds));

	writeWav("gol.wav", (44100 * durationSeconds), audioData, 44100);

}
Ejemplo n.º 2
0
int main(int argc, char **argv) {

	srand(time(NULL));

	char ** cells = calloc(C_WIDTH, sizeof(char * ));

	for (int i = 0; i < C_WIDTH; ++i) {
		cells[i] = calloc(C_HEIGHT, sizeof(char));
	}

	initCA(cells, C_WIDTH, C_HEIGHT);

	for (int i = 0; i < C_HEIGHT; ++i) {
		nextGenCA(cells, ca110, (i+1), C_WIDTH, C_HEIGHT);
	}

	int durationSeconds = 32;

	short int * audioData = createAudioData(cells, C_WIDTH, C_HEIGHT, 44100, (44100 * durationSeconds));

	writeWav("out.wav", (44100 * durationSeconds), audioData, 44100);

}
int main(int argc, char** argv) {
    //start gnome_init
    initGUI(argc, argv);
    cellsWide = DEFAULT_WIDTH / PIX_PER_CELL;

    //GUI components
    GtkWidget *window;
    GtkWidget *imageEventBox, *mainHBox, *sideBox, *quitButton,
              *settingFrame, *resetButton, *sideVBox, *widthFrame,
              *saveButton, *hSep, *label, *lowerSideVBox, *checkButton,
              *rulesFrame, *rulesClearButton, *rulesSetButton,
              *rulesHBox, *rulesVBox;
    GdkPixbuf *icon_buf_16, *icon_buf_32, *icon_buf_48,
              *icon_buf_64, *icon_buf_128, *bg;

    GList *icons = NULL;

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    //load icons from files	
    icon_buf_16 = gdk_pixbuf_new_from_file(ICON_16, NULL);
    icon_buf_32 = gdk_pixbuf_new_from_file(ICON_32, NULL);
    icon_buf_48 = gdk_pixbuf_new_from_file(ICON_48, NULL);
    icon_buf_64 = gdk_pixbuf_new_from_file(ICON_64, NULL);
    icon_buf_128 = gdk_pixbuf_new_from_file(ICON_128, NULL);

    //tell gnome to use these icons
    icons = g_list_append(icons, icon_buf_16);
    icons = g_list_append(icons, icon_buf_32);
    icons = g_list_append(icons, icon_buf_48);
    icons = g_list_append(icons, icon_buf_64);
    icons = g_list_append(icons, icon_buf_128);
    gtk_window_set_icon_list(GTK_WINDOW(window), icons);

    //connect the delete_event signal to the delete_event function
    g_signal_connect (G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);

    //here follows formatting, packing, etc of gtk widgets
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    gtk_window_set_title(GTK_WINDOW(window), "Cellular Automata");

    widthAdjust = gtk_spin_button_new_with_range(WIDTH_LOWER, WIDTH_UPPER, 1);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(widthAdjust), DEFAULT_WIDTH);
    widthFrame = gtk_frame_new("Set Width");
    gtk_container_add(GTK_CONTAINER(widthFrame), widthAdjust);
    gtk_container_set_border_width(GTK_CONTAINER(widthFrame), 10);

    ruleAdjust = gtk_spin_button_new_with_range(0, 255, 1);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(ruleAdjust), 30);
    settingFrame = gtk_frame_new("Set Rule #");
    gtk_container_add(GTK_CONTAINER(settingFrame), ruleAdjust);
    gtk_container_set_border_width (GTK_CONTAINER(settingFrame), 10);
    bg = gdk_pixbuf_new_from_file("./data/cellular_automata.png", NULL);
    imagebuf = gdk_pixbuf_scale_simple(bg, WIDTH, HEIGHT, GDK_INTERP_BILINEAR);
    mainImage = gtk_image_new_from_pixbuf(imagebuf);

    imageEventBox = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(imageEventBox), mainImage);
    mainHBox = gtk_hbox_new(FALSE, 0);
    sideVBox = gtk_vbox_new(FALSE, 0);
    lowerSideVBox = gtk_vbox_new(TRUE, 0);
    sideBox = gtk_vbox_new(TRUE, 0);
    checkButton = gtk_check_button_new_with_mnemonic("_Autosave Images");
    quitButton = gtk_button_new_from_stock (GTK_STOCK_QUIT);
    startButton = gtk_button_new_with_mnemonic("_Start");
    stopButton = gtk_button_new_with_mnemonic ("_Stop");
    saveButton = gtk_button_new_from_stock(GTK_STOCK_SAVE);
    gtk_widget_set_sensitive(GTK_WIDGET(stopButton), FALSE);
    resetButton = gtk_button_new_with_mnemonic("_Reset");

    //connect the proper callback functions to the proper signals
    g_signal_connect_swapped (G_OBJECT(quitButton), "clicked", G_CALLBACK(gtk_main_quit), G_OBJECT (window));
    g_signal_connect_swapped (G_OBJECT(startButton),"clicked", G_CALLBACK(getLoopy), G_OBJECT(ruleAdjust));
    g_signal_connect_swapped (G_OBJECT(stopButton),"clicked", G_CALLBACK(stopLoopy), G_OBJECT(ruleAdjust));
    g_signal_connect_swapped (G_OBJECT(resetButton),"clicked", G_CALLBACK(resetImage), G_OBJECT(resetButton));
    g_signal_connect_swapped(G_OBJECT(ruleAdjust),"value-changed", G_CALLBACK(updateRule), GTK_WIDGET(ruleAdjust));
    g_signal_connect_swapped(G_OBJECT(widthAdjust),"value-changed", G_CALLBACK(initCA), GTK_WIDGET(widthAdjust));

    g_signal_connect_swapped(G_OBJECT(saveButton),"clicked", G_CALLBACK(getPic), GTK_WIDGET(saveButton));
    g_signal_connect_swapped(G_OBJECT(checkButton),"toggled", G_CALLBACK(toggleSave), GTK_WIDGET(checkButton));

    hSep = gtk_hseparator_new();
    label = gtk_label_new("Save Image");

    //load the meta-rule file names
    std::vector<std::string> rulesList;
    std::string rulesDir("./"), suff(".rules");
    getRulesList(rulesDir, rulesList, suff);

    rulesComboBox = gtk_combo_box_new_text();

    gtk_combo_box_append_text(GTK_COMBO_BOX(rulesComboBox), "Rule Set");

    for (uint i = 0; i < rulesList.size(); i++) {
        gtk_combo_box_append_text(GTK_COMBO_BOX(rulesComboBox), rulesList[i].c_str());
    }
    if (rulesList.size()) {
        gtk_combo_box_set_active(GTK_COMBO_BOX(rulesComboBox), 0);
    }

    rulesFrame = gtk_frame_new("Rule Change Controls");
    rulesHBox = gtk_hbox_new(FALSE, 0);
    rulesVBox = gtk_vbox_new(TRUE, 0);	
    rulesClearButton = gtk_button_new_with_mnemonic("_Clear Rules Rule");
    rulesSetButton = gtk_button_new_with_mnemonic("Set _Rules Rule");

    g_signal_connect(G_OBJECT(rulesComboBox), "changed", G_CALLBACK(combo_selected), rulesFrame);
    g_signal_connect_swapped(G_OBJECT(rulesClearButton), "clicked", G_CALLBACK(clearRules), G_OBJECT(rulesClearButton));

    autoResetButton = gtk_check_button_new_with_mnemonic("_Auto Reset on Dead Lines");
    randomizeLengthButton = gtk_check_button_new_with_mnemonic("Randomize _Time Between Rule Changes");

    g_signal_connect_swapped(G_OBJECT(autoResetButton), "toggled", G_CALLBACK(autoResetFunction), G_OBJECT(autoResetButton));
    g_signal_connect_swapped(G_OBJECT(randomizeLengthButton), "toggled", G_CALLBACK(randomizeLengthFunction), G_OBJECT(randomizeLengthButton));

    //pack the widgets into their proper containers
    gtk_box_pack_start(GTK_BOX(rulesHBox), rulesClearButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(rulesVBox), rulesComboBox, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(rulesVBox), rulesHBox, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(rulesVBox), autoResetButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(rulesVBox), randomizeLengthButton, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(rulesFrame), rulesVBox);
    gtk_box_pack_start(GTK_BOX(sideVBox), settingFrame, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(sideVBox), stopButton, FALSE, FALSE,0);
    gtk_box_pack_start(GTK_BOX(sideVBox), startButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(sideVBox), resetButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(sideBox), sideVBox, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(sideVBox), rulesFrame, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(lowerSideVBox), label, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(lowerSideVBox), saveButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(lowerSideVBox), checkButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(lowerSideVBox), hSep, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(lowerSideVBox), quitButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(sideBox), lowerSideVBox, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(mainHBox), sideBox, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(mainHBox), imageEventBox, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), mainHBox);

    gtk_widget_show_all(window);

    //load black and white pixels into buffers
    blackb = gdk_pixbuf_new_subpixbuf(imagebuf, 0, 0, PIX_PER_CELL, PIX_PER_CELL);
    black = gdk_pixbuf_copy(blackb);
    gdk_pixbuf_fill(black, 0x000000ff);
    whiteb = gdk_pixbuf_new_subpixbuf(imagebuf, 0, 0, PIX_PER_CELL, PIX_PER_CELL);
    white = gdk_pixbuf_copy(whiteb);
    gdk_pixbuf_fill(white, 0xffffffff);

    //setup the cellular automaton=========================================
    initCA();

    //run the main loop of the program
    gtk_main();
    exit(0);
}