-/* $Id: main.c,v 1.4 2007/01/08 03:36:30 mmondor Exp $ */
+/* $Id: main.c,v 1.5 2007/01/08 09:09:07 mmondor Exp $ */
/*
* Copyright (c) 2006, Matthew Mondor
/* DEFINITIONS */
+struct scene {
+ const char *img, *mus;
+};
+
/* PRIVATE PROTOTYPES */
int main(int, char **);
-static int painting_switch(const char *);
-static int music_switch(const char *);
-static void frame_draw(void);
+static void screen_draw(void);
+static void scene_switch(const char *, const char *);
+static void scene_next(void);
static char *painting_file = NULL, *music_file = NULL;
+static const struct scene scenes[] = {
+ { "gardens_smallbridge", "adventure1" },
+ { "forest_entry", "forest"},
+ { "dungeon_deadend", "cavern" },
+ { "gardens_house", "castle" },
+ { NULL, NULL }
+};
+static int curscene = 0;
+
/* PRIVATE FUNCTIONS */
/* Initialization */
screen_init();
- (void) SDL_ShowCursor(0);
+ (void) SDL_ShowCursor(1);
(void) SDL_EnableKeyRepeat(0, 0);
+ painting_file = strdup("");
+ music_file = strdup("");
+
/*
* Ignore a few events with potentially high frequency but which
* we don't need
(void) Mix_ReserveChannels(2);
/* XXX */
- music_switch("1");
- painting_switch("gardens_smallbridge");
+ screen_draw();
+ scene_next();
/*
* Main loop.
while (!main_quit) {
SDL_Event ev;
- frame_draw();
(void) SDL_WaitEvent(NULL);
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
main_quit = 1;
break;
+ case SDL_KEYDOWN:
+ switch (ev.key.keysym.sym) {
+ case SDLK_ESCAPE:
+ main_quit = 1;
+ break;
+ case SDLK_SPACE:
+ scene_next();
+ break;
+ default:
+ break;
+ }
}
}
}
/* Fadeout music */
- (void) Mix_FadeOutMusic(1000);
- SDL_Delay(1000);
+ (void) Mix_FadeOutMusic(2000);
+ SDL_Delay(2000);
exit(EXIT_SUCCESS);
}
-static int
-painting_switch(const char *f)
+void
+screen_draw(void)
{
- SDL_Surface *s = NULL;
- char path[1024], *str = NULL;
-
- ASSERT(f != NULL);
-
- /* If image already current avoid reloading */
- if (painting_file != NULL && strcasecmp(f, painting_file) == 0)
- return 0;
-
- (void) snprintf(path, 1023, "img/%s.png", f);
- if ((s = IMG_Load(path)) != NULL && (str = strdup(f)) != NULL) {
- if (img_painting != NULL)
- SDL_FreeSurface(img_painting);
- img_painting = s;
- if (painting_file != NULL)
- free(painting_file);
- painting_file = str;
+ SDL_Rect r;
- return 0;
+ (void) SDL_BlitSurface(img_border, NULL, screen_surface, NULL);
+ r = (struct SDL_Rect){ 798, 16, 0, 0 };
+ (void) SDL_BlitSurface(img_textarea, NULL, screen_surface, &r);
+ if (img_painting != NULL) {
+ r = (SDL_Rect){ 16, 16, 0, 0 };
+ (void) SDL_BlitSurface(img_painting, NULL, screen_surface, &r);
}
+ /* XXX Draw any existing text, icons etc? */
- if (s != NULL)
- SDL_FreeSurface(s);
- if (str != NULL)
- free(str);
-
- return -1;
+ (void) SDL_Flip(screen_surface);
}
-static int
-music_switch(const char *f)
+static void
+scene_switch(const char *img, const char *mus)
{
+ SDL_Surface *s = NULL;
Mix_Music *m = NULL;
char path[1024], *str = NULL;
- int ret = -1;
-
- ASSERT(f != NULL);
-
- /* If music already current avoid reloading */
- if (music_file != NULL && strcasecmp(f, music_file) == 0)
- return 0;
-
- /* Fadeout and stop old music */
- (void) Mix_FadeOutMusic(1000);
- SDL_Delay(1000);
- (void) Mix_HaltMusic();
-
- (void) snprintf(path, 1023, "mus/%s.ogg", f);
- if ((m = Mix_LoadMUS(path)) != NULL && (str = strdup(f)) != NULL) {
- if (music != NULL)
- Mix_FreeMusic(music);
- music = m;
- if (music_file != NULL)
- free(music_file);
- music_file = str;
-
- m = NULL;
- str = NULL;
- ret = 0;
- }
+ int music_new = 0;
- if (m != NULL)
- Mix_FreeMusic(m);
- if (str != NULL)
- free(str);
+ ASSERT(img != NULL && mus != NULL);
- if (Mix_PlayMusic(music, -1) != 0) {
- (void) fprintf(stderr, "music_switch() - Mix_PlayMusic() - %s",
- Mix_GetError());
- ret = -1;
+ /*
+ * Because redrawing a lot while the music is just starting appears
+ * to degrade the music starting point, we change painting before
+ * changing music if we need to switch. We however first need to
+ * also fade out now if we're about to change musical track.
+ */
+ if (strcasecmp(mus, music_file) != 0) {
+ music_new = 1;
+ if (music != NULL) {
+ (void) Mix_FadeOutMusic(2000);
+ SDL_Delay(2000);
+ }
+ (void) Mix_HaltMusic();
+ }
+
+ /* Load new image as necessary and display it */
+ if (strcasecmp(img, painting_file) != 0) {
+ SDL_Rect r;
+
+ (void) snprintf(path, 1023, "img/%s.png", img);
+ if ((s = IMG_Load(path)) != NULL) {
+ if (img_painting != NULL)
+ SDL_FreeSurface(img_painting);
+ img_painting = s;
+ if ((str = strdup(img)) != NULL) {
+ if (painting_file != NULL)
+ free(painting_file);
+ painting_file = str;
+ }
+ painting_file = str;
+ } else
+ (void) fprintf(stderr,
+ "scene_switch() - IMG_Load(%s) - %s\n",
+ path, IMG_GetError());
+
+ r = (SDL_Rect){ 16, 16, 0, 0 };
+ (void) SDL_BlitSurface(img_painting, NULL, screen_surface, &r);
+ (void) SDL_Flip(screen_surface);
}
- return ret;
+ /* Load new music track if necessary and start it */
+ if (music_new) {
+ (void) snprintf(path, 1023, "mus/%s.ogg", mus);
+ if ((m = Mix_LoadMUS(path)) != NULL) {
+ if (music != NULL)
+ Mix_FreeMusic(music);
+ music = m;
+ if ((str = strdup(mus)) != NULL) {
+ if (music_file != NULL)
+ free(music_file);
+ music_file = str;
+ }
+ } else
+ (void) fprintf(stderr,
+ "scene_switch() - Mix_LoadMUS(%s) - %s\n",
+ path, Mix_GetError());
+
+ if (Mix_PlayMusic(music, -1) != 0)
+ (void) fprintf(stderr,
+ "scene_switch() - Mix_PlayMusic() - %s\n",
+ Mix_GetError());
+ }
}
-void
-frame_draw(void)
+static void
+scene_next(void)
{
- SDL_Rect r;
- (void) SDL_BlitSurface(img_border, NULL, screen_surface, NULL);
- r = (struct SDL_Rect){ 798, 16, 0, 0 };
- (void) SDL_BlitSurface(img_textarea, NULL, screen_surface, &r);
- r = (struct SDL_Rect){ 16, 16, 0, 0 };
- (void) SDL_BlitSurface(img_painting, NULL, screen_surface, &r);
+ if (scenes[curscene].img == NULL)
+ curscene = 0;
- (void) SDL_Flip(screen_surface);
+ scene_switch(scenes[curscene].img, scenes[curscene].mus);
+ curscene++;
}