static void dispose (GObject *object) { NMAWirelessDialogPrivate *priv = NMA_WIRELESS_DIALOG_GET_PRIVATE (object); if (!priv->disposed) return; priv->disposed = TRUE; model_free (priv->device_model, D_NAME_COLUMN); model_free (priv->connection_model, C_NAME_COLUMN); gtk_combo_box_set_model (GTK_COMBO_BOX (priv->sec_combo), NULL); if (priv->group) g_object_unref (priv->group); if (priv->connection) g_object_unref (priv->connection); if (priv->device) g_object_unref (priv->device); if (priv->ap) g_object_unref (priv->ap); g_object_unref (priv->builder); if (priv->client) g_object_unref (priv->client); G_OBJECT_CLASS (nma_wireless_dialog_parent_class)->dispose (object); }
END_TEST START_TEST (load_from_file) { size_t yaml_size = strlen((char *)YAML); FILE *input = tmpfile(); size_t written = fwrite(YAML, sizeof(char), yaml_size, input); assert_uint_eq(written, yaml_size); int ret = fflush(input); assert_int_eq(0, ret); rewind(input); loader_context *loader = make_file_loader(input); assert_not_null(loader); assert_int_eq(LOADER_SUCCESS, loader_status(loader)); document_model *model = load(loader); assert_not_null(model); assert_model_state(loader, model); fclose(input); model_free(model); loader_free(loader); }
Model *obj_load_model(char *filename) { Model *model; Obj *objFile; model = model_get_by_filename(filename); if (model) { return model; } objFile = obj_load(filename); if (!objFile) { return NULL; } model = model_new(); if (!model) { return NULL; } if (obj_file_convert_to_model(objFile,model) != 0) { model_free(model); return NULL; } return model; }
int main(int argc, char *argv[]) { // l10n setlocale(LC_MESSAGES, ""); bindtextdomain("comar", "/usr/share/locale"); bind_textdomain_codeset("comar", "UTF-8"); bind_textdomain_codeset("libc", "UTF-8"); textdomain("comar"); // Parse commandline options cfg_init(argc, argv); // Only root can register bus if (getuid() != 0) { puts(_("System service should be started as root.")); exit(1); } // Initialize main process if (proc_init(argc, argv, "Comar") != 0) { exit(1); } // Start logging if (log_start() != 0) { exit(1); } // Load models if (model_init() != 0) { exit(1); } // Initialize DB if (db_init() != 0) { exit(1); } while (!shutdown_activated) { // Listen for DBus calls dbus_listen(); if (!shutdown_activated) { log_info("DBus connection is lost. Waiting 5 seconds and trying again...\n"); sleep(5); } } model_free(); proc_finish(); return 0; }
void game_exit(void) { /*** debug ***/ fprintf(stdout, "[Trace] game_exit()\n"); // free model resources if (model_ptr != NULL) { model_free(&model_ptr); } // game_free called by framework // screens are freed by game_free }
int main(int argc, char **argv) { Gfx gfx; Model model; if(argc != 2) { printf("Invalid count of arguments!\nUsage %s FILENAME\n", argv[0]); return 1; } if(load_map(argv[1], &model)) return 1; model_static_field(&model); gfx_init(&gfx, model.w, model.h); double t = SDL_GetTicks()/1000.-0.016; while(1) { int i; int nsteps = 5; if(gfx_event(&gfx) < 0) break; double dt = SDL_GetTicks()/1000.-t; dt = 0.06; if(nsteps < dt*C/DX*5) { nsteps = dt*C/DX*5+0.5; printf("nsteps %d\n", nsteps); if(nsteps > 30) nsteps = 30; } for(i = 0; i < nsteps; i++) model_update(&model, dt/nsteps); double rho = 0; int x, y; for(x = 0; x < model.w; x++) { for(y = 0; y < model.h; y++) { rho += model.rho[y*model.w+x]; } } double tdt = SDL_GetTicks()/1000.-t; t = SDL_GetTicks()/1000.; printf("%f fps aval = %g\n", 1./tdt, model.phi[model.tp][model.h/2*model.w+model.w*8/10-2]); gfx_draw(&gfx, &model); } gfx_deinit(&gfx); model_free(&model); return 0; }
int model_gc_lua(lua_State *L) { model_t **pmf_ud = (model_t **)lua_touserdata(L, 1); model_t *pmf = *pmf_ud; if(pmf != NULL) { #ifdef ALLOW_EXPLICIT_FREE printf("Freeing pmf @ %p\n", pmf); #endif model_free(pmf); } return 0; }
/* ----- [ ai_start ] ------------------------------------------------------- */ void *ai_start(void *arg) { struct Var_conf *config = (struct Var_conf *) arg; SDL_Rect pos; int turning = 0; int i = 0,j = 0; int old_autopause = ENABLE_AUTOPAUSE; // Disable auto pause ENABLE_AUTOPAUSE = 0; // Init copy of model struct model *model_tmp = model_clone(config->model); printf("AI launched\n"); while((config->state == J_PLAY || config->state == J_PAUSE) && config->ai_started) { if(config->state == J_PLAY) { ai_best_pos(config,model_tmp,&pos,&turning); for(i = 0; i < turning;i++) { SDL_Event event; event.type = SDL_KEYDOWN; event.key.keysym.sym = SDLK_UP; if(SDL_PushEvent(&event) != 0) fprintf(stderr, "[ERROR] Cannot push keydown event in %s at line %d.\n", __FILE__,__LINE__); //turn(config,config->model,&(config->pc_cur_id),&(config->piece_pos), //HORLO,1,1); //SDL_Delay(INTERV_NEW/5); SDL_Delay(50); } ai_go(config,pos); } //SDL_Delay(INTERV_NEW/5); SDL_Delay(100); } printf("AI stopped\n"); // Free copy of model model_free(model_tmp); // Enable auto pause ENABLE_AUTOPAUSE = old_autopause; }
END_TEST START_TEST (duplicate_warn) { document_model *model = duplicate_setup(DUPE_WARN); node *root = model_document_root(model, 0); node *one = mapping_get(root, (uint8_t *)"one", 3ul); assert_noerr(); assert_not_null(one); assert_node_kind(one, SCALAR); assert_scalar_value(one, "bar"); assert_scalar_kind(one, SCALAR_STRING); model_free(model); }
END_TEST START_TEST (duplicate_fail) { size_t yaml_size = strlen((char *)DUPLICATE_KEY_YAML); loader_context *loader = make_string_loader(DUPLICATE_KEY_YAML, yaml_size); assert_not_null(loader); loader_set_dupe_strategy(loader, DUPE_FAIL); document_model *model = load(loader); assert_loader_failure(loader, ERR_DUPLICATE_KEY); model_free(model); loader_free(loader); }
END_TEST START_TEST (load_from_string) { size_t yaml_size = strlen((char *)YAML); loader_context *loader = make_string_loader(YAML, yaml_size); assert_not_null(loader); document_model *model = load(loader); assert_not_null(model); assert_int_eq(LOADER_SUCCESS, loader_status(loader)); assert_model_state(loader, model); model_free(model); loader_free(loader); }
void docking_cleanup(struct dock_pak *dock, struct task_pak *task) { gchar *path, *ext; GSList *list, *dir_list; struct file_pak *file; struct model_pak model; /* temporary model data storage */ model_init(&model); /* load gulp file energies into project */ /* TODO - scan for .res files */ /* TODO - if none - scan .got file for possible error reports */ path = dock->path; /* scan project directory for .res files */ dir_list = file_dir_list(path, 0); for (list=dir_list ; list ; list=g_slist_next(list)) { file = get_file_info(list->data, BY_EXTENSION); if (file) { if (file->id == GULP) { ext = find_char(list->data, '.', LAST); if (g_ascii_strncasecmp(ext, ".res", 3) == 0) { /* process dump file */ printf("%s\n", (gchar *) list->data); } } if (file->id == GULPOUT) { /* process output file */ read_gulp_output(list->data, &model); printf("%s : %f\n", (gchar *) list->data, model.gulp.energy); } } } /* cleanup */ g_slist_free(dir_list); model_free(&model); }
entity_t::~entity_t() { svec_iter_t iter; model_t *model; bvh_t *anim; svec_iter_init(&iter, &modelv); while ( (model = (model_t *) svec_iter_next(&iter)) ) model_free(model); svec_iter_init(&iter, &animv); while ( (anim = (bvh_t *) svec_iter_next(&iter)) ) bvh_free(anim); svec_free(&modelv); svec_free(&animv); }
int model_load(struct model *m, const char *path, int orf_thresh_level) { m->substmat = NULL; m->codon_scores = m->orf_thresh = NULL; m->substmat = uproc_substmat_load(UPROC_IO_GZIP, "%s/substmat", path); if (!m->substmat) { goto error; } m->codon_scores = uproc_matrix_load(UPROC_IO_GZIP, "%s/codon_scores", path); if (!m->codon_scores) { goto error; } switch (orf_thresh_level) { case 1: case 2: m->orf_thresh = uproc_matrix_load(UPROC_IO_GZIP, "%s/orf_thresh_e%d", path, orf_thresh_level); if (!m->orf_thresh) { goto error; } break; case 0: break; default: uproc_error_msg( UPROC_EINVAL, "ORF threshold level must be 0, 1, or 2"); goto error; } return 0; error: model_free(m); return -1; }
int main(int argc, char *argv[]) { char *program_name = argv[0]; double l2_sigma_sq = 0.0; int grafting = 0; int grafting_light = 0; lbfgs_parameter_t params; lbfgs_parameter_init(¶ms); params.past = 1; params.delta = 1e-7; int ch; while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) { switch (ch) { case OPTION_FTOL: params.ftol = str_to_double(optarg); break; case OPTION_GTOL: params.gtol = str_to_double(optarg); break; case OPTION_GRAFTING: grafting = str_to_int(optarg); break; case OPTION_GRAFTING_LIGHT: grafting_light = str_to_int(optarg); break; case OPTION_L1: params.orthantwise_c = str_to_double(optarg); break; case OPTION_L2: l2_sigma_sq = str_to_double(optarg); break; case OPTION_LINESEARCH: if (strcmp(optarg, "armijo") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING_ARMIJO; else if (strcmp(optarg, "backtracking") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING; else if (strcmp(optarg, "wolfe") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING_WOLFE; else if (strcmp(optarg, "strong_wolfe") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE; else { usage(program_name); return 1; } break; case OPTION_MINSTEP: fprintf(stderr,"backtracking\n"); params.min_step = str_to_double(optarg); break; case OPTION_MAXSTEP: params.max_step = str_to_double(optarg); break; case '?': default: usage(program_name); return 1; } } argc -= optind; argv += optind; if (argc != 0 && argc != 1) { usage(program_name); return 1; } if (grafting && grafting_light) { fprintf(stderr, "Grafting and grafting-light cannot be used simultaneously..."); return 1; } if ((grafting || grafting_light) && params.orthantwise_c == 0.) { fprintf(stderr, "Grafting requires a l1 norm coefficient..."); return 1; } fprintf(stderr, "l1 norm coefficient: %.4e\n", params.orthantwise_c); fprintf(stderr, "l2 prior sigma^2: %.4e\n\n", l2_sigma_sq); dataset_t ds; int fd = 0; if (argc == 1 && (fd = open(argv[0], O_RDONLY)) == -1) { fprintf(stderr, "Could not open %s\n", argv[0]); return 1; } int r = read_tadm_dataset(fd, &ds); if (r != TADM_OK) { fprintf(stderr, "Error reading data...\n"); return 1; } fprintf(stderr, "Features: %zu\n", ds.n_features); fprintf(stderr, "Contexts: %zu\n\n", ds.n_contexts); if (params.orthantwise_c != 0.0) { params.orthantwise_end = ds.n_features; // l1 prior only works with backtracking linesearch. params.linesearch = LBFGS_LINESEARCH_BACKTRACKING; } model_t model; if (grafting || grafting_light) model_new(&model, ds.n_features, true); else model_new(&model, ds.n_features, false); fprintf(stderr, "Iter\t-LL\t\txnorm\t\tgnorm\n\n"); if (grafting) r = maxent_lbfgs_grafting(&ds, &model, ¶ms, l2_sigma_sq, grafting); else if (grafting_light) r = maxent_lbfgs_grafting_light(&ds, &model, ¶ms, l2_sigma_sq, grafting_light); else r = maxent_lbfgs_optimize(&ds, &model, ¶ms, l2_sigma_sq); dataset_free(&ds); if (r != LBFGS_STOP && r != LBFGS_SUCCESS && r != LBFGS_ALREADY_MINIMIZED) { fprintf(stderr, "%s\n\n", err_to_string(lbfgs_errs, r)); model_free(&model); return 1; } for (int i = 0; i < ds.n_features; ++i) printf("%.8f\n", model.params[i]); model_free(&model); return 0; }
void testBaumwelch(){ int i, error, tl,z,z1,z2; double log_p,first_prob1,first_prob2, first_prob; double *proba; int *path; int* real_path; int *path1; int* real_path1; int *path2; int* real_path2; model *mo = NULL; sequence_t *my_output, *your_output; int seqlen = 1000; tl = 150; mo = malloc(sizeof(model)); if (mo==NULL) {fprintf(stderr,"Null Pointer in malloc(model).\n");} real_path = malloc(seqlen*sizeof(double)); if(!real_path){ printf("real_path hat kein platz gekriegt\n");} real_path1 = malloc(seqlen*sizeof(double)); if(!real_path1){ printf("real_path hat kein platz gekriegt\n");} real_path2 = malloc(seqlen*sizeof(double)); if(!real_path2){ printf("real_path hat kein platz gekriegt\n");} /* generate a model with variable number of states*/ generateModel(mo, 5); /*generate a random sequence*/ my_output = model_label_generate_sequences(mo, 0, seqlen, 10, seqlen); for (i=0; i<seqlen; i++){ printf("%d", my_output->state_labels[0][i]); } printf("\n"); /*viterbi*/ path = viterbi(mo, my_output->seq[0], my_output->seq_len[0], &first_prob); path1 = viterbi(mo, my_output->seq[1], my_output->seq_len[1], &first_prob1); path2 = viterbi(mo, my_output->seq[2], my_output->seq_len[2], &first_prob2); printf("\n viterbi-path\n"); z=0; z1=0; z2=0; for (i=0; i<my_output->seq_len[0]; i++){ if (path1[i] != -1) { real_path1[z1]=path1[i]; z1++; printf("%d", path1[i]); } else printf("hallo"); if (path2[i] != -1) { real_path2[z2]=path2[i]; z2++; printf("%d", path2[i]); } else printf("hallo"); if (path[i] != -1) { real_path[z]=path[i]; z++; printf("%d", path[i]); } } printf("\n"); printf("log-prob: %g\n",first_prob); my_output->state_labels[0]=real_path; my_output->state_labels[1]=real_path1; my_output->state_labels[2]=real_path2; for (i=0;i<seqlen;i++) printf("realpath[%i]=%i",i,real_path[i]); proba = malloc(sizeof(double)*tl); printf("No of Sequences = %d", my_output->seq_number); your_output = model_label_generate_sequences(mo, 0, seqlen, 1, seqlen); error = gradient_descent(&mo, your_output, .02, i); path = viterbi(mo, my_output->seq[0], my_output->seq_len[0], &log_p); free(path); /*reestimate_baum_welch_label(mo, my_output);*/ /*reestimate_baum_welch(mo, my_output);*/ /*reruns viterbi to check the training*/ printf("run viterbi second\n"); path = viterbi(mo, my_output->seq[0], my_output->seq_len[0], &log_p); for (i=0; i<(my_output->seq_len[0]*mo->N); i++){ if (path[i] != -1) {printf("%d", path[i]);} } printf("\n"); printf("log-prob: %g\n",log_p); /* freeing memory */ model_free(&mo); free(path); /*printf("sequence_free success: %d\n", */sequence_free(&my_output)/*)*/; free(my_output); }
void tag_teardown(void) { model_free(tagged_model); tagged_model = NULL; tagged_mapping_root = NULL; }
void model_teardown(void) { model_free(model); }
static gboolean connection_combo_init (NMAWirelessDialog *self, NMConnection *connection) { NMAWirelessDialogPrivate *priv = NMA_WIRELESS_DIALOG_GET_PRIVATE (self); GtkListStore *store; GtkTreeIter tree_iter; GtkWidget *widget; GtkCellRenderer *renderer; g_return_val_if_fail (priv->connection == NULL, FALSE); /* Clear any old model */ model_free (priv->connection_model, C_NAME_COLUMN); /* New model */ store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_OBJECT, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN); priv->connection_model = GTK_TREE_MODEL (store); if (connection) { NMSettingConnection *s_con; const char *id; s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION)); g_assert (s_con); id = nm_setting_connection_get_id (s_con); g_assert (id); gtk_list_store_append (store, &tree_iter); gtk_list_store_set (store, &tree_iter, C_NAME_COLUMN, id, C_CON_COLUMN, connection, -1); priv->connection = g_object_ref (connection); } widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "connection_combo")); gtk_cell_layout_clear (GTK_CELL_LAYOUT (widget)); renderer = gtk_cell_renderer_text_new (); gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (widget), renderer, TRUE); gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (widget), renderer, "text", C_NAME_COLUMN); gtk_combo_box_set_wrap_width (GTK_COMBO_BOX (widget), 1); gtk_combo_box_set_model (GTK_COMBO_BOX (widget), priv->connection_model); gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (widget), connection_combo_separator_cb, NULL, NULL); gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 0); g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (connection_combo_changed), self); gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->builder, "connection_label"))); gtk_widget_hide (widget); return TRUE; }
int main(int argc, char* argv[]) { // Crear una ventana de 500x500 pixels: int cw = 640; int ch = 480; cg_init(cw, ch, NULL); init_cggl(cw, ch); Model* model = model_new("knight"); int done = 0; while (!done) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: done = 1; break; case SDLK_UP: rot_speed += 5.0f; break; case SDLK_DOWN: rot_speed -= 5.0f; break; case SDLK_RIGHT: frame += 1; if (frame >= model->md2_model->header.num_frames) frame = 0; break; case SDLK_LEFT: frame -= 1; if (frame < 0) frame = model->md2_model->header.num_frames - 1; break; case SDLK_m: if (mode == CG_TRIANGLES) mode = CG_LINES; else mode = CG_TRIANGLES; break; default: break; } break; case SDL_QUIT : done = 1; break; default: break; } } cg_clear(); paint_model(model); // Actualizar la pantalla: cg_repaint(); double delta = get_delta(); rot_x += rot_speed*delta; rot_y += rot_speed*delta; rot_z += rot_speed*delta; } // Liberar recursos: model_free(model); cgCloseCGGL(); cg_close(); return 0; }
static int megahal_learn(brain_t brain, list_t *input) { uint_fast32_t i; uint32_t size; number_t order; model_t *model; int ret; (void)brain; /* We only learn from inputs which are long enough */ ret = db_model_get_order(brain, &order); if (ret) return ret; ret = list_size(input, &size); if (ret) return ret; if (size <= order) return OK; ret = model_alloc(brain, &model); if (ret) return ret; /* * Train the model in the forwards direction. Start by initializing * the context of the model. */ ret = model_init(model, MODEL_FORWARD); if (ret) goto fail; for (i = 0; i < size; i++) { word_t word; /* Get word symbol. */ ret = list_get(input, i, &word); if (ret) goto fail; /* Update the forward model. */ ret = model_update(model, word, 1); if (ret) goto fail; } /* * Add the sentence-terminating symbol. */ ret = model_update(model, 0, 1); if (ret) goto fail; /* * Train the model in the backwards direction. Start by initializing * the context of the model. */ ret = model_init(model, MODEL_BACKWARD); if (ret) goto fail; for (i = 0; i < size; i++) { word_t word; /* Get word symbol. */ ret = list_get(input, (size - 1) - i, &word); if (ret) goto fail; /* Update the backward model. */ ret = model_update(model, word, 1); if (ret) goto fail; } /* * Add the sentence-terminating symbol. */ ret = model_update(model, 0, 1); if (ret) goto fail; model_free(&model); return OK; fail: model_free(&model); return ret; }
bool_t model_md2_save(const model_t *orig_model, xbuf_t *xbuf, char **out_error) { char *error; int skinwidth, skinheight; char **skinfilenames; const skininfo_t *skininfo; model_t *model; const mesh_t *mesh; md2_data_t *md2data; md2_header_t *header; dtriangle_t *dtriangles; int i, j, k; model = model_merge_meshes(orig_model); mesh = &model->meshes[0]; skinwidth = (texwidth != -1) ? texwidth : 0; skinheight = (texheight != -1) ? texheight : 0; for (i = 0, skininfo = model->skininfo; i < model->num_skins; i++, skininfo++) { for (j = 0; j < skininfo->num_skins; j++) { int offset = skininfo->skins[j].offset; if (!mesh->skins[offset].components[SKIN_DIFFUSE]) { if (out_error) *out_error = msprintf("Model has missing skin."); model_free(model); return false; } if (skinwidth && skinheight && (skinwidth != mesh->skins[offset].components[SKIN_DIFFUSE]->width || skinheight != mesh->skins[offset].components[SKIN_DIFFUSE]->height)) { if (out_error) *out_error = msprintf("Model has skins of different sizes. Use -texwidth and -texheight to resize all images to the same size"); model_free(model); return false; } skinwidth = mesh->skins[offset].components[SKIN_DIFFUSE]->width; skinheight = mesh->skins[offset].components[SKIN_DIFFUSE]->height; /* if fullbright texture is a different size, resample it to match the diffuse texture */ if (mesh->skins[offset].components[SKIN_FULLBRIGHT] && (mesh->skins[offset].components[SKIN_FULLBRIGHT]->width != skinwidth || mesh->skins[offset].components[SKIN_FULLBRIGHT]->height != skinheight)) { image_rgba_t *image = image_resize(mem_globalpool, mesh->skins[offset].components[SKIN_FULLBRIGHT], skinwidth, skinheight); image_free(&mesh->skins[offset].components[SKIN_FULLBRIGHT]); mesh->skins[offset].components[SKIN_FULLBRIGHT] = image; } } } if (!skinwidth || !skinheight) { if (out_error) *out_error = msprintf("Model has no skin. Use -texwidth and -texheight to set the skin dimensions, or -tex to import a skin"); model_free(model); return false; } /* create 8-bit skins and save them to PCX files */ skinfilenames = (char**)qmalloc(sizeof(char*) * model->num_skins); for (i = 0, skininfo = model->skininfo; i < model->num_skins; i++, skininfo++) { image_paletted_t *pimage; int offset = skininfo->skins[0].offset; /* skingroups not supported, just take the first skin from the group */ skinfilenames[i] = md2_create_skin_filename(skininfo->skins[0].name); pimage = image_palettize(mem_globalpool, &palette_quake2, mesh->skins[offset].components[SKIN_DIFFUSE], mesh->skins[offset].components[SKIN_FULLBRIGHT]); /* FIXME - this shouldn't be a fatal error */ if (!image_paletted_save(skinfilenames[i], pimage, &error)) { if (out_error) *out_error = msprintf("Failed to write %s: %s", skinfilenames[i], error); qfree(error); qfree(pimage); for (j = 0; j < i; j++) qfree(skinfilenames[j]); qfree(skinfilenames); model_free(model); return false; } qfree(pimage); } /* optimize vertices for md2 format */ md2data = md2_process_vertices(model, mesh, skinwidth, skinheight); /* write header */ header = (md2_header_t*)xbuf_reserve_data(xbuf, sizeof(md2_header_t)); memcpy(header->ident, "IDP2", 4); header->version = LittleLong(8); header->skinwidth = LittleLong(skinwidth); header->skinheight = LittleLong(skinheight); header->framesize = LittleLong(sizeof(daliasframe_t) + sizeof(dtrivertx_t) * md2data->numvertices); header->num_skins = LittleLong(model->num_skins); header->num_vertices = LittleLong(md2data->numvertices); header->num_st = LittleLong(md2data->numtexcoords); header->num_tris = LittleLong(mesh->num_triangles); header->num_glcmds = 0; /* filled in later */ header->num_frames = LittleLong(model->num_frames); header->offset_skins = 0; header->offset_st = 0; header->offset_tris = 0; header->offset_frames = 0; header->offset_glcmds = 0; header->offset_end = 0; /* write skins */ header->offset_skins = LittleLong(xbuf_get_bytes_written(xbuf)); for (i = 0; i < model->num_skins; i++) { md2_skin_t md2skin; strlcpy(md2skin.name, skinfilenames[i], sizeof(md2skin.name)); xbuf_write_data(xbuf, sizeof(md2_skin_t), &md2skin); } /* write texcoords */ for (i = 0; i < md2data->numtexcoords; i++) { md2data->texcoords[i].s = LittleShort(md2data->texcoords[i].s); md2data->texcoords[i].t = LittleShort(md2data->texcoords[i].t); } header->offset_st = LittleLong(xbuf_get_bytes_written(xbuf)); xbuf_write_data(xbuf, sizeof(dstvert_t) * md2data->numtexcoords, md2data->texcoords); /* write triangles */ dtriangles = (dtriangle_t*)qmalloc(sizeof(dtriangle_t) * mesh->num_triangles); for (i = 0; i < mesh->num_triangles; i++) { for (j = 0; j < 3; j++) { dtriangles[i].index_xyz[j] = LittleShort(md2data->vertex_lookup[mesh->triangle3i[i*3+j]]); dtriangles[i].index_st[j] = LittleShort(md2data->texcoord_lookup[mesh->triangle3i[i*3+j]]); } } header->offset_tris = LittleLong(xbuf_get_bytes_written(xbuf)); xbuf_write_data(xbuf, sizeof(dtriangle_t) * mesh->num_triangles, dtriangles); /* write frames */ header->offset_frames = LittleLong(xbuf_get_bytes_written(xbuf)); for (i = 0; i < model->num_frames; i++) { daliasframe_t md2frame; for (j = 0; j < 3; j++) { md2frame.scale[j] = LittleFloat(md2data->frames[i].scale[j]); md2frame.translate[j] = LittleFloat(md2data->frames[i].translate[j]); } strlcpy(md2frame.name, model->frameinfo[i].frames[0].name, sizeof(md2frame.name)); xbuf_write_data(xbuf, sizeof(daliasframe_t), &md2frame); /* write vertices */ for (j = 0; j < md2data->numvertices; j++) { const dtrivertx_t *md2vertex = md2data->original_vertices + md2data->vertices[j] * model->num_frames + i; dtrivertx_t vtx; for (k = 0; k < 3; k++) vtx.v[k] = md2vertex->v[k]; vtx.lightnormalindex = md2vertex->lightnormalindex; xbuf_write_data(xbuf, sizeof(dtrivertx_t), &vtx); } } /* write glcmds */ triangles = dtriangles; num_tris = mesh->num_triangles; md2_build_glcmds(md2data->texcoords, skinwidth, skinheight); header->num_glcmds = LittleLong(numcommands); header->offset_glcmds = LittleLong(xbuf_get_bytes_written(xbuf)); for (i = 0; i < numcommands; i++) commands[i] = LittleLong(commands[i]); xbuf_write_data(xbuf, sizeof(int) * numcommands, commands); /* write end */ header->offset_end = LittleLong(xbuf_get_bytes_written(xbuf)); /* done */ qfree(dtriangles); md2_free_data(md2data); for (i = 0; i < model->num_skins; i++) qfree(skinfilenames[i]); qfree(skinfilenames); model_free(model); return true; }
void command_main_loop(int argc, char **argv) { gint n, status, num_tokens; gchar *inp, *out, *ext, *tmp, *line, *base, **buff; gboolean exit=FALSE; GSList *list, *files, *item, *structures; struct file_pak *inp_pak, *out_pak; struct model_pak *model; if (argc < 2) printf("gdis> Commandline interface. Enter ? for help.\n"); /* command line */ do { /* if additional keywords - process as single command line */ if (argc > 1) { /* convert options into single string for processing */ buff = g_malloc(argc*sizeof(gchar *)); for (n=1 ; n<argc ; n++) *(buff+n-1) = g_strdup(argv[n]); *(buff+argc-1) = NULL; line = g_strjoinv(" ", buff); g_strfreev(buff); exit = TRUE; } else { /* standard repeating prompt */ printf("gdis> "); line = file_read_line(stdin); } /* quit primitive */ if (g_ascii_strncasecmp("qu", line, 2) == 0 || g_ascii_strncasecmp("ex", line, 2) == 0) exit=TRUE; /* test primitive */ if (g_ascii_strncasecmp("te", line, 2) == 0) { buff = tokenize(line, &num_tokens); for (n=1 ; n<num_tokens ; n++) test_run(*(buff+n)); g_strfreev(buff); } /* copy primitive */ if (g_ascii_strncasecmp("co", line, 2) == 0 || g_ascii_strncasecmp("cp", line, 2) == 0) { buff = tokenize(line, &num_tokens); if (num_tokens > 2) { out_pak = get_file_info(*(buff+2), BY_EXTENSION); if (!out_pak) { printf("Error: unrecognized output filetype.\n"); break; } if (!out_pak->write_file) { printf("Error: write method for this filetype does not exist.\n"); break; } /* FIXME! - this is all wrong - should be no dependence on sysenv.cwd for path */ /* since this falls over with something like prompt:>gdis cp /tmp/gok.sot /tmp/dest/gok.meta */ ext = file_extension_get(*(buff+2)); files = file_dir_list_pattern(sysenv.cwd, *(buff+1)); for (list=files ; list ; list=g_slist_next(list)) { inp_pak = get_file_info(list->data, BY_EXTENSION); inp = g_build_filename(sysenv.cwd, list->data, NULL); /* FIXME - changed to correct a problem with Florian's metadata store */ // base = parse_strip(list->data); base = g_strdup(list->data); if (inp_pak) { if (inp_pak->read_file) { model = model_new(); status = inp_pak->read_file(inp, model); structures = g_slist_find(sysenv.mal, model); #if DEBUG_COMMAND_MAIN_LOOP printf("Items loaded = %d [status = %d]\n", g_slist_length(structures), status); #endif if (g_slist_length(structures) > 1) { /* multiple output for input files that contain more than one structure */ n = 1; for (item=structures ; item ; item=g_slist_next(item)) { model = item->data; tmp = g_strdup_printf("%s_%d.%s", base, n, ext); out = g_build_filename(sysenv.cwd, tmp, NULL); status = out_pak->write_file(out, model); #if DEBUG_COMMAND_MAIN_LOOP printf("a [%s] -> [%s]\n", inp, out); #endif g_free(out); g_free(tmp); model_free(model); n++; } } else { tmp = g_strdup_printf("%s.%s", base, ext); out = g_build_filename(sysenv.cwd, tmp, NULL); g_free(tmp); status = out_pak->write_file(out, model); #if DEBUG_COMMAND_MAIN_LOOP printf("b [%s] -> [%s]\n", inp, out); #endif g_free(out); model_free(model); } } else printf("Error: read method for this filetype does not exist.\n"); } else { printf("Error: unrecognized input filetype.\n"); } g_free(base); g_free(inp); } /* CURRENT hack */ if (!files) { /* FIXME */ } g_free(ext); free_slist(files); } else printf("Error: insufficient tokens for <copy> command\n"); g_strfreev(buff); } if (g_ascii_strncasecmp("ls", line, 2) == 0) { buff = tokenize(line, &num_tokens); if (num_tokens > 1) files = file_dir_list_pattern(sysenv.cwd, *(buff+1)); else files = file_dir_list(sysenv.cwd, FALSE); for (list=files ; list ; list=g_slist_next(list)) { printf("%s\n", (gchar *) list->data); } g_strfreev(buff); } if (g_ascii_strncasecmp("cd", line, 2) == 0) { buff = tokenize(line, &num_tokens); if (num_tokens > 1) { inp = g_build_path(DIR_SEP, sysenv.cwd, *(buff+1), NULL); set_path(inp); g_free(inp); } printf("%s\n", sysenv.cwd); g_strfreev(buff); } if (g_ascii_strncasecmp("pwd", line, 3) == 0) { printf("%s\n", sysenv.cwd); } if (g_ascii_strncasecmp("?", line, 1) == 0 || g_ascii_strncasecmp("h", line, 1) == 0) { printf("GDIS v%4.2f available commands\n\n", VERSION); printf("cd <destination>\n"); printf(" - changes to target directory\n"); printf("pwd\n"); printf(" - prints the current working directory\n"); printf("ls\n"); printf(" - lists the files in the current working directory\n"); printf("copy <source> <destination>\n"); printf(" - convert between filetypes, based on extension (wildcards allowed)\n"); printf("quit\n"); printf(" - exit program\n"); } /* done -> next line */ g_free(line); } while (!exit); }
model_t *model_parse_pmf(int len, const char *data) { int i,j; const char *p = data; const char *dend = data + len; // and now we crawl through the spec. // start with the header of "PMF",0x1A,1,0,0,0 char head[8]; if(memcmp(p, "PMF\x1A\x01\x00\x00\x00", 8)) { fprintf(stderr, "model_load_pmf: not a valid PMF v1 file\n"); return NULL; } p += 8; // then there's a uint32_t denoting how many body parts there are uint32_t bone_count = *(uint32_t *)p; p += 4; if(bone_count > MODEL_BONE_MAX) { fprintf(stderr, "model_parse_pmf: too many bones (%i > %i)\n" , bone_count, MODEL_BONE_MAX); return NULL; } model_t *pmf = model_new(bone_count); if(pmf == NULL) { error_perror("model_parse_pmf"); return NULL; } pmf->udtype = UD_PMF; // then, for each body part, for(i = 0; i < (int)bone_count; i++) { // there's a null-terminated 16-byte string (max 15 chars) denoting the part const char *namebuf = p; p += 16; if(namebuf[15] != '\x00') { fprintf(stderr, "model_load_pmf: name not null terminated\n"); model_free(pmf); return NULL; } // then there's a uint32_t denoting how many points there are in this body part uint32_t pt_count = *(uint32_t *)p; p += 4; // (just allocating the bone here) model_bone_t *bone = model_bone_new(pmf, pt_count); pmf = bone->parent; memcpy(bone->name, namebuf, 16); if(bone == NULL) { error_perror("model_parse_pmf"); model_free(pmf); return NULL; } // then there's a whole bunch of this: // uint16_t radius; // int16_t x,y,z; // uint8_t b,g,r,reserved; int bonelen = sizeof(model_point_t)*pt_count; memcpy(bone->pts, p, bonelen); p += bonelen; bone->ptlen = pt_count; // "reserved" needs to be 0 or else you suck // NO SIDECHANNELING YOUR NAME IN THERE // i'm going to enforce this in the loader // and will outright reject files which don't have 0 in ALL of these slots for(j = 0; j < bone->ptmax; j++) if(bone->pts[j].resv1 != 0) { fprintf(stderr, "model_load_pmf: file corrupted or made by a smartass\n"); model_free(pmf); return NULL; } // rinse, lather, repeat // units are 8:8 fixed point in terms of the vxl grid by default } return pmf; }
/* ----- [ undo_free_historic ] --------------------------------------------- */ static void undo_free_historic(struct Var_conf *config,struct Undo_i *item) { model_free(item->model); free(item); }