1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
//.h file
#include "Game.h"
using namespace sf;
class Object {
public:
//NullObject to return when GetObjectFromID fails.
static Object NullObject;
//Max amount of objects allowed on screen
static Object ActiveObjects[10];
//Returns a pointer to an Object class from its ID
static Object * GetObjectFromID(int);
//Attempts to create new object if space is allowed.
static int CreateNewObject(Texture, Vector2f);
Texture O_Texture;
Sprite O_Sprite;
int ID;
Vector2f Position;
//Constructor defaults ID to -1, which allows me to check if an object should be drawn and calculations preformed on it. I should've probably just used Dynamic Memory. Oh well.
Object() : ID(-1), O_Sprite(Texture()), Position(Vector2f(0,0)){
}
};
//.cpp file
Object Object::NullObject = Object();
Object * Object::GetObjectFromID(int IDQuery) {
for (int index = 0; index < 10; index++) {
if (ActiveObjects[index].ID == IDQuery) {
return &ActiveObjects[index];
}
}
return &Object::NullObject;
}
int Object::CreateNewObject(Texture Texture, Vector2f Position) {
for (int index = 0; 0 < 10; index++) {
if (ActiveObjects[index].ID == -1) {
ActiveObjects[index].ID = index;
ActiveObjects[index].Position = Position;
ActiveObjects[index].O_Texture = Texture;
ActiveObjects[index].O_Sprite.setTexture(ActiveObjects[index].O_Texture);
std::cout << "\nInstantiating new object with ID of " << index << " at pixel coordinates (" << Position.x << "," << Position.y << ")";
return index;
}
}
std::cout << "\nCouldn't create object, scene most likely full";
return -1;
}
Object Object::ActiveObjects[10];
void Initialize() {
//Throw in object declarations and stuff here
std::cout << "Physics game! -- Coded by Eric Pfister.\n___________________________________________________";
//Debug Code
//Loads Ball texture to pass to CreateNewObject()
Texture Ball = Texture();
Ball.loadFromFile("Ball.png");
//Debugging code to check size of texture, to make sure it was loaded correctly.
std::cout << "\n" << Ball.getSize().x << "," << Ball.getSize().y;
//Creates new object and returns its ID, gets object from its ID, then makes a pointer to it for debugging purposes.
Object *Pointer = Object::GetObjectFromID(Object::CreateNewObject(Ball,Vector2f(100,100)));
//Checks texture size to make sure that texture was stored correctly in O_Sprite
std::cout << "\n" << Object::ActiveObjects[0].O_Sprite.getTexture()->getSize().x;
//End of Debug code
}
void Draw() {
//Put drawing code in here
//Clears the window for drawing.
Game::_mainWindow.clear(Color(255, 255, 255));
//Goes through Object::ActiveObjects and checks if any object is to be drawn (ID isn't -1) then draws it.
for (int index = 0; index < 10; index++) {
if (Object::ActiveObjects[index].ID != -1) {
//This is the problem, upon running the program nothing gets drawn.
Game::_mainWindow.draw(Object::ActiveObjects[index].O_Sprite);
}
}
//Debug code, this works and draws a ball.
Texture Ball = Texture();
Ball.loadFromFile("Ball.png");
Game::_mainWindow.draw(Sprite(Ball);
Game::_mainWindow.display();
}
|