Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Last active July 11, 2026 17:52
Show Gist options
  • Select an option

  • Save jweinst1/17760d1b54af550885bd55d10e8c5fe2 to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/17760d1b54af550885bd55d10e8c5fe2 to your computer and use it in GitHub Desktop.
implementation of a tiny text box in sdl3
#include <SDL3/SDL.h>
#include <iostream>
#include <cstdint>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
static constexpr uint8_t textchtxt_0[] = {
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 1, 1, 1, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_1[] = {
3, 3, 1, 1, 1, 3, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 1, 3, 1, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 1, 1, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_2[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 3, 3, 3,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_3[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 1, 3
};
static constexpr uint8_t textchtxt_4[] = {
1, 1, 1, 1, 1, 3, 3, 3,
1, 3, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 3, 1, 3, 3,
1, 1, 1, 1, 1, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 1, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_5[] = {
1, 1, 1, 1, 1, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 1, 1, 1, 1, 1, 3, 3,
1, 3, 3, 3, 1, 3, 3, 3,
1, 3, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_6[] = {
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_7[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 1, 1, 1, 3,
3, 1, 3, 1, 3, 3, 3, 3,
3, 1, 1, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_8[] = {
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_9[] = {
3, 1, 1, 1, 1, 3, 3, 3,
1, 3, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 1, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_10[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_11[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 1, 3,
3, 1, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_12[] = {
1, 1, 1, 1, 1, 3, 3, 3,
1, 3, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 1, 3, 3,
1, 1, 1, 1, 1, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr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3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_18[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 1, 1, 1, 1, 1, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_19[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_20[] = {
1, 1, 1, 1, 1, 1, 1, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 1, 1, 1, 1, 1, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_21[] = {
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_22[] = {
3, 3, 3, 3, 1, 1, 3, 3,
3, 3, 3, 1, 3, 3, 1, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 1, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_23[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_24[] = {
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 1, 1, 1, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_25[] = {
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 1, 3, 3, 1, 3,
1, 3, 1, 3, 1, 3, 1, 3,
1, 3, 1, 3, 1, 3, 1, 3,
1, 1, 3, 3, 3, 1, 1, 3,
1, 1, 3, 3, 3, 1, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_26[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 1, 3,
3, 1, 1, 1, 1, 1, 3, 3
};
static constexpr uint8_t textchtxt_27[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 1, 3, 3, 1, 3,
1, 3, 3, 1, 3, 3, 1, 3,
1, 3, 1, 3, 1, 3, 1, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_28[] = {
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 1, 1, 1, 1, 1, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_29[] = {
1, 1, 3, 3, 3, 1, 1, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
1, 1, 3, 3, 3, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_30[] = {
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 3, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_31[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
1, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 1, 3, 1, 3, 3, 3,
3, 1, 3, 3, 3, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_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 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_37[] = {
1, 1, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
1, 1, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_38[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 1, 1, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_39[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 3, 1, 1, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_40[] = {
1, 3, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 1, 3, 3, 3,
1, 3, 3, 1, 3, 3, 3, 3,
1, 3, 1, 1, 3, 3, 3, 3,
1, 1, 3, 3, 1, 3, 3, 3,
1, 3, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_41[] = {
3, 3, 3, 3, 3, 1, 1, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 3, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_42[] = {
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 1, 1, 3,
3, 1, 3, 1, 1, 3, 3, 3,
3, 1, 1, 3, 3, 3, 3, 3,
3, 1, 3, 1, 1, 3, 3, 3,
3, 1, 3, 3, 3, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_43[] = {
3, 3, 1, 1, 3, 3, 3, 3,
1, 1, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 1, 1, 1, 1, 1, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_44[] = {
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 1, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_45[] = {
1, 1, 3, 3, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
1, 1, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_46[] = {
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 1, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_47[] = {
1, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_48[] = {
1, 3, 3, 3, 3, 3, 1, 3,
1, 1, 3, 3, 3, 1, 1, 3,
1, 3, 1, 3, 1, 3, 1, 3,
1, 3, 3, 1, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_49[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 1, 1, 3, 3, 3,
3, 3, 3, 1, 1, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 1, 1, 3, 3, 3,
3, 3, 3, 1, 1, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_50[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
1, 1, 1, 3, 1, 1, 3, 3,
1, 3, 3, 1, 3, 3, 1, 3,
1, 3, 3, 1, 3, 3, 1, 3,
1, 3, 3, 1, 3, 3, 1, 3,
1, 3, 3, 1, 3, 3, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_51[] = {
1, 1,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, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_56[] = {
3, 3, 1, 1, 1, 3, 3, 3,
3, 1, 3, 3, 3, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 1, 1, 1, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_57[] = {
3, 3, 3, 3, 3, 1, 1, 1,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 3, 1, 1, 1,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_58[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 3, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_59[] = {
1, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 1, 3, 3, 3,
1, 3, 1, 1, 1, 1, 1, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 3, 1, 1, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_60[] = {
1, 1, 1, 1, 1, 1, 3, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 3, 3, 3, 3, 3, 1, 3,
1, 1, 1, 1, 1, 1, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
1, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_61[] = {
1, 1, 1, 3, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
3, 3, 1, 3, 3, 3, 3, 3,
1, 1, 1, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_62[] = {
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 3, 3, 3, 3, 1, 3,
3, 1, 1, 1, 1, 1, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3
};
static constexpr uint8_t textchtxt_63[] = {
1, 1, 3, 3, 3, 3, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3,
1, 3, 1, 3, 3, 3, 1, 3,
3, 3, 1, 3, 3, 3, 1, 3,
3, 3, 1, 3, 3, 3, 1, 3,
3, 3, 3, 1, 3, 1, 3, 3,
3, 3, 3, 3, 1, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
};
static constexpr const uint8_t* TOTAL_TEXT_PICS[] = { textchtxt_0, textchtxt_1, textchtxt_2, textchtxt_3, textchtxt_4, textchtxt_5, textchtxt_6, textchtxt_7, textchtxt_8, textchtxt_9, textchtxt_10, textchtxt_11, textchtxt_12, textchtxt_13, textchtxt_14, textchtxt_15, textchtxt_16, textchtxt_17, textchtxt_18, textchtxt_19, textchtxt_20, textchtxt_21, textchtxt_22, textchtxt_23, textchtxt_24, textchtxt_25, textchtxt_26, textchtxt_27, textchtxt_28, textchtxt_29, textchtxt_30, textchtxt_31, textchtxt_32, textchtxt_33, textchtxt_34, textchtxt_35, textchtxt_36, textchtxt_37, textchtxt_38, textchtxt_39, textchtxt_40, textchtxt_41, textchtxt_42, textchtxt_43, textchtxt_44, textchtxt_45, textchtxt_46, textchtxt_47, textchtxt_48, textchtxt_49, textchtxt_50, textchtxt_51, textchtxt_52, textchtxt_53, textchtxt_54, textchtxt_55, textchtxt_56, textchtxt_57, textchtxt_58, textchtxt_59, textchtxt_60, textchtxt_61, textchtxt_62, textchtxt_63 };
static constexpr size_t TOTAL_TEXT_PIC_SIZE = sizeof(TOTAL_TEXT_PICS) / sizeof(TOTAL_TEXT_PICS[0]);
static constexpr size_t SQ_SCREEN_X_PX = 128;
static constexpr size_t SQ_SCREEN_Y_PX = 128;
static constexpr size_t TEXT_CHAR_BOX_PX = 8;
static constexpr float TEXT_CHAR_BOX_FPX = static_cast<float>(TEXT_CHAR_BOX_PX);
static constexpr SDL_Color TEXT_PALT[4] = {
{ 0, 0, 0, 0 },
{ 0, 0, 0, 255 }, // Index 1: Opaque Black
{ 127, 127, 127, 255 },
{ 255, 255, 255, 255 }
};
static SDL_Palette* makePalette() {
SDL_Palette* spritePalette = SDL_CreatePalette(4);
SDL_SetPaletteColors(spritePalette, TEXT_PALT, 0, 4);
return spritePalette;
}
// SDL_FRect dst_rect = { playerScreenX, playerScreenY, MYGAME_SQSIZE, MYGAME_SQSIZE };
struct KeyButton {
SDL_Texture* text = nullptr;
char key = '\0';
SDL_FRect rect{0.0f, 0.0f, TEXT_CHAR_BOX_FPX, TEXT_CHAR_BOX_FPX};
void setRect(SDL_FRect* frect) {
rect = *frect;
}
};
struct TextPosMeta {
bool highlight = false;
};
template<size_t width>
struct TextBar {
std::vector<TextPosMeta> metas;
std::vector<KeyButton*> letters;
float posX = 0.0f;
float posY = 0.0f;
void highlight(float x, float y) {
float maxY = posY + TEXT_CHAR_BOX_FPX;
float maxX = posX + (TEXT_CHAR_BOX_FPX * width);
if (x >= maxX || y >= maxY || x < posX)
return;
const int toHighlightX = static_cast<int>(x - posX) / TEXT_CHAR_BOX_PX;
size_t curPoint = letters.size() > width ? (letters.size() - width) : 0;
size_t highlightIndex = curPoint + toHighlightX;
if (highlightIndex >= letters.size())
return;
printf("Highlighting %zu\n", highlightIndex);
metas[highlightIndex].highlight = true;
}
void clearHighlight() {
for (size_t i = 0; i < metas.size(); ++i)
{
metas[i].highlight = false;
}
}
void draw(SDL_Renderer* rend) {
float curX = posX;
SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
size_t curPoint = letters.size() > width ? (letters.size() - width) : 0;
for (; curPoint < letters.size(); ++curPoint)
{
SDL_FRect rect{curX, posY, TEXT_CHAR_BOX_FPX, TEXT_CHAR_BOX_FPX};
SDL_RenderTexture(rend, letters[curPoint]->text , nullptr, &rect);
if (metas[curPoint].highlight) {
SDL_SetRenderDrawColor(rend, 0, 0, 255, 100);
SDL_RenderFillRect(rend, &rect);
}
curX += TEXT_CHAR_BOX_FPX;
}
}
};
template<size_t xsize, size_t ysize>
struct KeyBoard {
static_assert(xsize * ysize == TOTAL_TEXT_PIC_SIZE);
static_assert((xsize & 1) == 0);
static_assert((ysize & 1) == 0);
static constexpr size_t totalSize = xsize * ysize;
KeyButton board[totalSize];
float posX = 0.0f;
float posY = 0.0f;
SDL_Palette* palette = nullptr;
void initAll(SDL_Renderer* rend) {
palette = makePalette();
char curKey = (char)48;
for (size_t i = 0; i < totalSize; ++i)
{
KeyButton* curButton = &board[i];
curButton->key = curKey++;
SDL_Texture* texture1 = SDL_CreateTexture(rend, SDL_PIXELFORMAT_INDEX8, SDL_TEXTUREACCESS_STATIC, TEXT_CHAR_BOX_PX, TEXT_CHAR_BOX_PX);
SDL_SetTextureBlendMode(texture1, SDL_BLENDMODE_BLEND);
SDL_SetTexturePalette(texture1, palette);
SDL_UpdateTexture(texture1, nullptr, TOTAL_TEXT_PICS[i], 8);
curButton->text = texture1;
curButton->rect.x = posX + (static_cast<float>(i & (xsize - 1)) * TEXT_CHAR_BOX_FPX);
curButton->rect.y = posY + (static_cast<float>(i / xsize ) * TEXT_CHAR_BOX_FPX);
}
}
void deinitAll() {
for (size_t i = 0; i < totalSize; ++i) {
KeyButton* curButton = &board[i];
SDL_DestroyTexture(curButton->text);
}
SDL_DestroyPalette(palette);
}
void draw(SDL_Renderer* rend) {
// SDL_RenderTexture(renderer, chosen, nullptr, &dst_rect);
for (size_t i = 0; i < totalSize; ++i) {
KeyButton* curButton = &board[i];
SDL_RenderTexture(rend, curButton->text , nullptr, &(curButton->rect));
}
}
inline bool isInBoard(int x, int y) {
return x > -1 && y > -1 && x < xsize && y < ysize;
}
KeyButton* getButton(float x, float y) {
const int boardX = static_cast<int>(x - posX) / TEXT_CHAR_BOX_PX;
const int boardY = static_cast<int>(y - posY) / TEXT_CHAR_BOX_PX;
printf("Got button x=%d y=%d\n", boardX, boardY);
if (!isInBoard(boardX, boardY)) {
return nullptr;
}
return &board[(boardY * xsize) + boardX];
}
};
int main(int argc, char const *argv[])
{
if (!SDL_Init(SDL_INIT_VIDEO)) {
std::cerr << "Initialization failed: " << SDL_GetError() << "\n";
return -1;
}
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
if (!SDL_CreateWindowAndRenderer("The Text Box", SQ_SCREEN_X_PX, SQ_SCREEN_Y_PX, 0, &window, &renderer)) {
std::cerr << "Failed to create window/renderer: " << SDL_GetError() << "\n";
return -1;
}
bool running = true;
// Fixed timestep setup (60 updates per second)
const Uint64 FIXED_DELTA_TIME_NS = 1000000000 / 60;
Uint64 current_time_ns = SDL_GetTicksNS();
Uint64 previous_time_ns = current_time_ns;
Uint64 accumulator_ns = 0;
KeyBoard<16, 4> myBoard;
myBoard.posY = 96.0f;
myBoard.initAll(renderer);
TextBar<16> myTextBox;
bool isMousePressed = false;
float startPressedX = 0.0f, startPressedY = 0.0f;
while (running) {
current_time_ns = SDL_GetTicksNS();
Uint64 elapsed_ns = current_time_ns - previous_time_ns;
previous_time_ns = current_time_ns;
if (elapsed_ns > 250000000) elapsed_ns = 250000000;
accumulator_ns += elapsed_ns;
// ==========================================
// 1. INPUT DETECTION (OS Events & Mouse)
// ==========================================
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
running = false;
}
// Detect Mouse Clicks in SDL3
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
// SDL3 provides mouse coordinates as clean floats
float mousepre_x = event.button.x;
float mousepre_y = event.button.y;
isMousePressed = true;
startPressedX = event.button.x;
startPressedY = event.button.y;
std::cout << "Clicked x=" << mousepre_x << ", y=" << mousepre_y << "\n";
KeyButton* gotButton = myBoard.getButton(mousepre_x, mousepre_y);
if (gotButton != nullptr) {
myTextBox.letters.push_back(gotButton);
myTextBox.metas.push_back(TextPosMeta{});
}
}
}
else if (event.type == SDL_EVENT_MOUSE_MOTION) {
if (isMousePressed) {
float currentX = event.motion.x;
float currentY = event.motion.y;
std::cout << "Dragging to x=" << currentX << ", y=" << currentY << "\n";
myTextBox.highlight(currentX, currentY);
// TODO: Update your text highlighter logic here using
// startX/startY and currentX/currentY to define the selection area.
}
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
if (event.button.button == SDL_BUTTON_LEFT) {
isMousePressed = false;
std::cout << "Mouse Released.\n";
myTextBox.clearHighlight();
}
}
}
// ==========================================
// 2. FIXED PHYSICS / LOGIC TIMESTEP
// ==========================================
while (accumulator_ns >= FIXED_DELTA_TIME_NS) {
// Update item animations, physics, and skilling timers here
//playerPathMover.moveSprite();
// based on path, change target.
accumulator_ns -= FIXED_DELTA_TIME_NS;
}
// ==========================================
// 3. PAINT / RENDERING THE WINDOW
// ==========================================
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
//SDL_RenderFillRect(renderer, &specrect);
myBoard.draw(renderer);
myTextBox.draw(renderer);
// C. Command the GPU to push the backbuffer into the visible monitor frame
SDL_RenderPresent(renderer);
}
myBoard.deinitAll();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment