Levelauswahl auf Badge.
[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 typedef struct jumpnrun_level_header {
14 size_t tile_count;
15 size_t item_count;
16 size_t enemy_count;
17 } jumpnrun_level_header;
18
19 typedef struct jumpnrun_level {
20 jumpnrun_level_header header;
21
22 vec2d start_pos;
23
24 jumpnrun_tile *tiles;
25 jumpnrun_item *items;
26 jumpnrun_enemy *enemies;
27 } jumpnrun_level;
28
29 enum {
30 JUMPNRUN_LEVEL_LOAD_OK,
31 JUMPNRUN_LEVEL_LOAD_ERROR
32 };
33
34 size_t jumpnrun_level_count(void);
35 void jumpnrun_levels_dump(void);
36
37 // Use stack-local VLAs to store dynamic content.
38 #define JUMPNRUN_LEVEL_MAKE_SPACE(var) \
39 jumpnrun_tile var ## _tiles [var.header.tile_count]; \
40 jumpnrun_item var ## _items [var.header.item_count]; \
41 jumpnrun_enemy var ## _enemies[var.header.enemy_count]; \
42 \
43 var.tiles = var ## _tiles; \
44 var.items = var ## _items; \
45 var.enemies = var ## _enemies;
46 #else
47
48 #ifdef __linux__
49
50 int jumpnrun_load_level_header_from_file(jumpnrun_level *dest, FILE *fd);
51 int jumpnrun_load_level_from_file (jumpnrun_level *dest, FILE *fd);
52
53 #define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
54 memset(&(lv), 0, sizeof(lv)); \
55 FILE *fd = fopen((lvname), "r"); \
56 if(fd == NULL) return JUMPNRUN_ERROR; \
57 int err = jumpnrun_load_level_header_from_file(&(lv), fd); \
58 if(err != 0) { \
59 fclose(fd); \
60 return JUMPNRUN_ERROR; \
61 } \
62 JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
63 err = jumpnrun_load_level_from_file(&(lv), fd); \
64 fclose(fd); \
65 if(err != 0) return JUMPNRUN_ERROR;
66 #else
67 #include <drivers/fatfs/ff.h>
68
69 int jumpnrun_load_level_header_from_file(jumpnrun_level *dest, FIL *fd);
70 int jumpnrun_load_level_from_file (jumpnrun_level *dest, FIL *fd);
71
72 #define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
73 memset(&(lv), 0, sizeof(lv)); \
74 FIL fd; \
75 if(FR_OK != f_open(&fd, (lvname), FA_OPEN_EXISTING | FA_READ)) { \
76 return JUMPNRUN_ERROR; \
77 } \
78 if(0 != jumpnrun_load_level_header_from_file(&(lv), &fd)) { \
79 f_close(&fd); \
80 return JUMPNRUN_ERROR; \
81 } \
82 JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
83 int err = jumpnrun_load_level_from_file(&(lv), &fd); \
84 f_close(&fd); \
85 if(err != 0) { \
86 return JUMPNRUN_ERROR; \
87 }
88 #endif
89
90 #endif
This page took 0.047504 seconds and 5 git commands to generate.