BBitmap* FallLeaves::_RandomBitmap(int32 size) { BBitmap* bitmap = new BBitmap(BRect(0, 0, size, size), B_RGBA32); // Randomly select one of the leaf images switch (RAND_NUM(1, kNumLeafTypes)) { case 1: BIconUtils::GetVectorIcon(kLeaf1, sizeof(kLeaf1), bitmap); break; case 2: BIconUtils::GetVectorIcon(kLeaf2, sizeof(kLeaf2), bitmap); break; case 3: BIconUtils::GetVectorIcon(kLeaf3, sizeof(kLeaf3), bitmap); break; case 4: BIconUtils::GetVectorIcon(kLeaf4, sizeof(kLeaf4), bitmap); break; case 5: BIconUtils::GetVectorIcon(kLeaf5, sizeof(kLeaf5), bitmap); break; case 6: BIconUtils::GetVectorIcon(kLeaf6, sizeof(kLeaf6), bitmap); break; } return bitmap; }
/* Create a leaf. If the "above" parameter is true, it will create the leaf in a random location above the screen. If it's false, the leaf will be created just above the screen, ready to come it. */ Leaf* FallLeaves::_CreateLeaf(BView* view, bool above) { // The Z axis (how far away the leaf is) // determines the size and speed int32 z = RAND_NUM(40, 100); // Use this array to ensure unique Z values. while (fZUsed[z]) { z++; if (z > 100) z = 40; } fZUsed[z] = true; // The lower the Z axis number, the smaller the leaf int32 size = (fSize * z) / 100; // Create the leaf Leaf* leaf = new Leaf(_RandomBitmap(size)); leaf->SetZ(z); // The lower the Z axis number, the slower the leaf int32 maxSpeedFromScreenSize = view->Bounds().IntegerHeight(); int32 maxSpeed = (maxSpeedFromScreenSize * fSpeed) / kMaxSpeed; int32 speed = (maxSpeed * z) / 100; leaf->SetSpeed(speed); BRect boundary(-(size / 2), -view->Bounds().Height(), view->Bounds().Width() - (size / 2), view->Bounds().Height()); leaf->SetBoundary(boundary); // Set it to a random position BPoint pos; pos.x = RAND_NUM((int32)boundary.left, boundary.IntegerWidth()); pos.y = -size; if (above) pos.y = -(RAND_NUM(size, boundary.IntegerHeight())); leaf->SetPos(pos); return leaf; }
void Terrain::generateHeightMap(){ int i,j; int stride = (this->side_size-1) / 2; int high_index_i, high_index_j, midpoint_index_i, midpoint_index_j, odd_line; float new_midpoint; float ratio = powf(2.0,-this->roughness_constant); float cur_range = this->range; float rand_f, new_height; while (stride > 0){ // Perform the diamond step for (i=0; i < this->side_size-1; i+=stride*2){ for (j=0; j < this->side_size-1; j+=stride*2){ rand_f = RAND_NUM(cur_range); terrain[i+stride][j+stride] = avgSquareHeight(i,j,stride*2) + rand_f; } } // Perform the square step odd_line = 0; for (i=0; i < this->side_size-1; i+=stride){ odd_line = (odd_line == 0); for (j=0; j < this->side_size-1; j+=stride){ if ((odd_line) && !j){ j += stride; } rand_f = RAND_NUM(cur_range); terrain[i][j] = avgDiamondHeight(i,j,stride) + rand_f; // Enable wrapping by copying over edges if (i == 0){ terrain[this->side_size-1][j] = terrain[i][j]; } if (j == 0){ terrain[i][this->side_size-1] = terrain[i][j]; } j += stride; } } cur_range *= ratio; stride /= 2; } }