main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <iostream>
  2. #define _USE_MATH_DEFINES
  3. #include <cmath>
  4. #include "SDL2/SDL.h"
  5. #include <SDL2/SDL_image.h>
  6. #include <SDL2/SDL_timer.h>
  7. #include <vector>
  8. #include <thread>
  9. #include <chrono>
  10. #include "definitions.hpp"
  11. #include "Map.hpp"
  12. #include "Particle.hpp"
  13. #include "Tile.hpp"
  14. numeric_t my_rand(numeric_t min, numeric_t max){
  15. numeric_t rand_x = (numeric_t)(rand())/(numeric_t)(RAND_MAX);
  16. if(rand_x == 1)
  17. rand_x = 0;
  18. return min + rand_x * (max - min);
  19. }
  20. Uint8 clamp_Uint8(numeric_t x){
  21. if(x >= 255.0f)
  22. return 255;
  23. if(x <= 0.0f)
  24. return 0;
  25. return (Uint8) x;
  26. }
  27. int main(int argc, char *argv[]){
  28. size_t width = 600;
  29. size_t height = 400;
  30. size_t population = 100000;
  31. numeric_t increase = 20;
  32. numeric_t decay = 13;
  33. numeric_t diffusion = 1.5;
  34. numeric_t speed = 30;
  35. numeric_t steering = 180;
  36. numeric_t sampling_distance = 3;
  37. numeric_t sampling_angle = 30;
  38. numeric_t average_pop = (numeric_t)population / (numeric_t)(width * height);
  39. numeric_t average_pheromone = (numeric_t)population * (numeric_t)(increase) / (numeric_t)(width * height);
  40. Map map(width, height);
  41. map.increase = increase;
  42. map.decay = decay;
  43. map.diffusion = diffusion;
  44. map.speed = speed;
  45. map.steering = steering;
  46. map.sampling_angle = sampling_angle;
  47. map.sampling_distance = sampling_distance;
  48. {
  49. std::vector<Particle> particles;
  50. particles.reserve(population);
  51. for(size_t i = 0 ; i < population ; i++){
  52. fpoint_t pos{my_rand(0, width), my_rand(0, height)};
  53. numeric_t angle = my_rand(0, 2*M_PI);
  54. fpoint_t dir{(fcoord_t) cos(angle), (fcoord_t) sin(angle)};
  55. particles.push_back(Particle(pos, dir));
  56. // std::cout << "pos=" << pos.x << ";" << pos.x << " dir=" << dir.x << ";" << dir.y << "\n";
  57. }
  58. map.populate(std::move(particles));
  59. }
  60. SDL_Window* win;
  61. SDL_Renderer* renderer;
  62. Uint64 time_last;
  63. Uint64 time_now;
  64. numeric_t delta_time;
  65. SDL_CreateWindowAndRenderer(width, height, 0, &win, &renderer);
  66. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  67. SDL_RenderClear(renderer);
  68. time_now = SDL_GetTicks64();
  69. bool running = true;
  70. while(running){SDL_Event event;
  71. // Events management
  72. while (SDL_PollEvent(&event)) {
  73. if(event.type == SDL_QUIT) {
  74. running = false;
  75. break;
  76. }else if(event.type == SDL_KEYDOWN){
  77. if(event.key.keysym.scancode == SDL_SCANCODE_K){
  78. map.increase = 0;
  79. map.decay = 1000;
  80. map.diffusion = 0.3;
  81. map.steering = 0;
  82. } else if(event.key.keysym.scancode == SDL_SCANCODE_D){
  83. map.increase = 20;
  84. map.decay = 13;
  85. map.diffusion = 1.5;
  86. map.steering = 180;
  87. }
  88. }
  89. }
  90. time_last = time_now;
  91. time_now = SDL_GetTicks64();
  92. delta_time = (time_now - time_last) / 1000.0f;
  93. // std::cout << delta_time << "\n";
  94. map.update(delta_time);
  95. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  96. SDL_RenderClear(renderer);
  97. for(size_t i = 0 ; i < height ; ++i){
  98. for(size_t j = 0 ; j < width ; ++j){
  99. Uint8 pheromone = clamp_Uint8( map.tiles().at(i * width + j).pheromone * 255.0f/average_pheromone );
  100. Uint8 tile_pop = clamp_Uint8( map.tiles().at(i * width + j).population * 20.0f/average_pop );
  101. SDL_SetRenderDrawColor(renderer, pheromone, tile_pop, tile_pop, 1);
  102. // SDL_SetRenderDrawColor(renderer, pheromone, tile_pop, pheromone, 1);
  103. SDL_RenderDrawPoint(renderer, j, i);
  104. }
  105. }
  106. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  107. SDL_RenderPresent(renderer);
  108. }
  109. // destroy SDL things
  110. SDL_DestroyWindow(win);
  111. SDL_DestroyRenderer(renderer);
  112. SDL_Quit();
  113. return 0;
  114. }