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