From 3bacf773c7b9dc7bcc90c2edd214cbe24d931e49 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 15 Nov 2013 21:29:18 -0500 Subject: Move demo out of main(). --- diff --git a/src/demo.c b/src/demo.c new file mode 100644 index 0000000..24ebfc5 --- /dev/null +++ b/src/demo.c @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2013 Patrick "P. J." McDermott + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + */ + +#include +#include "logging.h" +#include "resources/image.h" +#include "resources/map.h" +#include "viewport.h" +#include "area.h" +#include "palettes.h" +#include "demo.h" + +#define DEMO() \ + do { \ + vp->x = x + map->tilewidth / 2 - vp->w / 2; \ + vp->y = y + map->tileheight / 2 - vp->h / 2; \ + /* \ + * See the following resources when computing realistic \ + * daylight cycles: \ + * * \ + * * \ + */ \ + if (i % i_max < i_max / 6 * 2) { \ + /* 00:00 - 04:00 */ \ + t_max = i_max / 6 * 2; \ + pal_start = map->palettes[MAP_PALETTE_MORN] \ + .palette->palette; \ + pal_end = map->palettes[MAP_PALETTE_DAY] \ + .palette->palette; \ + } else if (i % i_max < i_max / 6 * 4) { \ + /* 04:00 - 12:00 */ \ + t_max = i_max / 6 * 2; \ + pal_start = map->palettes[MAP_PALETTE_DAY] \ + .palette->palette; \ + pal_end = map->palettes[MAP_PALETTE_EVE] \ + .palette->palette; \ + } else if (i % i_max < i_max / 6 * 5) { \ + /* 12:00 - 20:00 */ \ + t_max = i_max / 6 * 1; \ + pal_start = map->palettes[MAP_PALETTE_EVE] \ + .palette->palette; \ + pal_end = map->palettes[MAP_PALETTE_NIGHT] \ + .palette->palette; \ + } else { \ + /* 04:00 - 12:00 */ \ + t_max = i_max / 6 * 1; \ + pal_start = map->palettes[MAP_PALETTE_NIGHT] \ + .palette->palette; \ + pal_end = map->palettes[MAP_PALETTE_MORN] \ + .palette->palette; \ + } \ + if (!cycle_palettes_cosine(i, t_max, \ + pal_start, pal_end, pal)) { \ + warn("Failed to cycle palettes"); \ + } \ + set_area_palette(a, pal); \ + if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, \ + pal->colors, 0, pal->ncolors)) { \ + warn("Failed to set palette"); \ + } \ + render_area_to_viewport(a, vp); \ + SDL_Flip(vp->screen); \ + SDL_Delay(10); \ + ++i; \ + } while (0) + +void +demo(void) +{ + struct viewport *vp; + struct map *map; + struct area *a; + SDL_Palette *pal; + struct map_exit *e; + + vp = init_viewport(320, 240, 8); + + map = map_get(MAPSDIR "/db16-route.tmx"); + if (map == NULL) { + map = map_get("data/maps/db16-route.tmx"); + } + if (map == NULL) { + err(1, "Where's the map, George?"); + } + pal = map->palettes[MAP_PALETTE_DAY].palette->palette; + if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, pal->colors, + 0, pal->ncolors)) { + warn("Failed to set palette"); + } + a = area_new(map, vp->screen->format->palette); + + for (e = map->map_exits_head; e != NULL; e = e->next) { + debug("Map exit (size %dx%d) at (%d,%d) " + "to map %s (size %dx%d) at (%d,%d)", + e->width, e->height, + e->x, e->y, + e->target_map_name, + e->target_map->width, e->target_map->height, + e->target_x_coord, e->target_y_coord); + } + + /* Demo */ + { + Uint32 start, end; + int i, i_max, t_max; + int x, y; + SDL_Palette *pal_start, *pal_end; + + pal = malloc(sizeof(*pal)); + pal->colors = calloc(64, sizeof(*pal->colors)); + pal->ncolors = 64; + + i = 0; + i_max = 1440; + start = SDL_GetTicks(); + x = 9 * map->tilewidth ; + y = 58 * map->tileheight; + for (; y > 50 * map->tileheight; y -= 1) DEMO(); /* Up */ + for (; x < 12 * map->tilewidth ; x += 1) DEMO(); /* Right */ + for (; y > 43 * map->tileheight; y -= 1) DEMO(); /* Up */ + for (; x > 5 * map->tilewidth ; x -= 1) DEMO(); /* Left */ + for (; y > 32 * map->tileheight; y -= 1) DEMO(); /* Up */ + for (; x < 13 * map->tilewidth ; x += 1) DEMO(); /* Right */ + for (; y > 31 * map->tileheight; y -= 1) DEMO(); /* Up */ + for (; x < 16 * map->tilewidth ; x += 1) DEMO(); /* Right */ + for (; y < 34 * map->tileheight; y += 1) DEMO(); /* Down */ + for (; x < 24 * map->tilewidth ; x += 1) DEMO(); /* Right */ + for (; y > 15 * map->tileheight; y -= 1) DEMO(); /* Up */ + for (; x > 11 * map->tilewidth ; x -= 1) DEMO(); /* Left */ + for (; y > 5 * map->tileheight; y -= 1) DEMO(); /* Up */ + for (; x < 17 * map->tilewidth ; x += 1) DEMO(); /* Right */ + for (; y > 1 * map->tileheight; y -= 1) DEMO(); /* Up */ + end = SDL_GetTicks(); + debug("Rendered %d frames in %d milliseconds", i, end - start); + } +} diff --git a/src/demo.h b/src/demo.h new file mode 100644 index 0000000..ca577dd --- /dev/null +++ b/src/demo.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2013 Patrick "P. J." McDermott + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + */ + +#ifndef DEMO_H +#define DEMO_H + +void demo(void); + +#endif diff --git a/src/local.mk b/src/local.mk index 571b907..76b986d 100644 --- a/src/local.mk +++ b/src/local.mk @@ -7,6 +7,8 @@ sdlex_SOURCES += \ src/base64.h \ src/compression.c \ src/compression.h \ + src/demo.c \ + src/demo.h \ src/init.c \ src/init.h \ src/logging.c \ diff --git a/src/main.c b/src/main.c index c4ec5ea..93ac007 100644 --- a/src/main.c +++ b/src/main.c @@ -16,138 +16,29 @@ * . */ -#include +#include +#include #include "init.h" #include "logging.h" -#include "resources/image.h" -#include "resources/map.h" -#include "viewport.h" -#include "area.h" -#include "palettes.h" +#include "demo.h" -#define DEMO() \ - do { \ - vp->x = x + map->tilewidth / 2 - vp->w / 2; \ - vp->y = y + map->tileheight / 2 - vp->h / 2; \ - /* \ - * See the following resources when computing realistic \ - * daylight cycles: \ - * * \ - * * \ - */ \ - if (i % i_max < i_max / 6 * 2) { \ - /* 00:00 - 04:00 */ \ - t_max = i_max / 6 * 2; \ - pal_start = map->palettes[MAP_PALETTE_MORN] \ - .palette->palette; \ - pal_end = map->palettes[MAP_PALETTE_DAY] \ - .palette->palette; \ - } else if (i % i_max < i_max / 6 * 4) { \ - /* 04:00 - 12:00 */ \ - t_max = i_max / 6 * 2; \ - pal_start = map->palettes[MAP_PALETTE_DAY] \ - .palette->palette; \ - pal_end = map->palettes[MAP_PALETTE_EVE] \ - .palette->palette; \ - } else if (i % i_max < i_max / 6 * 5) { \ - /* 12:00 - 20:00 */ \ - t_max = i_max / 6 * 1; \ - pal_start = map->palettes[MAP_PALETTE_EVE] \ - .palette->palette; \ - pal_end = map->palettes[MAP_PALETTE_NIGHT] \ - .palette->palette; \ - } else { \ - /* 04:00 - 12:00 */ \ - t_max = i_max / 6 * 1; \ - pal_start = map->palettes[MAP_PALETTE_NIGHT] \ - .palette->palette; \ - pal_end = map->palettes[MAP_PALETTE_MORN] \ - .palette->palette; \ - } \ - if (!cycle_palettes_cosine(i, t_max, \ - pal_start, pal_end, pal)) { \ - warn("Failed to cycle palettes"); \ - } \ - set_area_palette(a, pal); \ - if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, \ - pal->colors, 0, pal->ncolors)) { \ - warn("Failed to set palette"); \ - } \ - render_area_to_viewport(a, vp); \ - SDL_Flip(vp->screen); \ - SDL_Delay(10); \ - ++i; \ - } while (0) +static void usage(const char *program_name, FILE *stream); int -main(void) +main(int argc, char **argv) { - struct viewport *vp; - struct map *map; - struct area *a; - SDL_Palette *pal; - struct map_exit *e; + const char *game; - init(); - vp = init_viewport(320, 240, 8); - - map = map_get(MAPSDIR "/db16-route.tmx"); - if (map == NULL) { - map = map_get("data/maps/db16-route.tmx"); - } - if (map == NULL) { - err(1, "Where's the map, George?"); - } - pal = map->palettes[MAP_PALETTE_DAY].palette->palette; - if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, pal->colors, - 0, pal->ncolors)) { - warn("Failed to set palette"); - } - a = area_new(map, vp->screen->format->palette); - - for (e = map->map_exits_head; e != NULL; e = e->next) { - debug("Map exit (size %dx%d) at (%d,%d) " - "to map %s (size %dx%d) at (%d,%d)", - e->width, e->height, - e->x, e->y, - e->target_map_name, - e->target_map->width, e->target_map->height, - e->target_x_coord, e->target_y_coord); + if (argc != 2) { + usage(argv[0], stderr); + exit(1); } + game = argv[1]; - /* Demo */ - { - Uint32 start, end; - int i, i_max, t_max; - int x, y; - SDL_Palette *pal_start, *pal_end; - - pal = malloc(sizeof(*pal)); - pal->colors = calloc(64, sizeof(*pal->colors)); - pal->ncolors = 64; + init(); - i = 0; - i_max = 1440; - start = SDL_GetTicks(); - x = 9 * map->tilewidth ; - y = 58 * map->tileheight; - for (; y > 50 * map->tileheight; y -= 1) DEMO(); /* Up */ - for (; x < 12 * map->tilewidth ; x += 1) DEMO(); /* Right */ - for (; y > 43 * map->tileheight; y -= 1) DEMO(); /* Up */ - for (; x > 5 * map->tilewidth ; x -= 1) DEMO(); /* Left */ - for (; y > 32 * map->tileheight; y -= 1) DEMO(); /* Up */ - for (; x < 13 * map->tilewidth ; x += 1) DEMO(); /* Right */ - for (; y > 31 * map->tileheight; y -= 1) DEMO(); /* Up */ - for (; x < 16 * map->tilewidth ; x += 1) DEMO(); /* Right */ - for (; y < 34 * map->tileheight; y += 1) DEMO(); /* Down */ - for (; x < 24 * map->tilewidth ; x += 1) DEMO(); /* Right */ - for (; y > 15 * map->tileheight; y -= 1) DEMO(); /* Up */ - for (; x > 11 * map->tilewidth ; x -= 1) DEMO(); /* Left */ - for (; y > 5 * map->tileheight; y -= 1) DEMO(); /* Up */ - for (; x < 17 * map->tilewidth ; x += 1) DEMO(); /* Right */ - for (; y > 1 * map->tileheight; y -= 1) DEMO(); /* Up */ - end = SDL_GetTicks(); - debug("Rendered %d frames in %d milliseconds", i, end - start); + if (strcmp(game, "demo") == 0) { + demo(); } quit(0); @@ -155,3 +46,10 @@ main(void) /* Control doesn't actually reach here. */ return 0; } + +void +usage(const char *program_name, FILE *stream) +{ + fprintf(stream, "Usage: %s \n", program_name); + fputs(" can be \"demo\".\n", stream); +} -- cgit v0.9.1