Skip to content

HandcrankEngine/HandcrankEngine

Repository files navigation

Warning

This custom game engine is under early development and should not be used.

Handcrank Engine

Build Lint Tests

Quick Start

#include "Handcrank/Handcrank.hpp"

using namespace Handcrank;

auto game = new Game();

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    return game->Run();
}

Examples

  1. Rendering Rectangle Object
  2. Creating Custom Objects
  3. Update and FixedUpdate Events
  4. Inline Update and FixedUpdate Events
  5. KeyPressed and KeyReleased States
  6. Mouse Position
  7. MouseOver and MouseOut Events
  8. MouseDown and MouseUp Events
  9. Loading Fonts and Rendering Text
  10. Display FPS
  11. Loading and Rendering Images

Rendering Rectangle Object

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/RectRenderObject.hpp"

using namespace Handcrank;

auto game = new Game();

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    auto square = std::make_unique<RectRenderObject>();

    square->SetFillColor(255, 0, 0, 255);

    game->AddChildObject(std::move(square));

    return game->Run();
}

Creating Custom Objects

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/RectRenderObject.hpp"

using namespace Handcrank;

auto game = new Game();

class RedSquare : public RectRenderObject
{

  public:
    void Start() override { SetFillColor(255, 0, 0, 255); }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<RedSquare>()));

    return game->Run();
}

Update and FixedUpdate Events

#include <iostream>

#include "Handcrank/Handcrank.hpp"

using namespace Handcrank;

auto game = new Game();

class LoopDebugger : public RenderObject
{

  public:
    void Update(const double deltaTime) override
    {
        std::cout << "Update" << std::endl;
    }
    void FixedUpdate(const double fixedDeltaTime) override
    {
        std::cout << "Fixed Update" << std::endl;
    }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<LoopDebugger>()));

    return game->Run();
}

Inline Update and FixedUpdate Events

#include <iostream>

#include "Handcrank/Handcrank.hpp"

using namespace Handcrank;

auto game = new Game();

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    auto loopDebugger = std::make_unique<RenderObject>();

    loopDebugger->SetUpdate([](RenderObject *ref, const double deltaTime)
                            { std::cout << "Update" << std::endl; });

    loopDebugger->SetFixedUpdate([](RenderObject *ref, const double fixedDeltaTime)
                                 { std::cout << "Fixed Update" << std::endl; });

    game->AddChildObject(std::move(loopDebugger));

    return game->Run();
}

KeyPressed and KeyReleased States

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/RectRenderObject.hpp"

using namespace Handcrank;

auto game = new Game();

class Button : public RectRenderObject
{

  public:
    void Start() override { SetFillColor(255, 0, 0, 255); }
    void Update(const double deltaTime) override
    {
        if (game->keyPressedState[SDLK_SPACE])
        {
            SetFillColor(255, 0, 0, 100);
        }
        else if (game->keyReleasedState[SDLK_SPACE])
        {
            SetFillColor(255, 0, 0, 255);
        }
    }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<Button>()));

    return game->Run();
}

Mouse Position

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/RectRenderObject.hpp"

using namespace Handcrank;

auto game = new Game();

class Cursor : public RectRenderObject
{

  public:
    void Start() override { SetFillColor(255, 0, 0, 255); }
    void Update(const double deltaTime) override
    {
        SetRect(game->mousePosition->x - 50, game->mousePosition->y - 50);
    }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<Cursor>()));

    return game->Run();
}

MouseOver and MouseOut Events

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/RectRenderObject.hpp"

using namespace Handcrank;

auto game = new Game();

class Button : public RectRenderObject
{

  public:
    void Start() override { SetFillColor(255, 0, 0, 255); }
    void OnMouseOver() override { SetFillColor(255, 0, 0, 100); }
    void OnMouseOut() override { SetFillColor(255, 0, 0, 255); }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<Button>()));

    return game->Run();
}

MouseDown and MouseUp Events

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/RectRenderObject.hpp"

using namespace Handcrank;

auto game = new Game();

class Button : public RectRenderObject
{

  public:
    void Start() override { SetFillColor(255, 0, 0, 255); }
    void OnMouseDown() override { SetFillColor(255, 0, 0, 100); }
    void OnMouseUp() override { SetFillColor(255, 0, 0, 255); }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<Button>()));

    return game->Run();
}

Loading Fonts and Rendering Text

Note

Fonts can be loaded by using SDL_LoadFont but it will only work for local builds where the font is always in the same path. Use SDL_LoadFontRW on header files generated by using xxd -i "Font.ttf" "Font.h" or the included bin/compile-static-assets.h bash script to make sure they work even after the build file has been moved.

#include "../fonts/Roboto/Roboto-Regular.h"

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/TextRenderObject.hpp"

#include "Handcrank/sdl/SDL_TTF_Utilities.hpp"

using namespace Handcrank;

auto game = new Game();

auto font = SDL_LoadFontRW(fonts_Roboto_Roboto_Regular_ttf,
                           fonts_Roboto_Roboto_Regular_ttf_len, 30);

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    auto label = std::make_unique<TextRenderObject>();

    label->SetFont(font);

    label->SetText("Hello, World");

    game->AddChildObject(std::move(label));

    return game->Run();
}

Display FPS

#include "../fonts/Roboto/Roboto-Regular.h"

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/TextRenderObject.hpp"

#include "Handcrank/sdl/SDL_TTF_Utilities.hpp"

using namespace Handcrank;

auto game = new Game();

auto robotoFont = SDL_LoadFontRW(fonts_Roboto_Roboto_Regular_ttf,
                                 fonts_Roboto_Roboto_Regular_ttf_len, 30);

class FPS : public TextRenderObject
{
  public:
    void Start() override { SetFont(robotoFont); }
    void Update(const double deltaTime) override
    {
        SetText(std::to_string(game->GetFPS()));
    }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<FPS>()));

    return game->Run();
}

Loading and Rendering Images

Note

Similar to fonts, images can be loaded by using SDL_LoadTexture but it will only work for local builds where the image is always in the same path. Use SDL_LoadTextureRW on header files generated by using xxd -i "Image.png" "Image.h" or the included bin/compile-static-assets.h bash script to make sure they work even after the build file has been moved.

#include <algorithm>

#include "../images/sdl_logo.h"

#include "Handcrank/Handcrank.hpp"
#include "Handcrank/ImageRenderObject.hpp"

using namespace Handcrank;

auto game = new Game();

class SDL_Logo : public ImageRenderObject
{
  public:
    void Start() override
    {
        LoadTextureRW(game->GetRenderer(), images_sdl_logo_png,
                      images_sdl_logo_png_len);
    }
};

auto main() -> int
{
    game->SetTitle("Handcrank Engine");

    game->AddChildObject(std::move(std::make_unique<SDL_Logo>()));

    return game->Run();
}

About

⚠️ This custom game engine is under early development and should not be used.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published