townengine/apps/examples/circle-raster/game.c

55 lines
1.8 KiB
C

#include "twn_game_api.h"
#include "state.h"
#include <malloc.h>
#include <math.h>
/* Emits `x` and `y` for every intersecting cell */
/* We snap position to the nearest corner, which means there's no aliasing */
/* It works great for integer radii */
#define m_iter_circle_pixels(p_center_x, p_center_y, p_radius) \
for (float y = (p_center_y + ceilf(p_radius)) - 1; y > (p_center_y - ceilf(p_radius)) - 1; --y) \
for (float x = p_center_x - ceilf(sqrtf(p_radius * p_radius - (y - p_center_y + (y <= p_center_y)) * (y - p_center_y + (y <= p_center_y)))); x < p_center_x + ceilf(sqrtf(p_radius * p_radius - (y - p_center_y + (y <= p_center_y)) * (y - p_center_y + (y <= p_center_y)))); ++x)
void game_tick(void) {
if (ctx.initialization_needed) {
if (!ctx.udata) {
ctx.udata = ccalloc(1, sizeof (struct state));
struct state *state = ctx.udata;
state->r = 10;
}
}
struct state *state = ctx.udata;
Vec2 const mouse_snap = {floorf(ctx.mouse_position.x / 8) * 8, floorf(ctx.mouse_position.y / 8) * 8};
input_action("up", CONTROL_LEFT_MOUSE);
input_action("down", CONTROL_RIGHT_MOUSE);
if (input_action_just_pressed("up"))
state->r += 1;
if (input_action_just_pressed("down"))
state->r -= 1;
float const rs = state->r * state->r;
float const cr = ceilf(state->r);
for (float iy = cr - 1; iy > -cr - 1; --iy) {
float const dx = ceilf(sqrtf(rs - (iy + (iy <= 0)) * (iy + (iy <= 0))));
for (float ix = -dx; ix < dx; ++ix) {
draw_box((Rect){mouse_snap.x + ix * 8, mouse_snap.y + iy * 8, 8, 8}, 1, (Color){255, 0, 0, 125});
}
}
draw_circle(mouse_snap, state->r * 8, (Color){125, 125, 125, 125});
}
void game_end(void) {
/* do your deinitialization here */
struct state *state = ctx.udata;
free(state);
}