Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. #include "Application.h"
  2. #include <SDL2/SDL.h>
  3.  
  4. class ParticleManager
  5. {
  6.     struct Vec3
  7.     {
  8.         Vec3(float X, float Y, float Z)
  9.             : x(X), y(Y), z(Z) { }
  10.         float x, y, z;
  11.        
  12.         friend void operator += (const Vec3& a)
  13.         {
  14.             x += a.x;
  15.             y += a.y;
  16.             z += a.z;
  17.         }
  18.     };
  19.    
  20.     struct Particle
  21.     {
  22.         Vec3 pos;
  23.         Vec3 vel;
  24.         float mass;
  25.     };
  26.    
  27. public:
  28.     ParticleManager(size_t count)
  29.     {
  30.         m_particles.resize(count);
  31.         randomize();
  32.     }
  33.    
  34.     void render(SDL_Renderer *ren)
  35.     {
  36.         SDL_SetRenderDrawColor(ren, 0, 0, 0, 80);
  37.         for(int i = 0; i < m_particles.size(); ++i)
  38.         {
  39.             SDL_RenderDrawPoint(ren,
  40.              m_centerX + m_particles[i].pos.x / m_particles.pos.z,
  41.              m_centerY + m_particles[i].pos.y / m_particles.pos.z);
  42.         }
  43.     }
  44.    
  45.     void update(float dt)
  46.     {
  47.         // calk acceleration
  48.         for(int i = 0; i < m_particles.size(); ++i)
  49.         {
  50.             for(int j = i + 1; j < m_particles.size(); ++j)
  51.             {
  52.                 Vec3 d = (m_particles[j].pos - m_particles[i].pos);
  53.                 float len = d.len();
  54.                 float force = G * m_particles[i].mass * m_particles[j].mass / len / len;
  55.                 m_particles[i].vx += d * force * dt / m_particles[i].mass;
  56.                 m_particles[j].vy += -d * force * dt / m_particles[j].mass;
  57.             }
  58.         }
  59.            
  60.         // update
  61.         for(int i = 0; i < m_particles.size(); ++i)
  62.         {
  63.             m_particles[i].pos += m_particles[i].vel * dt;
  64.         }
  65.     }
  66.    
  67. private:
  68.     std::vector<Particle> m_particles;
  69. };
  70.  
  71. class App : public Application
  72. {
  73. public:
  74.     App() : Application(1080, 1920) { }
  75.    
  76. protected:
  77.     virtual void onRender(SDL_Renderer *ren) override
  78.     {
  79.         SDL_SetRenderDrawColor(ren, 255, 0, 0, 0);
  80.         SDL_RenderDrawLine(ren, 0, 0, getWidth(), getHeight());
  81.     }
  82.    
  83.     virtual void onFingerDown(int x, int y) override
  84.     {
  85.         exit();
  86.     }
  87. };
  88.  
  89. APP_MAIN(App)
  90.  
  91.  
  92. #pragma once
  93. #include <SDL2/SDL.h>
  94.  
  95. class Application
  96. {
  97. public:
  98.     Application(int width, int height)
  99.         : m_width(width), m_height(height)
  100.         { }
  101.  
  102.     void run()
  103.     {
  104.         initSystems();
  105.         onInit();
  106.        
  107.         SDL_Event e;
  108.         Uint32 ticks = SDL_GetTicks();
  109.         while(m_running)
  110.         {
  111.             while(SDL_PollEvent(&e))
  112.             {
  113.                 handleEvent(e);
  114.             }
  115.            
  116.             Uint32 newTicks = SDL_GetTicks();
  117.             onUpdate((newTicks - ticks) / 1000.0);
  118.             if(m_clear)
  119.             {
  120.                 SDL_SetRenderDrawColor(m_ren, m_clearColor.r, m_clearColor.g, m_clearColor.b, 255);
  121.                 SDL_RenderClear(m_ren);
  122.             }
  123.            
  124.             onRender(m_ren);
  125.             if(m_delay != 0)
  126.                 SDL_Delay(m_delay);
  127.         }
  128.        
  129.         onShutdown();
  130.         shutdownSystems();
  131.     }
  132.    
  133. protected:
  134.     virtual void onInit() { }
  135.     virtual void onUpdate(float dt) { }
  136.     virtual void onRender(SDL_Renderer *ren) { }
  137.     virtual void onShutdown() { }
  138.    
  139.     virtual void onFingerDown(int x, int y) { }
  140.     virtual void onFingerUp(int x, int y) { }
  141.     virtual void onFingerMotion(int x, int y) { }
  142.    
  143.     void exit()
  144.     {
  145.         m_running = false;
  146.     }
  147.    
  148.     void setClearParams(bool clear, Uint8 r, Uint8 g, Uint8 b)
  149.     {
  150.         m_clear = clear;
  151.         m_clearColor.r = r;
  152.         m_clearColor.g = g;
  153.         m_clearColor.b = b;
  154.     }
  155.    
  156.     void setBlendMode(SDL_BlendMode mode)
  157.     {
  158.         SDL_SetRenderDrawBlendMode(m_ren, mode);
  159.     }
  160.    
  161.     void setDelay(int delay)
  162.     {
  163.         m_delay = delay;
  164.     }
  165.    
  166.     int getWidth() const { return m_width; }
  167.     int getHeight() const { return m_height; }
  168.    
  169. private:
  170.     void initSystems()
  171.     {
  172.         SDL_Init(SDL_INIT_VIDEO);
  173.         m_win = SDL_CreateWindow("-", 0, 0, m_width, m_height, SDL_WINDOW_SHOWN);
  174.         m_ren = SDL_CreateRenderer(m_win, -1, SDL_RENDERER_ACCELERATED);
  175.     }
  176.    
  177.     void shutdownSystems()
  178.     {
  179.         SDL_DestroyRenderer(m_ren);
  180.         SDL_DestroyWindow(m_win);
  181.         SDL_Quit();
  182.     }
  183.    
  184.     void handleEvent(SDL_Event& event)
  185.     {
  186.         switch(event.type)
  187.         {
  188.         case SDL_FINGERDOWN:
  189.         {
  190.             int x = event.tfinger.x * m_width;
  191.             int y = event.tfinger.y * m_height;
  192.             onFingerDown(x, y);
  193.             break;
  194.         }
  195.         case SDL_FINGERUP:
  196.         {
  197.             int x = event.tfinger.x * m_width;
  198.             int y = event.tfinger.y * m_height;
  199.             onFingerUp(x, y);
  200.             break;
  201.         }
  202.         case SDL_FINGERMOTION:
  203.         {
  204.             int x = event.tfinger.x * m_width;
  205.             int y = event.tfinger.y * m_height;
  206.             onFingerMotion(x, y);
  207.             break;
  208.         }
  209.         }
  210.     }
  211.  
  212. private:
  213.     SDL_Window *m_win;
  214.     SDL_Renderer *m_ren;
  215.    
  216.     bool m_running = true;
  217.     bool m_clear = true;
  218.    
  219.     int m_width;
  220.     int m_height;
  221.    
  222.     int m_delay = 40;
  223.    
  224.     SDL_Color m_clearColor = {0};
  225. };
  226.  
  227. #define APP_MAIN(app) int main(int argc, char *argv[]) { app().run(); return 0; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement