void Tile::loadFrom(PACKFILE *file, TileRepository *tileRepository) { // Load tile name from file and look it up in the tile repository char name[32]; pack_fgets(name, 32, file); setType(tileRepository->getTileType(name)); obstacle = pack_igetw(file); }
/* loads object names from a header file, if they are missing */ static void load_header(DATAFILE *dat, AL_CONST char *filename) { char buf[160], buf2[160]; int datsize, i, c, c2; PACKFILE *f; datsize = 0; while (dat[datsize].type != DAT_END) datsize++; f = pack_fopen(filename, F_READ); if (f) { while (pack_fgets(buf, 160, f) != 0) { if (strncmp(buf, "#define ", 8) == 0) { c2 = 0; c = 8; while ((buf[c]) && (buf[c] != ' ')) buf2[c2++] = buf[c++]; buf2[c2] = 0; while (buf[c]==' ') c++; i = 0; while ((buf[c] >= '0') && (buf[c] <= '9')) { i *= 10; i += buf[c] - '0'; c++; } if ((i < datsize) && (!*get_datafile_property(dat+i, DAT_NAME))) datedit_set_property(dat+i, DAT_NAME, buf2); } } pack_fclose(f); } else { /* don't let the error propagate */ errno = 0; } }
void TiledMap::loadFrom(PACKFILE *file, TileRepository *tileRepository) { ASSERT(file); // Remove the objects from the map list<Object*>::iterator i; while (!objects.empty()) { i = objects.begin(); delete (*i); objects.erase(i); } // Load the map header int version = pack_igetw(file); int layers = pack_igetw(file); // Load the tile data //allegro_message("Loading %d layers from map version %d", layers, version); for (int i = 0; i < layers; i++) { mapLayers[i]->loadFrom(file, tileRepository); } // Load object data if (version == 3) { int nrObjects = pack_igetw(file); for (int i = 0; i < nrObjects; i++) { int x = pack_igetw(file); //int(TILES_W * (*i)->x), file); int y = pack_igetw(file); //pack_iputw(int(TILES_H * (*i)->y), file); char *className = new char[64]; int objectInstance = 0; pack_fgets(className, 64, file); // Spawn the object // Assumes Lua environment is set up and such of course lua_pushstring(L, className); lua_gettable(L, LUA_GLOBALSINDEX); if (!lua_isnil(L, -1)) { lua_call(L, putLuaArguments(L, "m", this), 1); if (lua_istable(L, -1)) { objectInstance = lua_ref(L, -1); } else { console.log(CON_QUIT, CON_ALWAYS, "Error while instaniating object \"%s\"", className); } } else { console.log(CON_QUIT, CON_ALWAYS, "Error: could not find object class \"%s\"", className); } lua_getref(L, objectInstance); lua_pushstring(L, "_pointer"); lua_gettable(L, -2); Object* obj = (Object*)lua_touserdata(L, -1); obj->x = (double(x) / TILES_W); obj->y = (double(y) / TILES_H); obj->className = className; // Assign class name (maybe not the best place for this) lua_pop(L, 1); } } mapWidth = mapLayers[0]->getWidth(); mapHeight = mapLayers[0]->getHeight(); }
/* load_txt_font: * Loads a scripted font. */ FONT *load_txt_font(AL_CONST char *filename, RGB *pal, void *param) { char buf[1024], *font_str, *start_str = 0, *end_str = 0; char font_filename[1024]; FONT *f, *f2, *f3, *f4; PACKFILE *pack; int begin, end, glyph_pos=32; pack = pack_fopen(filename, F_READ); if (!pack) return NULL; f = f2 = f3 = f4 = NULL; while(pack_fgets(buf, sizeof(buf)-1, pack)) { font_str = strtok(buf, " \t"); if (font_str) start_str = strtok(0, " \t"); if (start_str) end_str = strtok(0, " \t"); if (!font_str || !start_str) { if (f) destroy_font(f); if (f2) destroy_font(f2); pack_fclose(pack); return NULL; } if(font_str[0] == '-') font_str[0] = '\0'; begin = strtol(start_str, 0, 0); if (end_str) end = strtol(end_str, 0, 0); else end = -1; if(begin <= 0 || (end > 0 && end < begin)) { if (f) destroy_font(f); if (f2) destroy_font(f2); pack_fclose(pack); return NULL; } /* Load the font that needs to be merged with the current font */ if (font_str[0]) { if (f2) destroy_font(f2); if (exists(font_str)) { f2 = load_font(font_str, pal, param); } else if (is_relative_filename(font_str)) { replace_filename(font_filename, filename, font_str, sizeof(font_filename)); f2 = load_font(font_filename, pal, param); } else { f2 = NULL; } if (f2) glyph_pos=get_font_range_begin(f2, -1); } if (!f2) { if (f) destroy_font(f); pack_fclose(pack); return NULL; } if (end == -1) end = begin + get_font_range_end(f2,-1) - glyph_pos; /* transpose the font to the range given in the .txt file */ f4=extract_font_range(f2,glyph_pos,glyph_pos + (end - begin)); if (f4 && (begin != glyph_pos)) { transpose_font(f4, begin - glyph_pos); } glyph_pos += (end - begin) + 1; /* FIXME: More efficient way than to repeatedely merge into a new font? */ if (f && f4) { f3 = f; f = merge_fonts(f4, f3); destroy_font(f4); destroy_font(f3); } else { f = f4; } f3=f4=NULL; } if (f2) destroy_font(f2); pack_fclose(pack); return f; }
/* _unix_load_modules: * Find a modules.lst file and load the modules listed in it. */ void _unix_load_modules(int system_driver) { PACKFILE *f; char fullpath[1024]; char *fullpath_slash; char buf[1024]; char buf2[1024]; char **pathptr; char *filename; void *handle; void (*init)(int); MODULE *m; /* Read the ALLEGRO_MODULES environment variable. * But don't do it if we are root (for obvious reasons). */ if (geteuid() != 0) { char *env = getenv("ALLEGRO_MODULES"); if (env) { snprintf(fullpath, sizeof fullpath, "%s/%s", env, "modules.lst"); fullpath[(sizeof fullpath) - 1] = 0; f = pack_fopen(uconvert_ascii(fullpath, buf), F_READ); if (f) goto found; } } for (pathptr = module_path; *pathptr; pathptr++) { snprintf(fullpath, sizeof fullpath, "%s/%d.%d.%d/modules.lst", *pathptr, ALLEGRO_VERSION, ALLEGRO_SUB_VERSION, ALLEGRO_WIP_VERSION); fullpath[(sizeof fullpath) - 1] = 0; f = pack_fopen(uconvert_ascii(fullpath, buf), F_READ); if (f) goto found; } return; found: TRACE(PREFIX_I "Loading modules from \"%s\".\n", fullpath); fullpath_slash = strrchr(fullpath, '/'); while (true) { if (!pack_fgets(buf, sizeof buf, f)) break; filename = uconvert_toascii(buf, buf2); strip(filename); if ((filename[0] == '#') || (strlen(filename) == 0)) continue; if (!fullpath_slash) { snprintf(fullpath, sizeof fullpath, filename); fullpath[(sizeof fullpath) - 1] = 0; } else { snprintf(fullpath_slash+1, (sizeof fullpath) - (fullpath_slash - fullpath) - 1, filename); fullpath[(sizeof fullpath) - 1] = 0; } if (!exists(uconvert_ascii(fullpath, buf))) continue; handle = dlopen(fullpath, RTLD_NOW); if (!handle) { /* useful during development */ /* printf("Error loading module: %s\n", dlerror()); */ continue; } init = dlsym(handle, "_module_init"); if (init) init(system_driver); m = al_malloc(sizeof(MODULE)); if (m) { m->handle = handle; m->next = module_list; module_list = m; } } pack_fclose(f); }