Initial Commit with small working example.
This commit is contained in:
commit
b0e17c96d3
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
a.out
|
||||
*~
|
28
Ball.h
Normal file
28
Ball.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef BALL_H
|
||||
#define BALL_H
|
||||
|
||||
#include "GraphicsObject.h"
|
||||
//#include "PhysicsObject.h"
|
||||
|
||||
class Ball : public GraphicsObject
|
||||
{
|
||||
public:
|
||||
Ball() : GraphicsObject(&Ball::SetX, this) // pointer to self (specific instance)
|
||||
{
|
||||
instance_num = ++instance_counter;
|
||||
}
|
||||
static void SetX(int &x, void *instance)
|
||||
{
|
||||
Ball *ball = static_cast<Ball*>(instance);
|
||||
ball->x = ball->instance_num; // prove we're accessing the right one
|
||||
return;
|
||||
}
|
||||
|
||||
private:
|
||||
static int instance_counter;
|
||||
int instance_num;
|
||||
};
|
||||
|
||||
int Ball::instance_counter = 0;
|
||||
|
||||
#endif
|
35
GraphicsObject.h
Normal file
35
GraphicsObject.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef GRAPHICSOBJECT_H
|
||||
#define GRAPHICSOBJECT_H
|
||||
|
||||
class GraphicsObject
|
||||
{
|
||||
public:
|
||||
// 2nd arg: pointer to callback function that takes a reference to an int as an argument
|
||||
GraphicsObject(int set_x)
|
||||
{
|
||||
x = set_x;
|
||||
user_data = NULL;
|
||||
callback = NULL; // function pointer
|
||||
}
|
||||
GraphicsObject(void (*set_callback)(int&, void*), void *set_user_data)
|
||||
{
|
||||
x = 0;
|
||||
user_data = set_user_data;
|
||||
callback = set_callback; // function pointer
|
||||
}
|
||||
int X()
|
||||
{
|
||||
// If you are SUPPOSED to callback, ...
|
||||
if(callback)
|
||||
// ... do so to ask for the new X value
|
||||
callback(x, user_data);
|
||||
return x; // either this was just set by callback, or it was always set by constructor.
|
||||
}
|
||||
//private:
|
||||
int x;
|
||||
void *user_data;
|
||||
private:
|
||||
void (*callback)(int&, void*);
|
||||
};
|
||||
|
||||
#endif
|
20
GraphicsSystem.h
Normal file
20
GraphicsSystem.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef GRAPHICSSYSTEM_H
|
||||
#define GRAPHICSSYSTEM_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "GraphicsObject.h"
|
||||
|
||||
class GraphicsSystem
|
||||
{
|
||||
public:
|
||||
void AddObject(GraphicsObject *obj)
|
||||
{
|
||||
objects.push_back(obj);
|
||||
}
|
||||
|
||||
//private:
|
||||
std::vector<GraphicsObject*> objects;
|
||||
};
|
||||
|
||||
#endif
|
16
PhysicsObject.h
Normal file
16
PhysicsObject.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef PHYSICSOBJECT_H
|
||||
#define PHYSICSOBJECT_H
|
||||
|
||||
#include "IPhysics.h"
|
||||
|
||||
class PhysicsObject
|
||||
: public IPhysics
|
||||
{
|
||||
public:
|
||||
PhysicsObject() : IPhysics(&x) {}
|
||||
int GetX() { Update(); return x; }
|
||||
private:
|
||||
int x;
|
||||
};
|
||||
|
||||
#endif
|
22
main.cpp
Normal file
22
main.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "GraphicsSystem.h"
|
||||
|
||||
#include "GraphicsObject.h"
|
||||
#include "Ball.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
GraphicsSystem system;
|
||||
|
||||
GraphicsObject graphics_obj(7);
|
||||
system.AddObject(&graphics_obj);
|
||||
Ball ball1;
|
||||
system.AddObject(&ball1);
|
||||
Ball ball2;
|
||||
system.AddObject(&ball2);
|
||||
|
||||
for(auto obj : system.objects)
|
||||
for(auto i = 0; i < 5; ++i)
|
||||
std::cout << obj->X() << std::endl;
|
||||
}
|
Loading…
Reference in New Issue
Block a user