From b0e17c96d3815198689597eadbda1bd938e9c472 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Tue, 23 May 2017 16:40:02 -0400 Subject: [PATCH] Initial Commit with small working example. --- .gitignore | 2 ++ Ball.h | 28 ++++++++++++++++++++++++++++ GraphicsObject.h | 35 +++++++++++++++++++++++++++++++++++ GraphicsSystem.h | 20 ++++++++++++++++++++ PhysicsObject.h | 16 ++++++++++++++++ build | 1 + main.cpp | 22 ++++++++++++++++++++++ 7 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 Ball.h create mode 100644 GraphicsObject.h create mode 100644 GraphicsSystem.h create mode 100644 PhysicsObject.h create mode 100755 build create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c7cc22b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +a.out +*~ diff --git a/Ball.h b/Ball.h new file mode 100644 index 0000000..520bae2 --- /dev/null +++ b/Ball.h @@ -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(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 diff --git a/GraphicsObject.h b/GraphicsObject.h new file mode 100644 index 0000000..8a2476e --- /dev/null +++ b/GraphicsObject.h @@ -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 diff --git a/GraphicsSystem.h b/GraphicsSystem.h new file mode 100644 index 0000000..83ea55f --- /dev/null +++ b/GraphicsSystem.h @@ -0,0 +1,20 @@ +#ifndef GRAPHICSSYSTEM_H +#define GRAPHICSSYSTEM_H + +#include + +#include "GraphicsObject.h" + +class GraphicsSystem +{ +public: + void AddObject(GraphicsObject *obj) + { + objects.push_back(obj); + } + +//private: + std::vector objects; +}; + +#endif diff --git a/PhysicsObject.h b/PhysicsObject.h new file mode 100644 index 0000000..e26c7bb --- /dev/null +++ b/PhysicsObject.h @@ -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 diff --git a/build b/build new file mode 100755 index 0000000..f14ceca --- /dev/null +++ b/build @@ -0,0 +1 @@ +clang++ main.cpp --std=c++11 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..868523f --- /dev/null +++ b/main.cpp @@ -0,0 +1,22 @@ +#include + +#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; +}