чување 4872820c9b59da8d24ec06e79b84b0ba562058f2
родитељ e2fef8f1d90de8455ca1e0c85f23ead9ca07ab7e
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Sun, 10 Aug 2025 20:34:59 +0200
Code cleanup
Diffstat:
| M | linije.c | | | 77 | +++++++++++++++++------------------------------------------------------------ |
измењених датотека: 1, додавања: 17(+), брисања: 60(-)
diff --git a/linije.c b/linije.c
@@ -214,11 +214,9 @@ void init_state(struct State* state);
int load_textures(struct State* state);
void location_delete(struct State* state, const int index);
int location_index(const struct State* state, const SDL_FPoint* location);
-int location_pop(struct State* state, SDL_FPoint* location);
void location_push(struct State* state, const SDL_FPoint* location);
void mark_five(struct State* state, SDL_FPoint* start, SDL_FPoint* end);
void mark_path(struct Cell* c);
-int passable(const struct State* state, const int x, const int y);
void pop_strings(struct State* state);
void prepare_dialog(struct State* state, const int w, const int h,
const char** text, const int show);
@@ -931,7 +929,6 @@ void
init_state(struct State* state)
{
assert(state != NULL);
-
init_animation(&state->animation);
state->background = NULL;
state->grid = NULL;
@@ -1012,18 +1009,6 @@ location_index(const struct State* state, const SDL_FPoint* location)
return -1;
}
-int
-location_pop(struct State* state, SDL_FPoint* location)
-{
- assert((state != NULL) && (location != NULL));
- if (state->location_pointer == 0)
- return 1;
- state->location_pointer--;
- location->x = state->location_stack[state->location_pointer].x;
- location->y = state->location_stack[state->location_pointer].y;
- return 0;
-}
-
void
location_push(struct State* state, const SDL_FPoint* location)
{
@@ -1088,14 +1073,6 @@ mark_path(struct Cell* c)
mark_path(c->path_prev);
}
-int
-passable(const struct State* state, const int x, const int y)
-{
- assert(state != NULL);
- return x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT
- && state->grid[INDEX(x, y)].contents == CELL_EMPTY;
-}
-
void
pop_strings(struct State* state)
{
@@ -1153,7 +1130,6 @@ print_matrix(const struct Cell* M)
}
printf("\n");
}
- // printf("\n");
}
void
@@ -1310,6 +1286,7 @@ render_dialog(struct State* state)
assert(state != NULL);
assert(state->dialog_text != NULL);
+ assert(state->text_scale_factor != 0.0f);
ptext = state->dialog_text;
w = state->dialog_dims.x;
@@ -1363,6 +1340,7 @@ render_selection(struct State* state, const float startx, const float starty)
float csf;
assert(state != NULL);
+ assert(state->cell_scale_factor != 0.0f);
csf = state->cell_scale_factor;
get_sprite_xy(CELL_SELECTED, &crx, &cry);
@@ -1501,12 +1479,14 @@ reset(struct State* state)
struct Cell* c = NULL;
int x, y;
+ assert(state != NULL);
state->location_pointer = 0;
for (y = 0; y < GRID_HEIGHT; y++)
for (x = 0; x < GRID_WIDTH; x++)
{
c = &state->grid[INDEX(x, y)];
c->contents = CELL_EMPTY;
+ c->empty = 1;
c->popping = 0;
location_push(state, &c->coords);
}
@@ -1552,10 +1532,12 @@ screen_coord_to_grid_coord(struct State* state, const int x, const int y,
{
float _factor_x, _factor_y;
float _startx, _starty;
+ float csf;
assert(state != NULL);
assert((to_x != NULL) && (to_y != NULL)
&& (state->cell_scale_factor != 0.0f));
+ csf = state->cell_scale_factor;
_factor_x = (float)state->display_width / state->screen_width;
_factor_y = (float)state->display_height / state->screen_height;
_startx = startx * _factor_x;
@@ -1563,17 +1545,15 @@ screen_coord_to_grid_coord(struct State* state, const int x, const int y,
/* clang-format off */
*to_x = x >= _startx
- ? (x < _startx + _factor_x * state->cell_scale_factor
+ ? (x < _startx + _factor_x * csf
* CELL_SIZE * GRID_WIDTH
- ? (x - _startx) / (_factor_x * state->cell_scale_factor
- * CELL_SIZE)
+ ? (x - _startx) / (_factor_x * csf * CELL_SIZE)
: GRID_WIDTH - 1)
: 0;
*to_y = y >= _starty
- ? (y < _starty + _factor_y * state->cell_scale_factor
+ ? (y < _starty + _factor_y * csf
* CELL_SIZE * GRID_HEIGHT
- ? (y - _starty) / (_factor_y * state->cell_scale_factor
- * CELL_SIZE)
+ ? (y - _starty) / (_factor_y * csf * CELL_SIZE)
: GRID_HEIGHT - 1)
: 0;
/* clang-format on */
@@ -1585,10 +1565,12 @@ screen_coord_to_grid_coordF(struct State* state, const float x, const float y,
{
float _factor_x, _factor_y;
float _startx, _starty;
+ float csf;
assert(state != NULL);
assert((to_x != NULL) && (to_y != NULL)
&& (state->cell_scale_factor != 0.0f));
+ csf = state->cell_scale_factor;
_factor_x = (float)state->display_width / state->screen_width;
_factor_y = (float)state->display_height / state->screen_height;
_startx = startx * _factor_x;
@@ -1596,17 +1578,13 @@ screen_coord_to_grid_coordF(struct State* state, const float x, const float y,
/* clang-format off */
*to_x = x >= _startx
- ? (x < _startx + _factor_x * state->cell_scale_factor
- * CELL_SIZE * GRID_WIDTH
- ? (x - _startx) / (_factor_x * state->cell_scale_factor
- * CELL_SIZE)
+ ? (x < _startx + _factor_x * csf * CELL_SIZE * GRID_WIDTH
+ ? (x - _startx) / (_factor_x * csf * CELL_SIZE)
: GRID_WIDTH - 1)
: 0;
*to_y = y >= _starty
- ? (y < _starty + _factor_y * state->cell_scale_factor
- * CELL_SIZE * GRID_HEIGHT
- ? (y - _starty) / (_factor_y * state->cell_scale_factor
- * CELL_SIZE)
+ ? (y < _starty + _factor_y * csf * CELL_SIZE * GRID_HEIGHT
+ ? (y - _starty) / (_factor_y * csf * CELL_SIZE)
: GRID_HEIGHT - 1)
: 0;
/* clang-format on */
@@ -1704,7 +1682,6 @@ step_animation(struct State* state)
}
}
- // print_path(&grid[INDEXFI(animation.start.x, animation.start.y)]);
state->animation.position.x += state->animation.delta.x;
state->animation.position.y += state->animation.delta.y;
state->animation.step++;
@@ -1760,8 +1737,6 @@ main(int argc, char** argv)
unsigned int last_time, current_time;
int x, y;
- /*atexit(&cleanup);*/
-
init_state(&state);
if (!SDL_Init(SDL_INIT_VIDEO))
@@ -1819,24 +1794,6 @@ main(int argc, char** argv)
c->down = &state.grid[INDEX(x, y + 1)];
}
- /*grid.width = GRID_WIDTH;
- grid.height = GRID_HEIGHT;
- grid.cells = (char**)calloc(GRID_HEIGHT, sizeof(char*));
- if (!grid.cells)
- {
- SDL_LogError(SDL_LOG_CATEGORY_ERROR, "calloc failed");
- exit(1);
- }
- for (y = 0; y < GRID_HEIGHT; y++)
- {
- grid.cells[y] = (char*)calloc(GRID_WIDTH, 1);
- if (!grid.cells[y])
- {
- SDL_LogError(SDL_LOG_CATEGORY_ERROR, "calloc failed");
- exit(1);
- }
- }*/
-
if (load_textures(&state) < 0)
do_exit(1, &state);
@@ -1862,9 +1819,9 @@ main(int argc, char** argv)
current_time = SDL_GetTicks();
if (state.animation.active)
{
+ draw(&state);
if (current_time > last_time + 1000 / FPS)
{
- draw(&state);
step_animation(&state);
last_time = current_time;
}