Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SDL2/SDL.h>
- #include <stdlib.h>
- #include <time.h>
- const int FRAME_TIME_TARGET = 16.667;
- const int SCREEN_WIDTH = 1024;
- const int SCREEN_HEIGHT = 1024;
- const int SCREEN_OFFSET_X = 3400;
- const int SCREEN_OFFSET_Y = 600;
- const int GRID_SIZE = 16; // Size of each grid cell
- const int GRID_ROWS = SCREEN_HEIGHT / GRID_SIZE;
- const int GRID_COLS = SCREEN_WIDTH / GRID_SIZE;
- float gridCell[GRID_ROWS][GRID_COLS];
- float MAX_SLOPE=0.05;
- float PROPAGATE_AMPLITUDE=0.025;
- bool DISPLAY_NORMAL=false;
- void renderWholeGrid(SDL_Renderer* renderer) {
- // Clear the renderer
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- // Set a different color for each cell
- for (int i = 0; i < GRID_ROWS; ++i) {
- for (int j = 0; j < GRID_COLS; ++j) {
- int red,green,blue;
- if (DISPLAY_NORMAL == 1) {
- float sumSlope_x = 0.0f;
- float sumSlope_y = 0.0f;
- int maskRadius = 1; // Adjust the mask radius as needed
- for (int s = -maskRadius; s <= maskRadius; ++s) {
- for (int t = -maskRadius; t <= maskRadius; ++t) {
- // Calculate indices of the neighboring cell
- int in = i + s;
- int jn = j + t;
- // Make sure array index isn't out of bounds
- in = std::min(GRID_ROWS - 1, std::max(0, in));
- jn = std::min(GRID_COLS - 1, std::max(0, jn));
- // Calculate slope with the neighboring cell
- float slope = gridCell[i][j] - gridCell[in][jn];
- sumSlope_x += slope * s;
- sumSlope_y += slope * t;
- }
- }
- // Normalize the normal vector
- float length = sqrt(sumSlope_x * sumSlope_x + sumSlope_y * sumSlope_y);
- if (length > 0) {
- sumSlope_x /= length;
- sumSlope_y /= length;
- }
- // Map the normalized components to color values (assuming positive values)
- red = static_cast<int>((sumSlope_x + 1.0f) * 0.5f * 255);
- green = static_cast<int>((sumSlope_y + 1.0f) * 0.5f * 255);
- blue = 127; // Set blue to a constant value for simplicity
- } else {
- red = gridCell[i][j] * 255;
- green = gridCell[i][j] * 255;
- blue = gridCell[i][j] * 255;
- }
- SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
- SDL_Rect cell = {j * GRID_SIZE, i * GRID_SIZE, GRID_SIZE, GRID_SIZE};
- SDL_RenderFillRect(renderer, &cell);
- }
- }
- // Present the renderer
- SDL_RenderPresent(renderer);
- }
- //Spills to neighbouring cells if slope is too high
- void updateHeight(SDL_Renderer* renderer) {
- for (int i = 0; i < GRID_ROWS; i++) {
- for (int j = 0; j < GRID_COLS; j++) {
- //loop through masked neighbors
- int maskRadius=2;
- for(int s=-maskRadius;s<=maskRadius;s++){
- for(int t=-maskRadius;t<=maskRadius;t++){
- //offset to neighbor
- int in = i+s;
- int jn = j+t;
- // Make sure array index isn't out of bounds
- in = std::min(GRID_ROWS - 1, std::max(0, in));
- jn = std::min(GRID_COLS - 1, std::max(0, jn));
- //spill to neighbor
- float slope = gridCell[i][j]-gridCell[in][jn];
- bool tooHigh = abs(slope)>MAX_SLOPE;
- float centerDistance = sqrt(s*s+t*t)/sqrt(2*maskRadius*maskRadius);
- gridCell[i][j] -= tooHigh*PROPAGATE_AMPLITUDE*(slope/2)*(1-centerDistance);
- gridCell[in][jn]+= tooHigh*PROPAGATE_AMPLITUDE*(slope/2)*(1-centerDistance);
- }
- }
- }
- }
- }
- // Variables for key press cooldown
- const Uint32 COOLDOWN_TIME = 50;
- Uint32 lastKeyPressTime = 0;
- // Variables to track mouse drag
- bool isMouseDragging = false;
- int dragStartX, dragStartY;
- bool quit = false;
- SDL_Event e;
- void detectKeys(){
- while (SDL_PollEvent(&e) != 0) {
- if (e.type == SDL_QUIT) {
- quit = true;
- break;
- }
- if (e.type == SDL_KEYDOWN) {
- if (e.key.keysym.sym == SDLK_ESCAPE) {
- quit = true;
- continue;
- }
- if (e.key.keysym.sym == SDLK_n) {
- DISPLAY_NORMAL = !DISPLAY_NORMAL; // Toggle DISPLAY_NORMAL
- std::cout << "DISPLAY_NORMAL: " << DISPLAY_NORMAL << std::endl; // Print to console
- continue;
- }
- if (e.key.keysym.sym == SDLK_r) {
- for (int i = 0; i < GRID_ROWS; ++i) {
- for (int j = 0; j < GRID_COLS; ++j) {
- int squareSize = 9;
- gridCell[i][j] = ( i/squareSize + j/squareSize )%2; //checker board
- gridCell[i][j]*= rand()/(float)RAND_MAX; //noise on white squares
- }
- }
- continue;
- }
- continue;
- }
- if (e.type == SDL_MOUSEBUTTONDOWN) {
- if (e.button.button == SDL_BUTTON_LEFT || e.button.button == SDL_BUTTON_RIGHT) {
- isMouseDragging = true;
- SDL_GetMouseState(&dragStartX, &dragStartY);
- }
- continue;
- }
- if (e.type == SDL_MOUSEBUTTONUP) {
- if (e.button.button == SDL_BUTTON_LEFT || e.button.button == SDL_BUTTON_RIGHT) {
- isMouseDragging = false;
- }
- continue;
- }
- if (e.type == SDL_MOUSEMOTION) {
- if (isMouseDragging) {
- int mouseX, mouseY;
- SDL_GetMouseState(&mouseX, &mouseY);
- // Calculate the grid position based on the mouse coordinates
- int gridX = mouseX / GRID_SIZE;
- int gridY = mouseY / GRID_SIZE;
- // Determine the value based on the mouse button
- float valueToSet = (e.button.button == SDL_BUTTON_LEFT) ? 1.0f : 0.0f;
- // Update the grid cells based on the value
- int maskRadius = 2;
- for (int s = -maskRadius; s <= maskRadius; s++) {
- for (int t = -maskRadius; t <= maskRadius; t++) {
- gridCell[gridY + s][gridX + t] = valueToSet;
- }
- }
- }
- continue;
- }
- }
- }
- int main() {
- srand(time(NULL));
- //Generate initial height map
- for (int i = 0; i < GRID_ROWS; ++i) {
- for (int j = 0; j < GRID_COLS; ++j) {
- int squareSize = 9;
- gridCell[i][j] = ( i/squareSize + j/squareSize )%2; //checker board
- gridCell[i][j]*= rand()/(float)RAND_MAX; //noise on white squares
- }
- }
- // Initialize SDL
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
- return 1;
- }
- // Create a window
- SDL_Window* window = SDL_CreateWindow("Grid Renderer", SCREEN_OFFSET_X, SCREEN_OFFSET_Y, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (window == nullptr) {
- std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
- return 2;
- }
- // Create a renderer for the window
- SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- if (renderer == nullptr) {
- std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
- return 3;
- }
- Uint32 frameStartTime = SDL_GetTicks();
- frameStartTime+=FRAME_TIME_TARGET;
- renderWholeGrid(renderer);
- //Render loop
- while (!quit) {
- // Calculate the time to render a frame
- Uint32 currentFrameTime = SDL_GetTicks() - frameStartTime;
- // Wait till FRAME_TIME_TARGET is reached
- if (currentFrameTime < FRAME_TIME_TARGET) {
- SDL_Delay(FRAME_TIME_TARGET - currentFrameTime);
- detectKeys();
- }
- updateHeight(renderer);
- renderWholeGrid(renderer);
- frameStartTime = SDL_GetTicks();
- }
- // Clean up and exit
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement