Skip to content

josiest/ion

Repository files navigation

ion

ion is a small-scoped library meant to make working with SDL easier. It serves two primary functions

  1. it provides result-style resource handlers for various SDL and OpenGL components
  2. it provides a higher level event-handling interface than pure SDL

ion is still in the very early stages of development. Since this is currently a personal project, more features will be added as I need them.

Installation

The goal is to eventually add ion to different C++ package managers, but I'm just a mere student and I don't have the time to do that right now. For now you'll need to download the source code and build it with cmake, then install it in a place that other CMake projects can find it

$ git clone https://github.com/josiest/ion.git && cd ion
$ mkdir build && cd build
$ cmake ..
$ cmake --build .

Then if you're on linux, run $ sudo cmake --install .. This will install the ion into /usr/include

Finally, add the following to your CMakeLists.txt

find_package(ion REQUIRED)
...
include_directories(ion::ion)
target_link_libraries(<project-name> PRIVATE ion::ion)

Examples

Here's a simple example of how you might use ion. This example renders a fibonacci-like gradient pattern to the screen.

#include <ion/ion.hpp>
#include <SDL.h>

#include <cmath>
#include <cstdint>

SDL_Color lerp(SDL_Color const & a, SDL_Color const & b, float t)
{
    auto intlerp = [](std::uint8_t x, std::uint8_t y, float t) {
        return static_cast<std::uint8_t>(std::lerp(x, y, t));
    };
    return SDL_Color{intlerp(a.r, b.r, t), intlerp(a.g, b.g, t),
                     intlerp(a.b, b.b, t), 0xff};
}

void render(ion::hardware_renderable auto & window)
{
    // the initial color
    SDL_Color blue{48, 118, 217, 255};
    // the final color
    SDL_Color red{219, 0, 66, 255};

    // clear the screen
    SDL_SetRenderDrawColor(window, red.r, red.g, red.b, red.a);
    SDL_RenderClear(window);

    // the dimensions of the rect to draw
    SDL_Rect rect{0, 0, 0, 0};
    SDL_GetWindowSize(window, &rect.w, &rect.h);
    rect.w /= 2;

    // draw the fibonacci-like patern
    int n = 8;
    for (int k = 0; k < n; k++) {

        // calculate the intermediate color
        float const t = static_cast<float>(k)/n;
        auto const c = lerp(blue, red, t);

        // draw the rect
        SDL_SetRenderDrawColor(window, c.r, c.g, c.b, c.a); 
        SDL_RenderFillRect(window, &rect);

        // split in half horizontally when k is even
        if (k % 2 == 0) {
            rect.x += rect.w;
            rect.h /= 2;
        }
        // split in half vertically when k is odd
        else {
            rect.y += rect.h;
            rect.w /= 2;
        }
    }
    SDL_RenderPresent(window);
}

int main()
{
    // create the sdl event-handler: quit when sdl's quit event is triggered
    ion::event_system events;
    events.subscribe(SDL_QUIT, &ion::input::quit_on_event);

    // initialize sdl - initialize this before other sdl resources
    ion::sdl_context sdl;

    // create a window, specifying the title and dimensions
    auto window = ion::hardware_renderer::basic_window("A simple window", 800, 600);
    render(window); // render once at the beginning of the program

    // busy loop until the user quits
    while (not ion::input::has_quit()) {
        events.process_queue();
    }
}

To run this example from the ion project directory run the following code

$ mkdir examples/simple/build && cd examples/simple/build
$ cmake ..
$ cmake --build .
$ ./simple

simple example window

About

A simple framework for rapid game prototyping with SDL and C++

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published