void read_from(ReadSource in, BitMap& out) { read(in, out.base_addr); read(in, out.row_bytes); read(in, out.bounds); if (out.base_addr != 0) { throw Exception("PixMap::base_addr must be 0"); } }
void Starfield::draw() const { const RgbColor slowColor = GetRGBTranslateColorShade(kStarColor, MEDIUM); const RgbColor mediumColor = GetRGBTranslateColorShade(kStarColor, LIGHT); const RgbColor fastColor = GetRGBTranslateColorShade(kStarColor, LIGHTER); switch (g.ship.get() ? g.ship->presenceState : kNormalPresence) { default: if (!_warp_stars) { Points points; // we're not warping in any way for (const scrollStarType* star : range(_stars, _stars + kScrollStarNum)) { if (star->speed != kNoStar) { const RgbColor* color = &slowColor; if (star->speed == kMediumStarSpeed) { color = &mediumColor; } else if (star->speed == kFastStarSpeed) { color = &fastColor; } points.draw(star->location, *color); } } break; } case kWarpInPresence: case kWarpOutPresence: case kWarpingPresence: { Lines lines; for (const scrollStarType* star : range(_stars, _stars + kScrollStarNum)) { if (star->speed != kNoStar) { const RgbColor* color = &slowColor; if (star->speed == kMediumStarSpeed) { color = &mediumColor; } else if (star->speed == kFastStarSpeed) { color = &fastColor; } if (star->age > 1) { lines.draw(star->location, star->oldLocation, *color); } } } break; } } Points points; for (const scrollStarType* star : range(_stars + kSparkStarOffset, _stars + kAllStarNum)) { if ((star->speed != kNoStar) && (star->age > 0)) { const RgbColor color = GetRGBTranslateColorShade(star->hue, (star->age >> kSparkAgeToShadeShift) + 1); points.draw(star->location, color); } } }
void PixMap::read_direct_image(ReadSource in, unique_ptr<RasterImage>& image) const { if (pixel_type != RGB_DIRECT) { throw Exception("image is not direct"); } if (pack_type != 4) { throw Exception(format("unsupported pack_type {0}", pack_type)); } image.reset(new RasterImage(bounds)); if (row_bytes == 0) { return; } size_t bytes_read = 0; for (int y = 0; y < bounds.height(); ++y) { Bytes bytes; if (row_bytes <= 250) { bytes.resize(read<uint8_t>(in)); bytes_read += 1; } else { bytes.resize(read<uint16_t>(in)); bytes_read += 2; } in.shift(bytes.data(), bytes.size()); bytes_read += bytes.size(); BytesSlice remainder(bytes); Bytes components; while (!remainder.empty()) { uint8_t header = read<uint8_t>(remainder); if (header >= 0x80) { components.push(0x101 - header, read<uint8_t>(remainder)); } else { size_t size = header + 1; components.push(size, '\0'); uint8_t* data = components.data() + components.size() - size; remainder.shift(data, size); } } const int16_t w = bounds.width(); const BytesSlice red = components.slice((cmp_count - 3) * w, w); const BytesSlice green = components.slice((cmp_count - 2) * w, w); const BytesSlice blue = components.slice((cmp_count - 1) * w); for (int x = 0; x < w; ++x) { image->set(x, y, AlphaColor(red.at(x), green.at(x), blue.at(x))); } } if ((bytes_read % 2) != 0) { in.shift(1); } }
void BitsSlice::shift(uint8_t* data, size_t size) { if (size == 0) { return; } else if (size + _bit_index > 8) { throw Exception(format("unhandled case ({0} + {1})", size, _bit_index)); } uint8_t byte = _bytes.at(0); *data = bits(byte, _bit_index, _bit_index + size); _bit_index += size; if (_bit_index == 8) { _bytes.shift(1); _bit_index = 0; } }
TEST_F(SerializeTest, NonEmptyArrayTest) { vector<Json> a; a.push_back(Json::number(1.0)); a.push_back(Json::number(2.0)); a.push_back(Json::number(3.0)); EXPECT_THAT(Json::array(a), SerializesTo(format("[{0},{1},{2}]", 1.0, 2.0, 3.0))); }
static void alter_condition_true_yet(Handle<Action> action) { const auto alter = action->argument.alterConditionTrueYet; int32_t begin = alter.first; int32_t end = begin + std::max(0, alter.count_minus_1) + 1; for (auto l: range(begin, end)) { g.level->condition(l)->set_true_yet(alter.true_yet); } }
static void alter_hidden(Handle<Action> action) { const auto alter = action->argument.alterHidden; int32_t begin = alter.first; int32_t end = begin + std::max(0, alter.count_minus_1) + 1; for (auto i: range(begin, end)) { UnhideInitialObject(i); } }
void ArrayPixMap::resize(Size new_size) { using sfz::swap; using std::min; ArrayPixMap new_pix_map(new_size.width, new_size.height); Size min_size(min(size().width, new_size.width), min(size().height, new_size.height)); Rect transfer = min_size.as_rect(); new_pix_map.view(transfer).copy(view(transfer)); _size = new_size; swap(_bytes, new_pix_map._bytes); }
virtual void visit_object(const StringMap<Json>& value) const { if (_state == NEW) { _state = UNLOCKED_CHAPTERS; if (value.find("unlocked-levels") != value.end()) { value.find("unlocked-levels")->second.accept(*this); } _state = NEW; return; } throw Exception("invalid ledger content"); }
ScenarioList::ScenarioList() { linked_ptr<Entry> factory_scenario(new Entry); factory_scenario->identifier.assign("com.biggerplanet.ares"); factory_scenario->title.assign("Ares"); factory_scenario->download_url.assign("http://www.arescentral.com"); factory_scenario->author.assign("Bigger Planet"); factory_scenario->author_url.assign("http://www.biggerplanet.com"); u32_to_version(0x01010100, factory_scenario->version); _scenarios.push_back(factory_scenario); ScopedGlob g; const String home(utf8::decode(getenv("HOME"))); const StringSlice scenarios("Library/Application Support/Antares/Scenarios"); const StringSlice info("scenario-info/128.nlAG"); String str(format("{0}/{1}/*/{2}", home, scenarios, info)); CString c_str(str); glob(c_str.data(), 0, NULL, &g.data); size_t prefix_len = home.size() + scenarios.size() + 2; size_t suffix_len = info.size() + 1; for (int i = 0; i < g.data.gl_matchc; ++i) { const String path(utf8::decode(g.data.gl_pathv[i])); StringSlice identifier = path.slice(prefix_len, path.size() - prefix_len - suffix_len); if (identifier == factory_scenario->identifier) { continue; } MappedFile file(path); BytesSlice data(file.data()); scenarioInfoType info; read(data, info); linked_ptr<Entry> entry(new Entry); entry->identifier.assign(identifier); entry->title.assign(info.titleString); entry->download_url.assign(info.downloadURLString); entry->author.assign(info.authorNameString); entry->author_url.assign(info.authorURLString); u32_to_version(info.version, entry->version); _scenarios.push_back(entry); } }
TEST_F(SerializeTest, NonEmptyObjectTest) { StringMap<Json> o; o.insert(make_pair("one", Json::number(1.0))); o.insert(make_pair("two", Json::number(2.0))); o.insert(make_pair("three", Json::number(3.0))); EXPECT_THAT(Json::object(o), SerializesTo(format( "{{" "\"one\":{0}," "\"three\":{1}," "\"two\":{2}" "}}", 1.0, 3.0, 2.0))); }
void PixMap::read_packed_image( sfz::ReadSource in, const ColorTable& clut, unique_ptr<RasterImage>& image) const { if (pixel_type != INDEXED) { throw Exception("image is not indexed"); } image.reset(new RasterImage(bounds)); if (row_bytes == 0) { return; } size_t bytes_read = 0; for (int y = 0; y < bounds.height(); ++y) { Bytes bytes; if (row_bytes <= 250) { bytes.resize(read<uint8_t>(in)); bytes_read += 1; } else { bytes.resize(read<uint16_t>(in)); bytes_read += 2; } in.shift(bytes.data(), bytes.size()); bytes_read += bytes.size(); int32_t x = 0; BytesSlice remainder = bytes; while (!remainder.empty()) { uint8_t header = read<uint8_t>(remainder); if (header >= 0x80) { uint8_t value = read<uint8_t>(remainder); uint8_t size = 0x101 - header; for (int j = 0; j < size; ++j) { if (x < bounds.width()) { image->set(x + bounds.left, y + bounds.top, lookup(clut, value)); } ++x; } } else { uint8_t size = header + 1; for (int j = 0; j < size; ++j) { if (x < bounds.width()) { uint8_t value = read<uint8_t>(remainder); image->set(x + bounds.left, y + bounds.top, lookup(clut, value)); } ++x; } } } } if ((bytes_read % 2 == 1)) { in.shift(1); } }
// TODO(sfiera): give CString a move constructor so we don't need to define this. AntaresScenarioListEntry(AntaresScenarioListEntry&& other): identifier(String(utf8::decode(other.identifier.data()))), title(String(utf8::decode(other.title.data()))), download_url(String(utf8::decode(other.download_url.data()))), author(String(utf8::decode(other.author.data()))), author_url(String(utf8::decode(other.author_url.data()))), version_string(other.version_string), version(String(utf8::decode(other.version.data()))) { }
void Starfield::show() { if (g.ship.get() && g.ship->active && (g.ship->presenceState != kWarpInPresence) && (g.ship->presenceState != kWarpOutPresence) && (g.ship->presenceState != kWarpingPresence)) { if (_warp_stars) { // we were warping but now are not; erase warped stars _warp_stars = false; for (scrollStarType* star : range(_stars, _stars + kScrollStarNum)) { if (star->speed != kNoStar) { if (star->age < 2) { ++star->age; } } } } } else { // we're warping now _warp_stars = true; for (scrollStarType* star : range(_stars, _stars + kScrollStarNum)) { if (star->speed != kNoStar) { if (star->age < 2) { ++star->age; } } } } for (scrollStarType* star : range(_stars + kScrollStarNum, _stars + kAllStarNum)) { if (star->speed != kNoStar) { if (star->age <= 0) { star->speed = kNoStar; } } } _last_clip_bottom = viewport().bottom; }
void read_from(ReadSource in, AddressedPixMap& out) { read(in, out.base_addr); PixMap& parent = out; read(in, parent); }
void read_from(ReadSource in, PixMap& out) { read(in, out.row_bytes); out.row_bytes &= 0x3fff; read(in, out.bounds); read(in, out.pm_version); read(in, out.pack_type); read(in, out.pack_size); read(in, out.h_res); read(in, out.v_res); read(in, out.pixel_type); read(in, out.pixel_size); read(in, out.cmp_count); read(in, out.cmp_size); read(in, out.plane_bytes); read(in, out.pm_table); read(in, out.pm_reserved); if (out.plane_bytes != 0) { throw Exception("PixMap::plane_bytes must be 0"); } if (out.pm_reserved != 0) { throw Exception("PixMap::pm_reserved must be 0"); } switch (out.pixel_type) { case INDEXED: { switch (out.pixel_size) { case 1: case 2: case 4: case 8: break; default: throw Exception(format("indexed pixels may not have size {0}", out.pixel_size)); } if ((out.pack_type != 0) || (out.pack_size != 0)) { throw Exception("indexed pixels may not be packed"); } if ((out.cmp_count != 1) || (out.cmp_size != out.pixel_size)) { throw Exception("indexed pixels must have one component"); } break; } case RGB_DIRECT: { switch (out.pixel_size) { case 16: { throw Exception(format("unsupported pixel_size {0}", out.pixel_size)); break; } case 32: { if (out.cmp_size != 8) { throw Exception("32-bit direct pixels must have cmp_size 8"); } break; } default: { throw Exception(format("direct pixels may not have size {0}", out.pixel_size)); } } if ((out.cmp_count != 3) && (out.cmp_count != 4)) { throw Exception("direct pixels must have three or four components"); } break; } default: { throw Exception(format("illegal PixMap pixel_type {0}", out.pixel_type)); } } }
void Starfield::move(ticks by_units) { if (!g.ship.get() || !g.ship->active) { return; } const Rect viewport = antares::viewport(); const Rect play_screen = antares::play_screen(); const fixedPointType slowVelocity = { scale_by(g.ship->velocity.h * kSlowStarFraction * by_units.count(), gAbsoluteScale), scale_by(g.ship->velocity.v * kSlowStarFraction * by_units.count(), gAbsoluteScale), }; const fixedPointType mediumVelocity = { scale_by(g.ship->velocity.h * kMediumStarFraction * by_units.count(), gAbsoluteScale), scale_by(g.ship->velocity.v * kMediumStarFraction * by_units.count(), gAbsoluteScale), }; const fixedPointType fastVelocity = { scale_by(g.ship->velocity.h * kFastStarFraction * by_units.count(), gAbsoluteScale), scale_by(g.ship->velocity.v * kFastStarFraction * by_units.count(), gAbsoluteScale), }; for (scrollStarType* star : range(_stars, _stars + kScrollStarNum)) { const fixedPointType* velocity; switch (star->speed) { case kSlowStarSpeed: velocity = &slowVelocity; break; case kMediumStarSpeed: velocity = &mediumVelocity; break; case kFastStarSpeed: velocity = &fastVelocity; break; default: case kNoStar: continue; } star->motionFraction.h += velocity->h; star->motionFraction.v += velocity->v; int32_t h; if (star->motionFraction.h >= Fixed::zero()) { h = more_evil_fixed_to_long(star->motionFraction.h + Fixed::from_float(0.5)); } else { h = more_evil_fixed_to_long(star->motionFraction.h - Fixed::from_float(0.5)) + 1; } star->location.h += h; star->motionFraction.h -= Fixed::from_long(h); int32_t v; if (star->motionFraction.v >= Fixed::zero()) { v = more_evil_fixed_to_long(star->motionFraction.v + Fixed::from_float(0.5)); } else { v = more_evil_fixed_to_long(star->motionFraction.v - Fixed::from_float(0.5)) + 1; } star->location.v += v; star->motionFraction.v -= Fixed::from_long(v); if ((star->location.h < viewport.left) && (star->oldLocation.h < viewport.left)) { star->location.h += play_screen.width() - 1; star->location.v = Randomize(play_screen.height()) + viewport.top; star->motionFraction.h = star->motionFraction.v = Fixed::zero(); star->speed = RandomStarSpeed(); star->age = 0; } else if ( (star->location.h >= viewport.right) && (star->oldLocation.h >= viewport.right)) { star->location.h -= play_screen.width(); star->location.v = Randomize(play_screen.height()) + viewport.top; star->motionFraction.h = star->motionFraction.v = Fixed::zero(); star->speed = RandomStarSpeed(); star->age = 0; } else if ((star->location.v < viewport.top) && (star->oldLocation.v < viewport.top)) { star->location.h = Randomize(play_screen.width()) + viewport.left; star->location.v += play_screen.height() - 1; star->motionFraction.h = star->motionFraction.v = Fixed::zero(); star->speed = RandomStarSpeed(); star->age = 0; } else if ( (star->location.v >= play_screen.bottom) && (star->oldLocation.v >= play_screen.bottom)) { star->location.h = Randomize(play_screen.width()) + viewport.left; star->location.v -= play_screen.height(); star->motionFraction.h = star->motionFraction.v = Fixed::zero(); star->speed = RandomStarSpeed(); star->age = 0; } if (_warp_stars && (star->age == 0)) { switch (star->speed) { case kSlowStarSpeed: velocity = &slowVelocity; break; case kMediumStarSpeed: velocity = &mediumVelocity; break; case kFastStarSpeed: velocity = &fastVelocity; break; case kNoStar: throw std::runtime_error("invalid value of star->speed."); } star->location.h -= mFixedToLong(velocity->h); star->location.v -= mFixedToLong(velocity->v); } } for (scrollStarType* star : range(_stars + kSparkStarOffset, _stars + kAllStarNum)) { if (star->speed == kNoStar) { continue; } star->age -= star->speed * by_units.count(); star->motionFraction.h += star->velocity.h * by_units.count() + slowVelocity.h; star->motionFraction.v += star->velocity.v * by_units.count() + slowVelocity.v; int32_t h; if (star->motionFraction.h >= Fixed::zero()) { h = more_evil_fixed_to_long(star->motionFraction.h + Fixed::from_float(0.5)); } else { h = more_evil_fixed_to_long(star->motionFraction.h - Fixed::from_float(0.5)) + 1; } star->location.h += h; star->motionFraction.h -= Fixed::from_long(h); int32_t v; if (star->motionFraction.v >= Fixed::zero()) { v = more_evil_fixed_to_long(star->motionFraction.v + Fixed::from_float(0.5)); } else { v = more_evil_fixed_to_long(star->motionFraction.v - Fixed::from_float(0.5)) + 1; } star->location.v += v; star->motionFraction.v -= Fixed::from_long(v); } }
void read_from(ReadSource in, Rect& out) { read(in, out.top); read(in, out.left); read(in, out.bottom); read(in, out.right); }
void ArrayPixMap::swap(ArrayPixMap& other) { using sfz::swap; using std::swap; swap(_size, other._size); swap(_bytes, other._bytes); }
void read_from(ReadSource in, fixed32_t& out) { read(in, out.int_value); }
void Starfield::prepare_to_move() { for (scrollStarType* star : range(_stars, _stars + kAllStarNum)) { star->oldLocation = star->location; } }