Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ jobs:
build:
name: Test Build
runs-on: ubuntu-latest
env:
EM_VERSION: 5.0.3
EM_CACHE_FOLDER: "emsdk-cache"
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup cache
uses: actions/cache@v4
with:
path: "_deps"
key: deps-${{ runner.os }}
path: ${{ env.EM_CACHE_FOLDER }}
key: ${{ env.EM_VERSION }}-${{ runner.os }}

- uses: mymindstorm/setup-emsdk@v14
with:
version: 4.0.6
actions-cache-folder: "emsdk-cache"
version: ${{ env.EM_VERSION }}
actions-cache-folder: ${{ env.EM_CACHE_FOLDER }}

- name: Download ports
run: |
embuilder build harfbuzz

- name: Run CMake
run: emcmake cmake --preset release
- name: Run CMake (Release & Examples)
run: emcmake cmake --preset release -DASW_BUILD_EXAMPLES=ON

- name: Make
run: cmake --build --preset release
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ docs/
_deps/
.DS_Store
build/
bin/

.DS_Store
15 changes: 10 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ if(TARGET freetype AND NOT TARGET Freetype::Freetype)
add_library(Freetype::Freetype ALIAS freetype)
endif()

CPMAddPackage("gh:libsdl-org/SDL#release-3.2.8")
CPMAddPackage("gh:libsdl-org/SDL_image#release-3.2.4")
CPMAddPackage("gh:libsdl-org/SDL_ttf#release-3.2.0")
# V3 not released yet
CPMAddPackage("gh:libsdl-org/SDL_mixer#a83eb9c59c55b48da72dc7b062c78d8dc52ec322")
CPMAddPackage("gh:libsdl-org/SDL#release-3.4.2")
CPMAddPackage("gh:libsdl-org/SDL_image#release-3.4.0")
CPMAddPackage("gh:libsdl-org/SDL_ttf#release-3.2.2")
CPMAddPackage("gh:libsdl-org/SDL_mixer#release-3.2.0")

