Schrift fest in Firmware eingebacken, damit die auch funktioniert, wenn auf dem
[hackover2013-badge-firmware.git] / badge / jumpnrun / levels.h
1 #ifndef INCLUDED_JUMPNRUN_LEVEL_H
2 #define INCLUDED_JUMPNRUN_LEVEL_H
3
4 #include "enemies.h"
5 #include "items.h"
6 #include "tiles.h"
7
8 #include "../util/rectangle.h"
9
10 #include <stddef.h>
11 #include <stdio.h>
12
13 #define JUMPNRUN_PATH "/hackio"
14
15 typedef struct jumpnrun_level_header {
16 uint16_t tile_count;
17 uint16_t item_count;
18 uint16_t enemy_count;
19 } jumpnrun_level_header;
20
21 typedef struct jumpnrun_level {
22 jumpnrun_level_header header;
23
24 vec2d start_pos;
25 uint8_t start_lives;
26
27 jumpnrun_tile *tiles;
28 jumpnrun_item *items;
29 jumpnrun_enemy *enemies;
30 } jumpnrun_level;
31
32 enum {
33 JUMPNRUN_LEVEL_LOAD_OK,
34 JUMPNRUN_LEVEL_LOAD_ERROR
35 };
36
37 size_t jumpnrun_level_count(void);
38 void jumpnrun_levels_dump(void);
39
40 // Use stack-local VLAs to store dynamic content.
41 #define JUMPNRUN_LEVEL_MAKE_SPACE(var) \
42 jumpnrun_tile var ## _tiles [var.header.tile_count]; \
43 jumpnrun_item var ## _items [var.header.item_count]; \
44 jumpnrun_enemy var ## _enemies[var.header.enemy_count]; \
45 \
46 var.tiles = var ## _tiles; \
47 var.items = var ## _items; \
48 var.enemies = var ## _enemies;
49 #else
50
51 #ifdef __linux__
52
53 int jumpnrun_load_level_header_from_file(jumpnrun_level *dest, FILE *fd);
54 int jumpnrun_load_level_from_file (jumpnrun_level *dest, FILE *fd);
55
56 #define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
57 memset(&(lv), 0, sizeof(lv)); \
58 FILE *fd = fopen((lvname), "r"); \
59 if(fd == NULL) return JUMPNRUN_ERROR; \
60 int err = jumpnrun_load_level_header_from_file(&(lv), fd); \
61 if(err != 0) { \
62 fclose(fd); \
63 return JUMPNRUN_ERROR; \
64 } \
65 JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
66 err = jumpnrun_load_level_from_file(&(lv), fd); \
67 fclose(fd); \
68 if(err != 0) return JUMPNRUN_ERROR;
69 #else
70 #include <drivers/fatfs/ff.h>
71
72 int jumpnrun_load_level_header_from_file(jumpnrun_level *dest, FIL *fd);
73 int jumpnrun_load_level_from_file (jumpnrun_level *dest, FIL *fd);
74
75 #define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
76 memset(&(lv), 0, sizeof(lv)); \
77 FIL fd; \
78 if(FR_OK != f_chdir(JUMPNRUN_PATH) || \
79 FR_OK != f_open(&fd, (lvname), FA_OPEN_EXISTING | FA_READ)) { \
80 return JUMPNRUN_ERROR; \
81 } \
82 if(0 != jumpnrun_load_level_header_from_file(&(lv), &fd)) { \
83 f_close(&fd); \
84 return JUMPNRUN_ERROR; \
85 } \
86 JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
87 int err = jumpnrun_load_level_from_file(&(lv), &fd); \
88 f_close(&fd); \
89 if(err != 0) { \
90 return JUMPNRUN_ERROR; \
91 }
92 #endif
93
94 #endif
This page took 0.048231 seconds and 5 git commands to generate.