Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Application.h"
- #include <SDL2/SDL.h>
- class ParticleManager
- {
- struct Vec3
- {
- Vec3(float X, float Y, float Z)
- : x(X), y(Y), z(Z) { }
- float x, y, z;
- friend void operator += (const Vec3& a)
- {
- x += a.x;
- y += a.y;
- z += a.z;
- }
- };
- struct Particle
- {
- Vec3 pos;
- Vec3 vel;
- float mass;
- };
- public:
- ParticleManager(size_t count)
- {
- m_particles.resize(count);
- randomize();
- }
- void render(SDL_Renderer *ren)
- {
- SDL_SetRenderDrawColor(ren, 0, 0, 0, 80);
- for(int i = 0; i < m_particles.size(); ++i)
- {
- SDL_RenderDrawPoint(ren,
- m_centerX + m_particles[i].pos.x / m_particles.pos.z,
- m_centerY + m_particles[i].pos.y / m_particles.pos.z);
- }
- }
- void update(float dt)
- {
- // calk acceleration
- for(int i = 0; i < m_particles.size(); ++i)
- {
- for(int j = i + 1; j < m_particles.size(); ++j)
- {
- Vec3 d = (m_particles[j].pos - m_particles[i].pos);
- float len = d.len();
- float force = G * m_particles[i].mass * m_particles[j].mass / len / len;
- m_particles[i].vx += d * force * dt / m_particles[i].mass;
- m_particles[j].vy += -d * force * dt / m_particles[j].mass;
- }
- }
- // update
- for(int i = 0; i < m_particles.size(); ++i)
- {
- m_particles[i].pos += m_particles[i].vel * dt;
- }
- }
- private:
- std::vector<Particle> m_particles;
- };
- class App : public Application
- {
- public:
- App() : Application(1080, 1920) { }
- protected:
- virtual void onRender(SDL_Renderer *ren) override
- {
- SDL_SetRenderDrawColor(ren, 255, 0, 0, 0);
- SDL_RenderDrawLine(ren, 0, 0, getWidth(), getHeight());
- }
- virtual void onFingerDown(int x, int y) override
- {
- exit();
- }
- };
- APP_MAIN(App)
- #pragma once
- #include <SDL2/SDL.h>
- class Application
- {
- public:
- Application(int width, int height)
- : m_width(width), m_height(height)
- { }
- void run()
- {
- initSystems();
- onInit();
- SDL_Event e;
- Uint32 ticks = SDL_GetTicks();
- while(m_running)
- {
- while(SDL_PollEvent(&e))
- {
- handleEvent(e);
- }
- Uint32 newTicks = SDL_GetTicks();
- onUpdate((newTicks - ticks) / 1000.0);
- if(m_clear)
- {
- SDL_SetRenderDrawColor(m_ren, m_clearColor.r, m_clearColor.g, m_clearColor.b, 255);
- SDL_RenderClear(m_ren);
- }
- onRender(m_ren);
- if(m_delay != 0)
- SDL_Delay(m_delay);
- }
- onShutdown();
- shutdownSystems();
- }
- protected:
- virtual void onInit() { }
- virtual void onUpdate(float dt) { }
- virtual void onRender(SDL_Renderer *ren) { }
- virtual void onShutdown() { }
- virtual void onFingerDown(int x, int y) { }
- virtual void onFingerUp(int x, int y) { }
- virtual void onFingerMotion(int x, int y) { }
- void exit()
- {
- m_running = false;
- }
- void setClearParams(bool clear, Uint8 r, Uint8 g, Uint8 b)
- {
- m_clear = clear;
- m_clearColor.r = r;
- m_clearColor.g = g;
- m_clearColor.b = b;
- }
- void setBlendMode(SDL_BlendMode mode)
- {
- SDL_SetRenderDrawBlendMode(m_ren, mode);
- }
- void setDelay(int delay)
- {
- m_delay = delay;
- }
- int getWidth() const { return m_width; }
- int getHeight() const { return m_height; }
- private:
- void initSystems()
- {
- SDL_Init(SDL_INIT_VIDEO);
- m_win = SDL_CreateWindow("-", 0, 0, m_width, m_height, SDL_WINDOW_SHOWN);
- m_ren = SDL_CreateRenderer(m_win, -1, SDL_RENDERER_ACCELERATED);
- }
- void shutdownSystems()
- {
- SDL_DestroyRenderer(m_ren);
- SDL_DestroyWindow(m_win);
- SDL_Quit();
- }
- void handleEvent(SDL_Event& event)
- {
- switch(event.type)
- {
- case SDL_FINGERDOWN:
- {
- int x = event.tfinger.x * m_width;
- int y = event.tfinger.y * m_height;
- onFingerDown(x, y);
- break;
- }
- case SDL_FINGERUP:
- {
- int x = event.tfinger.x * m_width;
- int y = event.tfinger.y * m_height;
- onFingerUp(x, y);
- break;
- }
- case SDL_FINGERMOTION:
- {
- int x = event.tfinger.x * m_width;
- int y = event.tfinger.y * m_height;
- onFingerMotion(x, y);
- break;
- }
- }
- }
- private:
- SDL_Window *m_win;
- SDL_Renderer *m_ren;
- bool m_running = true;
- bool m_clear = true;
- int m_width;
- int m_height;
- int m_delay = 40;
- SDL_Color m_clearColor = {0};
- };
- #define APP_MAIN(app) int main(int argc, char *argv[]) { app().run(); return 0; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement