чување 0c24dad52c742a272bd7be51abc53e5437205bc5
родитељ 7c5debb5d2944784b1b132e10349f2c3d3d8b865
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Tue, 15 Aug 2023 18:21:11 +0200
l02.dat: New file; lav-sdl.c: Optimize loading, scale_factor calculation
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| A | l02.dat | | | 22 | ++++++++++++++++++++++ |
| M | lav-sdl.c | | | 51 | +++++++++++++++++++++++++-------------------------- |
измењених датотека: 2, додавања: 47(+), брисања: 26(-)
diff --git a/l02.dat b/l02.dat
@@ -0,0 +1,22 @@
+1 1
+40 20
+0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 1
+1 1 1 1 1 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1
+0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 1 0
+0 1 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 0 0 1 0 0 1 1 1 0 1 0 1 0 1 0 1 0 1 1 1
+0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1
+0 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1 0 1 0 1 0 1 1 1
+0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1
+0 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1
+0 1 0 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 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#define DEFAULT_HEIGHT 600
+#define DEFAULT_WIDTH 1280
+#define DEFAULT_HEIGHT 960
#define LABYRINTH_MAXHEIGHT 20
#define LABYRINTH_MAXWIDTH 40
#define LOCATION_STACK_MAX (LABYRINTH_MAXWIDTH * LABYRINTH_MAXHEIGHT)
@@ -30,6 +30,7 @@
#define WIN_TITLE "Lavirint (SDL version)"
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
/* clang-format off */
enum {
@@ -509,16 +510,17 @@ load_labyrinth(const char* pathname)
if (ch == EOF)
continue;
- if (ch == '1')
+ if (strchr("01", ch))
{
- labyrinth.cells[y][x] = CELL_WALL;
- x++;
- if (firstline)
- labyrinth.width++;
- }
- else if (ch == '0')
- {
- labyrinth.cells[y][x] = CELL_EMPTY;
+ if (x == LABYRINTH_MAXWIDTH || y == LABYRINTH_MAXHEIGHT)
+ {
+ print_error("Labyrinth overflow at %d, %d"
+ " (ch == %c)",
+ x, y, ch);
+ exit(1);
+ }
+ labyrinth.cells[y][x] = (ch == '0' ? CELL_EMPTY :
+ CELL_WALL);
x++;
if (firstline)
labyrinth.width++;
@@ -536,19 +538,6 @@ load_labyrinth(const char* pathname)
print_debug("Unknown character in labyrinth file: '%c'"
" = %X",
ch, ch);
-
- if (x == LABYRINTH_MAXWIDTH)
- {
- x = 0;
- y++;
- firstline = 0;
- labyrinth.height++;
- }
- if (y == LABYRINTH_MAXHEIGHT)
- {
- print_error("Labyrinth overflow");
- exit(1);
- }
}
hero.x = start.x;
@@ -722,9 +711,19 @@ reset(void)
void
update_size(void)
{
+ float factor1;
+ float factor2;
+
SDL_GetWindowSize(window, &screen_width, &screen_height);
- scale_factor = MIN((float)screen_width / DEFAULT_WIDTH,
- (float)screen_height / DEFAULT_HEIGHT);
+
+ factor1 = (float)screen_width / DEFAULT_WIDTH;
+ factor2 = (float)screen_height / DEFAULT_HEIGHT;
+
+ scale_factor = MAX(factor1, factor2);
+ if (LABYRINTH_MAXWIDTH * CELL_SIZE * scale_factor > screen_width
+ || LABYRINTH_MAXHEIGHT * CELL_SIZE * scale_factor >
+ screen_height)
+ scale_factor = MIN(factor1, factor2);
}
int