taisei/src/stage.c

695 lines
17 KiB
C
Raw Normal View History

/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
2017-09-12 03:28:15 +02:00
* Copyright (c) 2011-2017, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2017, Andrei Alexeyev <akari@alienslab.net>.
*/
#include "util.h"
#include "stage.h"
#include <time.h>
#include "global.h"
2012-07-28 22:53:53 +02:00
#include "video.h"
2017-01-24 14:40:57 +01:00
#include "resource/bgm.h"
#include "replay.h"
#include "config.h"
#include "player.h"
#include "menu/ingamemenu.h"
#include "menu/gameovermenu.h"
#include "audio.h"
Implemented a simple and consistent logging subsystem The goal of this change is mainly to clean up Taisei's codebase and improve its console output. I've been frustrated by files littered with inconsistent printf/fprintf/warnx/errx calls for a long time, and now I actually did something about it. All the above functions are now considered deprecated and result in a compile-time warning when used. Instead, the following macros should be used: log_debug(format, ...) log_info(format, ...) log_warn(format, ...) log_err(format, ...) As you can see, all of them have the same printf-like interface. But they have different functionality and purpose: log_debug is intended for very verbose and specific information. It does nothing in release builds, much like assert(), so don't use expressions with side-effects in its arguments. log_info is for various status updates that are expected during normal operation of the program. log_warn is for non-critical failures or other things that may be worth investigating, but don't inherently render the program non-functional. log_err is for when the only choice is to give up. Like errx, it also terminates the program. Unlike errx, it actually calls abort(), which means the cleanup functions are not ran -- but on the other hand, you get a debuggable backtrace. However, if you're trying to catch programming errors, consider using assert() instead. All of them produce output that contains a timestamp, the log level identifier, the calling function's name, and the formatted message. The newline at the end of the format string is not required -- no, it is actually *prohibited*. The logging system will take care of the line breaks by itself, don't litter the code with that shit. Internally, the logging system is based on the SDL_RWops abstraction, and may have multiple, configurable destinations. This makes it easily extensible. Currently, log_debug and log_info are set to write to stdout, log_warn and log_err to stderr, and all of them also to the file log.txt in the Taisei config directory. Consequently, the nasty freopen hacks we used to make Taisei write to log files on Windows are no longer needed -- which is a very good thing, considering they probably would break if the configdir path contains UTF-8 characters. SDL_RWFromFile does not suffer this limitation. As an added bonus, it's also thread-safe. Note about printf and fprintf: in very few cases, the logging system is not a good substitute for these functions. That is, when you care about writing exactly to stdout/stderr and about exactly how the output looks. However, I insist on keeping the deprecation warnings on them to not tempt anyone to use them for logging/debugging out of habit and/or laziness. For this reason, I've added a tsfprintf function to util.c. It is functionally identical to fprintf, except it returns void. Yes, the name is deliberately ugly. Avoid using it if possible, but if you must, only use it to write to stdout or stderr. Do not write to actual files with it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
#include "log.h"
#include "stagetext.h"
#include "stagedraw.h"
2017-02-27 15:27:48 +01:00
static size_t numstages = 0;
StageInfo *stages = NULL;
static void add_stage(uint16_t id, StageProcs *procs, StageType type, const char *title, const char *subtitle, AttackInfo *spell, Difficulty diff) {
2017-02-27 15:27:48 +01:00
++numstages;
stages = realloc(stages, numstages * sizeof(StageInfo));
StageInfo *stg = stages + (numstages - 1);
memset(stg, 0, sizeof(StageInfo));
stg->id = id;
stg->procs = procs;
stg->type = type;
stralloc(&stg->title, title);
stralloc(&stg->subtitle, subtitle);
stg->spell = spell;
stg->difficulty = diff;
}
static void end_stages(void) {
add_stage(0, NULL, 0, NULL, NULL, NULL, 0);
2017-02-27 15:27:48 +01:00
}
2017-02-27 16:36:43 +01:00
static void add_spellpractice_stages(int *spellnum, bool (*filter)(AttackInfo*), uint16_t spellbits) {
for(int i = 0 ;; ++i) {
2017-02-27 15:27:48 +01:00
StageInfo *s = stages + i;
2017-02-27 16:36:43 +01:00
if(s->type == STAGE_SPELL) {
break;
}
2017-02-27 15:27:48 +01:00
for(AttackInfo *a = s->spell; a->rule; ++a) {
2017-02-27 16:36:43 +01:00
if(!filter(a)) {
continue;
}
2017-02-27 15:27:48 +01:00
for(Difficulty diff = D_Easy; diff < D_Easy + NUM_SELECTABLE_DIFFICULTIES; ++diff) {
if(a->idmap[diff - D_Easy] >= 0) {
2017-02-27 16:36:43 +01:00
uint16_t id = spellbits | a->idmap[diff - D_Easy] | (s->id << 8);
char *title = strfmt("Spell %d", ++(*spellnum));
char *subtitle = strjoin(a->name, " ~ ", difficulty_name(diff), NULL);
2017-02-27 15:27:48 +01:00
add_stage(id, s->procs->spellpractice_procs, STAGE_SPELL, title, subtitle, a, diff);
2017-02-27 15:27:48 +01:00
s = stages + i; // stages just got realloc'd, so we must update the pointer
free(title);
free(subtitle);
2017-02-27 15:27:48 +01:00
}
}
}
2017-02-27 15:27:48 +01:00
}
2017-02-27 16:36:43 +01:00
}
static bool spellfilter_normal(AttackInfo *spell) {
return spell->type != AT_ExtraSpell;
}
static bool spellfilter_extra(AttackInfo *spell) {
return spell->type == AT_ExtraSpell;
}
void stage_init_array(void) {
int spellnum = 0;
// id procs type title subtitle spells diff
add_stage(1, &stage1_procs, STAGE_STORY, "Stage 1", "Misty Lake", (AttackInfo*)&stage1_spells, D_Any);
add_stage(2, &stage2_procs, STAGE_STORY, "Stage 2", "Walk Along the Border", (AttackInfo*)&stage2_spells, D_Any);
add_stage(3, &stage3_procs, STAGE_STORY, "Stage 3", "Through the Tunnel of Light", (AttackInfo*)&stage3_spells, D_Any);
add_stage(4, &stage4_procs, STAGE_STORY, "Stage 4", "Forgotten Mansion", (AttackInfo*)&stage4_spells, D_Any);
add_stage(5, &stage5_procs, STAGE_STORY, "Stage 5", "Climbing the Tower of Babel", (AttackInfo*)&stage5_spells, D_Any);
add_stage(6, &stage6_procs, STAGE_STORY, "Stage 6", "Roof of the World", (AttackInfo*)&stage6_spells, D_Any);
2017-02-27 16:36:43 +01:00
// generate spellpractice stages
add_spellpractice_stages(&spellnum, spellfilter_normal, STAGE_SPELL_BIT);
add_spellpractice_stages(&spellnum, spellfilter_extra, STAGE_SPELL_BIT | STAGE_EXTRASPELL_BIT);
2017-02-27 15:27:48 +01:00
end_stages();
#ifdef DEBUG
2017-02-27 15:27:48 +01:00
for(int i = 0; stages[i].procs; ++i) {
if(stages[i].type == STAGE_SPELL && !(stages[i].id & STAGE_SPELL_BIT)) {
2017-03-13 17:03:51 +01:00
log_fatal("Spell stage has an ID without the spell bit set: 0x%04x", stages[i].id);
}
2017-02-26 13:17:48 +01:00
for(int j = 0; stages[j].procs; ++j) {
if(i != j && stages[i].id == stages[j].id) {
2017-03-13 17:03:51 +01:00
log_fatal("Duplicate ID 0x%04x in stages array, indices: %i, %i", stages[i].id, i, j);
}
}
}
2017-02-27 15:27:48 +01:00
#endif
}
2017-02-15 13:53:24 +01:00
void stage_free_array(void) {
2017-02-26 13:17:48 +01:00
for(StageInfo *stg = stages; stg->procs; ++stg) {
2017-02-27 15:27:48 +01:00
free(stg->title);
free(stg->subtitle);
free(stg->progress);
2017-02-15 13:53:24 +01:00
}
2017-02-27 15:27:48 +01:00
free(stages);
2017-02-15 13:53:24 +01:00
}
// NOTE: This returns the stage BY ID, not by the array index!
StageInfo* stage_get(uint16_t n) {
2017-02-26 13:17:48 +01:00
for(StageInfo *stg = stages; stg->procs; ++stg)
if(stg->id == n)
return stg;
return NULL;
}
2017-02-10 23:34:48 +01:00
StageInfo* stage_get_by_spellcard(AttackInfo *spell, Difficulty diff) {
if(!spell)
return NULL;
2017-02-26 13:17:48 +01:00
for(StageInfo *stg = stages; stg->procs; ++stg)
2017-02-10 23:34:48 +01:00
if(stg->spell == spell && stg->difficulty == diff)
return stg;
return NULL;
}
StageProgress* stage_get_progress_from_info(StageInfo *stage, Difficulty diff, bool allocate) {
2017-02-11 12:38:50 +01:00
// D_Any stages will have a separate StageProgress for every selectable difficulty.
// Stages with a fixed difficulty setting (spellpractice, extra stage...) obviously get just one and the diff parameter is ignored.
// This stuff must stay around until progress_save(), which happens on shutdown.
// So do NOT try to free any pointers this function returns, that will fuck everything up.
if(!stage) {
return NULL;
}
bool fixed_diff = (stage->difficulty != D_Any);
if(!fixed_diff && (diff < D_Easy || diff > D_Lunatic)) {
return NULL;
}
if(!stage->progress) {
if(!allocate) {
return NULL;
}
2017-02-11 12:38:50 +01:00
size_t allocsize = sizeof(StageProgress) * (fixed_diff ? 1 : NUM_SELECTABLE_DIFFICULTIES);
stage->progress = malloc(allocsize);
memset(stage->progress, 0, allocsize);
}
return stage->progress + (fixed_diff ? 0 : diff - D_Easy);
}
StageProgress* stage_get_progress(uint16_t id, Difficulty diff, bool allocate) {
return stage_get_progress_from_info(stage_get(id), diff, allocate);
2017-02-11 12:38:50 +01:00
}
2017-02-26 13:17:48 +01:00
static void stage_start(StageInfo *stage) {
global.timer = 0;
global.frames = 0;
global.game_over = 0;
2012-08-07 17:01:26 +02:00
global.shake_view = 0;
player_stage_pre_init(&global.plr);
2017-02-26 13:17:48 +01:00
if(stage->type == STAGE_SPELL) {
global.plr.lives = 0;
2017-02-26 13:17:48 +01:00
global.plr.bombs = 0;
2017-09-11 21:09:30 +02:00
global.plr.power = PLR_SPELLPRACTICE_POWER;
} else if(global.is_practice_mode) {
global.plr.lives = PLR_STGPRACTICE_LIVES;
global.plr.bombs = PLR_STGPRACTICE_BOMBS;
global.plr.power = PLR_STGPRACTICE_POWER;
2017-02-26 13:17:48 +01:00
}
reset_sounds();
}
2017-10-10 16:06:46 +02:00
static bool ingame_menu_interrupts_bgm(void) {
return global.stage->type != STAGE_SPELL;
}
void stage_pause(void) {
MenuData menu;
if(global.replaymode == REPLAY_PLAY) {
create_ingame_menu_replay(&menu);
} else {
create_ingame_menu(&menu);
}
2017-10-10 16:06:46 +02:00
if(ingame_menu_interrupts_bgm()) {
stop_bgm(false);
}
pause_sounds();
menu_loop(&menu);
2017-10-10 16:06:46 +02:00
if(global.game_over) {
stop_sounds();
if(ingame_menu_interrupts_bgm() || global.game_over != GAMEOVER_RESTART) {
fade_bgm();
}
} else {
resume_sounds();
resume_bgm();
}
}
void stage_gameover(void) {
if(global.stage->type == STAGE_SPELL && config_get_int(CONFIG_SPELLSTAGE_AUTORESTART)) {
global.game_over = GAMEOVER_RESTART;
return;
}
MenuData menu;
create_gameover_menu(&menu);
2017-02-26 13:17:48 +01:00
// XXX: The save_bgm and restore_bgm code is broken.
// Disabled it for now, as we don't have a gameover BGM yet
// When we do, it needs to be fixed.
// It should be probably implemented by the backend, it has a better idea
// about how to manage its own state than the audio frontend code.
/*
2017-02-26 13:17:48 +01:00
bool interrupt_bgm = (global.stage->type != STAGE_SPELL);
if(interrupt_bgm) {
save_bgm();
2017-10-02 04:34:51 +02:00
start_bgm("gameover");
2017-02-26 13:17:48 +01:00
}
*/
2017-02-26 13:17:48 +01:00
pause_sounds();
2017-10-10 16:06:46 +02:00
if(ingame_menu_interrupts_bgm()) {
stop_bgm(false);
}
menu_loop(&menu);
resume_sounds();
2017-02-26 13:17:48 +01:00
/*
2017-02-26 13:17:48 +01:00
if(interrupt_bgm) {
restore_bgm();
}
*/
resume_bgm();
}
bool stage_input_handler_gameplay(SDL_Event *event, void *arg) {
TaiseiEvent type = TAISEI_EVENT(event->type);
int32_t code = event->user.code;
2012-08-13 17:50:28 +02:00
switch(type) {
case TE_GAME_KEY_DOWN:
if(code == KEY_HAHAIWIN) {
2017-02-15 18:10:56 +01:00
#ifdef DEBUG
2017-02-26 13:17:48 +01:00
stage_finish(GAMEOVER_WIN);
2017-02-15 18:10:56 +01:00
#endif
break;
}
2017-03-06 13:02:46 +01:00
#ifndef DEBUG // no cheating for peasants
if( code == KEY_IDDQD ||
code == KEY_POWERUP ||
code == KEY_POWERDOWN)
2017-03-06 13:02:46 +01:00
break;
#endif
player_event_with_replay(&global.plr, EV_PRESS, code);
2012-08-13 17:50:28 +02:00
break;
case TE_GAME_KEY_UP:
player_event_with_replay(&global.plr, EV_RELEASE, code);
2012-08-07 05:28:41 +02:00
break;
case TE_GAME_PAUSE:
stage_pause();
2012-08-13 17:50:28 +02:00
break;
case TE_GAME_AXIS_LR:
player_event_with_replay(&global.plr, EV_AXIS_LR, (uint16_t)code);
2012-08-15 16:36:39 +02:00
break;
case TE_GAME_AXIS_UD:
player_event_with_replay(&global.plr, EV_AXIS_UD, (uint16_t)code);
2012-08-15 16:36:39 +02:00
break;
2012-08-13 17:50:28 +02:00
default: break;
}
return false;
2012-08-13 17:50:28 +02:00
}
bool stage_input_handler_replay(SDL_Event *event, void *arg) {
if(event->type == MAKE_TAISEI_EVENT(TE_GAME_PAUSE)) {
stage_pause();
}
return false;
2012-08-13 17:50:28 +02:00
}
void replay_input(void) {
ReplayStage *s = global.replay_stage;
int i;
events_poll((EventHandler[]){
{ .proc = stage_input_handler_replay },
{NULL}
}, EFLAG_GAME);
for(i = s->playpos; i < s->numevents; ++i) {
ReplayEvent *e = s->events + i;
2012-07-29 15:35:40 +02:00
if(e->frame != global.frames)
break;
2012-07-29 15:35:40 +02:00
switch(e->type) {
case EV_OVER:
2012-08-04 06:37:59 +02:00
global.game_over = GAMEOVER_DEFEAT;
break;
case EV_CHECK_DESYNC:
s->desync_check = e->value;
break;
case EV_FPS:
s->fps = e->value;
break;
default: {
2017-10-31 10:48:30 +01:00
player_event(&global.plr, e->type, (int16_t)e->value, NULL, NULL);
break;
}
}
}
2012-08-07 05:28:41 +02:00
s->playpos = i;
player_applymovement(&global.plr);
}
void stage_input(void) {
events_poll((EventHandler[]){
{ .proc = stage_input_handler_gameplay },
{NULL}
}, EFLAG_GAME);
player_fix_input(&global.plr);
player_applymovement(&global.plr);
}
2017-02-26 13:17:48 +01:00
static void stage_logic(void) {
player_logic(&global.plr);
process_enemies(&global.enemies);
process_projectiles(&global.projs, true);
2011-04-29 10:26:37 +02:00
process_items();
process_lasers();
process_projectiles(&global.particles, false);
update_sounds();
if(global.boss && !global.dialog)
process_boss(&global.boss);
if(global.dialog && (global.plr.inputflags & INFLAG_SKIP) && global.frames - global.dialog->page_time > 3)
page_dialog(&global.dialog);
global.frames++;
if(!global.dialog && (!global.boss || boss_is_fleeing(global.boss)))
global.timer++;
2017-02-26 20:27:10 +01:00
if(global.replaymode == REPLAY_PLAY &&
global.frames == global.replay_stage->events[global.replay_stage->numevents-1].frame - FADE_TIME &&
2017-02-26 13:17:48 +01:00
global.game_over != GAMEOVER_TRANSITIONING) {
stage_finish(GAMEOVER_DEFEAT);
}
// BGM handling
if(global.dialog && global.dialog->messages[global.dialog->pos].side == BGM) {
stage_start_bgm(global.dialog->messages[global.dialog->pos].msg);
page_dialog(&global.dialog);
}
}
void stage_clear_hazards(bool force) {
for(Projectile *p = global.projs; p; p = p->next) {
2017-11-05 15:02:49 +01:00
if(p->type == EnemyProj || p->type == FakeProj)
p->type = DeadProj;
}
for(Laser *l = global.lasers; l; l = l->next) {
if(!l->unclearable || force)
l->dead = true;
}
}
void stage_clear_hazards_instantly(bool force) {
for(Projectile *p = global.projs, *next; p; p = next) {
next = p->next;
2017-11-05 15:02:49 +01:00
if(p->type == EnemyProj || p->type == FakeProj || p->type == DeadProj) {
create_bpoint(p->pos);
delete_projectile(&global.projs, p);
}
}
// TODO: clear these instantly as well
for(Laser *l = global.lasers; l; l = l->next) {
if(!l->unclearable || force)
l->dead = true;
}
}
2017-02-26 13:17:48 +01:00
static void stage_free(void) {
delete_enemies(&global.enemies);
2017-05-05 03:44:05 +02:00
delete_enemies(&global.plr.slaves);
2011-04-29 10:26:37 +02:00
delete_items();
delete_lasers();
delete_projectiles(&global.projs);
delete_projectiles(&global.particles);
if(global.dialog) {
delete_dialog(global.dialog);
global.dialog = NULL;
}
if(global.boss) {
free_boss(global.boss);
global.boss = NULL;
}
stagetext_free();
}
2017-02-26 13:17:48 +01:00
static void stage_finalize(void *arg) {
global.game_over = (intptr_t)arg;
}
void stage_finish(int gameover) {
assert(global.game_over != GAMEOVER_TRANSITIONING);
global.game_over = GAMEOVER_TRANSITIONING;
set_transition_callback(TransFadeBlack, FADE_TIME, FADE_TIME*2, stage_finalize, (void*)(intptr_t)gameover);
2017-10-08 08:01:52 +02:00
fade_bgm();
if(global.replaymode == REPLAY_PLAY) {
return;
}
StageProgress *p = stage_get_progress_from_info(global.stage, global.diff, true);
if(p) {
++p->num_cleared;
}
2017-02-26 13:17:48 +01:00
}
static void stage_preload(void) {
difficulty_preload();
projectiles_preload();
player_preload();
items_preload();
boss_preload();
if(global.stage->type != STAGE_SPELL)
enemies_preload();
global.stage->procs->preload();
preload_resources(RES_TEXTURE, RESF_PERMANENT,
"hud",
"star",
"titletransition",
NULL);
preload_resources(RES_SHADER, RESF_PERMANENT,
"stagetitle",
"ingame_menu",
"circleclipped_indicator",
NULL);
}
static void display_stage_title(StageInfo *info) {
stagetext_add(info->title, VIEWPORT_W/2 + I * (VIEWPORT_H/2-40), AL_Center, _fonts.mainmenu, rgb(1, 1, 1), 50, 85, 35, 35);
stagetext_add(info->subtitle, VIEWPORT_W/2 + I * (VIEWPORT_H/2), AL_Center, _fonts.standard, rgb(1, 1, 1), 60, 85, 35, 35);
}
void stage_start_bgm(const char *bgm) {
start_bgm(bgm);
if(current_bgm.title && current_bgm.started_at >= 0) {
2017-10-02 02:48:34 +02:00
char txt[strlen(current_bgm.title) + 6];
snprintf(txt, sizeof(txt), "BGM: %s", current_bgm.title);
stagetext_add(txt, VIEWPORT_W-15 + I * (VIEWPORT_H-20), AL_Right, _fonts.standard, rgb(1, 1, 1), 30, 85, 35, 35);
}
}
typedef struct StageFrameState {
StageInfo *stage;
int transition_delay;
uint16_t last_replay_fps;
} StageFrameState;
static bool stage_fpslimit_condition(void *arg) {
return (global.replaymode != REPLAY_PLAY || !gamekeypressed(KEY_SKIP)) && !global.frameskip;
}
static bool stage_frame(void *arg) {
StageFrameState *fstate = arg;
StageInfo *stage = fstate->stage;
((global.replaymode == REPLAY_PLAY) ? replay_input : stage_input)();
if(global.game_over != GAMEOVER_TRANSITIONING) {
if((!global.boss || boss_is_fleeing(global.boss)) && !global.dialog) {
stage->procs->event();
}
if(stage->type == STAGE_SPELL && !global.boss && global.game_over != GAMEOVER_RESTART) {
stage_finish(GAMEOVER_WIN);
fstate->transition_delay = 60;
}
}
if(!global.timer && stage->type != STAGE_SPELL) {
// must be done here to let the event function start a BGM first
display_stage_title(stage);
}
replay_stage_check_desync(global.replay_stage, global.frames, (tsrand() ^ global.plr.points) & 0xFFFF, global.replaymode);
stage_logic();
if(global.replaymode == REPLAY_RECORD && global.plr.points > progress.hiscore) {
progress.hiscore = global.plr.points;
}
if(fstate->transition_delay) {
--fstate->transition_delay;
}
if(global.frameskip && global.frames % global.frameskip) {
if(!fstate->transition_delay) {
update_transition();
}
return true;
}
fpscounter_update(&global.fps);
if(global.replaymode == REPLAY_RECORD) {
uint16_t replay_fps = (uint16_t)rint(global.fps.fps);
if(replay_fps != fstate->last_replay_fps) {
replay_stage_event(global.replay_stage, global.frames, EV_FPS, replay_fps);
fstate->last_replay_fps = replay_fps;
}
}
tsrand_lock(&global.rand_game);
tsrand_switch(&global.rand_visual);
stage_draw_scene(stage);
tsrand_unlock(&global.rand_game);
tsrand_switch(&global.rand_game);
draw_transition();
if(!fstate->transition_delay) {
update_transition();
}
SDL_GL_SwapWindow(video.window);
return global.game_over <= 0;
}
2017-02-26 13:17:48 +01:00
void stage_loop(StageInfo *stage) {
assert(stage);
assert(stage->procs);
assert(stage->procs->preload);
2017-02-26 13:17:48 +01:00
assert(stage->procs->begin);
assert(stage->procs->end);
assert(stage->procs->draw);
assert(stage->procs->event);
assert(stage->procs->shader_rules);
if(global.game_over == GAMEOVER_WIN) {
global.game_over = 0;
} else if(global.game_over) {
return;
}
// I really want to separate all of the game state from the global struct sometime
2017-02-26 13:17:48 +01:00
global.stage = stage;
stage_preload();
2017-02-05 02:25:17 +01:00
uint32_t seed = (uint32_t)time(0);
tsrand_switch(&global.rand_game);
2012-07-27 19:11:45 +02:00
tsrand_seed_p(&global.rand_game, seed);
2017-02-26 13:17:48 +01:00
stage_start(stage);
if(global.replaymode == REPLAY_RECORD) {
if(config_get_int(CONFIG_SAVE_RPY)) {
global.replay_stage = replay_create_stage(&global.replay, stage, seed, global.diff, &global.plr);
2017-02-15 18:34:47 +01:00
// make sure our player state is consistent with what goes into the replay
player_init(&global.plr);
2017-02-15 18:34:47 +01:00
replay_stage_sync_player_state(global.replay_stage, &global.plr);
} else {
global.replay_stage = NULL;
}
Implemented a simple and consistent logging subsystem The goal of this change is mainly to clean up Taisei's codebase and improve its console output. I've been frustrated by files littered with inconsistent printf/fprintf/warnx/errx calls for a long time, and now I actually did something about it. All the above functions are now considered deprecated and result in a compile-time warning when used. Instead, the following macros should be used: log_debug(format, ...) log_info(format, ...) log_warn(format, ...) log_err(format, ...) As you can see, all of them have the same printf-like interface. But they have different functionality and purpose: log_debug is intended for very verbose and specific information. It does nothing in release builds, much like assert(), so don't use expressions with side-effects in its arguments. log_info is for various status updates that are expected during normal operation of the program. log_warn is for non-critical failures or other things that may be worth investigating, but don't inherently render the program non-functional. log_err is for when the only choice is to give up. Like errx, it also terminates the program. Unlike errx, it actually calls abort(), which means the cleanup functions are not ran -- but on the other hand, you get a debuggable backtrace. However, if you're trying to catch programming errors, consider using assert() instead. All of them produce output that contains a timestamp, the log level identifier, the calling function's name, and the formatted message. The newline at the end of the format string is not required -- no, it is actually *prohibited*. The logging system will take care of the line breaks by itself, don't litter the code with that shit. Internally, the logging system is based on the SDL_RWops abstraction, and may have multiple, configurable destinations. This makes it easily extensible. Currently, log_debug and log_info are set to write to stdout, log_warn and log_err to stderr, and all of them also to the file log.txt in the Taisei config directory. Consequently, the nasty freopen hacks we used to make Taisei write to log files on Windows are no longer needed -- which is a very good thing, considering they probably would break if the configdir path contains UTF-8 characters. SDL_RWFromFile does not suffer this limitation. As an added bonus, it's also thread-safe. Note about printf and fprintf: in very few cases, the logging system is not a good substitute for these functions. That is, when you care about writing exactly to stdout/stderr and about exactly how the output looks. However, I insist on keeping the deprecation warnings on them to not tempt anyone to use them for logging/debugging out of habit and/or laziness. For this reason, I've added a tsfprintf function to util.c. It is functionally identical to fprintf, except it returns void. Yes, the name is deliberately ugly. Avoid using it if possible, but if you must, only use it to write to stdout or stderr. Do not write to actual files with it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
log_debug("Random seed: %u", seed);
StageProgress *p = stage_get_progress_from_info(stage, global.diff, true);
if(p) {
log_debug("You played this stage %u times", p->num_played);
log_debug("You cleared this stage %u times", p->num_cleared);
++p->num_played;
2017-09-11 21:09:30 +02:00
if(!global.plr.continues_used) {
2017-09-11 21:09:30 +02:00
p->unlocked = true;
}
}
} else {
if(!global.replay_stage) {
2017-03-13 17:03:51 +01:00
log_fatal("Attemped to replay a NULL stage");
return;
}
ReplayStage *stg = global.replay_stage;
Implemented a simple and consistent logging subsystem The goal of this change is mainly to clean up Taisei's codebase and improve its console output. I've been frustrated by files littered with inconsistent printf/fprintf/warnx/errx calls for a long time, and now I actually did something about it. All the above functions are now considered deprecated and result in a compile-time warning when used. Instead, the following macros should be used: log_debug(format, ...) log_info(format, ...) log_warn(format, ...) log_err(format, ...) As you can see, all of them have the same printf-like interface. But they have different functionality and purpose: log_debug is intended for very verbose and specific information. It does nothing in release builds, much like assert(), so don't use expressions with side-effects in its arguments. log_info is for various status updates that are expected during normal operation of the program. log_warn is for non-critical failures or other things that may be worth investigating, but don't inherently render the program non-functional. log_err is for when the only choice is to give up. Like errx, it also terminates the program. Unlike errx, it actually calls abort(), which means the cleanup functions are not ran -- but on the other hand, you get a debuggable backtrace. However, if you're trying to catch programming errors, consider using assert() instead. All of them produce output that contains a timestamp, the log level identifier, the calling function's name, and the formatted message. The newline at the end of the format string is not required -- no, it is actually *prohibited*. The logging system will take care of the line breaks by itself, don't litter the code with that shit. Internally, the logging system is based on the SDL_RWops abstraction, and may have multiple, configurable destinations. This makes it easily extensible. Currently, log_debug and log_info are set to write to stdout, log_warn and log_err to stderr, and all of them also to the file log.txt in the Taisei config directory. Consequently, the nasty freopen hacks we used to make Taisei write to log files on Windows are no longer needed -- which is a very good thing, considering they probably would break if the configdir path contains UTF-8 characters. SDL_RWFromFile does not suffer this limitation. As an added bonus, it's also thread-safe. Note about printf and fprintf: in very few cases, the logging system is not a good substitute for these functions. That is, when you care about writing exactly to stdout/stderr and about exactly how the output looks. However, I insist on keeping the deprecation warnings on them to not tempt anyone to use them for logging/debugging out of habit and/or laziness. For this reason, I've added a tsfprintf function to util.c. It is functionally identical to fprintf, except it returns void. Yes, the name is deliberately ugly. Avoid using it if possible, but if you must, only use it to write to stdout or stderr. Do not write to actual files with it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
log_debug("REPLAY_PLAY mode: %d events, stage: \"%s\"", stg->numevents, stage_get(stg->stage)->title);
2012-08-07 05:28:41 +02:00
tsrand_seed_p(&global.rand_game, stg->seed);
Implemented a simple and consistent logging subsystem The goal of this change is mainly to clean up Taisei's codebase and improve its console output. I've been frustrated by files littered with inconsistent printf/fprintf/warnx/errx calls for a long time, and now I actually did something about it. All the above functions are now considered deprecated and result in a compile-time warning when used. Instead, the following macros should be used: log_debug(format, ...) log_info(format, ...) log_warn(format, ...) log_err(format, ...) As you can see, all of them have the same printf-like interface. But they have different functionality and purpose: log_debug is intended for very verbose and specific information. It does nothing in release builds, much like assert(), so don't use expressions with side-effects in its arguments. log_info is for various status updates that are expected during normal operation of the program. log_warn is for non-critical failures or other things that may be worth investigating, but don't inherently render the program non-functional. log_err is for when the only choice is to give up. Like errx, it also terminates the program. Unlike errx, it actually calls abort(), which means the cleanup functions are not ran -- but on the other hand, you get a debuggable backtrace. However, if you're trying to catch programming errors, consider using assert() instead. All of them produce output that contains a timestamp, the log level identifier, the calling function's name, and the formatted message. The newline at the end of the format string is not required -- no, it is actually *prohibited*. The logging system will take care of the line breaks by itself, don't litter the code with that shit. Internally, the logging system is based on the SDL_RWops abstraction, and may have multiple, configurable destinations. This makes it easily extensible. Currently, log_debug and log_info are set to write to stdout, log_warn and log_err to stderr, and all of them also to the file log.txt in the Taisei config directory. Consequently, the nasty freopen hacks we used to make Taisei write to log files on Windows are no longer needed -- which is a very good thing, considering they probably would break if the configdir path contains UTF-8 characters. SDL_RWFromFile does not suffer this limitation. As an added bonus, it's also thread-safe. Note about printf and fprintf: in very few cases, the logging system is not a good substitute for these functions. That is, when you care about writing exactly to stdout/stderr and about exactly how the output looks. However, I insist on keeping the deprecation warnings on them to not tempt anyone to use them for logging/debugging out of habit and/or laziness. For this reason, I've added a tsfprintf function to util.c. It is functionally identical to fprintf, except it returns void. Yes, the name is deliberately ugly. Avoid using it if possible, but if you must, only use it to write to stdout or stderr. Do not write to actual files with it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
log_debug("Random seed: %u", stg->seed);
2017-02-15 18:34:47 +01:00
global.diff = stg->diff;
player_init(&global.plr);
2017-02-15 18:34:47 +01:00
replay_stage_sync_player_state(stg, &global.plr);
2012-08-07 05:28:41 +02:00
stg->playpos = 0;
}
player_stage_post_init(&global.plr);
2017-02-26 13:17:48 +01:00
stage->procs->begin();
StageFrameState fstate = { .stage = stage };
fpscounter_reset(&global.fps);
loop_at_fps(stage_frame, stage_fpslimit_condition, &fstate, FPS);
if(global.replaymode == REPLAY_RECORD) {
replay_stage_event(global.replay_stage, global.frames, EV_OVER, 0);
if(global.game_over == GAMEOVER_WIN) {
global.replay_stage->flags |= REPLAY_SFLAG_CLEAR;
}
}
2017-02-26 13:17:48 +01:00
stage->procs->end();
stage_free();
2012-07-27 19:11:45 +02:00
tsrand_switch(&global.rand_visual);
free_all_refs();
stop_sounds();
}