Browse Source

Store world terraform action as seeds

Implement new class named Seed.
DricomDragon 4 years ago
parent
commit
8e5d5840a7
1 changed files with 39 additions and 10 deletions
  1. 39 10
      main.cpp

+ 39 - 10
main.cpp

@@ -2,19 +2,39 @@
 #include <iostream>
 #include <SDL/SDL.h>
 
+#include <cstdlib>
+#include <ctime>
+
 #define BLOC_SIZE 16
 #define WIDTH 100
 #define HEIGHT 50
 #define BLOC_TOTAL 4
+#define MAX_LEVEL 1000 // * 10-3
+
+enum BlocType {WATER, MOUNTAIN, GRASS, DESERT};
+
+class Seed {
+	public:
+		BlocType type;
+		int y, x;
+		float level;
+
+	public:
+		Seed():type(GRASS), y(HEIGHT / 2), x(WIDTH / 2), level(1.0f) {}
+		void randomize() {
+			type = (BlocType) (rand() % BLOC_TOTAL);
+			y = rand() % HEIGHT;
+			x = rand() % WIDTH;
+			level = 0.001f * (rand() % MAX_LEVEL);
+		}
+};
 
-enum blocType {WATER, MOUNTAIN, GRASS, DESERT};
-
-void putBloc(blocType bloc, float world[HEIGHT][WIDTH][BLOC_TOTAL], int y, int x, float level = 1.0f) {
+void putSeed(Seed s, float world[HEIGHT][WIDTH][BLOC_TOTAL]) {
 	int dist;
 	for (int j(0); j < HEIGHT; j++)
 		for (int i(0); i < WIDTH; i++) {
-			dist = (j - y)*(j - y) + 1 + (i - x)*(i - x);
-			world[j][i][bloc] += level / dist;
+			dist = (j - s.y)*(j - s.y) + 1 + (i - s.x)*(i - s.x);
+			world[j][i][s.type] += s.level / dist;
 		}
 }
 
@@ -75,6 +95,9 @@ int main()
 	// [1.3] Set title
 	SDL_WM_SetCaption("Biome Box", 0);
 
+	// [1.4] Init random
+	srandom(time(0));
+
 	/// [2] Create components
 	// [2.1] Create window
 	SDL_Surface* screen = SDL_SetVideoMode(WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
@@ -93,7 +116,7 @@ int main()
 	// [2.3] Create variables
 	SDL_Rect pawnPos({0, 0, 0, 0});
 	SDL_Rect cursorPos({0, 0, 0, 0}); // Bloc coordinates
-	int indexCurrent(0);
+	Seed seed;
 
 	// [2.4] Create world
 	float world[HEIGHT][WIDTH][BLOC_TOTAL];
@@ -139,12 +162,18 @@ int main()
 					break;
 				case SDL_MOUSEBUTTONDOWN:
 					if (event.button.button == 1) {
-						putBloc((blocType)indexCurrent, world, cursorPos.y, cursorPos.x);
+						seed.x = cursorPos.x;
+						seed.y = cursorPos.y;
+						putSeed(seed, world);
 						drawWorld(world, tileSet, map);
 					}
 					if (event.button.button == 3) {
-						indexCurrent++;
-						if (indexCurrent >= BLOC_TOTAL) indexCurrent = 0;
+						int seedType = (int) seed.type;
+
+						seedType++;
+						if (seedType >= BLOC_TOTAL) seedType = 0;
+
+						seed.type = (BlocType) seedType;
 					}
 					break;
 			} // end switch event type
@@ -158,7 +187,7 @@ int main()
 
 		SDL_BlitSurface(map, NULL, screen, NULL);
 
-		SDL_BlitSurface(tileSet[indexCurrent], NULL, screen, &pawnPos);
+		SDL_BlitSurface(tileSet[seed.type], NULL, screen, &pawnPos);
 
 		SDL_Flip(screen);