save replay

This commit is contained in:
KikooDX 2021-12-23 11:52:37 +01:00
parent 9d644f28c5
commit e2b532c956
5 changed files with 41 additions and 1 deletions

View File

@ -5,7 +5,7 @@ project(JTMM2 C)
include(GenerateG3A)
include(Fxconv)
find_package(Gint 2.6.0 REQUIRED)
find_package(Gint 2.6.0 REQUIRED) # dev branch
include_directories(inc)

View File

@ -26,3 +26,4 @@
#define MISSILE_ACCEL 0.06f
#define MISSILE_FRICTION (MISSILE_ACCEL / MISSILE_MAX_SPEED)
#define MISSILE_COOLDOWN (TARGET_FPS / 2)
#define REPLAY_SIZE 18000 /* 5 minutes */

View File

@ -1,4 +1,5 @@
#pragma once
#include "conf.h"
enum Key {
K_LEFT,
@ -17,13 +18,18 @@ enum Key {
};
enum KeyState { KS_UP, KS_DOWN, KS_PRESS };
typedef unsigned char PackedFrame;
struct Input {
enum Key keys[K_COUNT];
enum KeyState states[K_COUNT];
int replay_cursor;
PackedFrame replay[REPLAY_SIZE];
};
void input_init(void);
void input_update(void);
void input_write_replay(void);
int input_down(enum Key);
int input_pressed(enum Key);

View File

@ -1,11 +1,16 @@
#include "input.h"
#include <fcntl.h>
#include <gint/keyboard.h>
#include <sys/stat.h>
#include <unistd.h>
static struct Input input;
static const int default_map[K_COUNT] = {
KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_SHIFT, KEY_ALPHA,
KEY_EXIT, KEY_TAN, KEY_F3, KEY_F2, KEY_F1, KEY_F6};
static PackedFrame pack_frame(void);
void
input_init(void)
{
@ -15,6 +20,8 @@ input_init(void)
input.keys[i] = default_map[i];
input.states[i] = KS_UP;
}
i = REPLAY_SIZE;
input.replay_cursor = 0;
}
void
@ -28,6 +35,20 @@ input_update(void)
? (kdown ? KS_PRESS : KS_UP)
: (kdown ? KS_DOWN : KS_UP);
}
input.replay[input.replay_cursor++] = pack_frame();
if (input.replay_cursor >= REPLAY_SIZE)
input.replay_cursor = REPLAY_SIZE - 1;
}
void
input_write_replay(void)
{
const int fd = open("jtmm2.rep", O_WRONLY | O_CREAT | O_TRUNC);
if (fd != -1) {
write(fd, input.replay,
input.replay_cursor * sizeof(PackedFrame));
close(fd);
}
}
int
@ -47,3 +68,13 @@ input_up(enum Key k)
{
return input.states[k] == KS_UP;
}
static PackedFrame
pack_frame(void)
{
PackedFrame r = 0;
int i = K_EXIT;
while (i-- > 0)
r |= input_down(i) << i;
return r;
}

View File

@ -8,6 +8,7 @@
#include "util.h"
#include <gint/cpu.h>
#include <gint/display.h>
#include <gint/gint.h>
#include <gint/timer.h>
static struct Player player;
@ -72,6 +73,7 @@ deinit(void)
{
level_deinit();
missile_manager_free();
gint_world_switch(GINT_CALL(input_write_replay));
timer_stop(timer);
}