threaded screenshoting (now with gamestates redraw!)

This commit is contained in:
Sebastian Krzyszkowiak 2017-09-09 00:42:57 +02:00
parent 450e33b2e3
commit a906b14e08
3 changed files with 40 additions and 8 deletions

View file

@ -163,6 +163,20 @@ SYMBOL_INTERNAL void* GamestateLoadingThread(void *arg) {
return NULL; return NULL;
} }
SYMBOL_INTERNAL void* ScreenshotThread(void *arg) {
struct ScreenshotThreadData *data = arg;
ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_USER_DOCUMENTS_PATH);
char filename[255];
snprintf(filename, 255, "%s_%ju_%ju.png", data->game->name, (uintmax_t)time(NULL), (uintmax_t)clock());
al_set_path_filename(path, filename);
al_save_bitmap(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), data->bitmap);
PrintConsole(data->game, "Screenshot stored in %s", al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
al_destroy_path(path);
al_destroy_bitmap(data->bitmap);
free(data);
return NULL;
}
SYMBOL_INTERNAL void GamestateProgress(struct Game *game) { SYMBOL_INTERNAL void GamestateProgress(struct Game *game) {
struct Gamestate *tmp = game->_priv.loading.current; struct Gamestate *tmp = game->_priv.loading.current;
game->_priv.loading.progress++; game->_priv.loading.progress++;

View file

@ -48,6 +48,11 @@ struct GamestateLoadingThreadData {
int bitmap_flags; int bitmap_flags;
}; };
struct ScreenshotThreadData {
struct Game *game;
ALLEGRO_BITMAP *bitmap;
};
void DrawGamestates(struct Game *game); void DrawGamestates(struct Game *game);
void LogicGamestates(struct Game *game); void LogicGamestates(struct Game *game);
void EventGamestates(struct Game *game, ALLEGRO_EVENT *ev); void EventGamestates(struct Game *game, ALLEGRO_EVENT *ev);
@ -58,6 +63,7 @@ void DrawConsole(struct Game *game);
void Console_Load(struct Game *game); void Console_Load(struct Game *game);
void Console_Unload(struct Game *game); void Console_Unload(struct Game *game);
void* GamestateLoadingThread(void *arg); void* GamestateLoadingThread(void *arg);
void* ScreenshotThread(void *arg);
void GamestateProgress(struct Game *game); void GamestateProgress(struct Game *game);
void* AddGarbage(struct Game *game, void* data); void* AddGarbage(struct Game *game, void* data);
void ClearGarbage(struct Game *game); void ClearGarbage(struct Game *game);

View file

@ -145,7 +145,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
al_install_joystick(); al_install_joystick();
al_set_new_display_flags(ALLEGRO_OPENGL_ES_PROFILE | ALLEGRO_PROGRAMMABLE_PIPELINE | (game->config.fullscreen ? ALLEGRO_FULLSCREEN_WINDOW : ALLEGRO_WINDOWED) | ALLEGRO_RESIZABLE | ALLEGRO_OPENGL); al_set_new_display_flags(ALLEGRO_PROGRAMMABLE_PIPELINE | (game->config.fullscreen ? ALLEGRO_FULLSCREEN_WINDOW : ALLEGRO_WINDOWED) | ALLEGRO_RESIZABLE | ALLEGRO_OPENGL);
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
al_set_new_display_flags((al_get_new_display_flags() | ALLEGRO_WINDOWED) ^ ALLEGRO_FULLSCREEN_WINDOW); al_set_new_display_flags((al_get_new_display_flags() | ALLEGRO_WINDOWED) ^ ALLEGRO_FULLSCREEN_WINDOW);
#endif #endif
@ -502,13 +502,25 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void *g) {
game->_priv.showconsole = true; game->_priv.showconsole = true;
PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed/60.0); PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed/60.0);
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_F12)) { } else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_F12)) {
ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_USER_DOCUMENTS_PATH); DrawGamestates(game);
char filename[255] = { }; int flags = al_get_new_bitmap_flags();
snprintf(filename, 255, "%s_%lld_%ld.png", game->name, (long long)time(NULL), clock()); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
al_set_path_filename(path, filename); ALLEGRO_BITMAP *bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display));
al_save_bitmap(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), al_get_backbuffer(game->display)); al_set_new_bitmap_flags(flags);
PrintConsole(game, "Screenshot stored in %s", al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); ALLEGRO_BITMAP *target = al_get_target_bitmap();
al_destroy_path(path); al_set_target_bitmap(bitmap);
al_draw_bitmap(al_get_backbuffer(game->display), 0, 0, 0);
al_set_target_bitmap(target);
PrintConsole(game, "Screenshot made! Storing...");
struct ScreenshotThreadData *data = malloc(sizeof(struct ScreenshotThreadData));
data->game = game;
data->bitmap = bitmap;
#ifndef LIBSUPERDERPY_SINGLE_THREAD
al_run_detached_thread(ScreenshotThread, data);
#else
ScreenshotThread(data);
#endif
} }
EventGamestates(game, &ev); EventGamestates(game, &ev);
} }