瀏覽代碼

Create desert at button clic

Create generic function putBloc to set up biome at cursor position.
DricomDragon 4 年之前
父節點
當前提交
9415c5e911
共有 1 個文件被更改,包括 17 次插入2 次删除
  1. 17 2
      main.cpp

+ 17 - 2
main.cpp

@@ -9,6 +9,12 @@
 
 enum blocType {WATER, MOUNTAIN, GRASS, DESERT};
 
+void putBloc(blocType bloc, float world[HEIGHT][WIDTH][BLOC_TOTAL], int y, int x, float level = 1.0f) {
+	for (int j(0); j < HEIGHT; j++)
+		for (int i(0); i < WIDTH; i++)
+			world[j][i][bloc] += level / ((j - y)*(j - y) + 1 + (i - x)*(i - x));
+}
+
 void drawWorld(float world[HEIGHT][WIDTH][BLOC_TOTAL], SDL_Surface* tileSet[], SDL_Surface* out) {
 	SDL_Rect pos({0, 0, 0, 0});
 	SDL_Surface* select; // Current surface selected ! DO NOT FREE !
@@ -63,6 +69,7 @@ int main()
 
 	// [2.3] Create variables
 	SDL_Rect pawnPos({0, 0, 0, 0});
+	SDL_Rect cursorPos({0, 0, 0, 0}); // Bloc coordinates
 
 	// [2.4] Create world
 	float world[HEIGHT][WIDTH][BLOC_TOTAL];
@@ -110,8 +117,16 @@ int main()
 						done = true;
 					break;
 				case SDL_MOUSEMOTION:
-					pawnPos.x = (event.motion.x / pawn->w) * pawn->w;
-					pawnPos.y = (event.motion.y / pawn->h) * pawn->h;
+					cursorPos.x = event.motion.x / BLOC_SIZE;
+					cursorPos.y = event.motion.y / BLOC_SIZE;
+					pawnPos.x = cursorPos.x * BLOC_SIZE;
+					pawnPos.y = cursorPos.y * BLOC_SIZE;
+					break;
+				case SDL_MOUSEBUTTONDOWN:
+					if (event.button.button == 1) {
+						putBloc(DESERT, world, cursorPos.y, cursorPos.x);
+						drawWorld(world, tileSet, map);
+					}
 					break;
 			} // end switch event type
 		} // end of message processing