void mprepare_vars(struct mprepare_state *s) { mfile f = s->f; block_t heap = s->heap; vlist reads, writes, defines; imported_modules = s->lmodules; all_writable = f->vclass == f_plain; all_readable = f->vclass == f_plain || !s->all_loaded; readable = writable = definable = NULL; if (f->name) { this_module = alloc_string(f->name); SET_READONLY(this_module); } else this_module = NULL; /* Change status of variables */ for (defines = f->defines; defines; defines = defines->next) { u16 n = global_lookup(s->ccontext->gstate, defines->var); struct string *omod; int ostatus = module_vstatus(s->ccontext->gstate, n, &omod); if (!module_vset(s->ccontext->gstate, n, var_module, this_module)) log_error("cannot define %s: belongs to module %s", defines->var, omod->str); else if (ostatus == var_write) warning("%s was writable", defines->var); definable = new_glist(heap, n, definable); } for (writes = f->writes; writes; writes = writes->next) { u16 n = global_lookup(s->ccontext->gstate, writes->var); if (!module_vset(s->ccontext->gstate, n, var_write, NULL)) { struct string *belongs; module_vstatus(s->ccontext->gstate, n, &belongs); log_error("cannot write %s: belongs to module %s", writes->var, belongs->str); } writable = new_glist(heap, n, writable); } for (reads = f->reads; reads; reads = reads->next) readable = new_glist(heap, global_lookup(s->ccontext->gstate, reads->var), readable); }
int add_glist(char *str, GLISTPTR *list) { GLISTPTR newptr,cptr,pptr; char temp_buf[256]; char *name=temp_buf; /* make local copy of string */ strncpy(temp_buf,str,255); temp_buf[255]=0; while (!isspace((int)*name)&&*name!=0) name++; if (*name==0) name=temp_buf; else { *name++=0; while (isspace((int)*name)&&*name!=0) name++; if (*name==0) name=temp_buf; } if ( (newptr = new_glist(temp_buf, name)) != NULL) { if (*list==NULL) *list=newptr; else { cptr=pptr=*list; while(cptr!=NULL) { pptr=cptr; cptr=cptr->next; }; pptr->next = newptr; } } return newptr==NULL; }
void tree_update(TreeNode *root) { GList *moved; int i = 0; //fprintf(stderr,"Updating tree.\n"); // On deplace de l'arbre vers la liste les actors qui on changé de noeud ou en bordure, et on supprime ceux en fin de vie moved = new_glist(); tree_remChangedActors(root, moved); //fprintf(stderr,"Changed actors removed from tree.\n"); //glist_debug(moved); // Et on les réinjecte à la racine glist_startIter(moved); while(!glist_endIter(moved)) { //fprintf(stderr,"Injecting %d : %p\n", i, glist_getCurrentData(moved)); tree_addActor(root, (Actor*) glist_getCurrentData(moved), 1); glist_iter(moved); ++i; } del_glist(moved); //fprintf(stderr,"Changed actors reinjected in the tree.\n"); // On verifie s'il faut des merges tree_detectMerge(root); }
void resman_addToFreeList(struct Actor *toFree) { if(freeList == NULL) freeList = new_glist(); glist_addCell(freeList, toFree); }
static int save_malloced(void *data) { if ((g_save = new_glist(g_save, data)) == NULL) return (-1); return (0); }
int main() { // Ressources unsigned long t_debut, t_fin; float dt, waitShoot = 0; BITMAP *buffer; int fin = 0, v = 200; Map *map; DepthList *depthList; Rect screen_pos, map_pos; Actor *joueur; // Initialisation fprintf(stderr,"Initialisation ...\n"); timeBeginPeriod(1); set_uformat(U_ASCII); set_color_depth(32); allegro_init(); install_keyboard(); install_mouse(); srand(time(NULL)); if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1024, 768, 0, 0)) ERREUR("Echec du lancement du mode graphique."); buffer = create_bitmap(SCREEN_W, SCREEN_H); resman_loadSprites(); fprintf(stderr,"Chargement des ressources ...\n"); map = new_map("media/map/test1"); map_pos.x = map_pos.y = 0; map_pos.w = map->w; map_pos.h = map->h; actList = new_glist(); root = new_tree(map_pos); map_addEntities(map, actList, root); depthList = new_dlist(); screen_pos.w = SCREEN_W; screen_pos.h = SCREEN_H; // Ajout du joueur joueur = actor_addJoueur(actList, root, 500, 500); // Intro debut(); // Boucle principale fprintf(stderr,"Debut !\n"); t_debut = timeGetTime(); while(!fin) { // Gestion clavier if(key[KEY_ESC]) { fin = 1; } if(key[KEY_W]) { joueur->vit_y = -v; joueur->direction_regard = HAUT; joueur->etat = ETAT_MARCHE; } else if(key[KEY_S]) { joueur->vit_y = v; joueur->direction_regard = BAS; joueur->etat = ETAT_MARCHE; } else joueur->vit_y = 0; if(key[KEY_A]) { joueur->vit_x = -v; joueur->direction_regard = GAUCHE; joueur->etat = ETAT_MARCHE; } else if(key[KEY_D]) { joueur->vit_x = v; joueur->direction_regard = DROITE; joueur->etat = ETAT_MARCHE; } else joueur->vit_x = 0; if(joueur->vit_x != 0 && joueur->vit_y != 0) { joueur->vit_x /= sqrt(2); joueur->vit_y /= sqrt(2); } if(!key[KEY_W] && !key[KEY_D] && !key[KEY_S] && !key[KEY_A]) joueur->etat = ETAT_REPOS; if(key[KEY_Q]) { if(waitShoot <= 0) { waitShoot = .1; actor_addTree(actList, root, mouse_x + screen_pos.x, mouse_y + screen_pos.y); } } waitShoot -= dt; if(mouse_b&1) { float vx, vy, v; if(waitShoot <= 0) { waitShoot = .3; vx = mouse_x - (joueur->pos_x - screen_pos.x); vy = mouse_y - (joueur->pos_y - screen_pos.y); v = sqrt(vx*vx + vy*vy); vx = vx/v; vy = vy/v; actor_addMissile(actList, root, joueur->pos_x + vx*joueur->w*1.5, joueur->pos_y + vy*joueur->h*1.5, vx*300, vy*300); } } if(key[KEY_P]) { FILE *fd = fopen("arbres.txt", "w+"); Actor *act; glist_startIter(actList); while(!glist_endIter(actList)) { act = glist_getCurrentData(actList); if(act->type == ACT_TREE) fprintf(fd, "%d\n%d\n", (int) act->pos_x, (int) act->pos_y); glist_iter(actList); } fclose(fd); } // Double buffer clear_bitmap(buffer); render_map(buffer, map, screen_pos.x, screen_pos.y); // Mises à jour resman_updateSprites(&dt); actor_spawnMonster(actList, root); actor_ia(actList, joueur); // Deplacement glist_startIter(actList); while(!glist_endIter(actList)) { actor_update(glist_getCurrentData(actList), map_pos, map, dt); if( ((Actor*) glist_getCurrentData(actList))->deleting) { glist_remCell(actList, glist_getCurrentId(actList)); } else glist_iter(actList); } // Cadrage ecran screen_pos.x = joueur->pos_x - SCREEN_W/2; screen_pos.y = joueur->pos_y - SCREEN_H/2; // Collision tree_collisionDetection(root); // Affichage tree_update(root); dlist_getActorsFromTree(depthList, root, screen_pos); dlist_update(depthList, screen_pos); dlist_blit(depthList, buffer, screen_pos); draw_cursor(buffer); textprintf_centre_ex(buffer, font, SCREEN_W/2, 5, makecol(0, 0, 0), makecol(255, 0, 0), " Vies restantes : %d | Score : %d ", joueur->vie, score); // Rafraichissement écran vsync(); blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // Gestion du temps t_fin = timeGetTime(); dt = ((float)t_fin - t_debut)/1000; t_debut = t_fin; // Test fin de jeu if(joueur->deleting) fin = 1; resman_freeList(); } // Game over gameover(); // Fin timeEndPeriod(1); delete_map(map); del_tree(root); del_dlist(depthList); del_glist(actList); destroy_bitmap(buffer); resman_freeSprites(); allegro_exit(); return 0; }