static bool Configure(SoundTemplate &self, const tinyxml2::XMLElement *element, unsigned int id) { // get sound length float length; element->QueryFloatAttribute("length", &length); size_t count = xs_CeilToInt(length * AUDIO_FREQUENCY); // reserve space self.Reserve(count); // get expression std::vector<unsigned int> buffer; Expression::Loader<float>::ConfigureRoot(element, buffer, sScalarNames, sScalarDefault); // set up a context EntityContext context(&buffer[0], buffer.size(), 0, id); // for each sample... for (size_t i = 0; i < count; ++i, context.Restart()) { // evaluate the expression context.mParam = float(i) / AUDIO_FREQUENCY; float value = Expression::Evaluate<float>(context); // add a sample self.Append(short(Clamp(xs_RoundToInt(value * SHRT_MAX), SHRT_MIN, SHRT_MAX))); } return true; }
void FMaterial::GetTexCoordInfo(FTexCoordInfo *tci, float x, float y) const { if (x == 1.f) { tci->mRenderWidth = mRenderWidth; tci->mScale.X = tex->Scale.X; tci->mTempScale.X = 1.f; } else { float scale_x = x * tex->Scale.X; tci->mRenderWidth = xs_CeilToInt(mWidth / scale_x); tci->mScale.X = scale_x; tci->mTempScale.X = x; } if (y == 1.f) { tci->mRenderHeight = mRenderHeight; tci->mScale.Y = tex->Scale.Y; tci->mTempScale.Y = 1.f; } else { float scale_y = y * tex->Scale.Y; tci->mRenderHeight = xs_CeilToInt(mHeight / scale_y); tci->mScale.Y = scale_y; tci->mTempScale.Y = y; } if (tex->bHasCanvas) { tci->mScale.Y = -tci->mScale.Y; tci->mRenderHeight = -tci->mRenderHeight; } tci->mWorldPanning = tex->bWorldPanning; tci->mWidth = mWidth; }
// Gunner Constructor Gunner::Gunner(const GunnerTemplate &aTemplate, unsigned int aId) : Updatable(aId) { SetAction(Action(this, &Gunner::Update)); Entity *entity = Database::entity.Get(mId); #ifdef GUNNER_TRACK_DEQUE mTrackPos.push_back(entity->GetPosition()); mTrackPos.push_back(entity->GetPosition()); #else mTrackCount = xs_CeilToInt(aTemplate.mFollowLength/GUNNER_TRACK_GRANULARITY) + 1; mTrackPos = new Vector2[mTrackCount]; mTrackFirst = mTrackLast = 0; mTrackPos[0] = entity->GetPosition(); #endif mTrackLength = 0.0f; }
void BuildPathingGrid(const int aZoneSize, const int aCellSize) { if (grid_handle) { glCallList(grid_handle); return; } // create a new grid handle grid_handle = glGenLists(1); // create a new list glNewList(grid_handle, GL_COMPILE); // get world boundary const b2AABB &boundary = Collidable::GetBoundary(); // get zone extents int zx0 = xs_FloorToInt(boundary.lowerBound.x / aZoneSize); int zx1 = xs_CeilToInt(boundary.upperBound.x / aZoneSize); int zy0 = xs_FloorToInt(boundary.lowerBound.y / aZoneSize); int zy1 = xs_CeilToInt(boundary.upperBound.y / aZoneSize); // cells per zone int cell_side = aZoneSize / aCellSize; int cell_count = cell_side * cell_side; // get the collision world b2World *world = Collidable::GetWorld(); // create a probe fixture b2PolygonShape probeshape; probeshape.SetAsBox(aCellSize * 0.5f, aCellSize * 0.5f, b2Vec2(aCellSize * 0.5f, aCellSize * 0.5f), 0.0f); b2BodyDef probebodydef; b2Body *probebody = world->CreateBody(&probebodydef); b2FixtureDef probefixturedef; probefixturedef.shape = &probeshape; probefixturedef.isSensor = true; probebody->CreateFixture(&probefixturedef); // for each zone row... for (int zy = zy0; zy < zy1; ++zy) { // for each zone column... for (int zx = zx0; zx < zx1; ++zx) { // get zone boundary b2AABB zone_aabb; zone_aabb.lowerBound.x = float((zx) * aZoneSize); zone_aabb.lowerBound.y = float((zy) * aZoneSize); zone_aabb.upperBound.x = float((zx + 1) * aZoneSize); zone_aabb.upperBound.y = float((zy + 1) * aZoneSize); // draw zone boundary glBegin(GL_LINE_LOOP); glColor4f(1, 1, 1, 1); glVertex2f(zone_aabb.lowerBound.x, zone_aabb.lowerBound.y); glVertex2f(zone_aabb.upperBound.x, zone_aabb.lowerBound.y); glVertex2f(zone_aabb.upperBound.x, zone_aabb.upperBound.y); glVertex2f(zone_aabb.lowerBound.x, zone_aabb.upperBound.y); glEnd(); // initialize cell map unsigned char *cell_map = static_cast<unsigned char *>(_alloca(cell_count)); memset(cell_map, 0xFF, cell_count); // for each cell row for (int row = 0; row < cell_side; ++row) { // for each cell column... for (int col = 0; col < cell_side; ++col) { // probe filter static const b2Filter aFilter; // get fixtures intersecting the cell b2AABB cell_aabb; cell_aabb.lowerBound.x = zone_aabb.lowerBound.x + (col) * aCellSize; cell_aabb.lowerBound.y = zone_aabb.lowerBound.y + (row) * aCellSize; cell_aabb.upperBound.x = zone_aabb.lowerBound.x + (col + 1) * aCellSize; cell_aabb.upperBound.y = zone_aabb.lowerBound.y + (row + 1) * aCellSize; GridQueryCallback callback(aFilter); world->QueryAABB(&callback, cell_aabb); // cell type (0=empty, 1=blocked) cell_map[row * cell_side + col] = callback.mBlocker != NULL; } } // initialize slab map unsigned char *slab_map = static_cast<unsigned char *>(_alloca(cell_count)); memset(slab_map, 0xFF, cell_count); int slab_count = 0; // for each cell row for (int row = 0; row < cell_side; ++row) { // for each cell column... for (int col = 0; col < cell_side; ++col) { // skip assigned spaces if (slab_map[row * cell_side + col] != 0xFF) continue; // cell type unsigned char cell = cell_map[row * cell_side + col]; // allocate a new index unsigned char index = unsigned char(slab_count++); assert(index < 0xFF); // find horizontal extent int c0 = col; int c1 = cell_side; for (int c = c0; c < c1; ++c) { if ((cell_map[row * cell_side + c] != cell) || ((slab_map[row * cell_side + c] != 0xFF) && (slab_map[row * cell_side + c] != index))) { c1 = c; break; } } // find vertical extent int r0 = row; int r1 = cell_side; for (int r = r0; r < r1; ++r) { for (int c = c0; c < c1; ++c) { if ((cell_map[r * cell_side + c] != cell) || ((slab_map[r * cell_side + c] != 0xFF) && (slab_map[r * cell_side + c] != index))) { r1 = r; break; } } } // fill slab for (int r = r0; r < r1; ++r) { for (int c = c0; c < c1; ++c) { slab_map[r * cell_side + c] = index; } } assert(c0 < c1 && r0 < r1); // set slab extents //titleslab[index][0] = c0; //titleslab[index][1] = c1; //titleslab[index][2] = r0; //titleslab[index][3] = r1; b2AABB slab_aabb; slab_aabb.lowerBound.x = zone_aabb.lowerBound.x + c0 * aCellSize; slab_aabb.lowerBound.y = zone_aabb.lowerBound.y + r0 * aCellSize; slab_aabb.upperBound.x = zone_aabb.lowerBound.x + c1 * aCellSize; slab_aabb.upperBound.y = zone_aabb.lowerBound.y + r1 * aCellSize; AddGridSlab(slab_aabb, cell_color[cell]); // skip visited columns col = c1 - 1; } } DebugPrint("zone %d %d slabs %d\n", zx, zy, slab_count); } } // destroy the probe fixture world->DestroyBody(probebody); glEndList(); }