| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #include <iostream>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include "SDL2/SDL.h"
- #include <SDL2/SDL_image.h>
- #include <SDL2/SDL_timer.h>
- #include <vector>
- #include <thread>
- #include <chrono>
- #include "definitions.hpp"
- #include "Map.hpp"
- #include "Particle.hpp"
- #include "Tile.hpp"
- numeric_t my_rand(numeric_t min, numeric_t max){
- numeric_t rand_x = (numeric_t)(rand())/(numeric_t)(RAND_MAX);
- if(rand_x == 1)
- rand_x = 0;
- return min + rand_x * (max - min);
- }
- Uint8 clamp_Uint8(numeric_t x){
- if(x >= 255.0f)
- return 255;
- if(x <= 0.0f)
- return 0;
- return (Uint8) x;
- }
- int main(int argc, char *argv[]){
- size_t width = 600;
- size_t height = 400;
- size_t population = 100000;
- numeric_t increase = 20;
- numeric_t decay = 13;
- numeric_t diffusion = 1.5;
- numeric_t speed = 30;
- numeric_t steering = 180;
- numeric_t sampling_distance = 3;
- numeric_t sampling_angle = 30;
- numeric_t average_pop = (numeric_t)population / (numeric_t)(width * height);
- numeric_t average_pheromone = (numeric_t)population * (numeric_t)(increase) / (numeric_t)(width * height);
- Map map(width, height);
- map.increase = increase;
- map.decay = decay;
- map.diffusion = diffusion;
- map.speed = speed;
- map.steering = steering;
- map.sampling_angle = sampling_angle;
- map.sampling_distance = sampling_distance;
- {
- std::vector<Particle> particles;
- particles.reserve(population);
- for(size_t i = 0 ; i < population ; i++){
- fpoint_t pos{my_rand(0, width), my_rand(0, height)};
- numeric_t angle = my_rand(0, 2*M_PI);
- fpoint_t dir{(fcoord_t) cos(angle), (fcoord_t) sin(angle)};
- particles.push_back(Particle(pos, dir));
- // std::cout << "pos=" << pos.x << ";" << pos.x << " dir=" << dir.x << ";" << dir.y << "\n";
- }
- map.populate(std::move(particles));
- }
- SDL_Window* win;
- SDL_Renderer* renderer;
- Uint64 time_last;
- Uint64 time_now;
- numeric_t delta_time;
- SDL_CreateWindowAndRenderer(width, height, 0, &win, &renderer);
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
- SDL_RenderClear(renderer);
- time_now = SDL_GetTicks64();
- bool running = true;
- while(running){SDL_Event event;
-
- // Events management
- while (SDL_PollEvent(&event)) {
- if(event.type == SDL_QUIT) {
- running = false;
- break;
- }else if(event.type == SDL_KEYDOWN){
- if(event.key.keysym.scancode == SDL_SCANCODE_K){
- map.increase = 0;
- map.decay = 1000;
- map.diffusion = 0.3;
- map.steering = 0;
- }
- }
- }
- time_last = time_now;
- time_now = SDL_GetTicks64();
- delta_time = (time_now - time_last) / 1000.0f;
- // std::cout << delta_time << "\n";
- map.update(delta_time);
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
- SDL_RenderClear(renderer);
- for(size_t i = 0 ; i < height ; ++i){
- for(size_t j = 0 ; j < width ; ++j){
- Uint8 pheromone = clamp_Uint8( map.tiles().at(i * width + j).pheromone * 255.0f/average_pheromone );
- Uint8 tile_pop = clamp_Uint8( map.tiles().at(i * width + j).population * 20.0f/average_pop );
- SDL_SetRenderDrawColor(renderer, pheromone, tile_pop, tile_pop, 1);
- // SDL_SetRenderDrawColor(renderer, pheromone, tile_pop, pheromone, 1);
- SDL_RenderDrawPoint(renderer, j, i);
- }
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(20));
- SDL_RenderPresent(renderer);
- }
- // destroy SDL things
- SDL_DestroyWindow(win);
- SDL_DestroyRenderer(renderer);
- SDL_Quit();
- return 0;
- }
|