struct jumpnrun_enemy;
typedef struct jumpnrun_enemy_type {
- unsigned animation_ticks_per_frame;
- size_t animation_length;
+ uint8_t animation_ticks_per_frame;
+ uint8_t animation_length;
badge_sprite const *animation_frames;
rectangle hitbox;
void jumpnrun_game_state_init(jumpnrun_game_state *state, jumpnrun_level const *lv) {
memset(state, 0, sizeof(*state));
- jumpnrun_player_spawn(&state->player, lv->start_pos, 3);
+ jumpnrun_player_spawn(&state->player, lv->start_pos, lv->start_lives);
}
void jumpnrun_game_state_respawn(jumpnrun_game_state *state, jumpnrun_level const *lv) {
#include <stddef.h>
#include <stdio.h>
-static vec2d const gravity () { return (vec2d) { FIXED_POINT(0, 0), FIXED_POINT(0, 56) }; }
-static vec2d const move_max () { return (vec2d) { FIXED_POINT(0, 600), FIXED_POINT(1, 300) }; }
-static fixed_point const accel_horiz () { return FIXED_POINT(0, 50); }
-static fixed_point const accel_vert () { return FIXED_POINT(0, 167); }
-static fixed_point const drag_factor () { return FIXED_POINT(0, 854); }
+static vec2d gravity () { return (vec2d) { FIXED_POINT(0, 0), FIXED_POINT(0, 56) }; }
+static vec2d move_max () { return (vec2d) { FIXED_POINT(0, 600), FIXED_POINT(1, 300) }; }
+static fixed_point accel_horiz () { return FIXED_POINT(0, 50); }
+static fixed_point accel_vert () { return FIXED_POINT(0, 167); }
+static fixed_point drag_factor () { return FIXED_POINT(0, 854); }
static inline int imax(int x, int y) {
return x < y ? y : x;
if(state->tick == 0) {
badge_framebuffer fb = { { { 0 } } };
- for(size_t tile = tilerange.first; tile < tilerange.last; ++tile) {
+ for(uint16_t tile = tilerange.first; tile < tilerange.last; ++tile) {
jumpnrun_render_tile(&fb, state, &lv->tiles[tile]);
}
- for(size_t item = 0; item < lv->header.item_count; ++item) {
+ for(uint16_t item = 0; item < lv->header.item_count; ++item) {
jumpnrun_item *item_obj = &lv->items[item];
if(item_obj->flags & JUMPNRUN_ITEM_COLLECTED) {
}
}
- for(size_t shot_ix = 0; shot_ix < JUMPNRUN_MAX_SHOTS; ++shot_ix) {
+ for(uint16_t shot_ix = 0; shot_ix < JUMPNRUN_MAX_SHOTS; ++shot_ix) {
jumpnrun_shot *shot = &state->shots[shot_ix];
jumpnrun_shot_process(shot);
if(jumpnrun_shot_spawned(shot)) {
}
}
- for(size_t enemy_ix = 0; enemy_ix < lv->header.enemy_count; ++enemy_ix) {
+ for(uint16_t enemy_ix = 0; enemy_ix < lv->header.enemy_count; ++enemy_ix) {
jumpnrun_enemy *enemy = &lv->enemies[enemy_ix];
jumpnrun_process_enemy(enemy, &fb, state, lv, &tilerange, &inertia_mod);
}
state->player.base.anim_frame = 0;
}
} else {
- for(size_t shot_ix = 0; shot_ix < JUMPNRUN_MAX_SHOTS; ++shot_ix) {
+ for(uint16_t shot_ix = 0; shot_ix < JUMPNRUN_MAX_SHOTS; ++shot_ix) {
jumpnrun_shot_process(&state->shots[shot_ix]);
}
- for(size_t enemy_ix = 0; enemy_ix < lv->header.enemy_count; ++enemy_ix) {
+ for(uint16_t enemy_ix = 0; enemy_ix < lv->header.enemy_count; ++enemy_ix) {
jumpnrun_enemy *enemy = &lv->enemies[enemy_ix];
jumpnrun_process_enemy(enemy, NULL, state, lv, &tilerange, &inertia_mod);
}
dest->start_pos.y = FIXED_INT( spos[1] * JUMPNRUN_TILE_PIXEL_HEIGHT);
}
+#ifdef __linux__
+ if(1 != fread(&dest->start_lives, 1, 1, fd)) {
+#else
+ if(FR_OK != f_read(fd, &dest->start_lives, sizeof(dest->start_lives), &count) || count != sizeof(dest->start_lives)) {
+#endif
+ return JUMPNRUN_LEVEL_LOAD_ERROR;
+ }
+
for(i = 0; i < dest->header.tile_count; ++i) {
#ifdef __linux__
if(1 != fread(buf, 3, 1, fd)) {
#include <stdio.h>
typedef struct jumpnrun_level_header {
- size_t tile_count;
- size_t item_count;
- size_t enemy_count;
+ uint16_t tile_count;
+ uint16_t item_count;
+ uint16_t enemy_count;
} jumpnrun_level_header;
typedef struct jumpnrun_level {
jumpnrun_level_header header;
vec2d start_pos;
+ uint8_t start_lives;
jumpnrun_tile *tiles;
jumpnrun_item *items;
var.tiles = var ## _tiles; \
var.items = var ## _items; \
var.enemies = var ## _enemies;
-#else
+#else
#ifdef __linux__
int jumpnrun_load_level_header_from_file(jumpnrun_level *dest, FILE *fd);
int jumpnrun_load_level_from_file (jumpnrun_level *dest, FILE *fd);
-#define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
- memset(&(lv), 0, sizeof(lv)); \
- FILE *fd = fopen((lvname), "r"); \
- if(fd == NULL) return JUMPNRUN_ERROR; \
- int err = jumpnrun_load_level_header_from_file(&(lv), fd); \
- if(err != 0) { \
- fclose(fd); \
- return JUMPNRUN_ERROR; \
- } \
- JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
- err = jumpnrun_load_level_from_file(&(lv), fd); \
- fclose(fd); \
+#define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
+ memset(&(lv), 0, sizeof(lv)); \
+ FILE *fd = fopen((lvname), "r"); \
+ if(fd == NULL) return JUMPNRUN_ERROR; \
+ int err = jumpnrun_load_level_header_from_file(&(lv), fd); \
+ if(err != 0) { \
+ fclose(fd); \
+ return JUMPNRUN_ERROR; \
+ } \
+ JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
+ err = jumpnrun_load_level_from_file(&(lv), fd); \
+ fclose(fd); \
if(err != 0) return JUMPNRUN_ERROR;
#else
#include <drivers/fatfs/ff.h>
int jumpnrun_load_level_header_from_file(jumpnrun_level *dest, FIL *fd);
int jumpnrun_load_level_from_file (jumpnrun_level *dest, FIL *fd);
-#define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
- memset(&(lv), 0, sizeof(lv)); \
- FIL fd; \
- if(FR_OK != f_open(&fd, (lvname), FA_OPEN_EXISTING | FA_READ)) { \
- return JUMPNRUN_ERROR; \
- } \
- if(0 != jumpnrun_load_level_header_from_file(&(lv), &fd)) { \
- f_close(&fd); \
- return JUMPNRUN_ERROR; \
- } \
- JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
- int err = jumpnrun_load_level_from_file(&(lv), &fd); \
- f_close(&fd); \
- if(err != 0) { \
- return JUMPNRUN_ERROR; \
+#define JUMPNRUN_LEVEL_LOAD(lv, lvname) \
+ memset(&(lv), 0, sizeof(lv)); \
+ FIL fd; \
+ if(FR_OK != f_open(&fd, (lvname), FA_OPEN_EXISTING | FA_READ)) { \
+ return JUMPNRUN_ERROR; \
+ } \
+ if(0 != jumpnrun_load_level_header_from_file(&(lv), &fd)) { \
+ f_close(&fd); \
+ return JUMPNRUN_ERROR; \
+ } \
+ JUMPNRUN_LEVEL_MAKE_SPACE(lv); \
+ int err = jumpnrun_load_level_from_file(&(lv), &fd); \
+ f_close(&fd); \
+ if(err != 0) { \
+ return JUMPNRUN_ERROR; \
}
#endif
V bird
W bird_straight
X bird_dip
+
+[parameters]
+L 5
} jumpnrun_tile;
typedef struct jumpnrun_tile_range {
- size_t first;
- size_t last; // actually one past last.
+ uint16_t first;
+ uint16_t last; // actually one past last.
} jumpnrun_tile_range;
static inline fixed_point tile_left (jumpnrun_tile const *tile) { return FIXED_INT(tile->pos.x * JUMPNRUN_TILE_PIXEL_WIDTH ); }
objmap = &item_types;
} else if(line == "[enemies]") {
objmap = &enemy_types;
+ } else if(line == "[parameters]") {
+ objmap = &level_params;
} else {
throw std::invalid_argument("Unkown type: " + line);
}
static_cast<uint16_t>(player_pos.second)
};
- dest.write(static_cast<char const *>(static_cast<void const *>(head)), sizeof(head));
- dest.write(static_cast<char const *>(static_cast<void const *>(spos)), sizeof(spos));
+ uint8_t lives = 3;
+ {
+ std::map<char, std::string>::const_iterator iter = level_params.find('L');
+ if(iter != level_params.end()) {
+ unsigned x;
+ std::istringstream parser(iter->second);
+ if(parser >> x) {
+ lives = static_cast<uint8_t>(x);
+ }
+ }
+ }
+
+ dest.write(static_cast<char const *>(static_cast<void const *>(head )), sizeof(head ));
+ dest.write(static_cast<char const *>(static_cast<void const *>(spos )), sizeof(spos ));
+ dest.write(static_cast<char const *>(static_cast<void const *>(&lives)), sizeof(lives));
dump_tiles(dest);
dump_items(dest);
std::map<char, std::string> tile_types;
std::map<char, std::string> item_types;
std::map<char, std::string> enemy_types;
+ std::map<char, std::string> level_params;
};
}