Skip to content

Commit

Permalink
Added beginner/expert & code screens with sine effect
Browse files Browse the repository at this point in the history
  • Loading branch information
twojstaryzdomu committed Aug 3, 2022
1 parent af03b0a commit baa6a22
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ blues: main.o sys.o util.o $(BB_SRCS:.c=.o)
bbja: main.o sys.o util.o $(JA_SRCS:.c=.o)
$(CC) $(LDFLAGS) -o $@ $^ $(SDL_LIBS) $(MODPLUG_LIBS)

pre2: main.o sys.o util.o $(P2_SRCS:.c=.o)
pre2: main.o sys.o sys_sine.o util.o $(P2_SRCS:.c=.o)
$(CC) $(LDFLAGS) -o $@ $^ $(SDL_LIBS) $(MODPLUG_LIBS)

clean:
Expand Down
214 changes: 190 additions & 24 deletions p2/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,157 @@ static void do_titus_screen() {
}
}

static void do_motif_screen() {
uint8_t *data = load_file("MOTIF.SQZ");
g_sys.sine = true;
if (data) {
g_sys.set_screen_palette(motif_palette_data, 0, 16, 6);
do {
video_resize();
video_copy_img(data);
video_copy_centred(g_res.background, ORIG_W, ORIG_H);
if (g_sys.cycle_palette) {
g_vars.palette = (g_vars.palette + g_sys.palette_offset + UNIQUE_PALETTES) % UNIQUE_PALETTES;
g_sys.set_screen_palette(unique_palettes_tbl[g_vars.palette], 0, 16, 6);
g_sys.cycle_palette = false;
}
g_sys.update_screen(g_res.vga, 0);
video_load_sprites();
video_draw_motif_string("MODE", 0, 0, 1);
char *s = g_vars.expert_flag
? "EXPERT"
: "BEGINNER";
video_draw_motif_string(s, 0, 32, 2);
g_sys.update_screen_cached(g_res.vga, 1, 1);
wait_input(1);
if (g_sys.input.direction) {
g_vars.expert_flag = !g_vars.expert_flag;
g_sys.input.direction = 0;
}
while (g_sys.paused)
wait_input(100);
if (g_sys.input.space)
break;
g_sys.render_clear_sprites();
} while (!g_vars.input.key_space || !g_sys.input.quit);
g_vars.input.key_space = 0;
g_sys.fade_out_palette();
free(data);
}
g_sys.sine = false;
}

static unsigned char atoh(unsigned char data) {
if (data > '9')
data += 9;
return (data &= 0x0F);
}

static void update_code(uint16_t *buf, uint8_t *nibble_index) {
if (g_sys.input.hex) {
int i = atoh(g_sys.input.hex);
if (i < 0xF + 1) {
if (*nibble_index < CODE_LEN) {
int l = (CODE_LEN - *nibble_index - 1) * 4;
*buf += i << l;
++*nibble_index;
} else {
wait_input(100);
memset(buf, 0, *nibble_index);
*nibble_index = 0;
}
}
g_sys.input.hex = 0;
}
}

static void print_codes() {
for (int l = 0; l < 8; l++)
print_debug(DBG_GAME, "print_codes: level %d = %04X expert %04X", l, random_get_number3(l), random_get_number3(l + 10));
}

static bool parse_code(uint16_t *buf, uint8_t *index) {
bool rc = false;
if (*index == CODE_LEN) {
print_debug(DBG_GAME, "parse_code: %04X", *buf);
uint8_t levels = 8;
uint8_t expert = 10;
for (uint8_t l = 0; l < levels; l++) {
for (uint16_t r = l; r < levels + expert; r += expert)
if (*buf == random_get_number3(r)) {
g_vars.level_num = l;
g_vars.expert_flag = r / expert;
rc = true;
break;
}
if (rc)
break;
}
if (!rc) {
if (*buf == level_code[g_vars.password_flag]) {
print_debug(DBG_GAME, "parse_code: password matched %04X", level_code[g_vars.password_flag]);
if (g_vars.password_flag == CODE_COUNT - 1)
print_debug(DBG_GAME, "parse_code: now enter level");
++g_vars.password_flag;
memset(buf, 0, sizeof(uint16_t));
*index = 0;
} else {
if (g_vars.password_flag == CODE_COUNT) {
if (*buf < 14)
g_vars.level_num = *buf;
g_vars.expert_flag = *buf / expert;
}
g_vars.password_flag = 0;
rc = true;
}
}
}
if (rc)
print_debug(DBG_GAME, "parse_code: level %d, expert %d", g_vars.level_num, g_vars.expert_flag);
return rc;
}

