STL- . , . Int. STL- , :
1) vector<int*>: , int* .
2) , , , - .
: - , , , , .
#include "stdafx.h"
#include <iostream>
#include <exception>
#include <list>
#include <conio.h>
using namespace std;
class Drawable
{
public:
virtual void Draw() = 0;
virtual ~Drawable() {};
};
class Movable
{
public:
virtual void Update(int deltaTMsec) = 0;
virtual ~Movable() {};
};
class Fish: public Drawable, public Movable
{
private:
int m_x;
int m_y;
static const int Speed = 600;
public:
Fish(int x, int y): m_x(x), m_y(y)
{
}
virtual void Update(int deltaTMsec)
{
cout << "Moving fish!\n";
m_x += deltaTMsec*Speed;
m_y += deltaTMsec*Speed;;
}
virtual void Draw()
{
cout << "Redrawing fish!\n";
}
};
class Hero: public Drawable, public Movable
{
private:
int m_x;
int m_y;
static const int Speed = 13;
public:
Hero(int x, int y): m_x(x), m_y(y)
{
}
virtual void Update(int deltaTMsec)
{
cout << "Moving hero!\n";
m_x += deltaTMsec*Speed;
m_y += deltaTMsec*Speed;;
}
virtual void Draw()
{
cout << "Redrawing hero!\n";
}
};
class Wall: public Drawable
{
private:
int m_x;
int m_y;
public:
Wall(int x, int y): m_x(x), m_y(y)
{
}
virtual void Draw()
{
cout << "Redrawing wall!\n";
}
};
int _tmain(int argc, _TCHAR* argv[])
{
auto fish = new Fish(13, 20);
auto mainHero = new Hero(13, 13);
auto wall = new Wall(33, 33);
list<Drawable*> drawableItems = { fish, mainHero, wall };
list<Movable*> movableItems = { fish, mainHero };
list<void*> allGameObjects = { fish, mainHero, wall };
// the main game circle
while (!_kbhit()) // not the end of the world!
{
for (Drawable* drawable: drawableItems)
drawable->Draw();
for (Movable* movable: movableItems)
movable->Update(13);
}
// clean memory
for (auto gameObjectIterator = allGameObjects.begin();
gameObjectIterator!= allGameObjects.end();)
{
delete *gameObjectIterator;
gameObjectIterator = allGameObjects.erase(gameObjectIterator);
}
drawableItems.clear();
movableItems.clear();
return 0;
|
|
}
, . , : , , , , : : , . .
: , , , .