taisei/src/stage.h

160 lines
5.1 KiB
C
Raw Normal View History

/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@alienslab.net>.
*/
#ifndef IGUARD_stage_h
#define IGUARD_stage_h
#include "taisei.h"
2017-02-27 15:27:48 +01:00
#include "projectile.h"
#include "boss.h"
#include "progress.h"
#include "difficulty.h"
#include "util/graphics.h"
#include "dialog.h"
2017-02-27 15:27:48 +01:00
/* taisei's strange macro language.
*
* sorry, I guess it is bad style, but I hardcode everything and in that case
* you'll find yourself soon in a situation where you have to spread your
* coherent thoughts over frames using masses of redundant ifs.
* I've just invented this thingy to keep track of my sanity.
*
*/
#define TIMER(ptr) int *__timep = ptr; int _i = 0, _ni = 0; _i = _ni = _i;
#define AT(t) if(*__timep == t)
#define FROM_TO(start,end,step) _ni = _ni; _i = (*__timep - (start))/(step); if(*__timep >= (start) && *__timep <= (end) && !((*__timep - (start)) % (step)))
2017-04-03 10:10:58 +02:00
// Like FROM_TO just with two different intervals:
// A pause interval step and an action interval dur. For dur frames something
2017-04-03 10:11:50 +02:00
// happens, then for step frames there is a break.
2017-04-03 10:10:58 +02:00
//
// Lastly, istep is the step inside the action interval. For istep = 2, only
// every second frame of dur is executed.
//
// Finally an example for step = 4, dur = 5, and istep = 2:
//
// A_A_A____A_A_A____A_A_A____...
//
// where A denotes a frame in which the body of FROM_TO_INT gets executed.
#define FROM_TO_INT(start, end, step, dur, istep) \
_i = (*__timep - (start))/(step+dur); _ni = ((*__timep - (start)) % (step+dur))/istep; \
if(*__timep >= (start) && *__timep <= (end) && (*__timep - (start)) % ((dur) + (step)) <= dur && !((*__timep - (start)) % (istep)))
#define GO_AT(obj, start, end, vel) if(*__timep >= (start) && *__timep <= (end)) (obj)->pos += (vel);
#define GO_TO(obj, p, f) (obj)->pos += (f)*((p) - (obj)->pos);
// This is newest addition to the macro zoo! It allows you to loop a sound like
// you loop your French- I mean your danmaku code. Nothing strange going on here.
#define PLAY_FOR(name,start, end) FROM_TO(start,end,2) { play_loop(name); }
// easy to soundify versions of FROM_TO and friends. Note how I made FROM_TO_INT even more complicated!
#define FROM_TO_SND(snd,start,end,step) PLAY_FOR(snd,start,end); FROM_TO(start,end,step)
#define FROM_TO_INT_SND(snd,start,end,step,dur,istep) FROM_TO_INT(start,end,step,dur,2) { play_loop(snd); }FROM_TO_INT(start,end,step,dur,istep)
2017-02-26 13:17:48 +01:00
typedef void (*StageProc)(void);
typedef bool (*ShaderRule)(Framebuffer*); // true = drawn to color buffer
// two highest bits of uint16_t, WAY higher than the amount of spells in this game can ever possibly be
#define STAGE_SPELL_BIT 0x8000
#define STAGE_EXTRASPELL_BIT 0x4000
typedef enum StageType {
STAGE_STORY = 1,
STAGE_EXTRA,
STAGE_SPELL,
2018-08-14 00:48:18 +02:00
STAGE_SPECIAL,
} StageType;
2017-02-27 15:27:48 +01:00
typedef struct StageProcs StageProcs;
2017-02-27 15:27:48 +01:00
struct StageProcs {
2017-02-26 13:17:48 +01:00
StageProc begin;
StageProc preload;
2017-02-26 13:17:48 +01:00
StageProc end;
StageProc draw;
StageProc event;
StageProc update;
2017-02-26 13:17:48 +01:00
ShaderRule *shader_rules;
2017-10-01 10:58:45 +02:00
ShaderRule *postprocess_rules;
2017-02-27 15:27:48 +01:00
StageProcs *spellpractice_procs;
};
2017-02-26 13:17:48 +01:00
typedef struct StageInfo {
uint16_t id; // must match type of ReplayStage.stage in replay.h
2017-02-26 13:17:48 +01:00
StageProcs *procs;
StageType type;
char *title;
char *subtitle;
AttackInfo *spell;
Difficulty difficulty;
2017-02-11 12:38:50 +01:00
// Do NOT access this directly!
// Use stage_get_progress or stage_get_progress_from_info, which will lazy-initialize it and pick the correct offset.
StageProgress *progress;
} StageInfo;
2017-02-27 15:27:48 +01:00
extern StageInfo *stages;
typedef struct StageClearBonus {
uint64_t base;
uint64_t lives;
uint64_t voltage;
uint64_t graze;
uint64_t total;
bool all_clear;
} StageClearBonus;
StageInfo* stage_get(uint16_t);
2017-02-10 23:34:48 +01:00
StageInfo* stage_get_by_spellcard(AttackInfo *spell, Difficulty diff);
StageProgress* stage_get_progress(uint16_t id, Difficulty diff, bool allocate);
StageProgress* stage_get_progress_from_info(StageInfo *stage, Difficulty diff, bool allocate);
void stage_init_array(void);
2017-02-15 13:53:24 +01:00
void stage_free_array(void);
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
void stage_enter(StageInfo *stage, CallChain next);
2017-02-26 13:17:48 +01:00
void stage_finish(int gameover);
2012-07-27 12:18:21 +02:00
void stage_pause(void);
void stage_gameover(void);
void stage_start_bgm(const char *bgm);
typedef enum ClearHazardsFlags {
CLEAR_HAZARDS_BULLETS = (1 << 0),
CLEAR_HAZARDS_LASERS = (1 << 1),
CLEAR_HAZARDS_FORCE = (1 << 2),
CLEAR_HAZARDS_NOW = (1 << 3),
CLEAR_HAZARDS_SPAWN_VOLTAGE = (1 << 4),
CLEAR_HAZARDS_ALL = CLEAR_HAZARDS_BULLETS | CLEAR_HAZARDS_LASERS,
} ClearHazardsFlags;
void stage_clear_hazards(ClearHazardsFlags flags);
void stage_clear_hazards_at(complex origin, double radius, ClearHazardsFlags flags);
2019-03-26 16:58:38 +01:00
void stage_clear_hazards_in_ellipse(Ellipse e, ClearHazardsFlags flags);
void stage_clear_hazards_predicate(bool (*predicate)(EntityInterface *ent, void *arg), void *arg, ClearHazardsFlags flags);
void stage_set_voltage_thresholds(uint easy, uint normal, uint hard, uint lunatic);
bool stage_is_cleared(void);
2019-03-11 00:21:43 +01:00
void stage_unlock_bgm(const char *bgm);
#include "stages/stage1.h"
#include "stages/stage2.h"
#include "stages/stage3.h"
#include "stages/stage4.h"
#include "stages/stage5.h"
#include "stages/stage6.h"
#endif // IGUARD_stage_h