static int allua_Path_clone(lua_State * L) { ALLUA_path path = allua_check_path(L, 1); ALLUA_path clone = al_clone_path(path); allua_pushPath(L, clone, true); return 1; }
/* Function: al_get_standard_path */ ALLEGRO_PATH *al_get_standard_path(int id) { ASSERT(active_sysdrv); ASSERT(active_sysdrv->vt); ASSERT(active_sysdrv->vt->get_path); if (id == ALLEGRO_EXENAME_PATH && active_sysdrv->user_exe_path) return al_clone_path(active_sysdrv->user_exe_path); if (id == ALLEGRO_RESOURCES_PATH && active_sysdrv->user_exe_path) { ALLEGRO_PATH *exe_dir = al_clone_path(active_sysdrv->user_exe_path); al_set_path_filename(exe_dir, NULL); return exe_dir; } if (active_sysdrv->vt->get_path) return active_sysdrv->vt->get_path(id); return NULL; }
/* Function: al_create_native_file_dialog */ ALLEGRO_NATIVE_DIALOG *al_create_native_file_dialog( ALLEGRO_PATH const *initial_path, char const *title, char const *patterns, int mode) { ALLEGRO_NATIVE_DIALOG *fc; fc = _AL_MALLOC(sizeof *fc); memset(fc, 0, sizeof *fc); if (initial_path) fc->initial_path = al_clone_path(initial_path); fc->title = al_ustr_new(title); fc->patterns = al_ustr_new(patterns); fc->mode = mode; return fc; }
static ALLEGRO_PATH *maybe_parent_dir(const ALLEGRO_PATH *path) { ALLEGRO_PATH *path2; if (!path) return NULL; if (!is_build_config_name(al_get_path_tail(path))) return NULL; path2 = al_clone_path(path); if (path2) { al_drop_path_tail(path2); al_set_path_filename(path2, NULL); ALLEGRO_DEBUG("Also searching %s\n", al_path_cstr(path2, '\\')); } return path2; }
Player::Player(Game& g, b2Vec2 p) : Character(g),swordFix(NULL), left(false), right(false), landed(true), contact(false), lastproc(0), life(100), attacking(false), attackcooldown(0) { bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x, p.y); bodyDef.fixedRotation = true; body = g.w.CreateBody(&bodyDef); dynamicBox.SetAsBox(.25f, 0.9f); fixtureDef.shape = &dynamicBox; fixtureDef.density = 5.0f; fixtureDef.friction = 0.0f; fixtureDef.filter.categoryBits = PLAYER; fixtureDef.filter.maskBits = MONSTER | TRIGGER | WALL | SWORD; (body->CreateFixture(&fixtureDef))->SetUserData(this); //add "feet" to detect floor dynamicBox.SetAsBox(0.2, 0.05, b2Vec2(0, 0.9f), 0); fixtureDef.isSensor = true; fixtureDef.density = 0.1; fixtureDef.filter.categoryBits = FOOT; fixtureDef.filter.maskBits = WALL | MONSTER; (body->CreateFixture(&fixtureDef))->SetUserData(this); //add sword to kill monsters dynamicBox.SetAsBox(0.4, 0.1, b2Vec2(0.65, 0), 0); swordDef.shape = &dynamicBox; swordDef.isSensor = true; swordDef.density = 0.1; swordDef.filter.categoryBits = SWORD; swordDef.filter.maskBits = MONSTER; // Loads the Spine model ALLEGRO_PATH *path, *resourceDir, *file; resourceDir= al_get_standard_path(ALLEGRO_RESOURCES_PATH); std::cerr << al_path_cstr(resourceDir, ALLEGRO_NATIVE_PATH_SEP) << std::endl; if (modelAtlas == NULL) { file = al_create_path("data/animations/hero.atlas"); path = al_clone_path(resourceDir); al_join_paths(path, file); al_destroy_path(file); modelAtlas = spAtlas_createFromFile(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), NULL); if (!modelAtlas) throw Failure("Failed to load the hero's atlas."); al_destroy_path(path); jsonSkel = spSkeletonJson_create(modelAtlas); file = al_create_path("data/animations/hero.json"); path = al_clone_path(resourceDir); al_join_paths(path, file); al_destroy_path(file); modelData = spSkeletonJson_readSkeletonDataFile(jsonSkel, al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); if (!modelData) throw Failure("Failed to load the hero's data."); al_destroy_path(path); al_destroy_path(resourceDir); stateData = spAnimationStateData_create(modelData); spAnimationStateData_setMixByName(stateData, "walk", "rest", 0.2f); spAnimationStateData_setMixByName(stateData, "rest", "walk", 0.2f); spAnimationStateData_setMixByName(stateData, "rest", "slash", 0.1f); spAnimationStateData_setMixByName(stateData, "slash", "rest", 0.1f); spAnimationStateData_setMixByName(stateData, "walk", "slash", 0.1f); spAnimationStateData_setMixByName(stateData, "slash", "walk", 0.1f); model = loadSkeleton(modelData, stateData); if (!model) throw Failure("Failed to load the hero's skeleton."); spAnimationState_setAnimationByName(model->state, 0, "rest", true); } }
GAME * game_init () { if (!al_init ()) { fprintf (stderr, "Failed to initialize Allegro.\n"); return NULL; } if (!al_init_image_addon ()) { fprintf (stderr, "Failed to initialize image addon.\n"); return NULL; } if (!al_install_keyboard ()) { fprintf (stderr, "Failed to install keyboard.\n"); return NULL; } al_init_font_addon (); if (!al_init_ttf_addon ()) { fprintf (stderr, "Failed to initialize ttf addon.\n"); return NULL; } if (!al_init_primitives_addon ()) { fprintf (stderr, "Failed to initialize primitives addon.\n"); return NULL; } GAME *game = al_malloc (sizeof (GAME)); if (!game) return NULL; srand (time (NULL)); game->running = true; game->paused = false; game->fullscreen = 1; game->windowed = 1; game->rrate = 60; game->suggest_vsync = 1; game->force_vsync = 0; game->current_npc = NULL; game->screen = screen_new (); char *filename; const char *str; filename = get_resource_path_str ("data/game.ini"); ALLEGRO_CONFIG *game_config = al_load_config_file (filename); al_free (filename); str = al_get_config_value (game_config, "", "org"); al_set_org_name (str); str = al_get_config_value (game_config, "", "app"); al_set_app_name (str); ALLEGRO_PATH *settpath = al_get_standard_path (ALLEGRO_USER_SETTINGS_PATH); ALLEGRO_PATH *gcpath = al_clone_path (settpath); al_set_path_filename (gcpath, "general.ini"); const char * gcpath_str = al_path_cstr (gcpath, ALLEGRO_NATIVE_PATH_SEP); ALLEGRO_CONFIG *gconfig = al_load_config_file (gcpath_str); if (!gconfig) { gconfig = al_create_config (); al_make_directory (al_path_cstr (settpath, ALLEGRO_NATIVE_PATH_SEP)); set_config_i (gconfig, "display", "width", game->screen.width); set_config_i (gconfig, "display", "height", game->screen.height); set_config_i (gconfig, "display", "fullscreen", game->fullscreen); set_config_i (gconfig, "display", "windowed", game->windowed); set_config_i (gconfig, "display", "refreshrate", game->rrate); set_config_i (gconfig, "display", "suggest_vsync", game->suggest_vsync); set_config_i (gconfig, "display", "force_vsync", game->force_vsync); } else { get_config_i (gconfig, "display", "width", &game->screen.width); get_config_i (gconfig, "display", "height", &game->screen.height); get_config_i (gconfig, "display", "fullscreen", &game->fullscreen); get_config_i (gconfig, "display", "windowed", &game->windowed); get_config_i (gconfig, "display", "refreshrate", &game->rrate); get_config_i (gconfig, "display", "suggest_vsync", &game->suggest_vsync); get_config_i (gconfig, "display", "force_vsync", &game->force_vsync); } al_save_config_file (gcpath_str, gconfig); al_destroy_path (settpath); al_destroy_path (gcpath); al_destroy_config (gconfig); int flags = 0; if (game->fullscreen == game->windowed) flags |= ALLEGRO_FULLSCREEN_WINDOW; else if (game->fullscreen) flags |= ALLEGRO_FULLSCREEN; else flags |= ALLEGRO_WINDOWED; al_set_new_display_option (ALLEGRO_VSYNC, game->suggest_vsync, ALLEGRO_SUGGEST); al_set_new_display_option (ALLEGRO_DEPTH_SIZE, 8, ALLEGRO_SUGGEST); al_set_new_display_flags (flags); al_set_new_display_refresh_rate (game->rrate); game->display = al_create_display (game->screen.width, game->screen.height); if (!game->display) { fprintf (stderr, "Failed to create display.\n"); al_free (game); return NULL; } al_set_new_bitmap_flags (ALLEGRO_VIDEO_BITMAP); game->timer = al_create_timer (1.0 / FPS); if (!game->timer) { fprintf (stderr, "Failed to create timer.\n"); al_free (game); return NULL; } game->screen.width = al_get_display_width (game->display); game->screen.height = al_get_display_height (game->display); screen_update_size (&game->screen, game->screen.width, game->screen.height); game->rrate = al_get_display_refresh_rate (game->display); game->event_queue = al_create_event_queue (); if (!game->event_queue) { fprintf (stderr, "Failed to create event queue.\n"); al_free (game); return NULL; } al_register_event_source (game->event_queue, al_get_display_event_source (game->display)); al_register_event_source (game->event_queue, al_get_timer_event_source (game->timer)); al_set_render_state (ALLEGRO_ALPHA_FUNCTION, ALLEGRO_RENDER_EQUAL); al_set_render_state (ALLEGRO_ALPHA_TEST_VALUE, 1); filename = get_resource_path_str ("data/sprites.ini"); game->sprites = sprite_load_sprites (filename); al_free (filename); filename = get_resource_path_str ("data/scenes.ini"); game->scenes = scene_load_file (filename); scene_load_scenes (game->scenes, game->sprites); al_free (filename); str = al_get_config_value (game_config, "", "scene"); game->current_scene = scene_get (game->scenes, str); str = al_get_config_value (game_config, "", "actor"); game->current_actor = sprite_new_actor (game->sprites, str); str = al_get_config_value (game_config, "", "portal"); SCENE_PORTAL *portal = scene_get_portal (game->scenes, str); al_destroy_config (game_config); filename = get_resource_path_str ("data/ui.ini"); game->ui = ui_load_file (filename); al_free (filename); sprite_center (game->current_actor, &portal->position); screen_center (&game->screen, portal->position, game->current_scene->map); return game; }
/* * Parses a map file * Given the path to a map file, returns a new map struct * The struct must be freed once it's done being used */ ALLEGRO_MAP *al_open_map(const char *dir, const char *filename) { xmlDoc *doc; xmlNode *root; ALLEGRO_MAP *map; unsigned i, j; ALLEGRO_PATH *cwd = al_get_standard_path(ALLEGRO_RESOURCES_PATH); ALLEGRO_PATH *resources = al_clone_path(cwd); ALLEGRO_PATH *maps = al_create_path(dir); al_join_paths(resources, maps); if (!al_change_directory(al_path_cstr(resources, ALLEGRO_NATIVE_PATH_SEP))) { fprintf(stderr, "Error: failed to change directory in al_parse_map()."); } al_destroy_path(resources); al_destroy_path(maps); // Read in the data file doc = xmlReadFile(filename, NULL, 0); if (!doc) { fprintf(stderr, "Error: failed to parse map data: %s\n", filename); return NULL; } // Get the root element, <map> root = xmlDocGetRootElement(doc); // Get some basic info map = MALLOC(ALLEGRO_MAP); map->width = atoi(get_xml_attribute(root, "width")); map->height = atoi(get_xml_attribute(root, "height")); map->tile_width = atoi(get_xml_attribute(root, "tilewidth")); map->tile_height = atoi(get_xml_attribute(root, "tileheight")); map->orientation = g_strdup(get_xml_attribute(root, "orientation")); map->tile_layer_count = 0; map->object_layer_count = 0; // Get the tilesets GSList *tilesets = get_children_for_name(root, "tileset"); map->tilesets = NULL; GSList *tileset_item = tilesets; while (tileset_item) { xmlNode *tileset_node = (xmlNode*)tileset_item->data; tileset_item = g_slist_next(tileset_item); ALLEGRO_MAP_TILESET *tileset = MALLOC(ALLEGRO_MAP_TILESET); tileset->firstgid = atoi(get_xml_attribute(tileset_node, "firstgid")); tileset->tilewidth = atoi(get_xml_attribute(tileset_node, "tilewidth")); tileset->tileheight = atoi(get_xml_attribute(tileset_node, "tileheight")); tileset->name = g_strdup(get_xml_attribute(tileset_node, "name")); // Get this tileset's image xmlNode *image_node = get_first_child_for_name(tileset_node, "image"); tileset->width = atoi(get_xml_attribute(image_node, "width")); tileset->height = atoi(get_xml_attribute(image_node, "height")); tileset->source = g_strdup(get_xml_attribute(image_node, "source")); tileset->bitmap = al_load_bitmap(tileset->source); // Get this tileset's tiles GSList *tiles = get_children_for_name(tileset_node, "tile"); tileset->tiles = NULL; GSList *tile_item = tiles; while (tile_item) { xmlNode *tile_node = (xmlNode*)tile_item->data; tile_item = g_slist_next(tile_item); ALLEGRO_MAP_TILE *tile = MALLOC(ALLEGRO_MAP_TILE); tile->id = tileset->firstgid + atoi(get_xml_attribute(tile_node, "id")); tile->tileset = tileset; tile->bitmap = NULL; // Get this tile's properties tile->properties = parse_properties(tile_node); // TODO: add a destructor tileset->tiles = g_slist_prepend(tileset->tiles, tile); } g_slist_free(tiles); //tileset->tiles = g_slist_reverse(tileset->tiles); // TODO: add a destructor map->tilesets = g_slist_prepend(map->tilesets, tileset); } g_slist_free(tilesets); //map->tilesets = g_slist_reverse(map->tilesets); // Create the map's master list of tiles cache_tile_list(map); // Get the layers GSList *layers = get_children_for_either_name(root, "layer", "objectgroup"); map->layers = NULL; GSList *layer_item = layers; while (layer_item) { xmlNode *layer_node = (xmlNode*)layer_item->data; layer_item = g_slist_next(layer_item); ALLEGRO_MAP_LAYER *layer = MALLOC(ALLEGRO_MAP_LAYER); layer->name = g_strdup(get_xml_attribute(layer_node, "name")); layer->properties = parse_properties(layer_node); char *layer_visible = get_xml_attribute(layer_node, "visible"); layer->visible = (layer_visible != NULL ? atoi(layer_visible) : 1); char *layer_opacity = get_xml_attribute(layer_node, "opacity"); layer->opacity = (layer_opacity != NULL ? atof(layer_opacity) : 1.0); if (!strcmp((const char*)layer_node->name, "layer")) { layer->type = TILE_LAYER; layer->width = atoi(get_xml_attribute(layer_node, "width")); layer->height = atoi(get_xml_attribute(layer_node, "height")); decode_layer_data(get_first_child_for_name(layer_node, "data"), layer); // Create any missing tile objects for (i = 0; i<layer->height; i++) { for (j = 0; j<layer->width; j++) { char id = al_get_single_tile_id(layer, j, i); if (id == 0) { continue; } ALLEGRO_MAP_TILE *tile = al_get_tile_for_id(map, id); if (!tile) { // wasn't defined in the map file, presumably because it had no properties tile = MALLOC(ALLEGRO_MAP_TILE); tile->id = id; tile->properties = g_hash_table_new(NULL, NULL); tile->tileset = NULL; tile->bitmap = NULL; // locate its tilemap GSList *tilesets = map->tilesets; ALLEGRO_MAP_TILESET *tileset_ref; while (tilesets) { ALLEGRO_MAP_TILESET *tileset = (ALLEGRO_MAP_TILESET*)tilesets->data; tilesets = g_slist_next(tilesets); if (tileset->firstgid <= id) { if (!tile->tileset || tileset->firstgid > tile->tileset->firstgid) { tileset_ref = tileset; } } } tile->tileset = tileset_ref; tileset_ref->tiles = g_slist_prepend(tileset_ref->tiles, tile); g_hash_table_insert(map->tiles, GINT_TO_POINTER(tile->id), tile); } // create this tile's bitmap if it hasn't been yet if (!tile->bitmap) { ALLEGRO_MAP_TILESET *tileset = tile->tileset; int id = tile->id - tileset->firstgid; int width = tileset->width / tileset->tilewidth; int x = (id % width) * tileset->tilewidth; int y = (id / width) * tileset->tileheight; tile->bitmap = al_create_sub_bitmap( tileset->bitmap, x, y, tileset->tilewidth, tileset->tileheight); } } } map->tile_layer_count++; map->tile_layers = g_slist_prepend(map->tile_layers, layer); } else if (!strcmp((const char*)layer_node->name, "objectgroup")) { layer->type = OBJECT_LAYER; layer->objects = NULL; layer->object_count = 0; // TODO: color? GSList *objects = get_children_for_name(layer_node, "object"); GSList *object_item = objects; while (object_item) { xmlNode *object_node = (xmlNode*)object_item->data; object_item = g_slist_next(object_item); ALLEGRO_MAP_OBJECT *object = MALLOC(ALLEGRO_MAP_OBJECT); object->layer = layer; object->name = g_strdup(get_xml_attribute(object_node, "name")); object->type = g_strdup(get_xml_attribute(object_node, "type")); object->x = atoi(get_xml_attribute(object_node, "x")); object->y = atoi(get_xml_attribute(object_node, "y")); char *object_width = get_xml_attribute(object_node, "width"); object->width = (object_width ? atoi(object_width) : 0); char *object_height = get_xml_attribute(object_node, "height"); object->height = (object_height ? atoi(object_height) : 0); char *gid = get_xml_attribute(object_node, "gid"); if (gid) { object->gid = atoi(gid); } char *object_visible = get_xml_attribute(object_node, "visible"); object->visible = (object_visible ? atoi(object_visible) : 1); // Get the object's properties object->properties = parse_properties(object_node); layer->objects = g_slist_prepend(layer->objects, object); layer->object_count++; } map->object_layer_count++; map->object_layers = g_slist_prepend(map->object_layers, layer); } else { fprintf(stderr, "Error: found invalid layer node \"%s\"\n", layer_node->name); continue; } map->layers = g_slist_prepend(map->layers, layer); } g_slist_free(layers); // If any objects have a tile gid, cache their image layer_item = map->layers; while (layer_item) { ALLEGRO_MAP_LAYER *layer = (ALLEGRO_MAP_LAYER*)layer_item->data; layer_item = g_slist_next(layer_item); if (layer->type != OBJECT_LAYER) { continue; } GSList *objects = layer->objects; while (objects) { ALLEGRO_MAP_OBJECT *object = (ALLEGRO_MAP_OBJECT*)objects->data; objects = g_slist_next(objects); if (!object->gid) { continue; } object->bitmap = al_get_tile_for_id(map, object->gid)->bitmap; object->width = map->tile_width; object->height = map->tile_height; } } xmlFreeDoc(doc); al_change_directory(al_path_cstr(cwd, ALLEGRO_NATIVE_PATH_SEP)); return map; }