diff --git a/README.md b/README.md index e25d470..11c35fe 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ An organized (divided by classes separated per files) cross-platform (Windows, L - R - reset box position. - WASD or Arrows - move camera. - Q/E - adjust zoom. -- F - go/exit fullscreen. +- F - go/exit fullscreen (may not work properly on Linux). ### For PS Vita - Cross - reset box position. diff --git a/src/BoxEntity.cpp b/src/BoxEntity.cpp index f106043..57c4cf8 100644 --- a/src/BoxEntity.cpp +++ b/src/BoxEntity.cpp @@ -1,15 +1,14 @@ #include "BoxEntity.h" -BoxEntity::BoxEntity(b2World* world, SDL_Renderer* renderer, const char* path_to_texture, float x_box, float y_box, float w_box, float h_box, float angle_box) +BoxEntity::BoxEntity(const char* path_to_texture, float x_box, float y_box, float w_box, float h_box, float angle_box) { - BoxEntity::world = world; BoxEntity::x = x_box + (w_box / 2); BoxEntity::y = y_box + (h_box / 2); BoxEntity::w = w_box; BoxEntity::h = h_box; BoxEntity::angle = angle_box; - BoxEntity::LoadTexture(renderer, path_to_texture); + BoxEntity::pathToTexture = path_to_texture; BoxEntity::bodyDef.type = b2_dynamicBody; BoxEntity::bodyDef.angle = BoxEntity::angle; @@ -17,16 +16,21 @@ BoxEntity::BoxEntity(b2World* world, SDL_Renderer* renderer, const char* path_to BoxEntity::vel.Set(0, 0.2f); - BoxEntity::body = BoxEntity::world->CreateBody(&bodyDef); - BoxEntity::body->SetLinearVelocity(BoxEntity::vel); - BoxEntity::boxShape.SetAsBox(BoxEntity::w / 2.0f, BoxEntity::h / 2.0f); BoxEntity::fixtureDef.shape = &(BoxEntity::boxShape); BoxEntity::fixtureDef.density = 1; BoxEntity::fixtureDef.friction = 0.3f; BoxEntity::fixtureDef.restitution = 0.5f; +} + +void BoxEntity::Register(b2World* world, SDL_Renderer* renderer) +{ + BoxEntity::body = world->CreateBody(&(BoxEntity::bodyDef)); + BoxEntity::body->SetLinearVelocity(BoxEntity::vel); BoxEntity::body->CreateFixture(&(BoxEntity::fixtureDef)); + + BoxEntity::LoadTexture(renderer); } void BoxEntity::Reset() diff --git a/src/BoxEntity.h b/src/BoxEntity.h index 2fb4d39..17eb925 100644 --- a/src/BoxEntity.h +++ b/src/BoxEntity.h @@ -16,16 +16,20 @@ class BoxEntity: public PhysicsObj public: /// @brief Create a new box entity. - /// @param world the world where box exists. - /// @param renderer the rendering context. /// @param path_to_texture path to the image file of texture. /// @param x_box position X of upper left corner of the box in Box2D meters. /// @param y_box position Y of upper left corner of the box in Box2D meters. /// @param w_box width of the box in Box2D meters. /// @param h_box height of the box in Box2D meters. /// @param angle_box angle of the box in radians. - BoxEntity(b2World* world, SDL_Renderer* renderer, const char* path_to_texture, float x_box, float y_box, float w_box, float h_box, float angle_box); + BoxEntity(const char* path_to_texture, float x_box, float y_box, float w_box, float h_box, float angle_box); + /// Register this box in the world and set its texture. Should be + /// called only when no world calculations are performing. + /// @param world link to the world where box should be registered. + /// @param renderer the rendering context. + void Register(b2World* world, SDL_Renderer* renderer); + /// @brief Set default position and angle of the box. void Reset(); diff --git a/src/FontManager.h b/src/FontManager.h index f1c449e..86f6175 100644 --- a/src/FontManager.h +++ b/src/FontManager.h @@ -2,3 +2,7 @@ #include +/// REALIZE +/// ME +/// ONE DAY +/// PLEAAAAAAASE diff --git a/src/PhysicsObj.cpp b/src/PhysicsObj.cpp index e2664ff..32091f2 100644 --- a/src/PhysicsObj.cpp +++ b/src/PhysicsObj.cpp @@ -5,9 +5,9 @@ PhysicsObj::~PhysicsObj() SDL_DestroyTexture(PhysicsObj::texture); } -void PhysicsObj::LoadTexture(SDL_Renderer* renderer, const char* path_to_texture) +void PhysicsObj::LoadTexture(SDL_Renderer* renderer) { - PhysicsObj::texture = SDL_CreateTextureFromSurface(renderer, IMG_Load(path_to_texture)); + PhysicsObj::texture = SDL_CreateTextureFromSurface(renderer, IMG_Load(PhysicsObj::pathToTexture)); } b2Body* PhysicsObj::getBody() diff --git a/src/PhysicsObj.h b/src/PhysicsObj.h index a05f315..9c41f77 100644 --- a/src/PhysicsObj.h +++ b/src/PhysicsObj.h @@ -33,13 +33,21 @@ class PhysicsObj /// @brief Fixture definition of the physics object. b2FixtureDef fixtureDef; + /// @brief Path to the image file of texture. + const char* pathToTexture; + public: ~PhysicsObj(); + + /// Register this ph. obj. in the world and set its texture. Should be + /// called only when no world calculations are performing. + /// @param world link to the world where ph. obj. should be registered. + /// @param renderer the rendering context. + virtual void Register(b2World* world, SDL_Renderer* renderer) = 0; /// @brief Load a texture for physics object. - /// @param renderer the rendering context, necessary in order to set texture. - /// @param path_to_texture path to the image file of texture. - void LoadTexture(SDL_Renderer* renderer, const char* path_to_texture); + /// @param renderer the rendering context. + void LoadTexture(SDL_Renderer* renderer); b2Body* getBody(); diff --git a/src/PlatformEntity.cpp b/src/PlatformEntity.cpp index 495f0d0..eb44cca 100644 --- a/src/PlatformEntity.cpp +++ b/src/PlatformEntity.cpp @@ -1,6 +1,6 @@ #include "PlatformEntity.h" -PlatformEntity::PlatformEntity(b2World* world, float x1_plat, float y1_plat, float x2_plat, float y2_plat) +PlatformEntity::PlatformEntity(float x1_plat, float y1_plat, float x2_plat, float y2_plat) { PlatformEntity::world = world; PlatformEntity::x1 = x1_plat; @@ -10,12 +10,15 @@ PlatformEntity::PlatformEntity(b2World* world, float x1_plat, float y1_plat, flo b2Vec2 start_point = b2Vec2(PlatformEntity::x1, PlatformEntity::y1); b2Vec2 end_point = b2Vec2(PlatformEntity::x2, PlatformEntity::y2); - - PlatformEntity::body = PlatformEntity::world->CreateBody(&bodyDef); PlatformEntity::platformShape.SetTwoSided(start_point, end_point); PlatformEntity::fixtureDef.shape = &(PlatformEntity::platformShape); +} + +void PlatformEntity::Register(b2World* world, SDL_Renderer* renderer) +{ + PlatformEntity::body = world->CreateBody(&(PlatformEntity::bodyDef)); PlatformEntity::body->CreateFixture(&(PlatformEntity::fixtureDef)); } diff --git a/src/PlatformEntity.h b/src/PlatformEntity.h index aa0ba26..76b5cee 100644 --- a/src/PlatformEntity.h +++ b/src/PlatformEntity.h @@ -15,12 +15,17 @@ class PlatformEntity: public PhysicsObj public: /// @brief Create a new platform entity. - /// @param world the world where platform exists. /// @param x1_plat position X of the start point of the platform. /// @param y1_plat position Y of the start point of the platform. /// @param x2_plat position X of the end point of the platform. /// @param y2_plat position Y of the end point of the platform. - PlatformEntity(b2World* world, float x1_plat, float y1_plat, float x2_plat, float y2_plat); + PlatformEntity(float x1_plat, float y1_plat, float x2_plat, float y2_plat); + + /// Register this platform in the world and set its texture. Should be + /// called only when no world calculations are performing. + /// @param world link to the world where platform should be registered. + /// @param renderer the rendering context. + void Register(b2World* world, SDL_Renderer* renderer = nullptr); void Reset(); diff --git a/src/WorldManager.cpp b/src/WorldManager.cpp index a16bb30..f916629 100644 --- a/src/WorldManager.cpp +++ b/src/WorldManager.cpp @@ -1,10 +1,10 @@ #include "WorldManager.h" #include "Controls.h" -WorldManager::WorldManager(int SCREEN_WIDTH, int SCREEN_HEIGHT, bool fpsCorrection, float move_speed, float zoom_speed) +WorldManager::WorldManager(int WINDOW_WIDTH, int WINDOW_HEIGHT, bool fpsCorrection, float move_speed, float zoom_speed) { - WorldManager::SCREEN_WIDTH = SCREEN_WIDTH; - WorldManager::SCREEN_HEIGHT = SCREEN_HEIGHT; + WorldManager::WINDOW_WIDTH = WINDOW_WIDTH; + WorldManager::WINDOW_HEIGHT = WINDOW_HEIGHT; WorldManager::speedCorrection = fpsCorrection; @@ -30,26 +30,26 @@ void WorldManager::initVideo() { SDL_Init(SDL_INIT_EVERYTHING); - if (WorldManager::SCREEN_WIDTH == 0 || WorldManager::SCREEN_HEIGHT == 0) + if (WorldManager::WINDOW_WIDTH == 0 || WorldManager::WINDOW_HEIGHT == 0) { SDL_DisplayMode dm; SDL_GetCurrentDisplayMode(0, &dm); - WorldManager::SCREEN_WIDTH = dm.w / 1.5; - WorldManager::SCREEN_HEIGHT = dm.h / 1.5; + WorldManager::WINDOW_WIDTH = dm.w / 1.5; + WorldManager::WINDOW_HEIGHT = dm.h / 1.5; } WorldManager::window = SDL_CreateWindow("Box2D", SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, WorldManager::SCREEN_WIDTH, WorldManager::SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + SDL_WINDOWPOS_CENTERED, WorldManager::WINDOW_WIDTH, WorldManager::WINDOW_HEIGHT, SDL_WINDOW_SHOWN); WorldManager::renderer = SDL_CreateRenderer(WorldManager::window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - SDL_RenderSetLogicalSize(WorldManager::renderer, WorldManager::SCREEN_WIDTH, WorldManager::SCREEN_HEIGHT); + SDL_RenderSetLogicalSize(WorldManager::renderer, WorldManager::WINDOW_WIDTH, WorldManager::WINDOW_HEIGHT); } void WorldManager::addObject(PhysicsObj* obj) { - WorldManager::objects.push_back(obj); + WorldManager::order.push_back(obj); } void WorldManager::deleteObject(int index) { @@ -59,6 +59,13 @@ void WorldManager::deleteObject(int index) bool WorldManager::Step() { + for (int i = WorldManager::order.size() - 1; i >= 0; i--) + { + WorldManager::objects.push_back(WorldManager::order[i]); + WorldManager::objects[WorldManager::objects.size() - 1]->Register(WorldManager::world, WorldManager::renderer); + WorldManager::order.pop_back(); + } + if (WorldManager::speedCorrection) WorldManager::b = WorldManager::a; diff --git a/src/WorldManager.h b/src/WorldManager.h index 41178ba..bae1327 100644 --- a/src/WorldManager.h +++ b/src/WorldManager.h @@ -9,11 +9,21 @@ class WorldManager { private: + /// @brief Main Box2D world. + b2World* world; + + /// @brief The rendering context. + SDL_Renderer* renderer; + /// Array of physics objects of the world. PhysicsObj is an abstract class and this array /// should only contain realizations of it (for example, BoxEntity). std::vector objects; - int SCREEN_WIDTH, SCREEN_HEIGHT; + /// Array of "ordered" physics objects that should be added to main ph. obj-s array when + /// no world calculations are performing. + std::vector order; + + int WINDOW_WIDTH, WINDOW_HEIGHT; /// @brief Is WorldManager should adjust speed/FPS? Should be used only when there's need in this. bool speedCorrection; @@ -33,12 +43,12 @@ class WorldManager public: /// @brief Init video, Box2D world and create WorldManager instance. - /// @param SCREEN_WIDTH resolution of the window width in pixels. - /// @param SCREEN_HEIGHT resolution of the window height in pixels. + /// @param WINDOW_WIDTH resolution of the window width in pixels. If 0, width would be adjusted automatically. + /// @param WINDOW_HEIGHT resolution of the window height in pixels. If 0, height would be adjusted automatically. /// @param fpsCorrection is WorldManager should adjust speed/FPS? Should be used only when there's need in this. /// @param move_speed amount of pixels added to camera offset variable in one frame when pressed relevant button. /// @param zoom_speed amount of coefficiency added to camera zoom variable in one frame when pressed relevant button. - WorldManager(int SCREEN_WIDTH, int SCREEN_HEIGHT, bool fpsCorrection = false, float move_speed = 10, float zoom_speed = 1); + WorldManager(int WINDOW_WIDTH, int WINDOW_HEIGHT, bool fpsCorrection = false, float move_speed = 10, float zoom_speed = 1); ~WorldManager(); /// @brief Add physics object realization into the PhysicsObj array. @@ -58,10 +68,4 @@ class WorldManager /// @brief Run main cycle of the program, exit only by initiative of user. void Cycle(); - - /// @brief Main Box2D world. - b2World* world; - - /// @brief The rendering context. - SDL_Renderer* renderer; }; diff --git a/src/main.cpp b/src/main.cpp index 7fa63ad..c4d45be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,8 @@ #include "PlatformEntity.h" #include "WorldManager.h" +#include + #ifdef Linux const bool isLinux = true; #else @@ -10,23 +12,23 @@ const bool isLinux = false; #endif #ifdef Vita -int SCREEN_WIDTH = 960; -int SCREEN_HEIGHT = 544; +int WINDOW_WIDTH = 960; +int WINDOW_HEIGHT = 544; #else -int SCREEN_WIDTH = 0; -int SCREEN_HEIGHT = 0; +int WINDOW_WIDTH = 0; +int WINDOW_HEIGHT = 0; #endif int main(int argv, char** args) { - WorldManager* wm = new WorldManager(SCREEN_WIDTH, SCREEN_HEIGHT, isLinux); + WorldManager* wm = new WorldManager(WINDOW_WIDTH, WINDOW_HEIGHT, isLinux); - wm->addObject(new BoxEntity(wm->world, wm->renderer, "assets/img/box.png", 0, 0, 2, 2, 0)); - wm->addObject(new BoxEntity(wm->world, wm->renderer, "assets/img/box.png", 2, 0, 2, 1, 45.0f)); - wm->addObject(new BoxEntity(wm->world, wm->renderer, "assets/img/box.png", 5, 0, 0.25, 1.5, 45.0f)); - wm->addObject(new BoxEntity(wm->world, wm->renderer, "assets/img/box.png", 5.5, 0, 0.25, 0.25, 45.0f)); + wm->addObject(new PlatformEntity(0, 6, 8, 6)); - wm->addObject(new PlatformEntity(wm->world, 0, 6, 8, 6)); + wm->addObject(new BoxEntity("assets/img/box.png", 0, 0, 2, 2, 0)); + wm->addObject(new BoxEntity("assets/img/box.png", 2, 0, 2, 1, 45.0f)); + wm->addObject(new BoxEntity("assets/img/box.png", 5, 0, 0.25, 1.5, 45.0f)); + wm->addObject(new BoxEntity("assets/img/box.png", 5.5, 0, 0.25, 0.25, 45.0f)); wm->Cycle();