Skip to content

OpenSiv3D中級サンプル

Kasugaccho edited this page May 19, 2019 · 1 revision

サンプルコード

縦横 同じサイズで地形生成

#include <Siv3D.hpp>
#include <DTL.hpp>

#include <cstddef>
#include <array>
#include <bitset>

void Main() {

	constexpr std::size_t matrix_size{ 128 };
	constexpr std::size_t dungeon_pixel_size{ 2 };

	Window::Resize(int32(matrix_size * dungeon_pixel_size), int32(matrix_size * dungeon_pixel_size));

	std::array<std::bitset<matrix_size>, matrix_size> matrix{ {} };
	dtl::shape::SimpleVoronoiIsland<bool>(100, 0.5, 1, 0).draw(matrix);
	dtl::utility::CellularAutomation<bool>().draw(matrix);

	while (System::Update())
		for (std::size_t row{}; row < matrix.size(); ++row)
			for (std::size_t col{}; col < matrix[row].size(); ++col)
				Rect(int32(col * dungeon_pixel_size), int32(row * dungeon_pixel_size), int32(dungeon_pixel_size)).draw((matrix[row][col]) ? Color(98, 170, 48) : Color(30, 30, 163));

}

sample

縦横 異なるサイズで地形生成

#include <Siv3D.hpp>
#include <DTL.hpp>

#include <cstddef>
#include <array>
#include <bitset>

void Main() {

	constexpr std::size_t matrix_size_x{ 196 };
	constexpr std::size_t matrix_size_y{ 128 };
	constexpr std::size_t dungeon_pixel_size{ 2 };

	Window::Resize(int32(matrix_size_x * dungeon_pixel_size), int32(matrix_size_y * dungeon_pixel_size));

	std::array<std::bitset<matrix_size_x>, matrix_size_y> matrix{ {} };
	dtl::shape::SimpleVoronoiIsland<bool>(100, 0.5, 1, 0).draw(matrix);
	dtl::utility::CellularAutomation<bool>().draw(matrix);

	while (System::Update())
		for (std::size_t row{}; row < matrix.size(); ++row)
			for (std::size_t col{}; col < matrix[row].size(); ++col)
				Rect(int32(col * dungeon_pixel_size), int32(row * dungeon_pixel_size), int32(dungeon_pixel_size)).draw((matrix[row][col]) ? Color(98, 170, 48) : Color(30, 30, 163));

}

sample2

生成ループ

#include <Siv3D.hpp>
#include <DTL.hpp>

#include <cstddef>
#include <array>
#include <bitset>

void Main() {

	constexpr std::size_t matrix_size_x{ 196 };
	constexpr std::size_t matrix_size_y{ 128 };
	constexpr std::size_t dungeon_pixel_size{ 2 };

	Window::Resize(int32(matrix_size_x * dungeon_pixel_size), int32(matrix_size_y * dungeon_pixel_size));

	std::array<std::bitset<matrix_size_x>, matrix_size_y> matrix{ {} };

	constexpr int loop_counter_max{ 20 };
	int loop_counter{ loop_counter_max };

	while (System::Update()) {
		if (loop_counter >= loop_counter_max) {

			dtl::utility::Init<bool>(0).draw(matrix);
			dtl::shape::SimpleVoronoiIsland<bool>(100, 0.5, 1, 0).draw(matrix);
			dtl::utility::CellularAutomation<bool>().draw(matrix);
			loop_counter = 0;
		}
		for (std::size_t row{}; row < matrix.size(); ++row)
			for (std::size_t col{}; col < matrix[row].size(); ++col)
				Rect(int32(col * dungeon_pixel_size), int32(row * dungeon_pixel_size), int32(dungeon_pixel_size)).draw((matrix[row][col]) ? Color(98, 170, 48) : Color(30, 30, 163));
		++loop_counter;
	}
}

sample3

Clone this wiki locally