# Add include
target_include_directories(
Expand All @@ -65,6 +64,12 @@ target_link_libraries(
z
)

# Examples
option(ASW_BUILD_EXAMPLES "Build ASW examples" OFF)
if(ASW_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

# Install
include(CMakePackageConfigHelpers)

Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ GENERATE_LATEX = NO
HTML_OUTPUT = docs
HAVE_DOT = YES
DOT_IMAGE_FORMAT = svg
EXTRACT_ALL = YES
EXTRACT_ALL = YES
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ FetchContent_MakeAvailable(asw)
cmake --preset debug
cmake --build --preset debug
```

Output is in the `lib/` directory.

### Building Examples

```sh
cmake --preset debug -DASW_BUILD_EXAMPLES=ON
cmake --build --preset debug
```

Output is in the `bin/` directory.
7 changes: 7 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.22)

add_subdirectory(keyboard)
add_subdirectory(mouse)
add_subdirectory(controller)
add_subdirectory(actions)
add_subdirectory(primitives)
2 changes: 2 additions & 0 deletions examples/actions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(example_actions main.cpp)
target_link_libraries(example_actions PRIVATE asw::asw)
148 changes: 148 additions & 0 deletions examples/actions/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/// @file main.cpp
/// @brief Action binding example
///
/// Demonstrates:
/// - Registering named actions with bind_action()
/// - Binding the same action to multiple input sources (keyboard + controller)
/// - is_action_down(), is_action_pressed(), is_action_released()
/// - get_action_strength() for analogue movement via a controller axis
///
/// Bindings:
/// "move_left" → A key, Left arrow, controller left axis (negative X)
/// "move_right" → D key, Right arrow, controller left axis (positive X)
/// "move_up" → W key, Up arrow, controller left axis (negative Y)
/// "move_down" → S key, Down arrow, controller left axis (positive Y)
/// "fire" → Space, controller A button
///
/// Controls:
/// WASD / arrows / left stick - move the box
/// Space / A button - fire (flash indicator)
/// Escape - quit

#include <asw/asw.h>

int main()
{
asw::core::init(800, 600);
asw::display::set_title("ASW Example - Action Bindings");
asw::core::print_info();

// --- Register actions ---
asw::input::bind_action("move_left", asw::input::KeyBinding { asw::input::Key::A });
asw::input::bind_action("move_left", asw::input::KeyBinding { asw::input::Key::Left });
asw::input::bind_action("move_left",
asw::input::ControllerAxisBinding { asw::input::ControllerAxis::LeftX, 0, 0.2F, false });

asw::input::bind_action("move_right", asw::input::KeyBinding { asw::input::Key::D });
asw::input::bind_action("move_right", asw::input::KeyBinding { asw::input::Key::Right });
asw::input::bind_action("move_right",
asw::input::ControllerAxisBinding { asw::input::ControllerAxis::LeftX, 0, 0.2F, true });

asw::input::bind_action("move_up", asw::input::KeyBinding { asw::input::Key::W });
asw::input::bind_action("move_up", asw::input::KeyBinding { asw::input::Key::Up });
asw::input::bind_action("move_up",
asw::input::ControllerAxisBinding { asw::input::ControllerAxis::LeftY, 0, 0.2F, false });

asw::input::bind_action("move_down", asw::input::KeyBinding { asw::input::Key::S });
asw::input::bind_action("move_down", asw::input::KeyBinding { asw::input::Key::Down });
asw::input::bind_action("move_down",
asw::input::ControllerAxisBinding { asw::input::ControllerAxis::LeftY, 0, 0.2F, true });

asw::input::bind_action("fire", asw::input::KeyBinding { asw::input::Key::Space });
asw::input::bind_action(
"fire", asw::input::ControllerButtonBinding { asw::input::ControllerButton::A });

asw::input::bind_action("quit", asw::input::KeyBinding { asw::input::Key::Escape });
asw::input::bind_action(
"quit", asw::input::ControllerButtonBinding { asw::input::ControllerButton::Start });

asw::Vec2<float> pos { 375.0F, 275.0F };

Check warning on line 59 in examples/actions/main.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Avoid explicitly specifying the template arguments by relying on the class template argument deduction.

See more on https://sonarcloud.io/project/issues?id=AdsGames_asw&issues=AZ0D8Mwqp0gnGQq5-UTz&open=AZ0D8Mwqp0gnGQq5-UTz&pullRequest=40
constexpr float box_size = 50.0F;
constexpr float base_speed = 4.0F;

int fire_frames = 0;

while (!asw::core::is_exiting()) {
asw::core::update();

if (asw::input::is_action_pressed("quit")) {
asw::core::exit();
}

// Move using analogue strength so a controller stick gives smooth speed
pos.x -= asw::input::get_action_strength("move_left") * base_speed;
pos.x += asw::input::get_action_strength("move_right") * base_speed;
pos.y -= asw::input::get_action_strength("move_up") * base_speed;
pos.y += asw::input::get_action_strength("move_down") * base_speed;

// Clamp to window
const auto win = asw::display::get_logical_size();
if (pos.x < 0) {
pos.x = 0;
}
if (pos.y < 0) {
pos.y = 0;
}
if (pos.x + box_size > static_cast<float>(win.x)) {
pos.x = static_cast<float>(win.x) - box_size;
}
if (pos.y + box_size > static_cast<float>(win.y)) {
pos.y = static_cast<float>(win.y) - box_size;
}

// Fire
if (asw::input::is_action_pressed("fire")) {
fire_frames = 12;
asw::log::info("Fire!");
}
if (fire_frames > 0) {
--fire_frames;
}

// --- Draw ---
asw::display::clear(asw::color::darkslategray);

// Fire flash
if (fire_frames > 0) {
const float alpha = static_cast<float>(fire_frames) / 12.0F;
asw::draw::circle_fill({ pos.x + box_size / 2.0F, pos.y + box_size / 2.0F },
box_size * 1.5F, asw::Color(255, 200, 0, static_cast<uint8_t>(alpha * 200)));
}

// Player box
const bool moving = asw::input::is_action_down("move_left")
|| asw::input::is_action_down("move_right") || asw::input::is_action_down("move_up")
|| asw::input::is_action_down("move_down");
asw::draw::rect_fill({ pos, { box_size, box_size } },
moving ? asw::color::cornflowerblue : asw::color::steelblue);
asw::draw::rect({ pos, { box_size, box_size } }, asw::color::white);

// Strength bars for left/right
const float sl = asw::input::get_action_strength("move_left");
const float sr = asw::input::get_action_strength("move_right");
const float su = asw::input::get_action_strength("move_up");
const float sd = asw::input::get_action_strength("move_down");

constexpr float bar_w = 100.0F;
constexpr float bar_h = 14.0F;
const float by = static_cast<float>(win.y) - 60.0F;

asw::draw::rect({ 50.0F, by, bar_w, bar_h }, asw::color::gray);
asw::draw::rect_fill({ 50.0F, by, bar_w * sl, bar_h }, asw::color::cyan);

asw::draw::rect({ 650.0F, by, bar_w, bar_h }, asw::color::gray);
asw::draw::rect_fill({ 650.0F, by, bar_w * sr, bar_h }, asw::color::cyan);

asw::draw::rect({ 350.0F, by - 20.0F, bar_h, bar_w }, asw::color::gray);
asw::draw::rect_fill({ 350.0F, by - 20.0F, bar_h, bar_w * su }, asw::color::cyan);

asw::draw::rect({ 380.0F, by - 20.0F, bar_h, bar_w }, asw::color::gray);
asw::draw::rect_fill({ 380.0F, by - 20.0F, bar_h, bar_w * sd }, asw::color::cyan);

asw::display::present();
}

asw::core::cleanup();

return 0;
}
2 changes: 2 additions & 0 deletions examples/controller/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(example_controller main.cpp)
target_link_libraries(example_controller PRIVATE asw::asw)
Loading
Loading