-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
182 lines (116 loc) · 3.93 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include "SDL.h"
#include "Defs.h"
#include "Input.h"
#include "Particle.h"
#include <vector>
#include <cmath>
#include <random>
#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h"
#include "GUI.h"
#include <stdio.h>
#if !SDL_VERSION_ATLEAST(2,0,17)
#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
#endif
using namespace std;
vector<Particle> red_particles;
vector<Particle> blue_particles;
vector<Particle> green_particles;
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> wdistr(0, WIDTH);
std::uniform_int_distribution<> hdistr(0, HEIGHT);// define the range
SDL_Window* window = SDL_CreateWindow("Particle Simulation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
void GenerateParticles(int red_cout, int blue_count, int green_count) {
for (int i = 0; i < red_cout; i++)
red_particles.push_back(Particle(wdistr(gen), hdistr(gen), 1, 1, 0.0f, 0.0f, 255, 0, 0));
for (int i = 0; i < green_count; i++)
green_particles.push_back(Particle(wdistr(gen),hdistr(gen), 1, 1, 0.0f, 0.0f, 0, 255, 0));
for (int i = 0; i < blue_count; i++)
blue_particles.push_back(Particle(wdistr(gen), hdistr(gen), 1, 1, 0.0f, 0.0f, 0, 0, 255));
}
void DrawAllParticles(SDL_Renderer* renderer) {
if (red_particles.size() > 0)
for (int i = 0; i <= red_particles.size() - 1; i++)
red_particles[i].Draw(renderer);
//red_particles[i].DrawCircle(renderer, 5);
if (blue_particles.size() > 0)
for (int i = 0; i <= blue_particles.size() - 1; i++)
blue_particles[i].Draw(renderer);
//blue_particles[i].DrawCircle(renderer, 5);
if (green_particles.size() > 0)
for (int i = 0; i <= green_particles.size() - 1; i++)
green_particles[i].Draw(renderer);
//green_particles[i].DrawCircle(renderer, 5);
}
void applyRule(vector<Particle>* particle1, vector<Particle>* particle2, float strength) {
for (int i = 0; i < particle1->size(); i++) {
float fx = 0;
float fy = 0;
Particle* p1 = &particle1->at(i);
for (int j = 0; j < particle2->size(); j++) {
Particle* p2 = &particle2->at(j);
float dx = p1->x - p2->x ;
float dy = p1->y - p2->y;
float distance = sqrt(pow(dx, 2) + pow(dy, 2));
if (distance < 80 && distance>0) {
float force = 1 / distance * strength;
fx += dx * force;
fy += dy * force;
}
}
p1->x_vel = (p1->x_vel + fx)*0.5;
p1->y_vel = (p1->y_vel + fy) * 0.5;
p1->x += p1->x_vel;
p1->y += p1->y_vel;
if (p1->x <= 0 || p1->x >= WIDTH)
p1->x_vel = p1->x_vel* (-5);
if (p1->y <= 0 || p1->y >= HEIGHT)
p1->y_vel = p1->y_vel * (-1);
}
}
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
cout << "SDL initialization failed. SDL Error: " << SDL_GetError();
}
else
{
cout << "SDL initialization succeeded!";
}
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
if (!window) {
cout << "Error loading the window" << endl;
}
if (!renderer) {
cout << "No renderer"<<endl;
}
else {
cout << "renderer initialized" << endl;
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer2_Init(renderer);
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
while (1) {
CheckInputs(window, renderer);
PrepareGUIComponent(&red_particles, &green_particles, &blue_particles);
SDL_RenderClear(renderer);
DrawAllParticles(renderer);
RenderGUI(renderer, io);
ApplyAllRules(&red_particles, &green_particles, &blue_particles);
SDL_RenderPresent(renderer);
}
std::cin.get();
return 0;
}