void fillWorld(World *world) { int cx, cy, cz, bx, by, bz; for (cx = 0; cx < world->size; cx++) { for (cy = 0; cy < world->size; cy++) { for (cz = 0; cz < world->size; cz++) { for (bx = 0; bx < CHUNK_SIZE; bx++) { for (by = 0; by < CHUNK_SIZE; by++) { for (bz = 0; bz < CHUNK_SIZE; bz++) { int r = (!bx|(bx==CHUNK_SIZE-1))&(!bz|(bz==CHUNK_SIZE-1)); int m = (bx + by + bz) % 2 ? -1 : (-1 << 6); if (r) { m = 0; r = 255; } if (by == 0 && cy == 0) { setBlock(getChunk(world, cx, cy, cz), bx, by, bz, (Block){1, {{255 & (r|m), 255 & m, 255 & m, 255}}}); } // } else if (((bx == 0 && cx == 0) || (bz == 0 && cz == 0)) && by == 1 && cy == 0) { // setBlock(getChunk(world, cx, cy, cz), bx, by, bz, // (Block){1, {{255 & m, 255 & m, 255 & m, 255}}}); // } } } } renderChunk(getChunk(world, cx, cy, cz)); } } } }
char readChunk(Chunk *chunk, FILE *in) { RLE_BlockData buf; BlockData data; int count = 0, size, i = 0; Block *block; while (count < CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) { size = fread(&buf, sizeof(buf), 1, in); if (!size) { // file too short fprintf(stderr, "Error reading world file"); if (feof(in)) fputs("; file too short", stderr); else if (ferror(in)) fputs("; an error occured", stderr); fputs("\n", stderr); return 0; } // printf("read %d blocks\n", buf.count); count += buf.count; data = buf.data; while (buf.count > 0 && i < CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) { block = &chunk->blocks_lin[i]; if (data.logic) { block->logic = calloc(1, sizeof(Logic)); block->logic->type = data.logic_type; block->logic->roll = data.logic_roll; block->logic->pitch = data.logic_pitch; block->logic->yaw = data.logic_yaw; updateLogicModel(block); } if (data.data) { block->data = createModel(); block->data->chunk = createChunk(0, 0, 0); block->active = 1; if (!readChunk(block->data->chunk, in)) return 0; renderModel(block->data); } block->color = data.color; if (data.color.all) { block->active = 1; } i++; buf.count--; } } renderChunk(chunk); return 1; }
void TopdownTileRenderer::renderTile(const TilePos& tile_pos, RGBAImage& tile) { int texture_size = images->getTextureSize(); tile.setSize(getTileSize(), getTileSize()); for (int x = 0; x < tile_width; x++) { for (int z = 0; z < tile_width; z++) { mc::ChunkPos chunkpos(tile_pos.getX() * tile_width + x, tile_pos.getY() * tile_width + z); current_chunk = world->getChunk(chunkpos); if (current_chunk != nullptr) renderChunk(*current_chunk, tile, texture_size*16*x, texture_size*16*z); } } }
void setBlock(Chunk *chunk, int x, int y, int z, Block block) { Block *current = getBlock(chunk, x, y, z); if (current->logic) freeLogic(current->logic); block.nb_pos_x = current->nb_pos_x; block.nb_neg_x = current->nb_neg_x; block.nb_pos_y = current->nb_pos_y; block.nb_neg_y = current->nb_neg_y; block.nb_pos_z = current->nb_pos_z; block.nb_neg_z = current->nb_neg_z; *getBlock(chunk, x, y, z) = block; renderChunk(chunk); }
std::vector<int> render(size_t jump) { if (chunks.empty()) return {}; std::cerr << __FUNCTION__ << std::flush; std::vector<int> output; output.resize(chunks[0]().size() + jump * (chunks.size() - 1)); size_t currentSpot = 0; for (size_t i = 0; i < chunks.size(); ++i) { loadingPrint(i, chunks.size()); renderChunk(output, chunks[i], jump, i); } std::cerr << "done (" << output.size() << " samples)" << std::endl; return output; }
std::vector<int> render(size_t jump) { if (chunks.empty()) return {}; std::cerr << __FUNCTION__ << std::flush; std::vector<int> output; output.resize(windowSize + jump * (chunks.size() - 1)); size_t currentSpot = 0; for (size_t i = 0; i < chunks.size(); ++i) { loadingPrint(i, chunks.size()); Chunk chunk; chunk().resize(windowSize, Complex(0, 0)); chunk()[chunks[i].first].real(chunks[i].second); // std::cerr << chunks[i].first << " " << chunks[i].second << std::endl; renderChunk(output, chunk, jump, i); } std::cerr << "done (" << output.size() << " samples)" << std::endl; return output; }
void copyChunk(Chunk *dest, Chunk *src) { Block *srcBlock, *destBlock; for (int x = 0; x < CHUNK_SIZE; x++) { for (int y = 0; y < CHUNK_SIZE; y++) { for (int z = 0; z < CHUNK_SIZE; z++) { srcBlock = getBlock(src, x, y, z); destBlock = getBlock(dest, x, y, z); destBlock->active = srcBlock->active; destBlock->color = srcBlock->color; if (destBlock->logic) { free(destBlock->logic); destBlock->logic = NULL; } if (destBlock->data) { destBlock->data = NULL; } if (srcBlock->logic) { destBlock->logic = calloc(1, sizeof(Logic)); memcpy(destBlock->logic, srcBlock->logic, sizeof(Logic)); updateLogicModel(destBlock); } else if (srcBlock->data) { destBlock->data = createModel(); copyChunk(destBlock->data->chunk, srcBlock->data->chunk); renderModel(destBlock->data); } } } } renderChunk(dest); }
void MapView::drawChunk(int x, int z) { if (!this->isEnabled()) return; uchar *src = placeholder; // fetch the chunk Chunk *chunk = cache.fetch(x, z); if (chunk && (chunk->renderedAt != depth || chunk->renderedFlags != flags)) { renderChunk(chunk); } // this figures out where on the screen this chunk should be drawn // first find the center chunk int centerchunkx = floor(this->x / 16); int centerchunkz = floor(this->z / 16); // and the center chunk screen coordinates int centerx = image.width() / 2; int centery = image.height() / 2; // which need to be shifted to account for panning inside that chunk centerx -= (this->x - centerchunkx * 16) * zoom; centery -= (this->z - centerchunkz * 16) * zoom; // centerx,y now points to the top left corner of the center chunk // so now calculate our x,y in relation double chunksize = 16 * zoom; centerx += (x - centerchunkx) * chunksize; centery += (z - centerchunkz) * chunksize; int srcoffset = 0; uchar *bits = image.bits(); int imgstride = image.bytesPerLine(); int skipx = 0, skipy = 0; int blockwidth = chunksize, blockheight = chunksize; // now if we're off the screen we need to crop if (centerx < 0) { skipx = -centerx; centerx = 0; } if (centery < 0) { skipy = -centery; centery = 0; } // or the other side, we need to trim if (centerx + blockwidth > image.width()) blockwidth = image.width() - centerx; if (centery + blockheight > image.height()) blockheight = image.height() - centery; if (blockwidth <= 0 || skipx >= blockwidth) return; int imgoffset = centerx * 4 + centery * imgstride; if (chunk) src = chunk->image; // blit (or scale blit) for (int z = skipy; z < blockheight; z++, imgoffset += imgstride) { srcoffset = floor(z / zoom) * 16 * 4; if (zoom == 1.0) { memcpy(bits + imgoffset, src + srcoffset + skipx * 4, (blockwidth - skipx) * 4); } else { int xofs = 0; for (int x = skipx; x < blockwidth; x++, xofs += 4) memcpy(bits + imgoffset + xofs, src + srcoffset + static_cast<int>(floor(x / zoom) * 4), 4); } } }