static void do_code_screen() {
uint8_t *data = load_file("MOTIF.SQZ");
g_sys.sine = true;
if (data) {
char str[CODE_LEN + 1];
uint8_t nibble_index = 0;
uint16_t *buf = calloc(1, sizeof(int16_t));
g_sys.input.raw = true;
g_sys.set_screen_palette(motif_palette_data, 0, 16, 6);
do {
video_resize();
video_copy_img(data);
video_copy_centred(g_res.background, ORIG_W, ORIG_H);
if (g_sys.cycle_palette) {
g_vars.palette = (g_vars.palette + g_sys.palette_offset + UNIQUE_PALETTES) % UNIQUE_PALETTES;
g_sys.set_screen_palette(unique_palettes_tbl[g_vars.palette], 0, 16, 6);
g_sys.cycle_palette = false;
}
g_sys.update_screen(g_res.vga, 0);
video_load_sprites();
video_draw_motif_string("ENTER CODE", 0, -10, 2);
sprintf(str, "%04X", *buf);
video_draw_motif_string(str, 0, 40, 1);
g_sys.update_screen_cached(g_res.vga, 1, 1);
if (g_sys.input.space || parse_code(buf, &nibble_index))
break;
do {
wait_input(5);
} while (g_sys.paused);
update_code(buf, &nibble_index);
g_sys.render_clear_sprites();
} while (!g_vars.input.key_space || !g_sys.input.quit);
g_vars.input.key_space = 0;
g_sys.input.raw = false;
g_sys.fade_out_palette();
free(data);
print_codes();
}
g_sys.sine = false;
}

static bool fade_palettes(const uint8_t *target, uint8_t *current) {
bool flag = false;
for (int i = 0; i < 768; ++i) {
Expand Down Expand Up @@ -252,11 +403,11 @@ static void do_map(){
if (g_sys.input.quit || g_sys.input.space) {
break;
}
while (g_sys.paused) {
do {
wait_input(10);
}
} while (g_sys.paused);
}
wait_input(1000);
g_sys.sleep(1000);
g_sys.fade_out_palette();
free(g_res.map);
}
Expand Down Expand Up @@ -298,8 +449,8 @@ static void do_menu2() {
}
}

