Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
Make WorldManager::addObject() logic more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
hammerill committed Oct 10, 2022
1 parent e942313 commit 3e1ba24
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ An organized (divided by classes separated per files) cross-platform (Windows, L
- <kbd>R</kbd> - reset box position.
- <kbd>WASD</kbd> or <kbd>Arrows</kbd> - move camera.
- <kbd>Q</kbd>/<kbd>E</kbd> - adjust zoom.
- <kbd>F</kbd> - go/exit fullscreen.
- <kbd>F</kbd> - go/exit fullscreen (may not work properly on Linux).

### For PS Vita
- <kbd>Cross</kbd> - reset box position.
Expand Down
16 changes: 10 additions & 6 deletions src/BoxEntity.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
#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;
BoxEntity::bodyDef.position.Set(BoxEntity::x, BoxEntity::y);

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()
Expand Down
10 changes: 7 additions & 3 deletions src/BoxEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions src/FontManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

#include <SDL2/SDL.h>

/// REALIZE
/// ME
/// ONE DAY
/// PLEAAAAAAASE
4 changes: 2 additions & 2 deletions src/PhysicsObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 11 additions & 3 deletions src/PhysicsObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
9 changes: 6 additions & 3 deletions src/PlatformEntity.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}

Expand Down
9 changes: 7 additions & 2 deletions src/PlatformEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
25 changes: 16 additions & 9 deletions src/WorldManager.cpp
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)
{
Expand All @@ -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;

Expand Down
24 changes: 14 additions & 10 deletions src/WorldManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhysicsObj*> 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<PhysicsObj*> 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;
Expand All @@ -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.
Expand All @@ -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;
};
22 changes: 12 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@
#include "PlatformEntity.h"
#include "WorldManager.h"

#include <thread>

#ifdef Linux
const bool isLinux = true;
#else
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();

Expand Down

0 comments on commit 3e1ba24

Please sign in to comment.