Island Generator - procedurally generating a map of islands
Today I started my C++ journey!
My Goal is to create my own game in UE4, but first I want to get used to native C++
The Island Generator will work like this:
- Generate a Grid (static 2-dimensional Array) that is filled with 0`s
- Place random Island Centers
- Recursively grow from island centers (remember to save center coordinates)
- from Grid to PNG (libPNG)
- eventually create GUI and wrap it up as a small, functional program
Lessons I learned:
- rand() is pseudo random. Use srand(time(NULL)) for a seed based on runtime
- deallocation
- Constants
- Array initializer uses constants as parameters
Current State
Replies
Later on I will recursively expand from those island centers to generate naturally looking islands.
Afterwards I will use a png library to generate actual images that can be used as masks.
I've been looking into Perlin's documentation but as I want to create only black or white masks I will stick with my simpler approach.
Although as far as I can tell, Perlin is also using rand()
also, it would be nice if you put the source code into spoilers so the thread stays readable!
I am going to continue like I started, because this project is mostly about getting used to C++
But at some point I want to add Interpolation
Would be nice if you put your first post in spoiler so its not taking that much space. Was not talking about formatting the code
Will try that out aswell
Atleast I made some progress on the grow algorithm. The islands grow based on their centers into each direction.
They randomly pick some left or right distortion. The growth can randomly stop, because before each turn an if clause based on a random number decides whether or not to progress. Later on I will use Constants to make these variables changeable from the outside.
In extreme cases of reaching the grid border. The algorithm just continues on the bottom of the grid and vice versa!
Now the Print Method has it's own function scope so I can easily reuse it and keep my main() clear.
I am not very happy with my code structure though. I have alot of redundant code as I do the calculati0on for each direction. There might be a better way but thats how it is for now
Lessons learned today:
the rand() can give inaccurate results, because in very high numbers the probability can change. In my case it doesnt matter, but for Algorithms in crypto it can have a big impact.
Recursion becomes very complex when it comes to handle stuff simultaneously.
How the Debugger in Visual Studio works
Working with Multidimensional Arrays, if else clauses, for-loops, while loops, <random> header, how to implement probability
I cant show any result now, because I broke the current program.
Will continue tomorrow.
@Klunk Would be awesome if you put your message into spoilers so my thread stays readable. many thanks !
Everything should work correctly, as I pick my values from a range in between 0-30
I hope I get this one figured out.
I will now try to let the cells grow with more spread. As I want to translate my grad to a PNG pixel per pixel, it has to have a much wider spread.
I tried to include libpng as well as Magick++ (both external libraries with API's capable of writing images)
but I couldnt get it to work. I cant even compile the demo projects. Libs are tricky !
This is the basic layout !
There will be more sliders for options like randonmness, change in direction etc. I set up all important numbers with constants so it shouldnt be difficult to implement. When I get a new value, it will be changed throughout the whole logic. Although I have no idea yet how to connect the widgets with my logic yet, I think it will be a masterable challenge.
I also want everything to be nicely scalable (dynamic sizing)
The image in the middle of course will be displaying the generated image. I will probably run into troubles with my current file format .ppm, I hope it can be displayed.
The process of saving the generated image will include a classic "choose directory" menu.
I've been making some progress, although I had to remake the whole GUI several times.
And my code structure isnt the best either. But I already learned alot about C++
For example about object visibility and lifetime, Heap & Stack Allocation which caused me alot of problems that I fixed now.
Will post an update soon !