static ALLEGRO_PATH *userResourcePath() { #ifdef ALLEGRO_IPHONE return al_get_standard_path(ALLEGRO_USER_DOCUMENTS_PATH); #else return al_get_standard_path(ALLEGRO_USER_SETTINGS_PATH); #endif }
/* Testing function for al_get_standard_path */ void puts_standard_path(int path, char * name) { ALLEGRO_PATH * testpath; testpath = al_get_standard_path(path); puts(name); puts(al_path_cstr(testpath, ALLEGRO_NATIVE_PATH_SEP)); al_destroy_path(testpath); }
bool Message::Init() { ALLEGRO_PATH * pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "Avatar"); for (int i=0;i<7;i++) { char szFileName[255]; sprintf(szFileName,"av%03d.png",i); al_set_path_filename(pPath,szFileName); ALLEGRO_BITMAP * pBitmap = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); if (pBitmap!=NULL) { g_lstAvatar.push_back(pBitmap); } else { al_destroy_path(pPath); return false; } } al_destroy_path(pPath); return true; }
char* GetDataFilePath(char* filename) { char *result = 0; if (al_filename_exists(filename)) { return strdup(filename); } char origfn[255] = "data/"; strcat(origfn, filename); if (al_filename_exists(origfn)) { return strdup(origfn); } void TestPath(char* subpath) { ALLEGRO_PATH *tail = al_create_path(filename); ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); ALLEGRO_PATH *data = al_create_path(subpath); al_join_paths(path, data); al_join_paths(path, tail); //printf("Testing for %s\n", al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); if (al_filename_exists(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP))) { result = strdup(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); } al_destroy_path(tail); al_destroy_path(data); al_destroy_path(path); }
const char* getResource(const char* fmt, ...) { va_list ap; static char res[512]; static ALLEGRO_PATH *dir; static ALLEGRO_PATH *path; va_start(ap, fmt); memset(res, 0, 512); snprintf(res, 511, fmt, ap); if (!dir) { dir = al_get_standard_path(ALLEGRO_RESOURCES_PATH); #ifdef ALLEGRO_MSVC { /* Hack to cope automatically with MSVC workspaces. */ const char *last = al_get_path_component(dir, -1); if (0 == strcmp(last, "Debug") || 0 == strcmp(last, "RelWithDebInfo") || 0 == strcmp(last, "Release") || 0 == strcmp(last, "Profile")) { al_remove_path_component(dir, -1); } } #endif al_append_path_component(dir, "data"); } if (path) al_destroy_path(path); path = al_create_path(res); al_rebase_path(dir, path); return al_path_cstr(path, '/'); }
static void xdpy_set_window_title_default(ALLEGRO_DISPLAY *display, const char *title) { ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver(); ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display; { Atom WM_NAME = XInternAtom(system->x11display, "WM_NAME", False); Atom _NET_WM_NAME = XInternAtom(system->x11display, "_NET_WM_NAME", False); char *list[1] = { (char *) title }; XTextProperty property; Xutf8TextListToTextProperty(system->x11display, list, 1, XUTF8StringStyle, &property); XSetTextProperty(system->x11display, glx->window, &property, WM_NAME); XSetTextProperty(system->x11display, glx->window, &property, _NET_WM_NAME); XSetTextProperty(system->x11display, glx->window, &property, XA_WM_NAME); XFree(property.value); } { XClassHint *hint = XAllocClassHint(); if (hint) { ALLEGRO_PATH *exepath = al_get_standard_path(ALLEGRO_EXENAME_PATH); // hint doesn't use a const char*, so we use strdup to create a non const string hint->res_name = strdup(al_get_path_basename(exepath)); hint->res_class = strdup(al_get_path_basename(exepath)); XSetClassHint(system->x11display, glx->window, hint); free(hint->res_name); free(hint->res_class); XFree(hint); al_destroy_path(exepath); } } }
void InitConfig(void) { ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_USER_SETTINGS_PATH); ALLEGRO_PATH *data = al_create_path("SuperDerpy.ini"); al_join_paths(path, data); config = al_load_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); if (!config) config=al_create_config(); al_destroy_path(path); al_destroy_path(data); }
bool Framework::initialize(std::string config_filename) { if (initialized) return initialized; if (!al_init()) std::cerr << "al_init() failed" << std::endl; ALLEGRO_PATH *resource_path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_change_directory(al_path_cstr(resource_path, ALLEGRO_NATIVE_PATH_SEP)); al_destroy_path(resource_path); if (!al_install_mouse()) std::cerr << "al_install_mouse() failed" << std::endl; if (!al_install_keyboard()) std::cerr << "al_install_keyboard() failed" << std::endl; if (!al_install_joystick()) std::cerr << "al_install_joystick() failed" << std::endl; if (!al_install_audio()) std::cerr << "al_install_audio() failed" << std::endl; if (!al_init_native_dialog_addon()) std::cerr << "al_init_native_dialog_addon() failed" << std::endl; if (!al_init_primitives_addon()) std::cerr << "al_init_primitives_addon() failed" << std::endl; if (!al_init_image_addon()) std::cerr << "al_init_image_addon() failed" << std::endl; if (!al_init_font_addon()) std::cerr << "al_init_font_addon() failed" << std::endl; if (!al_init_ttf_addon()) std::cerr << "al_init_ttf_addon() failed" << std::endl; if (!al_init_acodec_addon()) std::cerr << "al_init_acodec_addon() failed" << std::endl; if (!al_reserve_samples(32)) std::cerr << "al_reserve_samples() failed" << std::endl; srand(time(NULL)); primary_timer = al_create_timer(ALLEGRO_BPS_TO_SECS(60)); al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR | ALLEGRO_MIPMAP); builtin_font = al_create_builtin_font(); event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_joystick_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(primary_timer)); al_register_event_source(event_queue, al_get_joystick_event_source()); al_register_event_source(event_queue, al_get_default_menu_event_source()); if (al_get_num_joysticks()) joystick = al_get_joystick(0); // make this better eventually else std::cerr << "no joystick(s) detected" << std::endl; instance = new Framework(config_filename); Attributes::create_datatype_definition( AllegroColorAttributeDatatype::IDENTIFIER, AllegroColorAttributeDatatype::to_val_func, AllegroColorAttributeDatatype::to_str_func ); initialized = true; return true; }
static void show_path(int id, const char *label) { ALLEGRO_PATH *path; const char *path_str; path = al_get_standard_path(id); path_str = (path) ? al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP) : "<none>"; log_printf("%s: %s\n", label, path_str); al_destroy_path(path); }
void DeinitConfig(void) { ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_USER_SETTINGS_PATH); ALLEGRO_PATH *data = al_create_path("SuperDerpy.ini"); al_make_directory(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); al_join_paths(path, data); al_save_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), config); al_destroy_path(path); al_destroy_path(data); al_destroy_config(config); }
Tileset::Tileset(const char* filename) { ALLEGRO_PATH *path; path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(path, RESOURCES_DIR); al_set_path_filename(path, filename); spritesheet_ = al_load_bitmap(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); al_destroy_path(path); }
/* Function: al_set_app_name */ void al_set_app_name(const char *app_name) { if (app_name) { _al_sane_strncpy(_al_app_name, app_name, sizeof(_al_app_name)); } else { ALLEGRO_PATH *path; path = al_get_standard_path(ALLEGRO_EXENAME_PATH); _al_sane_strncpy(_al_app_name, al_get_path_filename(path), sizeof(_al_app_name)); al_destroy_path(path); } }
SYMBOL_EXPORT void DeinitConfig(struct Game* game) { const ALLEGRO_FILE_INTERFACE* iface = al_get_new_file_interface(); al_set_standard_file_interface(); ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_USER_SETTINGS_PATH); ALLEGRO_PATH* data = al_create_path("SuperDerpy.ini"); al_make_directory(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); al_join_paths(path, data); al_save_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), game->_priv.config); al_destroy_path(path); al_destroy_path(data); al_destroy_config(game->_priv.config); al_set_new_file_interface(iface); }
/* * Calling LoadLibrary with a relative file name is a security risk: * see e.g. Microsoft Security Advisory (2269637) * "Insecure Library Loading Could Allow Remote Code Execution" */ HMODULE _al_win_safe_load_library(const char *filename) { ALLEGRO_PATH *path1 = NULL; ALLEGRO_PATH *path2 = NULL; char buf[MAX_PATH]; const char *other_dirs[3]; HMODULE lib = NULL; bool msvc_only = false; /* MSVC only: if the executable is in the build configuration directory, * which is also just under the current directory, then also try to load the * library from the current directory. This leads to less surprises when * running example programs. */ #if defined(ALLEGRO_MSVC) msvc_only = true; #endif /* Try to load the library from the directory containing the running * executable, the Windows system directories, or directories on the PATH. * Do NOT try to load the library from the current directory. */ if (al_is_system_installed()) { path1 = al_get_standard_path(ALLEGRO_RESOURCES_PATH); } else if (GetModuleFileName(NULL, buf, sizeof(buf)) < sizeof(buf)) { path1 = al_create_path(buf); } if (msvc_only) { path2 = maybe_parent_dir(path1); } other_dirs[0] = path1 ? al_path_cstr(path1, '\\') : NULL; other_dirs[1] = path2 ? al_path_cstr(path2, '\\') : NULL; other_dirs[2] = NULL; /* sentinel */ _al_sane_strncpy(buf, filename, sizeof(buf)); if (PathFindOnPath(buf, other_dirs)) { ALLEGRO_DEBUG("PathFindOnPath found: %s\n", buf); lib = load_library_at_path(buf); } else { ALLEGRO_WARN("PathFindOnPath failed to find %s\n", filename); } al_destroy_path(path1); al_destroy_path(path2); return lib; }
ALLEGRO_MAP *al_open_map_from(const char *root_dir, const char *dir, const char *filename) { // get cwd, and create path to specified directory ALLEGRO_PATH *cwd = al_get_standard_path(ALLEGRO_RESOURCES_PATH); const char *cwdstr = al_path_cstr(cwd, ALLEGRO_NATIVE_PATH_SEP); const char *build_path = strstr(cwdstr, root_dir); char *new_path = al_path_to_root(build_path); strcat(new_path, dir); // get map, and return to specified directory ALLEGRO_MAP *map = al_open_map(new_path, filename); al_change_directory(al_path_to_root(build_path)); return map; }
BolsaDinero * BolsaDinero::crear_bolsa_dinero(ActorManager *actmgr,int mov_to_dead) { ALLEGRO_BITMAP *bmp; MapaDeBits *bitm; BolsaDinero *bolsa_dinero_tmp; ALLEGRO_PATH *path; /* * para crear path relativos y poder distribuir el programa y ejecutarlo * fuera del IDE */ path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_remove_path_component(path,-1); al_append_path_component(path, "resources"); al_set_path_filename(path, "bolsa-dinero.png"); bolsa_dinero_tmp = new BolsaDinero(actmgr,mov_to_dead); bmp = al_load_bitmap(al_path_cstr(path, '/')); if(bmp == NULL) { al_show_native_message_box(al_get_current_display(), "Ventana de error", "error fatal", "Error al cargar resource bitmap", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } // Hacemos que no se vea el color negro en el bitmap de rana al_convert_mask_to_alpha(bmp, al_map_rgb(0,0,0)); bitm=new MapaDeBits(bolsa_dinero_tmp, bmp); bolsa_dinero_tmp->set_actor_graphic(bitm); bolsa_dinero_tmp->set_x(Game::rnd(0,Game::gfx_w-bolsa_dinero_tmp->get_w())); bolsa_dinero_tmp->set_y(Game::rnd(50,Game::gfx_h-bolsa_dinero_tmp->get_h())); bolsa_dinero_tmp->set_is_detected(true); bolsa_dinero_tmp->set_team(TEAM_BOLSA_DINERO); bolsa_dinero_tmp->set_collision_method(CollisionManager::PP_COLLISION); actmgr->add(bolsa_dinero_tmp); al_destroy_path(path); return bolsa_dinero_tmp; }
bool TKlingonBC::Init() { ALLEGRO_PATH * pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "Ships"); al_set_path_filename(pPath,"KlingonCruiser.png"); g_pKlingonBCBitmap = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); al_destroy_path(pPath); if (g_pKlingonBCBitmap==NULL) { return false; } else { return true; } }
std::shared_ptr<ALLEGRO_BITMAP> Tile::getFlameFrame(size_t num) { if(flames == NULL) { ALLEGRO_PATH *path; path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(path, RESOURCES_DIR); al_set_path_filename(path, "flames.png"); flames = al_load_bitmap(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); al_destroy_path(path); } if(num < 3) { return std::shared_ptr<ALLEGRO_BITMAP>(al_create_sub_bitmap(flames, num * 32, 0, 32, 32), al_destroy_bitmap); } return NULL; }
bool TEnterprise::Init() { ALLEGRO_PATH * pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "Ships"); al_set_path_filename(pPath,"ent01.png"); g_pEnterpriseBitmap = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); if (g_pEnterpriseBitmap == NULL) return false; pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "Interface"); al_set_path_filename(pPath,"eng01.png"); g_pEngineeringBitmap = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); if (g_pEngineeringBitmap == NULL) return false; pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "Interface"); al_set_path_filename(pPath,"Com_Trans.png"); g_pCommunicationBitmap = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); if (g_pCommunicationBitmap == NULL) return false; pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "Interface"); al_set_path_filename(pPath,"Switch.png"); g_pSwitchBitmap = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); if (g_pSwitchBitmap == NULL) return false; pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "Photon"); al_set_path_filename(pPath,"casing.png"); g_pPhotonCasing = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); if (g_pPhotonCasing == NULL) return false; pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_append_path_component(pPath, "graphics"); al_append_path_component(pPath, "probe"); al_set_path_filename(pPath,"casing.png"); g_pProbeCasing = al_load_bitmap(al_path_cstr(pPath,ALLEGRO_NATIVE_PATH_SEP)); if (g_pProbeCasing == NULL) return false; al_destroy_path(pPath); return true; }
/* drop build configuration directory tail */ static ALLEGRO_PATH *get_resources_path(void) { ALLEGRO_PATH *path; const char *last; path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); if (!path) return al_create_path(""); /* If the last directory component is the name of an MSVC build * configuration, then drop it. */ last = al_get_path_tail(path); if (last && (0 == strcmp(last, "Debug") || 0 == strcmp(last, "Release") || 0 == strcmp(last, "RelWithDebInfo") || 0 == strcmp(last, "Profile"))) { al_drop_path_tail(path); } return path; }
int init_bitmaps(Board *b){ // will load bitmaps from folders named 0, 1,..., 7 // inside the folder "icons", each containing 8 square bitmaps int i,j, k=0; char pathname[1000]; ALLEGRO_PATH *path; ALLEGRO_BITMAP *dispbuf = al_get_target_bitmap(); al_set_target_bitmap(NULL); // this is a workaround for android -- try removing later #ifdef ALLEGRO_ANDROID al_android_set_apk_file_interface(); #endif al_set_target_bitmap(dispbuf); for(i=0;i<b->h+1;i++){ for(j=0;j<b->n; j++){ al_utf8_encode(symbol_char[i][j], BF_CODEPOINT_START+ j + i*b->n); symbol_char[i][j][al_utf8_width(BF_CODEPOINT_START+ j + i*b->n)] = '\0'; } } // create buttons // xxx todo: improve these default_font = al_load_font(DEFAULT_FONT_FILE, 16, 0); if(!default_font) errlog("Error loading default font"); b->info_text_bmp = NULL; b->info_panel.bmp = NULL; // if this fails, buttons will be created anyway at update_bitmaps b->button_bmp[0] = al_load_bitmap("buttons/light-bulb.png"); b->button_bmp[1] = al_load_bitmap("buttons/question.png"); b->button_bmp[2] = al_load_bitmap("buttons/gear.png"); b->button_bmp[3] = al_load_bitmap("buttons/undo.png"); if(b->type_of_tiles == 2) return init_bitmaps_classic(b); if(b->type_of_tiles == 1){ // use bitmaps #ifndef ALLEGRO_ANDROID path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_path_cstr(path, '/'); #else path = al_create_path(""); #endif for(j=0; j<b->h; j++){ for(k=0; k<b->n; k++){ snprintf(pathname, 999, "%sicons/%d/%d.png", al_path_cstr(path, '/'), j, k); basic_bmp[j][k] = al_load_bitmap(pathname); if(!basic_bmp[j][k]){ errlog("Error loading %s.", pathname); unload_basic_bmps(b, j,k-1); al_destroy_path(path); return -1; } } } al_destroy_path(path); } // create symbols (alternatively we could load these from files!)) symbol_bmp[SYM_FORBIDDEN] = al_create_bitmap(256, 256); symbol_bmp[SYM_SWAPPABLE] = al_create_bitmap(3*256 + 2*b->clue_unit_space, 256); symbol_bmp[SYM_ONE_SIDE] = al_create_bitmap(256, 256); symbol_bmp[SYM_ONLY_ONE] = al_create_bitmap(256, 3*256); if( (!symbol_bmp[SYM_FORBIDDEN]) || (!symbol_bmp[SYM_SWAPPABLE]) || (!symbol_bmp[SYM_ONE_SIDE]) || !symbol_bmp[SYM_ONLY_ONE]){ fprintf(stderr, "Error creating bitmap.\n"); return -1; } al_set_target_bitmap(symbol_bmp[SYM_FORBIDDEN]); al_clear_to_color(NULL_COLOR); al_draw_line(1, 1, 254, 254, al_map_rgba_f(1,0,0,0.5),4); al_draw_line(1, 254, 254, 1, al_map_rgba_f(1,0,0,0.5),4); al_set_target_bitmap(symbol_bmp[SYM_SWAPPABLE]); al_clear_to_color(NULL_COLOR); al_draw_line(256*0.7,256*0.9, 256*(3-0.7),256*0.9, al_map_rgba_f(1,0,0,0.5), 2); al_draw_filled_triangle(256*0.5,256*0.9, 256*0.7,256, 256*0.7, 256*0.8, al_map_rgba_f(1,0,0,0.35)); al_set_target_bitmap(symbol_bmp[SYM_ONE_SIDE]); al_clear_to_color(NULL_COLOR); al_draw_filled_circle(256/2, 256/2, 0.03*256, WHITE_COLOR); al_draw_filled_circle(256/2 - 0.2*256, 256/2, 0.03*256, WHITE_COLOR); al_draw_filled_circle(256/2 + 0.2*256, 256/2, 0.03*256, WHITE_COLOR); al_set_target_bitmap(symbol_bmp[SYM_ONLY_ONE]); al_clear_to_color(NULL_COLOR); al_draw_filled_triangle(256*0.3, 256, 256*0.7, 256, 256*0.5, 256*0.7, al_map_rgba_f(1,0,0,0.5)); al_draw_filled_triangle(256*0.3, 256, 256*0.7, 256, 256*0.5, 256*1.3, al_map_rgba_f(1,0,0,0.5)); al_draw_line(256*0.5, 256*0.8, 256*0.5, 256*1.2, WHITE_COLOR, 3); al_set_target_bitmap(dispbuf); return 0; }
int MonopolyGame::loadResources() { reset(); // Normalize the paths. ALLEGRO_PATH *path = al_get_standard_path( ALLEGRO_RESOURCES_PATH ); al_append_path_component( path, "etc" ); al_change_directory( al_path_cstr( path, '/' ) ); // change the working directory // // Load the board's bitmap // al_set_path_filename(path, "board.png"); if( !fileExists( al_path_cstr(path, '/') ) ) { fprintf( stderr, "[ERROR] Cannot find bitmap: %s\n", al_path_cstr(path, '/') ); return -1; } else { m_alBoardImage = al_load_bitmap( al_path_cstr(path, '/') ); if( !m_alBoardImage ) { fprintf( stderr, "[ERROR] Failed loading board bitmap: %s\n", al_path_cstr(path, '/') ); return -1; } } // // Load the font set. // al_set_path_filename(path, "Emblem.ttf"); // Start building the fonts from 24pt. int initialSize = 24; if( !fileExists( al_path_cstr(path, '/') ) ) { fprintf( stderr, "[ERROR] Cannot find font file: %s\n", al_path_cstr(path, '/') ); return -1; } else { for( int fontCounter = 0; fontCounter < MAX_FONTS; fontCounter++ ) { m_fontCollection[fontCounter] = al_load_font( al_path_cstr(path, '/'), initialSize, 0 ); initialSize += 10; if( !m_fontCollection[fontCounter] ) { fprintf( stderr, "[ERROR] Failed loading font: %s size: %i\n", al_path_cstr(path, '/'), initialSize ); return -1; } } } // Now load the possible player pieces into memory. // The piece file locations are stored in the database. for( int pieceCounter = 0; pieceCounter < PLAYER_PIECES_COUNT; pieceCounter++ ) { std::string tempQuery = ""; std::string sFileName = ""; tempQuery = m_sqlConn.Format( "SELECT %s FROM %s WHERE %s = %i", "path", DB_PIECES_TABLE, "id", pieceCounter ); if( !m_sqlConn.SelectStr( sFileName, tempQuery.c_str() ) ) { // Append the filename to the resource path. al_set_path_filename( path, sFileName.c_str() ); if( !fileExists( al_path_cstr(path, '/') ) ) { fprintf( stderr, "[ERROR] Cannot find bitmap: %s\n", al_path_cstr(path, '/') ); return -1; } else { // Load the bitmap from the filename from above. m_alpieceImages[pieceCounter] = al_load_bitmap( al_path_cstr(path, '/') ); } if( !m_alpieceImages[pieceCounter] ) { // TODO: CURRENTLY I HAVE NO PLACE HOLDER IMAGES, SO THE APPLICATION WILL FAIL HERE! fprintf( stderr, "[ERROR] Failed loading bitmap: %s\n", al_path_cstr(path, '/') ); return -1; } } else { fprintf( stderr, "[ERROR] Error processing SQL statement: %s\n", tempQuery.c_str() ); return -1; } } // Assign random pieces to each player. for( int plCount = 0; plCount < NUM_PLAYERS; plCount++ ) { int* randomPlayerSel = rollDice( 1, PLAYER_PIECES_COUNT ); m_playerList[plCount].set_image( m_alpieceImages[randomPlayerSel[0]] ); } al_destroy_path( path ); // // Finally we will pull the property list from the database. // if( buildPropertyList() ) { fprintf( stderr, "Failure building Property List, Please ensure the database is available!\n" ); return -1; } return 0; }
bool initialize () { ALLEGRO_BITMAP *temp_color_bitmap; ALLEGRO_BITMAP *temp_mask_bitmap; global.quit = false; al_set_app_name ("Super Battle Thor II"); if (!al_init ()) { fputs ("Error: Could not start allegro.\n", stderr); return false; } al_set_new_display_option (ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); al_set_new_display_flags (ALLEGRO_GENERATE_EXPOSE_EVENTS); global.display = al_create_display (256, 192); if (!global.display) { fputs ("Error: Allegro could not create a display of 256x192 pixels.\n", stderr); return false; } if (!al_install_keyboard ()) { fputs ("Allegro could not initialize the keyboard system.\n", stderr); return false; } global.timer = al_create_timer (ALLEGRO_BPS_TO_SECS (60)); if (!global.timer) { fputs ("Allegro could not create a 60hrz timer.\n", stderr); return false; } global.queue = al_create_event_queue (); if (!global.queue) { fputs ("Allegro could not create an event queue.\n", stderr); return false; } al_register_event_source (global.queue, al_get_keyboard_event_source ()); al_register_event_source (global.queue, al_get_display_event_source (global.display)); al_register_event_source (global.queue, al_get_timer_event_source (global.timer)); if (!al_init_image_addon ()) { fputs ("Allegro could not initialize the image loading system.\n", stderr); return false; } if (!al_install_audio ()) { fputs ("Allegro could not initialize the audio system.\n", stderr); return false; } al_reserve_samples (16); if (!al_init_acodec_addon ()) { fputs ("Allegro could not initialize the audio file loading system.\n", stderr); return false; } al_init_font_addon (); /* load files */ global.path = al_get_standard_path (ALLEGRO_PROGRAM_PATH); al_append_path_component (global.path, "media"); al_set_path_filename (global.path, "proggy_tiny.png"); global.programing_font = al_load_bitmap_font (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.programing_font) { fputs ("Allegro could not load proggy_tiny.png.\n", stderr); return false; } al_set_path_filename (global.path, "mjollnir.png"); global.sprite_bitmap[SPRITE_THOR] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sprite_bitmap[SPRITE_THOR]) { fputs ("Allegro could not load mjollnir.png.\n", stderr); return false; } al_set_path_filename (global.path, "arrows.png"); global.arrow_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.arrow_bitmap) { fputs ("Allegro could not load arrows.png.\n", stderr); return false; } al_set_path_filename (global.path, "background_1_2_3_4.jpg"); global.backgrounds[BACKGROUND_1] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_1]) { fputs ("Allegro could not load background_1_2_3_4.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "background_5.jpg"); global.backgrounds[BACKGROUND_5] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_5]) { fputs ("Allegro could not load background_5.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "background_6.jpg"); global.backgrounds[BACKGROUND_6] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_6]) { fputs ("Allegro could not load background_6.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "title_screen.jpg"); global.backgrounds[BACKGROUND_TITLE] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_TITLE]) { fputs ("Allegro could not load title_screen.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "game_over.jpg"); global.backgrounds[BACKGROUND_GAME_OVER] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_GAME_OVER]) { fputs ("Allegro could not load game_over.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "you_win.jpg"); global.backgrounds[BACKGROUND_YOU_WIN] = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.backgrounds[BACKGROUND_YOU_WIN]) { fputs ("Allegro could not load you_win.jpg.\n", stderr); return false; } global.backgrounds[BACKGROUND_BLACK] = al_create_bitmap (256, 192); if (!global.backgrounds[BACKGROUND_BLACK]) { fputs ("Allegro could not make a 256x192 bitmap :o", stderr); return false; } al_set_target_bitmap (global.backgrounds[BACKGROUND_BLACK]); al_clear_to_color (al_map_rgb (0, 0, 0)); al_set_target_backbuffer (global.display); /* start copy paste code here becasue I'm lazy */ /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "0-thor.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 0-thor.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "0-thor_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 0-thor_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_THOR] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "1-giant.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 1-giant.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "1-giant_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 1-giant_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_GIANT] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "2-nidhogg.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 2-nidhogg.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "2-nidhogg_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 2-nidhogg_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_NIDHOGG] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "3-ran.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 3-ran.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "3-ran_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 3-ran_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_RAN] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "4-grendel.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 4-grendel.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "4-grendel_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 4-grendel_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_GRENDEL] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "5-fafnir.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 5-fafnir.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "5-fafnir_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 5-fafnir_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_FAFNIR] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "6-loki.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 6-loki.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "6-loki_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 6-loki_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_LOKI] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "7-fenrir.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load 7-fenrir.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "7-fenrir_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load 7-fenrir_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_FENRIR] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ al_set_path_filename (global.path, "fight.jpg"); temp_color_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_color_bitmap) { fputs ("Allegro could not load fight.jpg.\n", stderr); return false; } al_set_path_filename (global.path, "fight_mask.jpg"); temp_mask_bitmap = al_load_bitmap (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!temp_mask_bitmap) { fputs ("Allegro could not load fight_mask.jpg.\n", stderr); return false; } global.sprite_bitmap[SPRITE_FIGHT] = make_from_mask (temp_color_bitmap, temp_mask_bitmap); al_destroy_bitmap (temp_color_bitmap); al_destroy_bitmap (temp_mask_bitmap); /*------------------------------------------------------------------------*/ /* loading sounds here (still copy paste) */ al_set_path_filename (global.path, "titlescreen_loop.ogg"); global.sounds[SOUND_TITLESCREEN_LOOP] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_TITLESCREEN_LOOP]) { fputs ("Allegro could not load titlescreen_loop.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "begin_battle.ogg"); global.sounds[SOUND_BEGIN_BATTLE] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_BEGIN_BATTLE]) { fputs ("Allegro could not load begin_battle.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "battle_loop.ogg"); global.sounds[SOUND_BATTLE_LOOP] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_BATTLE_LOOP]) { fputs ("Allegro could not load battle_loop.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "dragon_loop.ogg"); global.sounds[SOUND_DRAGON_LOOP] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_DRAGON_LOOP]) { fputs ("Allegro could not load dragon_loop.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "hit_1.ogg"); global.sounds[SOUND_HIT_1] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_HIT_1]) { fputs ("Allegro could not load hit_1.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "hit_2.ogg"); global.sounds[SOUND_HIT_2] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_HIT_2]) { fputs ("Allegro could not load hit_2.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_1.ogg"); global.sounds[SOUND_LAUGH_1] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_1]) { fputs ("Allegro could not load laugh_1.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_2.ogg"); global.sounds[SOUND_LAUGH_2] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_2]) { fputs ("Allegro could not load laugh_2.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_3.ogg"); global.sounds[SOUND_LAUGH_3] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_3]) { fputs ("Allegro could not load laugh_3.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_4.ogg"); global.sounds[SOUND_LAUGH_4] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_4]) { fputs ("Allegro could not load laugh_4.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_5.ogg"); global.sounds[SOUND_LAUGH_5] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_5]) { fputs ("Allegro could not load laugh_5.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "laugh_6.ogg"); global.sounds[SOUND_LAUGH_6] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_LAUGH_6]) { fputs ("Allegro could not load laugh_6.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "killed_baddie.ogg"); global.sounds[SOUND_KILLED_BADDIE] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_KILLED_BADDIE]) { fputs ("Allegro could not load killed_baddie.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "got_hit.ogg"); global.sounds[SOUND_GOT_HIT] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_GOT_HIT]) { fputs ("Allegro could not load got_hit.ogg.\n", stderr); return false; } al_set_path_filename (global.path, "you_are_dead.ogg"); global.sounds[SOUND_YOU_ARE_DEAD] = al_load_sample (al_path_cstr (global.path, ALLEGRO_NATIVE_PATH_SEP)); if (!global.sounds[SOUND_YOU_ARE_DEAD]) { fputs ("Allegro could not you_are_dead.ogg.\n", stderr); return false; } global.music = NULL; al_destroy_path (global.path); return true; }
void Hormiga::crear_hormiga(ActorManager *actmgr,int tipo,Hormiguero *hormiguero_tmp, int pos_x,int pos_y) { ALLEGRO_BITMAP *bmp; MapaDeBits *bitm; Hormiga *hormiga_tmp; ALLEGRO_PATH *path; /* * para crear path relativos y poder distribuir el programa y ejecutarlo * fuera del IDE */ path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_remove_path_component(path,-1); al_append_path_component(path, "resources"); if (tipo == kRedAnt) { al_set_path_filename(path, "HormigaRoja.png"); hormiga_tmp = new Hormiga(actmgr,hormiguero_tmp,kRedAnt); } else { al_set_path_filename(path, "HormigaVerde.png"); hormiga_tmp = new Hormiga(actmgr,hormiguero_tmp,kGreenAnt); } bmp = al_load_bitmap(al_path_cstr(path, '/')); if(bmp == NULL) { al_show_native_message_box(al_get_current_display(), "Ventana de error", "error fatal", "Error al cargar resource bitmap", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } // Hacemos que no se vea el color negro en el bitmap de hormiga al_convert_mask_to_alpha(bmp, al_map_rgb(0,0,0)); bitm=new MapaDeBits(hormiga_tmp, bmp); hormiga_tmp->set_actor_graphic(bitm); if (pos_x == 0) { hormiga_tmp->set_x(Game::rnd(50,Game::gfx_w-hormiga_tmp->get_w())); } else { hormiga_tmp->set_x(pos_x); } if (pos_y == 0) { hormiga_tmp->set_y(Game::rnd(0,Game::gfx_h-hormiga_tmp->get_h())); } else { hormiga_tmp->set_y(pos_y); } hormiga_tmp->set_is_detected(true); if (tipo == kRedAnt) { hormiga_tmp->set_team(TEAM_HORMIGAS_ROJAS); } else { hormiga_tmp->set_team(TEAM_HORMIGAS_VERDES); } hormiga_tmp->set_collision_method(CollisionManager::PP_COLLISION); actmgr->add(hormiga_tmp); al_destroy_path(path); }
TILED_MAP* tiled_load_tmx_file (const char *filename) { TILED_MAP *map; xmlDoc *doc; xmlNode *root; char *str; if (!filename) return NULL; LIBXML_TEST_VERSION doc = xmlReadFile (filename, NULL, 0); if (!doc) return NULL; ALLEGRO_PATH *mapdir = al_create_path (filename); al_set_path_filename (mapdir, NULL); if (!al_change_directory (al_path_cstr (mapdir, ALLEGRO_NATIVE_PATH_SEP))) { printf ("Failed to change directory."); } al_destroy_path (mapdir); root = xmlDocGetRootElement (doc); map = al_malloc (sizeof (TILED_MAP)); map->width = get_int (root, "width", 0); map->height = get_int (root, "height", 0); map->tile_width = get_int (root, "tilewidth", 0); map->tile_height = get_int (root, "tileheight", 0); map->strings = _al_list_create (); map->properties = get_properties (root, map); map->tiles = NULL; str = get_xml_attribute (root, "orientation"); if (!strcmp (str, "orthogonal")) map->orientation = ORIENTATION_ORTHOGONAL; else if (!strcmp (str, "isometric")) map->orientation = ORIENTATION_ISOMETRIC; else if (!strcmp (str, "staggered")) map->orientation = ORIENTATION_STAGGERED; else map->orientation = ORIENTATION_UNKNOWN; // Tilesets LIST *tileset_nodes = get_children_for_name (root, 1, "tileset"); map->tilesets = create_list (_al_list_size (tileset_nodes)); LIST_ITEM *tileset_item = _al_list_front (tileset_nodes); while (tileset_item) { xmlNode *tileset_node = _al_list_item_data (tileset_item); TILED_TILESET *tileset = al_malloc (sizeof (TILED_TILESET)); tileset->first_gid = get_int (tileset_node, "firstgid", 1); tileset->tile_width = get_int (tileset_node, "tilewidth", 0); tileset->tile_height = get_int (tileset_node, "tileheight", 0); tileset->name = get_str (tileset_node, "name"); tileset->properties = get_properties (tileset_node, map); xmlNode *image_node = get_first_child_for_name (tileset_node, "image"); tileset->image_width = get_int (image_node, "width", 0); tileset->image_height = get_int (image_node, "height", 0); tileset->image_source = get_str (image_node, "source"); tileset->bitmap = al_load_bitmap (tileset->image_source); LIST *tile_nodes = get_children_for_name (tileset_node, 1, "tile"); int tiles_per_row = tileset->image_width / tileset->tile_width; tileset->num_tiles = (tileset->image_width * tileset->image_height) / (tileset->tile_width * tileset->tile_height); TILED_TILE *tile; tileset->tiles = al_malloc (tileset->num_tiles * sizeof (TILED_TILE)); for (int i = 0; i < tileset->num_tiles; i++) { tile = &tileset->tiles[i]; tile->id = i; tile->gid = i + tileset->first_gid; tile->tileset = tileset; tile->properties = NULL; int x = (i % tiles_per_row) * tileset->tile_width; int y = (i / tiles_per_row) * tileset->tile_height; tile->bitmap = al_create_sub_bitmap(tileset->bitmap, x, y, tileset->tile_width, tileset->tile_height); map->tiles = aa_insert (map->tiles, &tile->gid, tile, intcmp); } LIST_ITEM *tile_item = _al_list_front (tile_nodes); while (tile_item) { xmlNode *tile_node = (xmlNode*)_al_list_item_data (tile_item); int id = get_int (tile_node, "id", 0); tile = &tileset->tiles[id]; tile->properties = get_properties (tile_node, map); tile_item = _al_list_next (tile_nodes, tile_item); } _al_list_destroy (tile_nodes); _al_list_push_back_ex (map->tilesets, tileset, dtor_tileset); tileset_item = _al_list_next (tileset_nodes, tileset_item); } _al_list_destroy (tileset_nodes); // Layers LIST *layer_nodes = get_children_for_name (root, 2, "layer", "objectgroup"); map->layers = create_list (_al_list_size (layer_nodes)); map->layers_fore = _al_list_create (); map->layers_back = _al_list_create (); LIST_ITEM *layer_item = _al_list_front (layer_nodes); while (layer_item) { xmlNode *layer_node = _al_list_item_data (layer_item); TILED_LAYER *layer = NULL; const char* type_str = (const char *)layer_node->name; if (!strcmp (type_str, "layer")) { layer = al_malloc (sizeof (TILED_LAYER_TILE)); layer->type = LAYER_TYPE_TILE; } else if (!strcmp (type_str, "objectgroup")) { layer = al_malloc (sizeof (TILED_LAYER_OBJECT)); layer->type = LAYER_TYPE_OBJECT; } layer->name = get_str (layer_node, "name"); layer->width = get_int (layer_node, "width", 0); layer->height = get_int (layer_node, "height", 0); layer->opacity = get_float (layer_node, "opacity", 1.0); layer->map = map; layer->properties = get_properties (layer_node, map); char *order = aa_search (layer->properties, "order", charcmp); if (layer->type == LAYER_TYPE_TILE) { TILED_LAYER_TILE *tile_layer = (TILED_LAYER_TILE *)layer; tile_layer->tiles = al_malloc (layer->height * sizeof (TILED_TILE*)); xmlNode *data_node = get_first_child_for_name (layer_node, "data"); LIST *tile_nodes = get_children_for_name (data_node, 1, "tile"); LIST_ITEM *tile_item = _al_list_front (tile_nodes); for (int j = 0; j < layer->height; j++) { tile_layer->tiles[j] = al_malloc (layer->width * sizeof (TILED_TILE*)); for (int k = 0; k < layer->width; k++) { xmlNode *tile_node = (xmlNode*)_al_list_item_data (tile_item); int id = get_int (tile_node, "gid", 0); if (id == 0) { tile_layer->tiles[j][k] = NULL; } else { TILED_TILE *tile = aa_search (map->tiles, &id, intcmp); tile_layer->tiles[j][k] = tile; } tile_item = _al_list_next (tile_nodes, tile_item); } } _al_list_destroy (tile_nodes); } else if (layer->type == LAYER_TYPE_OBJECT) { TILED_LAYER_OBJECT *object_layer = (TILED_LAYER_OBJECT *)layer; object_layer->objects = _al_list_create (); LIST *object_nodes = get_children_for_name (layer_node, 1, "object"); LIST_ITEM *object_item = _al_list_front (object_nodes); while (object_item) { xmlNode *object_node = (xmlNode*)_al_list_item_data (object_item); TILED_OBJECT *cobj = NULL; int width = get_int (object_node, "width", 0); int gid = get_int (object_node, "gid", 0); int px = get_int (object_node, "x", 0); int py = get_int (object_node, "y", 0); if (width > 0) { TILED_OBJECT_RECT *obj = al_malloc (sizeof (TILED_OBJECT_RECT)); obj->object.type = OBJECT_TYPE_RECT; obj->width = width; obj->height = get_int (object_node, "height", 0); cobj = (TILED_OBJECT *) obj; } else if (gid > 0) { TILED_OBJECT_TILE *obj = al_malloc (sizeof (TILED_OBJECT_TILE)); obj->object.type = OBJECT_TYPE_TILE; obj->tile = aa_search (map->tiles, &gid, intcmp); cobj = (TILED_OBJECT *) obj; } else { TILED_OBJECT_GEOM *obj = al_malloc (sizeof (TILED_OBJECT_GEOM)); obj->object.type = OBJECT_TYPE_GEOM; xmlNode *poly_node = get_first_child_for_name (object_node, "polyline"); if (poly_node) { obj->points = get_float_points (poly_node, "points", &obj->num_points); offset_points (px, py, obj->points, obj->num_points); } else { poly_node = get_first_child_for_name (object_node, "polygon"); if (poly_node) obj->points = get_float_points (poly_node, "points", &obj->num_points); offset_points (px, py, obj->points, obj->num_points); } cobj = (TILED_OBJECT *) obj; } cobj->x = px; cobj->y = py; cobj->name = get_str (object_node, "name"); cobj->type_str = get_str (object_node, "type"); cobj->properties = get_properties (object_node, map); _al_list_push_back_ex (object_layer->objects, cobj, dtor_object); object_item = _al_list_next (object_nodes, object_item); } _al_list_destroy (object_nodes); } layer_item = _al_list_next (layer_nodes, layer_item); _al_list_push_back_ex (map->layers, layer, dtor_layer); if (order && !strcmp (order, "fore")) _al_list_push_back (map->layers_fore, layer); else _al_list_push_back (map->layers_back, layer); } _al_list_destroy (layer_nodes); xmlFreeDoc (doc); xmlCleanupParser (); ALLEGRO_PATH *respath = al_get_standard_path (ALLEGRO_RESOURCES_PATH); al_change_directory (al_path_cstr (respath, ALLEGRO_NATIVE_PATH_SEP)); al_destroy_path (respath); return map; }
void Hormiga::hit(Actor *who, int damage) { ALLEGRO_PATH *path; /* * para crear path relativos y poder distribuir el programa y ejecutarlo * fuera del IDE */ path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_remove_path_component(path,-1); al_append_path_component(path, "resources"); switch (who->get_team()) { case TEAM_HORMIGAS_ROJAS: break; case TEAM_HORMIGAS_VERDES: break; case TEAM_OSO: am->del(this); break; case TEAM_COMIDA: if (state == kForage) state=kGoHome; break; case TEAM_AGUA: if (state == kThirsty) state=kForage; break; case TEAM_VENENO: if (tipo == kGreenAnt) { //creo la hormiga en la misma posicion de la que acaba de chocar con el veneno Hormiga::crear_hormiga(am,kRedAnt,Hm,x,y); am->del(this); } break; case TEAM_HORMIGUERO: if ((who->get_y()==Hm->get_y()) && (who->get_x()==Hm->get_x()) && (state == kGoHome)) { /* * Si he llegado a mi hormiguero creo una nueva hormiga de mi tipo y me * pongo a buscara agua */ state=kThirsty; Hormiga::crear_hormiga(am,tipo,Hm); } break; case TEAM_RANA: if (tipo == kRedAnt) { am->del(this); } break; case TEAM_SCORE: /* * La envio al hormiguero */ x = Hm->get_x(); y = Hm->get_y(); break; case TEAM_BOLSA_DINERO: break; default: break; } al_destroy_path(path); }
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; }
// standard paths not yet implemented void init_standard_paths(void) { settings.path_to_executable [0] = 0; if (settings.option [OPTION_STANDARD_PATHS] == STANDARD_PATHS_EXECUTABLE) { // Unfortunately there does not seem to be any simple way to find the execution directory. // We can only get the full path of the executable, including the file name. // So we need to remove the file name from the end of the path: ALLEGRO_PATH* executable_path; executable_path = al_get_standard_path(ALLEGRO_EXENAME_PATH); if (executable_path == NULL) { // may still be okay... fpr("\nFailed to get executable path. Attempting to run using relative paths..."); return; } char filename [100]; // 100 should be plenty of room strncpy(filename, al_get_path_filename(executable_path), 95); // 95 should too int filename_length = strlen(filename); // const char* temp_path = al_path_cstr(executable_path, ALLEGRO_NATIVE_PATH_SEP); // this pointer should be valid until the path is modified // int temp_path_length = strlen(temp_path); char file_path [FILE_PATH_LENGTH]; strncpy(file_path, al_path_cstr(executable_path, ALLEGRO_NATIVE_PATH_SEP), FILE_PATH_LENGTH - 5); int file_path_length = strlen(file_path); if (file_path_length >= FILE_PATH_LENGTH - 10) { fpr("\nSorry, your file path (%s) is too long (%i characters; the maximum is %i).", file_path, file_path_length, FILE_PATH_LENGTH - 10); fpr("\nAttempting to run using relative paths..."); return; } file_path [file_path_length - filename_length] = 0; strcpy(settings.path_to_executable, file_path); al_destroy_path(executable_path); return; } if (settings.option [OPTION_STANDARD_PATHS] == STANDARD_PATHS_VARIOUS) { /* ALLEGRO_PATH* standard_path; // PATH_TYPE_RESOURCES: standard_path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); if (standard_path == NULL) // may still be okay... fpr("\nFailed to get resources path. Attempting to run using relative path..."); else { char temp_path [FILE_PATH_LENGTH]; strncpy(temp_path, al_path_cstr(standard_path, ALLEGRO_NATIVE_PATH_SEP), FILE_PATH_LENGTH - 10); int temp_path_length = strlen(temp_path); if (temp_path_length >= FILE_PATH_LENGTH - 20) { fpr("\nSorry, your resources path (%s) is too long (maximum %i).", temp_path, FILE_PATH_LENGTH - 20); fpr("\nAttempting to run using relative path..."); } else { strcpy(settings.standard_path [PATH_TYPE_RESOURCES], temp_path; } al_destroy_path(standard_path); } */ } // settings.option [OPTION_STANDARD_PATHS] is probably STANDARD_PATHS_NONE return; }
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); } }
/* * Parse a map file into an `ALLEGRO_MAP` struct. By default, relative values of `dir` * will be resolved relative to the path of the running executable. To change this, * call `al_find_resources_as(RELATIVE_TO_CWD);` first. */ ALLEGRO_MAP *al_open_map(const char *dir, const char *filename) { char *cwd = al_get_current_directory(); if (!cwd) { fprintf(stderr, "failed to get cwd; errno = %d", al_get_errno()); return NULL; } // `resources` will point to either cwd or the location of the running executable, // depending on what was passed in for `rel`. ALLEGRO_PATH *resources; switch (resources_rel_to) { case RELATIVE_TO_EXE: resources = al_get_standard_path(ALLEGRO_RESOURCES_PATH); break; case RELATIVE_TO_CWD: resources = al_create_path_for_directory(cwd); break; default: fprintf(stderr, "unexpected value for `resources_rel_to` in al_open_map(): %d\n", resources_rel_to); al_free(cwd); return NULL; } ALLEGRO_PATH *map_dir = al_create_path_for_directory(dir); // Change directory to <cwd>/dir if dir is relative, otherwise dir. ALLEGRO_PATH *new_path = (al_join_paths(resources, map_dir) ? resources : map_dir); const char *new_path_cstr = al_path_cstr(new_path, ALLEGRO_NATIVE_PATH_SEP); if (!al_change_directory(new_path_cstr)) { fprintf(stderr, "Error: failed to cd into `%s` in al_open_map().\n", new_path_cstr); al_destroy_path(resources); al_destroy_path(map_dir); al_free(cwd); return NULL; } al_destroy_path(resources); al_destroy_path(map_dir); // Read in the data file xmlDoc *doc = xmlReadFile(filename, NULL, 0); if (!doc) { fprintf(stderr, "Error: failed to parse map data: %s\n", filename); al_free(cwd); return NULL; } // Get the root element, <map> xmlNode *root = xmlDocGetRootElement(doc); // Get some basic info ALLEGRO_MAP *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); if (!tileset->bitmap) { fprintf(stderr, "Error: no bitmap available; did you load the image addon?\n"); } // 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 unsigned i, j; 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; if (tileset->bitmap) { 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")); object->bitmap = NULL; 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"); object->gid = (gid ? atoi(gid) : 0); 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(cwd); al_free(cwd); return map; }