Skip to content

Commit

Permalink
formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Oct 23, 2023
1 parent 7369cf1 commit 2fe4aa5
Show file tree
Hide file tree
Showing 61 changed files with 5,117 additions and 4,115 deletions.
20 changes: 13 additions & 7 deletions ashura/include/ashura/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct Animation
nanoseconds duration = milliseconds{256};
u64 iterations = 1;
AnimationCfg cfg = AnimationCfg::Default;
f64 speed = 1; // higher spead means faster time to completion than specified duration
f64 speed = 1; // higher spead means faster time to completion than specified duration

nanoseconds elapsed_duration = nanoseconds{0};
u64 iterations_done = 0;
Expand Down Expand Up @@ -89,7 +89,9 @@ struct Animation
{
cfg &= ~AnimationCfg::Loop;
iterations_done = iterations;
t = ((cfg & AnimationCfg::Alternate) == AnimationCfg::Alternate) ? (((iterations % 2) == 0) ? 0.0 : 1.0) : 1.0;
t = ((cfg & AnimationCfg::Alternate) == AnimationCfg::Alternate) ?
(((iterations % 2) == 0) ? 0.0 : 1.0) :
1.0;
}

void tick(nanoseconds interval)
Expand All @@ -99,21 +101,25 @@ struct Animation
return;
}

nanoseconds const tick_duration = nanoseconds{(i64) ((f64) interval.count() * (f64) speed)};
nanoseconds const tick_duration = nanoseconds{(i64) ((f64) interval.count() * (f64) speed)};
nanoseconds const total_elapsed_duration = elapsed_duration + tick_duration;
f64 const t_total = (((f64) total_elapsed_duration.count()) / (f64) duration.count());
u64 const t_iterations = (u64) t_total;
f64 const t_total = (((f64) total_elapsed_duration.count()) / (f64) duration.count());
u64 const t_iterations = (u64) t_total;

if (((cfg & AnimationCfg::Loop) != AnimationCfg::Loop) && t_iterations >= iterations)
{
elapsed_duration = total_elapsed_duration;
iterations_done = iterations;
t = ((cfg & AnimationCfg::Alternate) == AnimationCfg::Alternate) ? (((iterations % 2) == 0) ? 0.0 : 1.0) : 1.0;
t = ((cfg & AnimationCfg::Alternate) == AnimationCfg::Alternate) ?
(((iterations % 2) == 0) ? 0.0 : 1.0) :
1.0;
}
else
{
f64 const t_unsigned = t_total - (f64) t_iterations;
f64 const t_signed = ((cfg & AnimationCfg::Alternate) == AnimationCfg::Alternate) ? ((t_iterations % 2) == 0 ? t_unsigned : (1.0 - t_unsigned)) : t_unsigned;
f64 const t_signed = ((cfg & AnimationCfg::Alternate) == AnimationCfg::Alternate) ?
((t_iterations % 2) == 0 ? t_unsigned : (1.0 - t_unsigned)) :
t_unsigned;
elapsed_duration = total_elapsed_duration;
iterations_done = t_iterations;
t = t_signed;
Expand Down
9 changes: 5 additions & 4 deletions ashura/include/ashura/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ struct App
STX_MAKE_PINNED(App)

template <Impl<Widget> DerivedWidget>
explicit App(AppConfig icfg, DerivedWidget &&widget) :
cfg{icfg}, engine{icfg, std::move(widget)}
{}
explicit App(AppConfig icfg, DerivedWidget &&widget) : cfg{icfg}, engine{icfg, std::move(widget)}
{
}

void tick(std::chrono::nanoseconds interval);

~App()
{}
{
}

AppConfig cfg;
Engine engine;
Expand Down
498 changes: 318 additions & 180 deletions ashura/include/ashura/canvas.h

Large diffs are not rendered by default.

56 changes: 42 additions & 14 deletions ashura/include/ashura/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ struct Context
FrameStats frame_stats;
f32 text_scale_factor = 1;
Widget *root = nullptr;
stx::Vec<KeyEvent> key_events; // These are more of key state polling than key event state change notifications
stx::Vec<KeyEvent>
key_events; // These are more of key state polling than key event state change notifications

// TODO(lamarrr): expose current window here???

Expand All @@ -65,29 +66,33 @@ struct Context
}

Context()
{}
{
}

STX_MAKE_PINNED(Context)

~Context()
{
for (Subsystem *subsystem : subsystems)
{
ASH_LOG_INFO(Context, "Destroying subsystem: {} (type: {})", subsystem->get_name(), typeid(*subsystem).name());
ASH_LOG_INFO(Context, "Destroying subsystem: {} (type: {})", subsystem->get_name(),
typeid(*subsystem).name());
delete subsystem;
}
}

