/* fonction d'initialisation de notre chaine avec les 258 premiers elements de base * et declaration du tableau de pointeur sur un sous arbre-naire du dictionnaire * indexe par les codes */ arbre_n* init(){ arbre_n* ptr_tmp; chaine_octet* chaine_tmp = (chaine_octet*) malloc(sizeof(chaine_octet)); chaine_tmp->next = NULL; chaine_tmp->octet = 0; /* initialisation du premier element */ ptr_arbre_n = create_elem(0, chaine_tmp, 0); tab_arbre[0] = ptr_arbre_n; ptr_tmp = ptr_arbre_n; for (int i=1;i<256;i++){ chaine_tmp = (chaine_octet*) malloc(sizeof(chaine_octet)); chaine_tmp->next = NULL; chaine_tmp->octet = i; ptr_tmp->freres = create_elem(i, chaine_tmp, 0); // dernier element de la chaine pointe sur Nil ptr_tmp = ptr_tmp->freres; tab_arbre[i] = ptr_tmp; // on met le pointeur sur l'element i dans tab[i] } ptr_tmp->freres = create_elem(256, NULL, 0); // dernier element de la chaine pointe sur Nil ptr_tmp = ptr_tmp->freres; tab_arbre[256] = ptr_tmp; ptr_tmp->freres = create_elem(257, NULL, 0); // dernier element de la chaine pointe sur Nil ptr_tmp = ptr_tmp->freres; tab_arbre[257] = ptr_tmp; ptr_tmp->freres = create_elem(258, NULL, 0); // dernier element de la chaine pointe sur Nil tab_arbre[258] = ptr_tmp->freres; nb_elem = 259; return ptr_arbre_n; }
t_env *add_elem_end(t_env *list, char *name, char *arg) { t_env *tmp; tmp = list; if (!list) return (create_elem(name, arg)); while (list->next) list = list->next; list->next = create_elem(name, arg); return (tmp); }
int select_in_box(t_data *data, t_selection *s) { t_elem *elem; t_elem *new_elem; elem = data->wizards->head; while (elem) { if ((elem->wizard->cx > s->sx && elem->wizard->cx < s->cx && elem->wizard->cy > s->sy && elem->wizard->cy < s->cy) || (elem->wizard->cx < s->sx && elem->wizard->cx > s->cx && elem->wizard->cy < s->sy && elem->wizard->cy > s->cy) || (elem->wizard->cx < s->sx && elem->wizard->cx > s->cx && elem->wizard->cy > s->sy && elem->wizard->cy < s->cy) || (elem->wizard->cx > s->sx && elem->wizard->cx < s->cx && elem->wizard->cy < s->sy && elem->wizard->cy > s->cy)) { if (!(new_elem = create_elem(TYPE_NONE))) return (0); new_elem->wizard = elem->wizard; if (!(data->selected = list_push_back(data->selected, new_elem))) return (0); } elem = elem->next; } return (1); }
void LexList:: put_lex (const Lex & l) { ListElem * cur = first; if ( first == 0 ) { first = create_elem (l); } else { ListElem * prev; while ( cur != 0 ) { prev = cur; cur = cur->next; } cur = create_elem (l); prev->next = cur; } }
t_file *create_real_list(t_file *list, t_file *list2) { if (ft_strequ(list2->path, "") && ft_strequ(list2->name, "")) { free(list2->path); list2->path = ft_strdup("/"); } if (!list) { list = create_elem(list2->path, NULL, list2->name); if (list) list->av_name = ft_strdup(list2->av_name); } else add_elem_end_av(list2->path, list, list2->name, list2->av_name); return (list); }
/* fonction d'insertion d'un code dans la liste * insere de facon a conserver l'ordre alphabeteique*/ void insert (chaine_octet* prefixe, unsigned int suffixe){ arbre_n *ptr_parent, *ptr_new, *ptr_cour, *ptr_suiv; int i=0; ptr_parent = search(prefixe); //printf("------ "); //afficher_octet(prefixe); //printf(" ------"); chaine_octet* d = cat_str_char(prefixe, suffixe); //afficher_octet(d); ptr_new = create_elem(nb_elem, d, ptr_parent->code); /* on cherche l'emplacement exact du mot parmis les enfants du noeud qu'on vient de trouve */ if (ptr_parent->enfant == NULL){ ptr_parent->enfant = ptr_new; } else{ // on cherche la place dans l'ordre alphabetique ptr_cour = ptr_parent->enfant; ptr_suiv = ptr_cour->freres; /* les mots utilises pour chercher sont de longueur minimale 3 : ascii + car + '\0' */ while ((ptr_suiv != NULL) && (cmp_chaine_octet(ptr_new->mot, ptr_cour->mot)>0)){ ptr_cour = ptr_suiv; ptr_suiv = ptr_suiv->freres; } if (ptr_cour == ptr_parent->enfant && (cmp_chaine_octet(ptr_new->mot, ptr_cour->mot)<0)){ // insertion en tete ptr_parent->enfant = ptr_new; ptr_new->freres = ptr_cour; } else { //insertion diverse (en queue ou au milieu c'est pareil ptr_new->freres = ptr_suiv; ptr_cour->freres = ptr_new; } } tab_arbre[nb_elem] = ptr_new; //mise a jour du tableau de pointeur nb_elem++; }
void pack_trolley(tree *t, list *l, char *ware_name, int amount) { if(amount <= 0) return; char *key = make_key(ware_name); elem *e = get_elem_in_list_DB(l, key); if(e) { incr_amount(e, amount); node *n = get_node_in_tree(t, key); ware *w = (ware*)n->content; int tot_price = (w->price) * amount; incr_trolley_price(l, tot_price); } else { elem *new_elem = create_elem(); trolley *new_trolley = create_trolley(); new_elem->box = new_trolley; new_trolley->key = strdup(key); new_trolley->amount = amount; insert_elem_in_list(l, new_elem); node *n = get_node_in_tree(t, key); ware *w = (ware*)n->content; int tot_price = (w->price) * amount; incr_trolley_price(l, tot_price); } if(key) free(key); }
t_list *get_env_in_list(char **mainenv) { char **tmp; t_env *env; t_list *list; list = NULL; env = NULL; if ((env = malloc(sizeof(t_env)))) { while (*mainenv) { tmp = my_str_to_wordtab(*mainenv, '='); env->scope = tmp[0]; env->value = tmp[1]; printf("tmp1 = %s, tmp2 = %s\n", tmp[0], tmp[1]); if ((list = add_elem_to_list(list, create_elem(env))) == NULL) return (NULL); mainenv++; } } return (list); }
int main_loop(t_data *data) { SDL_Event event; int quit = 0; t_elem *elem; t_elem *new_elem; while (!quit) { while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) quit = 1; if (event.type == SDL_KEYDOWN) { if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) quit = 1; if (event.key.keysym.scancode == SDL_SCANCODE_UP) data->camera.y -= data->camera.speed; if (event.key.keysym.scancode == SDL_SCANCODE_DOWN) data->camera.y += data->camera.speed; if (event.key.keysym.scancode == SDL_SCANCODE_RIGHT) data->camera.x += data->camera.speed; if (event.key.keysym.scancode == SDL_SCANCODE_LEFT) data->camera.x -= data->camera.speed; if (event.key.keysym.scancode == SDL_SCANCODE_LSHIFT) data->shift = 1; if (event.key.keysym.scancode == SDL_SCANCODE_LCTRL) data->ctrl = 1; } if (event.type == SDL_KEYUP) { if (event.key.keysym.scancode == SDL_SCANCODE_LSHIFT) data->shift = 0; if (event.key.keysym.scancode == SDL_SCANCODE_LCTRL) data->ctrl = 0; } if (event.type == SDL_MOUSEMOTION) { data->mouse_x = event.motion.x; data->mouse_y = event.motion.y; } if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.button.button == SDL_BUTTON_LEFT) { if (!data->shift && !list_clear(data->selected)) return (0); elem = data->wizards->head; while (elem) { if (mouse_in_hitcl(data->mouse_x + data->camera.x, data->mouse_y + data->camera.y, &elem->wizard->hitcl)) { if (!(new_elem = create_elem(TYPE_NONE))) return (0); new_elem->wizard = elem->wizard; if (!(data->selected = list_push_back(data->selected, new_elem))) return (0); } elem = elem->next; } data->selection.status = 1; data->selection.sx = data->mouse_x + data->camera.x; data->selection.sy = data->mouse_y + data->camera.y; data->selection.cx = data->selection.sx; data->selection.cy = data->selection.sy; } if (event.button.button == SDL_BUTTON_RIGHT) { if (data->selected->size > 0) { if (!new_wizard_formation(data->selected, data->mouse_x + data->camera.x, data->mouse_y + data->camera.y)) return (0); } /* elem = data->selected->head; while (elem) { elem->wizard->vdest.x = data->mouse_x + data->camera.x; elem->wizard->vdest.y = data->mouse_y + data->camera.y; elem->wizard->move = 1; if (!set_new_wizard_angle(elem->wizard, elem->wizard->vdest.x, elem->wizard->vdest.y)) return (0); elem = elem->next; }*/ } } if (event.type == SDL_MOUSEBUTTONUP) { if (event.button.button == SDL_BUTTON_LEFT) { data->selection.status = 0; if (!select_in_box(data, &data->selection)) return (0); } } } if (!update_camera(data)) return (0); update_selection_box(data, &data->selection); elem = data->wizards->head; while (elem) { if (!update_wizard(elem->wizard)) return (0); update_wizard_collision(data->wizards, elem->wizard); elem = elem->next; } render(data); SDL_GL_SwapWindow(data->window); SDL_Delay(1000 / 60); } return (1); }