sKit  0.0.9
sKit_Example_Shapes.cpp
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <glm/common.hpp>
#include <stdexcept>
#include <vector>
const static int width = 1280;
const static int height = 800;
struct STATE {
SDL_Window *w;
SDL_Renderer *rdr;
};
auto SDL_AppInit(void **appstate, int argc, char **argv) -> SDL_AppResult {
SDL_Init(SDL_INIT_VIDEO);
auto state = new STATE;
if (!SDL_CreateWindowAndRenderer(
"", width, height, SDL_WINDOW_UTILITY, &state->w, &state->rdr
)) {
throw std::runtime_error(SDL_GetError());
};
*appstate = state;
return SDL_APP_CONTINUE;
}
constexpr float tau = std::numbers::pi_v<float> * 2.f;
auto SDL_AppIterate(void *appstate) -> SDL_AppResult {
auto state = reinterpret_cast<STATE *>(appstate);
SDL_SetRenderDrawColor(state->rdr, 0, 0, 0, 255);
SDL_RenderClear(state->rdr);
// Generate circle geometry
auto c = sKit::geom::circle<SDL_Vertex, int>(width / 2.f, height / 2.f, 100);
// Generate arc vertices
auto arc = sKit::geom::vertex::arc<SDL_FPoint>(
width / 3.f,
height / 3.f,
100,
glm::fract(SDL_GetTicks() * 0.0003) * tau,
sKit::math::map(sin(SDL_GetTicks() * 0.0005), -1, 1, 0.2, tau),
sKit::math::map(cos(SDL_GetTicks() * 0.0004), -1, 1, 25, 75)
);
// Render circle geometry
SDL_RenderGeometry(
state->rdr,
nullptr,
c.vertices.data(),
c.vertices.size(),
c.indices.data(),
c.indices.size()
);
// Render arc vertices as line
SDL_SetRenderDrawColor(state->rdr, 255, 255, 255, 255);
SDL_RenderLines(state->rdr, arc.data(), arc.size());
SDL_RenderPresent(state->rdr);
return SDL_APP_CONTINUE;
}
auto SDL_AppEvent(void *appstate, SDL_Event *event) -> SDL_AppResult {
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS;
} else if (event->key.key == SDLK_ESCAPE) {
return SDL_APP_SUCCESS;
} else
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
delete reinterpret_cast<STATE *>(appstate);
}
shapes.hpp
sKit::geom::circle< SDL_Vertex, int >
auto circle< SDL_Vertex, int >(float x, float y, float radius, unsigned int resolution) -> Geometry< SDL_Vertex, int >
sKit::geom::arc
auto arc(float x, float y, float radius, float theta, float width, float height, unsigned int resolution, SDL_FColor col) -> Geometry< SDL_Vertex, int >
Definition: shapes.cpp:27
sKit::math::map
auto map(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp) -> float
Given a value and an input range, map the value to an output range.
Definition: math.cpp:10