void register_subsystem(Subsystem *subsystem)
{
subsystems.push_inplace(subsystem).unwrap();
ASH_LOG_INFO(Context, "Registered subsystem: {} (type: {})", subsystem->get_name(), typeid(*subsystem).name());
ASH_LOG_INFO(Context, "Registered subsystem: {} (type: {})", subsystem->get_name(),
typeid(*subsystem).name());
}

template <typename T>
stx::Option<T *> get_subsystem(std::string_view name) const
{
stx::Span subsystem = subsystems.span().which([name](Subsystem *subsystem) { return subsystem->get_name() == name; });
stx::Span subsystem = subsystems.span().which(
[name](Subsystem *subsystem) { return subsystem->get_name() == name; });
if (!subsystem.is_empty())
{
return stx::Some(subsystem[0]->template as<T>());
Expand Down Expand Up @@ -207,7 +212,10 @@ struct Context
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
{
MouseClickEvent mouse_event{.mouse_id = MouseID{event.button.which}, .position = Vec2{AS(f32, event.button.x), AS(f32, event.button.y)}, .clicks = event.button.clicks};
MouseClickEvent mouse_event{.mouse_id = MouseID{event.button.which},
.position =
Vec2{AS(f32, event.button.x), AS(f32, event.button.y)},
.clicks = event.button.clicks};
switch (event.button.button)
{
case SDL_BUTTON_LEFT:
Expand Down Expand Up @@ -264,7 +272,10 @@ struct Context
}
for (auto &listener : win->event_listeners.mouse_motion)
{
listener.handle(MouseMotionEvent{.mouse_id = MouseID{event.motion.which}, .position = Vec2{AS(f32, event.motion.x), AS(f32, event.motion.y)}, .translation = Vec2{AS(f32, event.motion.xrel), AS(f32, event.motion.yrel)}});
listener.handle(MouseMotionEvent{
.mouse_id = MouseID{event.motion.which},
.position = Vec2{AS(f32, event.motion.x), AS(f32, event.motion.y)},
.translation = Vec2{AS(f32, event.motion.xrel), AS(f32, event.motion.yrel)}});
}
return true;
}
Expand All @@ -278,7 +289,10 @@ struct Context
}
for (auto &listener : win->event_listeners.mouse_wheel)
{
listener.handle(MouseWheelEvent{.mouse_id = MouseID{event.wheel.which}, .position = Vec2{AS(f32, event.wheel.mouseX), AS(f32, event.wheel.mouseY)}, .translation = Vec2{event.wheel.x, event.wheel.y}});
listener.handle(MouseWheelEvent{
.mouse_id = MouseID{event.wheel.which},
.position = Vec2{AS(f32, event.wheel.mouseX), AS(f32, event.wheel.mouseY)},
.translation = Vec2{event.wheel.x, event.wheel.y}});
}
return true;
}
Expand All @@ -293,7 +307,11 @@ struct Context
}
for (auto &listener : win->event_listeners.key)
{
listener.handle(KeyEvent{.key = event.key.keysym.sym, .modifiers = AS(KeyModifiers, event.key.keysym.mod), .action = event.type == SDL_EVENT_KEY_DOWN ? KeyAction::Press : KeyAction::Release});
listener.handle(KeyEvent{.key = event.key.keysym.sym,
.modifiers = AS(KeyModifiers, event.key.keysym.mod),
.action = event.type == SDL_EVENT_KEY_DOWN ?
KeyAction::Press :
KeyAction::Release});
}
return true;
}
Expand All @@ -312,33 +330,43 @@ struct Context

case SDL_EVENT_DROP_BEGIN:
{
ASH_LOG_INFO(Vulkan, "drop begin: {} x={}, y={}", event.drop.file == nullptr ? " " : event.drop.file, event.drop.x, event.drop.y);
ASH_LOG_INFO(Vulkan, "drop begin: {} x={}, y={}",
event.drop.file == nullptr ? " " : event.drop.file, event.drop.x,
event.drop.y);
return true;
}

case SDL_EVENT_DROP_COMPLETE:
{
ASH_LOG_INFO(Vulkan, "drop complete: {} x={}, y={}", event.drop.file == nullptr ? " " : event.drop.file, event.drop.x, event.drop.y);
ASH_LOG_INFO(Vulkan, "drop complete: {} x={}, y={}",
event.drop.file == nullptr ? " " : event.drop.file, event.drop.x,
event.drop.y);
return true;
}

case SDL_EVENT_DROP_FILE:
{
f32 x = 0, y = 0;
SDL_GetMouseState(&x, &y);
ASH_LOG_INFO(Vulkan, "drop file: {} x={}, y={}, {},{}", event.drop.file == nullptr ? " " : event.drop.file, event.drop.x, event.drop.y, x, y);
ASH_LOG_INFO(Vulkan, "drop file: {} x={}, y={}, {},{}",
event.drop.file == nullptr ? " " : event.drop.file, event.drop.x,
event.drop.y, x, y);
return true;
}

