Advertisement
MegaLoler

Terrible Physics...

Jul 24th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. #include "SDL/SDL.h"
  5. #include "SDL/SDL_gfxPrimitives.h"
  6.  
  7. #define WIDTH 320
  8. #define HEIGHT 240
  9.  
  10. #define FPS 60
  11. #define SPF 5
  12.  
  13. using namespace std;
  14.  
  15. struct Dimension
  16. {
  17.     public:
  18.         double x;
  19.         double y;
  20.        
  21.         Dimension();
  22.         Dimension(double x);
  23.         Dimension(double x, double y);
  24.        
  25.         void setValue(double x, double y);
  26.         void reset();
  27.        
  28.         Dimension unit();
  29.        
  30.         double length();
  31.         double distance(Dimension operend);
  32.         double dotProduct(Dimension operend);
  33.        
  34.         Dimension operator +(Dimension operend);
  35.         Dimension operator -(Dimension operend);
  36.         Dimension operator *(Dimension operend);
  37.         Dimension operator /(Dimension operend);
  38.         Dimension operator *(double operend);
  39.         Dimension operator /(double operend);
  40.        
  41.         bool operator ==(Dimension operend);
  42.         bool operator !=(Dimension operend);
  43.         bool operator >(Dimension operend);
  44.         bool operator <(Dimension operend);
  45.         bool operator >=(Dimension operend);
  46.         bool operator <=(Dimension operend);
  47. };
  48.  
  49. Dimension::Dimension()
  50. {
  51.     this->x = 0;
  52.     this->y = 0;
  53. }
  54.  
  55. Dimension::Dimension(double x)
  56. {
  57.     this->x = x;
  58.     this->y = x;
  59. }
  60.  
  61. Dimension::Dimension(double x, double y)
  62. {
  63.     this->x = x;
  64.     this->y = y;
  65. }
  66.  
  67. void Dimension::setValue(double x, double y)
  68. {
  69.     this->x = x;
  70.     this->y = y;
  71. }
  72.  
  73. void Dimension::reset()
  74. {
  75.     x = 0;
  76.     y = 0;
  77. }
  78.  
  79. Dimension Dimension::unit()
  80. {
  81.     return *this / length();
  82. }
  83.  
  84. double Dimension::length()
  85. {
  86.     return sqrt(pow(x, 2) + pow(y, 2));
  87. }
  88.  
  89. double Dimension::distance(Dimension operend)
  90. {
  91.     Dimension difference = operend - *this;
  92.     return difference.length();
  93. }
  94.  
  95. double Dimension::dotProduct(Dimension operend)
  96. {
  97.     return x * operend.x + y * operend.y;
  98. }
  99.  
  100. Dimension Dimension::operator +(Dimension operend)
  101. {
  102.     Dimension result = Dimension(x, y);
  103.     result.x += operend.x;
  104.     result.y += operend.y;
  105.     return result;
  106. }
  107.  
  108. Dimension Dimension::operator -(Dimension operend)
  109. {
  110.     Dimension result = Dimension(x, y);
  111.     result.x -= operend.x;
  112.     result.y -= operend.y;
  113.     return result;
  114. }
  115.  
  116. Dimension Dimension::operator *(Dimension operend)
  117. {
  118.     Dimension result = Dimension(x, y);
  119.     result.x *= operend.x;
  120.     result.y *= operend.y;
  121.     return result;
  122. }
  123.  
  124. Dimension Dimension::operator /(Dimension operend)
  125. {
  126.     Dimension result = Dimension(x, y);
  127.     result.x /= operend.x;
  128.     result.y /= operend.y;
  129.     return result;
  130. }
  131.  
  132. Dimension Dimension::operator *(double operend)
  133. {
  134.     Dimension result = Dimension(x, y);
  135.     result.x *= operend;
  136.     result.y *= operend;
  137.     return result;
  138. }
  139.  
  140. Dimension Dimension::operator /(double operend)
  141. {
  142.     Dimension result = Dimension(x, y);
  143.     result.x /= operend;
  144.     result.y /= operend;
  145.     return result;
  146. }
  147.  
  148. bool Dimension::operator ==(Dimension operend)
  149. {
  150.     return x == operend.x && y == operend.y;
  151. }
  152.  
  153. bool Dimension::operator !=(Dimension operend)
  154. {
  155.     return x != operend.x || y != operend.y;
  156. }
  157.  
  158. bool Dimension::operator >(Dimension operend)
  159. {
  160.     return x > operend.x && y > operend.y;
  161. }
  162.  
  163. bool Dimension::operator <(Dimension operend)
  164. {
  165.     return x < operend.x && y < operend.y;
  166. }
  167.  
  168. bool Dimension::operator >=(Dimension operend)
  169. {
  170.     return (x > operend.x && y > operend.y) || ((*this) == operend);
  171. }
  172.  
  173. bool Dimension::operator <=(Dimension operend)
  174. {
  175.     return (x < operend.x && y < operend.y) || ((*this) == operend);
  176. }
  177.  
  178. class Entity
  179. {
  180.     public:
  181.         Entity();
  182.         ~Entity();
  183.    
  184.         void update();
  185.         void render(SDL_Surface *surface);
  186.        
  187.         void keyDown(SDL_keysym key);
  188.         void keyUp(SDL_keysym key);
  189.         void mouseDown(unsigned short button, Dimension position);
  190.         void mouseUp(unsigned short button, Dimension position);
  191.         void mouseMotion(Dimension position, Dimension motion);
  192.        
  193.         Dimension getPosition();
  194.         Dimension getSize();
  195.        
  196.         unsigned long getBackgroundColor();
  197.        
  198.         void setParent(Entity *parent);
  199.        
  200.     protected:
  201.         long getTick();
  202.         long getFrame();
  203.        
  204.         void addEntity(Entity *entity);
  205.         void removeEntity(Entity *entity);
  206.         void removeEntity(int entity);
  207.         void clearEntities();
  208.        
  209.         void bringToFront(Entity *entity);
  210.         void bringToFront();
  211.        
  212.         void remove();
  213.        
  214.         void setFocused(bool focused);
  215.         bool isFocused();
  216.        
  217.         virtual void update2() = 0;
  218.         virtual void render2(SDL_Surface *surface) = 0;
  219.        
  220.         virtual void onKeyDown(SDL_keysym key) {}
  221.         virtual void onKeyUp(SDL_keysym key) {}
  222.         virtual void onMouseDown(unsigned short button, Dimension position) {}
  223.         virtual void onMouseUp(unsigned short button, Dimension position) {}
  224.         virtual void onMouseMotion(Dimension position, Dimension motion) {}
  225.         virtual void onClick(unsigned short button, Dimension position) {}
  226.         virtual void onDrag(unsigned short button, Dimension position, Dimension motion) {}
  227.         virtual void onFocus() {}
  228.         virtual void onUnfocus() {}
  229.        
  230.         Entity *getEntityInBounds(Dimension bounds);
  231.        
  232.         Dimension position;
  233.         Dimension size;
  234.        
  235.         unsigned long backgroundColor;
  236.        
  237.         Entity *getParent();
  238.        
  239.         vector<Entity *> contents;
  240.        
  241.         bool mouse;
  242.         unsigned short button;
  243.         Dimension mousePosition;
  244.        
  245.     private:
  246.         Entity *focus;
  247.        
  248.         bool focused;
  249.        
  250.         long tick;
  251.         long frame;
  252.        
  253.         Entity *parent;
  254. };
  255.  
  256. Entity::Entity()
  257. {
  258.     contents = vector<Entity *>();
  259.     focus = NULL;
  260.    
  261.     mouse = false;
  262.     button = 0;
  263.    
  264.     focused = false;
  265.    
  266.     tick = 0;
  267.     frame = 0;
  268.    
  269.     position = Dimension(0, 0);
  270.     size = Dimension(0, 0);
  271.    
  272.     backgroundColor = 0x000000;
  273.    
  274.     parent = NULL;
  275. }
  276.  
  277. Entity::~Entity()
  278. {
  279.     clearEntities();
  280. }
  281.  
  282. void Entity::update()
  283. {
  284.     update2();
  285.     tick++;
  286.    
  287.     vector<Entity *>::iterator it;
  288.     for(it = contents.begin(); it < contents.end(); it++)
  289.     {
  290.         (*it)->update();
  291.     }
  292. }
  293.  
  294. void Entity::render(SDL_Surface *surface)
  295. {
  296.     SDL_Surface *surface2 = SDL_CreateRGBSurface(SDL_HWSURFACE, size.x, size.y, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000);
  297.    
  298.     render2(surface2);
  299.     frame++;
  300.    
  301.     vector<Entity *>::iterator it;
  302.     for(it = contents.begin(); it < contents.end(); it++)
  303.     {
  304.         (*it)->render(surface2);
  305.     }
  306.    
  307.     SDL_Rect offset;
  308.     offset.x = position.x;
  309.     offset.y = position.y;
  310.    
  311.     SDL_BlitSurface(surface2, NULL, surface, &offset);
  312.     SDL_FreeSurface(surface2);
  313. }
  314.        
  315. void Entity::keyDown(SDL_keysym key)
  316. {
  317.     onKeyDown(key);
  318.    
  319.     if(focus != NULL)
  320.     {
  321.         focus->onKeyDown(key);
  322.     }
  323. }
  324.  
  325. void Entity::keyUp(SDL_keysym key)
  326. {
  327.     onKeyUp(key);
  328.    
  329.     if(focus != NULL)
  330.     {
  331.         focus->onKeyUp(key);
  332.     }
  333. }
  334.  
  335. void Entity::mouseDown(unsigned short button, Dimension position)
  336. {
  337.     mouse = true;
  338.     this->button = button;
  339.     mousePosition = position;
  340.     onMouseDown(button, position);
  341.    
  342.     Entity *oldFocus = focus;
  343.     focus = getEntityInBounds(position);   
  344.    
  345.     if(focus != oldFocus)
  346.     {
  347.         if(focus != NULL)
  348.         {
  349.             focus->setFocused(true);
  350.         }
  351.         if(oldFocus != NULL)
  352.         {
  353.             oldFocus->setFocused(false);
  354.         }
  355.     }
  356.    
  357.     if(focus != NULL)
  358.     {
  359.         focus->onMouseDown(button, position);
  360.     }
  361. }
  362.  
  363. void Entity::mouseUp(unsigned short button, Dimension position)
  364. {
  365.     mouse = false;
  366.     mousePosition = position;
  367.    
  368.     Entity *focus = getEntityInBounds(position);
  369.    
  370.     onMouseUp(button, position);
  371.     if(mouse)
  372.     {
  373.         onClick(button, position);
  374.    
  375.         if(focus != NULL && focus == this->focus)
  376.         {
  377.             focus->onClick(button, position);
  378.         }
  379.     }
  380.    
  381.     if(focus != NULL)
  382.     {
  383.         focus->onMouseUp(button, position);
  384.     }
  385. }
  386.  
  387. void Entity::mouseMotion(Dimension position, Dimension motion)
  388. {
  389.     mousePosition = position;
  390.    
  391.     onMouseMotion(position, motion);
  392.     if(mouse)
  393.     {
  394.         onDrag(button, position, motion);
  395.    
  396.         if(focus != NULL)
  397.         {
  398.             focus->onDrag(button, position, motion);
  399.         }
  400.     }
  401.    
  402.     Entity *focus = getEntityInBounds(position);
  403.    
  404.     if(focus != NULL)
  405.     {
  406.         focus->onMouseMotion(position, motion);
  407.     }
  408. }
  409.  
  410. Entity *Entity::getParent()
  411. {
  412.     return parent;
  413. }
  414.  
  415. void Entity::setParent(Entity *parent)
  416. {
  417.     this->parent = parent;
  418. }
  419.  
  420. Dimension Entity::getPosition()
  421. {
  422.     return position;
  423. }
  424.  
  425. Dimension Entity::getSize()
  426. {
  427.     return size;
  428. }
  429.  
  430. unsigned long Entity::getBackgroundColor()
  431. {
  432.     return backgroundColor;
  433. }
  434.  
  435. long Entity::getTick()
  436. {
  437.     return tick;
  438. }
  439.  
  440. long Entity::getFrame()
  441. {
  442.     return frame;
  443. }
  444.  
  445. void Entity::addEntity(Entity *entity)
  446. {
  447.     contents.push_back(entity);
  448.     entity->setParent(this);
  449. }
  450.  
  451. void Entity::removeEntity(Entity *entity)
  452. {
  453.     vector<Entity *>::iterator it;
  454.     for(it = contents.begin(); it < contents.end(); it++)
  455.     {
  456.         if(entity == *it)
  457.         {
  458.             contents.erase(it);
  459.             break;
  460.         }
  461.     }
  462. }
  463.  
  464. void Entity::removeEntity(int entity)
  465. {
  466.     contents.erase(contents.begin() + entity);
  467. }
  468.  
  469. void Entity::clearEntities()
  470. {
  471.     contents.clear();
  472. }
  473.  
  474. void Entity::bringToFront(Entity *entity)
  475. {
  476.     removeEntity(entity);
  477.     addEntity(entity);
  478. }
  479.  
  480. void Entity::bringToFront()
  481. {
  482.     if(parent != NULL)
  483.     {
  484.         parent->bringToFront(this);
  485.     }
  486. }
  487.  
  488. void Entity::remove()
  489. {
  490.     if(parent != NULL)
  491.     {
  492.         parent->removeEntity(this);
  493.     }
  494. }
  495.  
  496. void Entity::setFocused(bool focused)
  497. {
  498.     this->focused = focused;
  499.     if(focused)
  500.     {
  501.         onFocus();
  502.     }
  503.     else
  504.     {
  505.         onUnfocus();
  506.     }
  507. }
  508.  
  509. bool Entity::isFocused()
  510. {
  511.     return focused;
  512. }
  513.  
  514. Entity *Entity::getEntityInBounds(Dimension position)
  515. {
  516.     if(contents.size())
  517.     {
  518.         vector<Entity *>::iterator it;
  519.         for(it = contents.end() - 1; it >= contents.begin(); it--)
  520.         {
  521.             Entity *entity = *it;
  522.             if(position >= entity->getPosition() && position < entity->getPosition() + entity->getSize())
  523.             {
  524.                 return entity;
  525.             }
  526.         }
  527.     }
  528.    
  529.     return NULL;
  530. }
  531.  
  532. class PhysicalEntity: public Entity
  533. {
  534.     public:
  535.         PhysicalEntity();
  536.         ~PhysicalEntity();
  537.        
  538.         void applyForce(Dimension force);
  539.         virtual void collide(PhysicalEntity *entity) = 0;
  540.        
  541.         double getMass();
  542.         double getSpeed();
  543.         Dimension getVelocity();
  544.        
  545.     protected:
  546.         void update2();
  547.        
  548.         virtual void update3() {};
  549.         virtual void applyForces() {};
  550.        
  551.         Dimension velocity;
  552.         Dimension forces;
  553.        
  554.         double mass;
  555. };
  556.  
  557. PhysicalEntity::PhysicalEntity()
  558. {
  559.     velocity = Dimension(0, 0);
  560.     forces = Dimension(0, 0);
  561.    
  562.     mass = 1;
  563. }
  564.  
  565. PhysicalEntity::~PhysicalEntity()
  566. {
  567.    
  568. }
  569.  
  570. void PhysicalEntity::applyForce(Dimension force)
  571. {
  572.     forces = forces + force;
  573. }
  574.  
  575. double PhysicalEntity::getMass()
  576. {
  577.     return mass;
  578. }
  579.  
  580. double PhysicalEntity::getSpeed()
  581. {
  582.     return velocity.length();
  583. }
  584.  
  585. Dimension PhysicalEntity::getVelocity()
  586. {
  587.     return velocity;
  588. }
  589.  
  590. void PhysicalEntity::update2()
  591. {
  592.     update3();
  593.    
  594.     applyForces();
  595.    
  596.     velocity = velocity + forces / mass;
  597.     forces.reset();
  598.     position = position + velocity;
  599. }
  600.  
  601. class Point: public PhysicalEntity
  602. {
  603.     public:
  604.         Point(Dimension position);
  605.         ~Point();
  606.        
  607.         void collide(PhysicalEntity *entity);
  608.        
  609.     protected:
  610.         void render2(SDL_Surface *surface);
  611. };
  612.  
  613. Point::Point(Dimension position)
  614. {
  615.     this->position = position;
  616.     size = Dimension(2, 2);
  617. }
  618.  
  619. Point::~Point()
  620. {
  621.    
  622. }
  623.  
  624. void Point::render2(SDL_Surface *surface)
  625. {
  626.     SDL_FillRect(surface, NULL, 0xFFFFFF);
  627. }
  628.  
  629. void Point::collide(PhysicalEntity *entity)
  630. {
  631.     double distance = position.distance(entity->getPosition());
  632.     if(distance < 10)
  633.     {
  634.         Dimension unit = (entity->getPosition() - position).unit();
  635.         double magnitude = (10 - distance) * (mass * entity->getMass()) * 0.5 * 0.01;
  636.         entity->applyForce(unit * magnitude);
  637.         applyForce(unit * -magnitude);
  638.     }
  639. }
  640.  
  641. class PhysicalEnvironment: public Entity
  642. {
  643.     public:
  644.         PhysicalEnvironment();
  645.         ~PhysicalEnvironment();
  646.        
  647.     protected:
  648.         void update2();
  649.         void render2(SDL_Surface *surface);
  650.        
  651.         void applyGlobalForces(PhysicalEntity *entity);
  652.         void applyGlobalForce(Dimension force);
  653.         void applyRadialForce(Dimension force, Dimension position, double radius, bool attenuates);
  654.         void applyRadialForce(double force, Dimension position, double radius, bool attenuates);
  655.        
  656.         void onMouseDown(unsigned short button, Dimension position);
  657.         void onDrag(unsigned short button, Dimension position, Dimension motion);
  658.        
  659.         double gravity;
  660.         double airResistance;
  661.         double wallElasticity;
  662.        
  663.         int margin;
  664. };
  665.  
  666. PhysicalEnvironment::PhysicalEnvironment()
  667. {
  668.     size = Dimension(WIDTH, HEIGHT);
  669.    
  670.     gravity = 0.01;
  671.     airResistance = 0.005;
  672.     wallElasticity = 0.2;
  673.    
  674.     margin = 5;
  675. }
  676.  
  677. PhysicalEnvironment::~PhysicalEnvironment()
  678. {
  679.    
  680. }
  681.  
  682. void PhysicalEnvironment::update2()
  683. {
  684.     if(mouse && button == 3)
  685.     {
  686.         applyRadialForce(50, mousePosition, 100, true);
  687.     }
  688.    
  689.     vector<Entity *>::iterator it;
  690.     for(it = contents.begin(); it < contents.end(); it++)
  691.     {
  692.         PhysicalEntity *entity = (PhysicalEntity *)(*it);
  693.         applyGlobalForces(entity);
  694.        
  695.         vector<Entity *>::iterator it2;
  696.         for(it2 = it + 1; it2 < contents.end(); it2++)
  697.         {
  698.             PhysicalEntity *entity2 = (PhysicalEntity *)(*it2);
  699.             entity->collide(entity2);
  700.         }
  701.     }
  702. }
  703.  
  704. void PhysicalEnvironment::render2(SDL_Surface *surface)
  705. {
  706.    
  707. }
  708.  
  709. void PhysicalEnvironment::applyGlobalForces(PhysicalEntity *entity)
  710. {
  711.     entity->applyForce(Dimension(0, gravity * entity->getMass()));
  712.     entity->applyForce(entity->getVelocity() * -airResistance);
  713.    
  714.     if(entity->getPosition().x < margin)
  715.     {
  716.         entity->applyForce(Dimension((margin - entity->getPosition().x) * wallElasticity, 0));
  717.     }
  718.     else if(entity->getPosition().y < margin)
  719.     {
  720.         entity->applyForce(Dimension(0, (margin - entity->getPosition().y) * wallElasticity));
  721.     }
  722.     else if(entity->getPosition().x >= size.x - margin)
  723.     {
  724.         entity->applyForce(Dimension((size.x - margin - entity->getPosition().x) * wallElasticity, 0));
  725.     }
  726.     else if(entity->getPosition().y >= size.y - margin)
  727.     {
  728.         entity->applyForce(Dimension(0, (size.y - margin - entity->getPosition().y) * wallElasticity));
  729.     }
  730. }
  731.  
  732. void PhysicalEnvironment::applyGlobalForce(Dimension force)
  733. {
  734.     vector<Entity *>::iterator it;
  735.     for(it = contents.begin(); it < contents.end(); it++)
  736.     {
  737.         PhysicalEntity *entity = (PhysicalEntity *)(*it);
  738.         entity->applyForce(force);
  739.     }
  740. }
  741.  
  742. void PhysicalEnvironment::applyRadialForce(Dimension force, Dimension position, double radius, bool attenuates)
  743. {
  744.     vector<Entity *>::iterator it;
  745.     for(it = contents.begin(); it < contents.end(); it++)
  746.     {
  747.         PhysicalEntity *entity = (PhysicalEntity *)(*it);
  748.         double distance = position.distance(entity->getPosition());
  749.         if(distance <= radius)
  750.         {
  751.             if(attenuates)
  752.             {
  753.                 force = force / pow(distance, 2);
  754.             }
  755.             entity->applyForce(force);
  756.         }
  757.     }
  758. }
  759.  
  760. void PhysicalEnvironment::applyRadialForce(double force, Dimension position, double radius, bool attenuates)
  761. {
  762.     vector<Entity *>::iterator it;
  763.     for(it = contents.begin(); it < contents.end(); it++)
  764.     {
  765.         PhysicalEntity *entity = (PhysicalEntity *)(*it);
  766.         double distance = position.distance(entity->getPosition());
  767.         if(distance <= radius)
  768.         {
  769.             if(attenuates)
  770.             {
  771.                 force = force / pow(distance, 2);
  772.             }
  773.             entity->applyForce((entity->getPosition() - position).unit() * force);
  774.         }
  775.     }
  776. }
  777.  
  778. void PhysicalEnvironment::onMouseDown(unsigned short button, Dimension position)
  779. {
  780.     if(button == 1)
  781.     {
  782.         addEntity(new Point(position));
  783.     }
  784. }
  785.  
  786. void PhysicalEnvironment::onDrag(unsigned short button, Dimension position, Dimension motion)
  787. {
  788.     if(button == 1)
  789.     {
  790.         addEntity(new Point(position));
  791.     }
  792. }
  793.  
  794. class Engine
  795. {
  796.     public:
  797.         static Engine *engine;
  798.        
  799.         Engine();
  800.         ~Engine();
  801.        
  802.         void start();
  803.         void stop();
  804.        
  805.         void setContent(Entity *content);
  806.    
  807.     private:
  808.         bool running;
  809.        
  810.         long tick;
  811.         long frame;
  812.        
  813.         Entity *content;
  814.        
  815.         SDL_Surface *surface;
  816.        
  817.         void events();
  818.         void update();
  819.         void render();
  820. };
  821.    
  822. Engine *Engine::engine;
  823.  
  824. Engine::Engine()
  825. {
  826.     engine = this;
  827.    
  828.     running = false;
  829.    
  830.     tick = 0;
  831.     frame = 0;
  832.    
  833.     content = NULL;
  834.    
  835.     SDL_Init(SDL_INIT_EVERYTHING);
  836.     SDL_WM_SetCaption("Game Engine", 0);
  837.     surface = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  838. }
  839.  
  840. Engine::~Engine()
  841. {
  842.     stop();
  843.     SDL_Quit();
  844. }
  845.  
  846. void Engine::start()
  847. {
  848.     if(!running)
  849.     {
  850.         running = true;
  851.         while(running)
  852.         {
  853.             int now = SDL_GetTicks();
  854.            
  855.             events();
  856.             for(int i = 0; i < SPF; i++)
  857.             {
  858.                 update();
  859.                 tick++;
  860.             }
  861.             render();
  862.             frame++;
  863.            
  864.             int passed = SDL_GetTicks() - now;
  865.             int wait = 1000 / FPS;
  866.             int left = wait - passed;
  867.            
  868.             SDL_Delay(max(left, 0));
  869.         }
  870.     }
  871. }
  872.  
  873. void Engine::stop()
  874. {
  875.     running = false;
  876. }
  877.  
  878. void Engine::setContent(Entity *content)
  879. {
  880.     this->content = content;
  881. }
  882.  
  883. void Engine::events()
  884. {
  885.     if(content != NULL)
  886.     {
  887.         SDL_Event event;
  888.         while(SDL_PollEvent(&event))
  889.         {
  890.             switch(event.type)
  891.             {
  892.                 case SDL_KEYDOWN:
  893.                     content->keyDown(event.key.keysym);
  894.                     break;
  895.                    
  896.                 case SDL_KEYUP:
  897.                     content->keyUp(event.key.keysym);
  898.                     break;
  899.                    
  900.                 case SDL_MOUSEBUTTONDOWN:
  901.                     content->mouseDown(event.button.button, Dimension(event.button.x, event.button.y));
  902.                     break;
  903.                    
  904.                 case SDL_MOUSEBUTTONUP:
  905.                     content->mouseUp(event.button.button, Dimension(event.button.x, event.button.y));
  906.                     break;
  907.                    
  908.                 case SDL_MOUSEMOTION:
  909.                     content->mouseMotion(Dimension(event.motion.x, event.motion.y), Dimension(event.motion.xrel, event.motion.yrel));
  910.                     break;
  911.                    
  912.                 case SDL_QUIT:
  913.                     stop();
  914.                     break;
  915.             }
  916.         }
  917.     }
  918. }
  919.  
  920. void Engine::update()
  921. {
  922.     if(content != NULL)
  923.     {
  924.         content->update();
  925.     }
  926. }
  927.  
  928. void Engine::render()
  929. {
  930.     if(content != NULL)
  931.     {
  932.         content->render(surface);
  933.         SDL_Flip(surface);
  934.     }
  935. }
  936.  
  937. int main()
  938. {
  939.     Engine engine;
  940.    
  941.     engine.setContent(new PhysicalEnvironment());
  942.     engine.start();
  943.    
  944.     return 0;
  945. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement