PixelBuf SteepnessMap::GetRegion( const MapPixelCoordInt &pos, const MapPixelDeltaInt &size) const { auto fixed_bounds_pb = GetRegion_BoundsHelper(*this, pos, size); if (fixed_bounds_pb.GetData()) return fixed_bounds_pb; double mpp; if (!MetersPerPixel(m_orig_map, pos + size/2, &mpp)) { // Return zero-initialized memory block. return PixelBuf(size.x, size.y); } unsigned int bezier_pixels = Bezier::N_POINTS - 1; double inv_bezier_meters = 1 / (bezier_pixels * mpp); MapPixelCoordInt req_pos = pos - MapPixelDeltaInt(1, 1); MapPixelDeltaInt req_size = size + MapPixelDeltaInt(2, 2); auto orig_data = m_orig_map->GetRegion(req_pos, req_size); PixelBuf result(size.x, size.y); unsigned int *src = orig_data.GetRawData(); unsigned int *dest = result.GetRawData(); for (int x=0; x < size.x; x++) { for (int y=0; y < size.y; y++) { MapPixelCoordInt pos(x+1, y+1); MapBezierGradient grad = Fast3x3CenterGradient(src, pos, req_size); grad *= inv_bezier_meters; double grad_steepness = atan(grad.Abs()); int color_index = static_cast<int>(grad_steepness / (M_PI / 2) * 18); DEST(x, y) = steepness_colors[color_index]; } } return result; }
PixelBuf GradientMap::GetRegion( const MapPixelCoordInt &pos, const MapPixelDeltaInt &size) const { auto fixed_bounds_pb = GetRegion_BoundsHelper(*this, pos, size); if (fixed_bounds_pb.GetData()) return fixed_bounds_pb; //time_counter_loaddisc.Start(); MapPixelCoordInt req_pos = pos - MapPixelDeltaInt(1, 1); MapPixelDeltaInt req_size = size + MapPixelDeltaInt(2, 2); auto orig_data = m_orig_map->GetRegion(req_pos, req_size); //auto orig_data = LoadBufferFromBMP(L"example.bmp"); PixelBuf result(size.x, size.y); unsigned int *src = orig_data.GetRawData(); unsigned int *dest = result.GetRawData(); //time_counter_loaddisc.Stop(); //time_counter.Start(); for (int x=0; x < size.x; x++) { for (int y=0; y < size.y; y++) { unsigned int elevation = SRC(x+1, y+1); MapPixelCoordInt pos(x+1, y+1); MapBezierGradient grad = Fast3x3CenterGradient(src, pos, req_size); DEST(x, y) = HSV_to_RGB( 255*240/360 - elevation*255/4000, 255, ValueBetween(0, static_cast<int>(128+1.25*(grad.x-grad.y)), 255)); } } //time_counter.Stop(); return result; }
static CARD32* BitmapScanline_Inverted( CARD32 *src, CARD32 *dest, int count, int skipleft ) { while(count >= 4) { DEST(0) = ~SOURCE(0); DEST(1) = ~SOURCE(1); DEST(2) = ~SOURCE(2); DEST(3) = ~SOURCE(3); count -= 4; src += 4; #ifndef FIXEDBASE dest += 4; #endif } if(!count) return dest; DEST(0) = ~SOURCE(0); if(count == 1) RETURN(1); DEST(1) = ~SOURCE(1); if(count == 2) RETURN(2); DEST(2) = ~SOURCE(2); RETURN(3); }
static edge_ref connect(edge_ref a, edge_ref b) { edge_ref e = make_edge(); ODATA(e) = DEST(a); DDATA(e) = ORG(b); splice(e, LNEXT(a)); splice(SYM(e), b); return e; }
void _copyRowAndScale(const SRC* src, DEST* dest, int num, float scale) { if (scale==1.0) { for(int i=0; i<num; ++i) { *dest = DEST(*src); ++dest; ++src; } } else { for(int i=0; i<num; ++i) { *dest = DEST(float(*src)*scale); ++dest; ++src; } } }
PUBLIC err_t init_some_superblock(MemDev *mdev, SomeSB *sb) { err_t err; err = mdev->read(0, sizeof(*sb), DEST(sb)); if (err) return E_INTERN | err; return E_GOOD; }
PUBLIC err_t some_df_read(MemDev *mdev, laddr_t addr, SomeDF *df) { err_t err; some_df_clean(df); err = mdev->read(addr, sizeof(*df), DEST(df)); if (err) return E_HWR; return E_GOOD; }
/** * Identify dirname and basename of a path. * * @param[in] path, the path to be split up * @param[out] dir, the directory part of path * @param[out] base, the basename of path * * @return the length of input path * * NOTE! we force dir to be preallocated at minimum to length of path!!! */ PUBLIC u8 path_split (const path_t path, path_t dir, fid_t *base) { u8 l = path_length(path); if (!l) return 0; memcpy(DEST(dir), SRC(path), l * sizeof(*path)); dir[l-1] = EOP; *base = path[l-1]; return l; }
PUBLIC err_t some_fh_read(MemDev *mdev, laddr_t addr, SomeFH *fh) { err_t err; err = mdev->read(addr, sizeof(*fh), DEST(fh)); if (err) return E_HWR; if (fh_is_invalid(fh)) { some_fh_clean(fh); return E_FS; } return E_GOOD; }
/** * Consume a quoted string. The source string will be modified into the * destination string as follows: * * source: " b" * dest: b * * This is safe to use when the destination and source buffer are the same; * it will only ever contract the data, not expand it. */ static void consume_quoted(esh_t * esh, size_t *src_i, size_t *dest_i) { (void) esh; char quote = ESH_INSTANCE->buffer[*src_i]; for (++*src_i; *src_i < ESH_INSTANCE->cnt; ++*src_i) { char c = ESH_INSTANCE->buffer[*src_i]; if (c == quote) { // End of quoted string break; } else { DEST(ESH_INSTANCE)[*dest_i] = c; ++*dest_i; } } }
PUBLIC err_t some_sb_read(MemDev *mdev, SomeSB *sb) { err_t err; CHECK_PARAM__NOT_NULL(mdev); CHECK_PARAM__NOT_NULL(sb); some_sb_clean(sb); err = mdev->read(0, sizeof(*sb), DEST(sb)); if (err) return E_INTERN | err; if (sb_is_invalid(sb, mdev)) { some_sb_clean(sb); return E_FS; } return E_GOOD; }
/* ==================================================================== Load terrain types, weather information and hex tile icons. ==================================================================== */ int terrain_load( char *fname ) { int i, j, k; PData *pd, *sub, *subsub, *subsubsub; List *entries, *flags; char path[512], transitionPath[512]; char *flag, *str; char *domain = 0; int count; /* log info */ int log_dot_limit = 40; /* maximum of dots */ char log_str[128]; sprintf( transitionPath, "Scenario/%s", fname ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( pd = parser_read_file( fname, path ) ) == 0 ) goto parser_failure; // domain = determine_domain(pd, fname); // locale_load_domain(domain, 0/*FIXME*/); /* get weather */ if ( !parser_get_entries( pd, "weather", &entries ) ) goto parser_failure; weather_type_count = entries->count; weather_types = calloc( weather_type_count, sizeof( Weather_Type ) ); list_reset( entries ); i = 0; while ( ( sub = list_next( entries ) ) ) { weather_types[i].id = strdup( sub->name ); // if ( !parser_get_localized_string( sub, "name", domain, &weather_types[i].name ) ) goto parser_failure; if ( !parser_get_value( sub, "name", &str, 0 ) ) goto parser_failure; weather_types[i].name = strdup( str ); if ( !parser_get_value( sub, "ground_cond", &str, 0 ) ) goto parser_failure; weather_types[i].ground_conditions = strdup( str ); if ( !parser_get_values( sub, "flags", &flags ) ) goto parser_failure; list_reset( flags ); while ( ( flag = list_next( flags ) ) ) weather_types[i].flags |= check_flag( flag, fct_terrain ); i++; } /* hex tile geometry */ if ( !parser_get_int( pd, "hex_width", &hex_w ) ) goto parser_failure; if ( !parser_get_int( pd, "hex_height", &hex_h ) ) goto parser_failure; if ( !parser_get_int( pd, "hex_x_offset", &hex_x_offset ) ) goto parser_failure; if ( !parser_get_int( pd, "hex_y_offset", &hex_y_offset ) ) goto parser_failure; /* terrain icons */ terrain_icons = calloc( 1, sizeof( Terrain_Icons ) ); if ( !parser_get_value( pd, "fog", &str, 0 ) ) goto parser_failure; sprintf( transitionPath, "Graphics/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( terrain_icons->fog = load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ) ) == 0 ) goto failure; if ( !parser_get_value( pd, "danger", &str, 0 ) ) goto parser_failure; sprintf( transitionPath, "Graphics/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( terrain_icons->danger = load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ) ) == 0 ) goto failure; if ( !parser_get_value( pd, "grid", &str, 0 ) ) goto parser_failure; sprintf( transitionPath, "Graphics/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( terrain_icons->grid = load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ) ) == 0 ) goto failure; if ( !parser_get_value( pd, "frame", &str, 0 ) ) goto parser_failure; sprintf( transitionPath, "Graphics/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( terrain_icons->select = load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ) ) == 0 ) goto failure; if ( !parser_get_value( pd, "crosshair", &str, 0 ) ) goto parser_failure; sprintf( transitionPath, "Graphics/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( terrain_icons->cross = anim_create( load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ), 1000/config.anim_speed, hex_w, hex_h, sdl.screen, 0, 0 ) ) == 0 ) goto failure; anim_hide( terrain_icons->cross, 1 ); if ( !parser_get_value( pd, "explosion", &str, 0 ) ) goto parser_failure; sprintf( transitionPath, "Graphics/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( terrain_icons->expl1 = anim_create( load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ), 50/config.anim_speed, hex_w, hex_h, sdl.screen, 0, 0 ) ) == 0 ) goto failure; anim_hide( terrain_icons->expl1, 1 ); if ( ( terrain_icons->expl2 = anim_create( load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ), 50/config.anim_speed, hex_w, hex_h, sdl.screen, 0, 0 ) ) == 0 ) goto failure; anim_hide( terrain_icons->expl2, 1 ); /* terrain sounds */ #ifdef WITH_SOUND if ( parser_get_value( pd, "explosion_sound", &str, 0 ) ) { snprintf( transitionPath, 512, "Sound/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); terrain_icons->wav_expl = wav_load( path, 2 ); } if ( parser_get_value( pd, "select_sound", &str, 0 ) ) { snprintf( transitionPath, 512, "Sound/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); terrain_icons->wav_select = wav_load( path, 1 ); } #endif /* terrain data image columns */ if ( !parser_get_int( pd, "terrain_columns", &terrain_columns ) ) goto parser_failure; /* terrain data image rows */ if ( !parser_get_int( pd, "terrain_rows", &terrain_rows ) ) goto parser_failure; /* each ground condition type got its own image -- if it's named 'default' we point towards the image of weather_type 0 */ terrain_images = calloc( 1, sizeof( Terrain_Images ) ); terrain_images->images = calloc( weather_type_count / 4, sizeof( SDL_Surface* ) ); for ( j = 0; j < weather_type_count / 4; j++ ) { terrain_images->ground_conditions = strdup( weather_types[4*j].ground_conditions ); sprintf( path, "image/%s", weather_types[4*j].ground_conditions ); if ( !parser_get_value( pd, path, &str, 0 ) ) goto parser_failure; if ( STRCMP( "default", str ) && j > 0 ) { /* just a pointer */ terrain_images->images[j] = terrain_images->images[0]; } else { sprintf( transitionPath, "Graphics/%s", str ); search_file_name_exact( path, transitionPath, config.mod_name ); if ( ( terrain_images->images[j] = load_surf( path, SDL_SWSURFACE, 0, 0, 0, 0 ) ) == 0 ) goto parser_failure; SDL_SetColorKey( terrain_images->images[j], SDL_SRCCOLORKEY, get_pixel( terrain_images->images[j], 0, 0 ) ); } } /* fog image */ terrain_images->images_fogged = calloc( weather_type_count / 4, sizeof( SDL_Surface* ) ); for ( j = 0; j < weather_type_count / 4; j++ ) { if ( terrain_images->images[j] == terrain_images->images[0] && j > 0 ) { /* just a pointer */ terrain_images->images_fogged[j] = terrain_images->images_fogged[0]; } else { terrain_images->images_fogged[j] = create_surf( terrain_images->images[j]->w, terrain_images->images[j]->h, SDL_SWSURFACE ); FULL_DEST( terrain_images->images_fogged[j] ); FULL_SOURCE( terrain_images->images[j] ); blit_surf(); count = terrain_images->images[j]->w / hex_w; for ( i = 0; i < terrain_rows; i++ ) for ( k = 0; k < terrain_columns; k++ ) { DEST( terrain_images->images_fogged[j], k * hex_w, i * hex_h, hex_w, hex_h ); SOURCE( terrain_icons->fog, 0, 0 ); alpha_blit_surf( FOG_ALPHA ); } SDL_SetColorKey( terrain_images->images_fogged[j], SDL_SRCCOLORKEY, get_pixel( terrain_images->images_fogged[j], 0, 0 ) ); } } /* terrain types */ if ( !parser_get_entries( pd, "terrain", &entries ) ) goto parser_failure; terrain_type_count = entries->count; terrain_types = calloc( terrain_type_count, sizeof( Terrain_Type ) ); list_reset( entries ); i = 0; while ( ( sub = list_next( entries ) ) ) { /* id */ terrain_types[i].id = sub->name[0]; /* name */ if ( !parser_get_localized_string( sub, "name", domain, &terrain_types[i].name ) ) goto parser_failure; /* spot cost */ terrain_types[i].spt = calloc( weather_type_count, sizeof( int ) ); if ( !parser_get_pdata( sub, "spot_cost", &subsub ) ) goto parser_failure; for ( j = 0; j < weather_type_count; j++ ) if ( !parser_get_int( subsub, weather_types[j].id, &terrain_types[i].spt[j] ) ) goto parser_failure; /* image */ terrain_types[i].images = terrain_images->images; /* fog image */ terrain_types[i].images_fogged = terrain_images->images_fogged; /* mov cost */ terrain_types[i].mov = calloc( mov_type_count * weather_type_count, sizeof( int ) ); if ( !parser_get_pdata( sub, "move_cost", &subsub ) ) goto parser_failure; for ( k = 0; k < mov_type_count; k++ ) { if ( !parser_get_pdata( subsub, mov_types[k].id, &subsubsub ) ) goto parser_failure; for ( j = 0; j < weather_type_count; j++ ) { if ( !parser_get_value( subsubsub, weather_types[j].id, &str, 0 ) ) goto parser_failure; if ( str[0] == 'X' ) terrain_types[i].mov[j + k * weather_type_count] = 0; /* impassable */ else if ( str[0] == 'A' ) terrain_types[i].mov[j + k * weather_type_count] = -1; /* costs all */ else terrain_types[i].mov[j + k * weather_type_count] = atoi( str ); /* normal cost */ } } /* entrenchment */ if ( !parser_get_int( sub, "min_entr", &terrain_types[i].min_entr ) ) goto parser_failure; if ( !parser_get_int( sub, "max_entr", &terrain_types[i].max_entr ) ) goto parser_failure; /* initiative modification */ if ( !parser_get_int( sub, "max_init", &terrain_types[i].max_ini ) ) goto parser_failure; /* flags */ terrain_types[i].flags = calloc( weather_type_count, sizeof( int ) ); if ( !parser_get_pdata( sub, "flags", &subsub ) ) goto parser_failure; for ( j = 0; j < weather_type_count; j++ ) { if ( !parser_get_values( subsub, weather_types[j].id, &flags ) ) goto parser_failure; list_reset( flags ); while ( ( flag = list_next( flags ) ) ) terrain_types[i].flags[j] |= check_flag( flag, fct_terrain ); } /* next terrain */ i++; /* LOG */ strcpy( log_str, " [ ]" ); for ( k = 0; k < i * log_dot_limit / entries->count; k++ ) log_str[3 + k] = '*'; write_text( log_font, sdl.screen, log_x, log_y, log_str, 255 ); SDL_UpdateRect( sdl.screen, log_font->last_x, log_font->last_y, log_font->last_width, log_font->last_height ); } parser_free( &pd ); /* LOG */ write_line( sdl.screen, log_font, log_str, log_x, &log_y ); refresh_screen( 0, 0, 0, 0 ); free(domain); return 1; parser_failure: fprintf( stderr, "%s\n", parser_get_error() ); failure: terrain_delete(); if ( pd ) parser_free( &pd ); free(domain); printf(tr("If data seems to be missing, please re-run the converter lgc-pg.\n")); return 0; }
static void rec_delaunay(site_struct * sites[], int sl, int sh, edge_ref * le, edge_ref * re) { if (sh == sl + 2) { edge_ref a = make_edge(); ODATA(a) = sites[sl]; DDATA(a) = sites[sl + 1]; *le = a; *re = SYM(a); } else if (sh == sl + 3) { edge_ref a = make_edge(); edge_ref b = make_edge(); double ct = ccw(sites[sl], sites[sl + 1], sites[sl + 2]); splice(SYM(a), b); ODATA(a) = sites[sl]; DDATA(a) = sites[sl + 1]; ODATA(b) = sites[sl + 1]; DDATA(b) = sites[sl + 2]; if (ct == 0.0) { *le = a; *re = SYM(b); } else { edge_ref c = connect(b, a); if (ct > 0.0) { *le = a; *re = SYM(b); } else { *le = SYM(c); *re = c; } } } else { edge_ref ldo, ldi, rdi, rdo; edge_ref basel, lcand, rcand; int sm = (sl + sh) / 2; rec_delaunay(sites, sl, sm, &ldo, &ldi); rec_delaunay(sites, sm, sh, &rdi, &rdo); while (1) { if (leftof(ORG(rdi), ldi)) ldi = LNEXT(ldi); else if (rightof(ORG(ldi), rdi)) rdi = ONEXT(SYM(rdi)); else break; } basel = connect(SYM(rdi), ldi); if (ORG(ldi) == ORG(ldo)) ldo = SYM(basel); if (ORG(rdi) == ORG(rdo)) rdo = basel; while (1) { lcand = ONEXT(SYM(basel)); if (rightof(DEST(lcand), basel)) while (incircle (DEST(basel), ORG(basel), DEST(lcand), DEST(ONEXT(lcand)))) { edge_ref t = ONEXT(lcand); destroy_edge(lcand); lcand = t; } rcand = OPREV(basel); if (rightof(DEST(rcand), basel)) while (incircle (DEST(basel), ORG(basel), DEST(rcand), DEST(OPREV(rcand)))) { edge_ref t = OPREV(rcand); destroy_edge(rcand); rcand = t; } if (!rightof(DEST(lcand), basel) && !rightof(DEST(rcand), basel)) break; if (!rightof(DEST(lcand), basel) || (rightof(DEST(rcand), basel) && incircle(DEST(lcand), ORG(lcand), ORG(rcand), DEST(rcand)))) basel = connect(rcand, SYM(basel)); else basel = connect(SYM(basel), SYM(lcand)); } *le = ldo; *re = rdo; } }
static int leftof(site_struct * s, edge_ref e) { return ccw(s, ORG(e), DEST(e)) > 0.0; }
PUBLIC void some_fh_clean( SomeFH *fh ) { memset( DEST(fh), 0x00, sizeof(*fh) ); }
PUBLIC void some_sb_clean( SomeSB *sb ) { memset(DEST(sb), 0x00, sizeof(*sb)); sb->magic = SOMEFS_MAGIC; }