static bool do_menu() {
bool rc = false;
static int do_menu() {
int rc = 0;
uint8_t *data = load_file("MENU.SQZ");
if (data) {
g_sys.set_screen_palette(data, 0, 256, 6);
Expand All @@ -311,22 +462,24 @@ static bool do_menu() {
memset(g_vars.input.keystate, 0, sizeof(g_vars.input.keystate));
const uint32_t start = g_sys.get_timestamp();
while (!g_sys.input.quit) {
wait_input(30);
update_input(300);
if (g_vars.input.keystate[2] || g_vars.input.keystate[0x4F] || g_sys.input.space) {
g_sys.input.space = 0;
g_sys.fade_out_palette();
rc = 1;
break;
}
if (g_vars.input.keystate[3] || g_vars.input.keystate[0x50]) {
g_sys.fade_out_palette();
rc = 2;
break;
}
if (g_sys.resize) {
break;
}
if (!g_res.dos_demo && g_sys.get_timestamp() - start >= 15 * 1000) {
g_sys.fade_out_palette();
rc = true;
rc = 3;
break;
}
}
Expand Down Expand Up @@ -434,6 +587,7 @@ static void game_run(const char *data_path) {
do_present_screen();
}
g_vars.random.e = 0x1234;
g_vars.expert_flag = false;
g_vars.starttime = g_sys.get_timestamp();
while (!g_sys.input.quit) {
if (1) {
Expand All @@ -444,28 +598,40 @@ static void game_run(const char *data_path) {
if (g_res.dos_demo) {
do_demo_screen();
}
while (do_menu()) {
do_menu2();
int rc;
while ((rc = do_menu())) {
if (rc == 1)
break;
else if (rc == 2) {
do_code_screen();
if (g_vars.level_num || g_vars.expert_flag)
break;
} else
do_menu2();
}
if (g_sys.input.quit) {
break;
}
if (!(g_vars.level_num || g_vars.expert_flag))
do_motif_screen();
uint8_t level_num;
do {
g_sys.render_set_sprites_clipping_rect(0, 0, GAME_SCREEN_W, TILEMAP_SCREEN_H);
level_num = g_vars.level_num;
if (g_vars.level_num >= 8 && g_vars.level_num < 10 && 0 /* !g_vars.level_expert_flag */ ) {
do_castle_screen();
break;
}
if (g_vars.level_num < 10 && g_options.show_map)
do_map();
if (g_sys.input.quit)
break;
do_level();
print_debug(DBG_GAME, "previous level %d current %d", level_num, g_vars.level_num);
} while (!g_res.dos_demo && g_vars.level_num != level_num);
g_vars.level_num = 0;
g_vars.expert_flag = false;
}
uint8_t level_num;
do {
g_sys.render_set_sprites_clipping_rect(0, 0, GAME_SCREEN_W, TILEMAP_SCREEN_H);
level_num = g_vars.level_num;
if (g_vars.level_num >= 8 && g_vars.level_num < 10 && 0 /* !g_vars.level_expert_flag */ ) {
do_castle_screen();
break;
}
if (g_vars.level_num < 10 && g_options.show_map)
do_map();
if (g_sys.input.quit)
break;
do_level();
print_debug(DBG_GAME, "previous level %d current %d", level_num, g_vars.level_num);
} while (!g_res.dos_demo && g_vars.level_num != level_num);
}
sound_fini();
res_fini();
Expand Down
15 changes: 11 additions & 4 deletions p2/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "intern.h"
#include "message.h"
#include "sys.h"
#include "sys_sine.h"

extern struct options_t g_options;

Expand Down Expand Up @@ -33,9 +34,12 @@ extern struct options_t g_options;
#define MAP_W 640
#define MAP_H 200

#define UNIQUE_PALETTES 16
#define UNIQUE_PALETTES 17
#define LIGHT_PALETTE_OFFSET 12

#define CODE_LEN 4
#define CODE_COUNT sizeof(level_code) / sizeof(uint16_t)

struct club_anim_t {
const uint8_t *anim; /* uint16_t[4] : player spr, club spr, x, y */
uint8_t a;
Expand Down Expand Up @@ -142,7 +146,7 @@ struct boss_level5_leaf_t {
struct orb_t {
uint16_t x_pos;
uint16_t y_pos;
uint8_t index_tbl; /* cos_/sin_tbl */
uint8_t index_tbl; /* cos_/sin_tbl_sys */
uint8_t radius;
};

Expand Down Expand Up @@ -171,6 +175,8 @@ struct vars_t {
} input;

uint8_t level_num;
bool expert_flag;
uint8_t password_flag;
uint32_t score;
uint16_t score_extra_life;

Expand Down Expand Up @@ -357,6 +363,7 @@ extern const uint8_t present_palette_data[256 * 3];
extern const uint8_t menu_palette_data[16 * 3];
extern const uint8_t joystick_palette_data[16 * 3];
extern const uint8_t gameover_palette_data[16 * 3];
extern const uint8_t motif_palette_data[16 * 3];
extern const uint8_t *unique_palettes_tbl[UNIQUE_PALETTES];
extern const uint8_t spr_offs_tbl[922];
extern const uint8_t spr_size_tbl[922];
Expand All @@ -367,8 +374,6 @@ extern const struct club_anim_t club_anim_tbl[4];
extern const uint8_t player_anim_lut[32];
extern const uint8_t player_flying_anim_data[100]; /* uint16_t: player_spr_num, uint16_t: flying_spr_num, int8_t: dx, int8_t: dy */
extern const uint8_t vscroll_offsets_data[132];
extern const uint8_t cos_tbl[256];
extern const uint8_t sin_tbl[256];
extern const uint16_t monster_spr_tbl[48];
extern const uint8_t monster_anim_tbl[1100];
extern const uint8_t boss_minotaur_seq_data[86];
Expand All @@ -380,6 +385,7 @@ extern const uint8_t demo_anim_data[26];
extern const uint8_t gameover_anim1_data[18];
extern const uint8_t gameover_anim2_data[34];
extern const uint8_t gameover_anim3_data[20];
extern const uint16_t level_code[3];

/* game.c */
extern void update_input();
Expand Down Expand Up @@ -420,6 +426,7 @@ extern void video_draw_number(int offset, int num);
extern void video_draw_character_spr(int offset, uint8_t chr);
extern void video_draw_format_string(int offset, const char *format, ...);
extern void video_draw_string_centred(const char *s, bool clip);
extern void video_draw_motif_string(const char *s, bool clip, int y_offset, int wspace);
extern void video_draw_tile(const uint8_t *src, int x, int y);
extern void video_convert_tiles(uint8_t *data, int len);
extern void video_load_front_tiles();
Expand Down
Loading

0 comments on commit baa6a22

Please sign in to comment.