case SDL_EVENT_DROP_POSITION:
{
ASH_LOG_INFO(Vulkan, "drop position: {} x={}, y={}", event.drop.file == nullptr ? " " : event.drop.file, event.drop.x, event.drop.y);
ASH_LOG_INFO(Vulkan, "drop position: {} x={}, y={}",
event.drop.file == nullptr ? " " : event.drop.file, event.drop.x,
event.drop.y);
return true;
}

case SDL_EVENT_DROP_TEXT:
{
ASH_LOG_INFO(Vulkan, "drop text: {} x={}, y={}", event.drop.file == nullptr ? " " : event.drop.file, event.drop.x, event.drop.y);
ASH_LOG_INFO(Vulkan, "drop text: {} x={}, y={}",
event.drop.file == nullptr ? " " : event.drop.file, event.drop.x,
event.drop.y);
return true;
}

Expand Down
27 changes: 18 additions & 9 deletions ashura/include/ashura/curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ namespace ash
struct Curve
{
virtual ~Curve()
{}
{
}

virtual f32 operator()(f32 t) = 0;
};

struct Linear final : public Curve
{
virtual ~Linear() override
{}
{
}

virtual f32 operator()(f32 t) override
{
Expand All @@ -29,7 +31,8 @@ struct Linear final : public Curve
struct EaseIn final : public Curve
{
virtual ~EaseIn() override
{}
{
}

virtual f32 operator()(f32 t) override
{
Expand All @@ -40,7 +43,8 @@ struct EaseIn final : public Curve
struct EaseOut final : public Curve
{
virtual ~EaseOut() override
{}
{
}

virtual f32 operator()(f32 t) override
{
Expand All @@ -51,7 +55,8 @@ struct EaseOut final : public Curve
struct EaseInOut final : public Curve
{
virtual ~EaseInOut() override
{}
{
}

virtual f32 operator()(f32 t) override
{
Expand All @@ -60,10 +65,12 @@ struct EaseInOut final : public Curve
};

struct Quadratic final : public Curve
{};
{
};

struct Cubic final : public Curve
{};
{
};

struct QuadraticBezier final : public Curve
{
Expand All @@ -72,7 +79,8 @@ struct QuadraticBezier final : public Curve
f32 p2 = 0;

virtual ~QuadraticBezier() override
{}
{
}

virtual f32 operator()(f32 t) override
{
Expand All @@ -82,6 +90,7 @@ struct QuadraticBezier final : public Curve
// TODO(lamarrr): Splines, Bezier Curves, Hermite Curves, Catmull-Rom curves, B-Spline

struct Spline final : public Curve
{};
{
};

}; // namespace ash
11 changes: 6 additions & 5 deletions ashura/include/ashura/ecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ struct Registry
// check entity validity: (entity_id < nentities) && ((entity_free_mask[entity_id >> 6] >> (entity_id & 63)) & 1)
// free entity: free components && entity_free_mask[]
u64 **entity_component_map = nullptr;
u64 *entity_free_mask = nullptr; // we don't want to waste indices and have to update indirections
u64 *free_entity_indices = nullptr;
u64 entities_capacity = 0;
u64 nentities = 0;
u64 nfree_entities = 0;
u64 *entity_free_mask =
nullptr; // we don't want to waste indices and have to update indirections
u64 *free_entity_indices = nullptr;
u64 entities_capacity = 0;
u64 nentities = 0;
u64 nfree_entities = 0;
};

// say for example we want a system that processes elements in batches
Expand Down
3 changes: 2 additions & 1 deletion ashura/include/ashura/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct Engine
template <Impl<Widget> DerivedWidget>
Engine(AppConfig const &cfg, DerivedWidget &&root_widget) :
Engine{cfg, new DerivedWidget{std::move(root_widget)}}
{}
{
}

Engine(AppConfig const &cfg, Widget *root_widget);

Expand Down
2 changes: 1 addition & 1 deletion ashura/include/ashura/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ struct WindowEventListeners
stx::Vec<stx::UniqueFn<void(MouseClickEvent)>> mouse_click;
stx::Vec<stx::UniqueFn<void(MouseMotionEvent)>> mouse_motion;
stx::Vec<stx::UniqueFn<void(MouseWheelEvent)>> mouse_wheel;
stx::Vec<stx::UniqueFn<void(KeyEvent)>> key;
stx::Vec<stx::UniqueFn<void(KeyEvent)>> key;
};

struct GlobalEventListeners
Expand Down
Loading

0 comments on commit 2fe4aa5

Please sign in to comment.