-/* $Id: main.c,v 1.3 2007/01/07 09:26:43 mmondor Exp $ */
+/* $Id: main.c,v 1.4 2007/01/08 03:36:30 mmondor Exp $ */
/*
* Copyright (c) 2006, Matthew Mondor
int main(int, char **);
+static int painting_switch(const char *);
+static int music_switch(const char *);
+static void frame_draw(void);
+
/* PUBLIC GLOBALS */
(void) SDL_EventState(SDL_KEYUP, SDL_IGNORE);
/*
+ * System images
+ */
+ if ((img_border = IMG_Load("img/screen_borders.png")) == NULL) {
+ (void) fprintf(stderr, "main() - IMG_Load(border) - %s\n",
+ IMG_GetError());
+ exit(EXIT_FAILURE);
+ }
+ if ((img_textarea = IMG_Load("img/screen_textarea.png")) == NULL) {
+ (void) fprintf(stderr, "main() - IMG_Load(textarea) - %s\n",
+ IMG_GetError());
+ exit(EXIT_FAILURE);
+ }
+
+ /*
* Audio
*/
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) != 0) {
}
(void) Mix_AllocateChannels(4);
(void) Mix_ReserveChannels(2);
- if ((music = Mix_LoadMUS("mus/1.ogg")) == NULL) {
- (void) fprintf(stderr, "main() - Mix_LoadMUS() - %s\n",
- Mix_GetError());
- exit(EXIT_FAILURE);
- }
- if (Mix_PlayMusic(music, -1) != 0) {
- (void) fprintf(stderr, "main() - Mix_PlayMusic() - %s\n",
- Mix_GetError());
- exit(EXIT_FAILURE);
- }
+
+ /* XXX */
+ music_switch("1");
+ painting_switch("gardens_smallbridge");
/*
* Main loop.
while (!main_quit) {
SDL_Event ev;
+ frame_draw();
(void) SDL_WaitEvent(NULL);
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
exit(EXIT_SUCCESS);
}
+
+static int
+painting_switch(const char *f)
+{
+ 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;
+
+ return 0;
+ }
+
+ if (s != NULL)
+ SDL_FreeSurface(s);
+ if (str != NULL)
+ free(str);
+
+ return -1;
+}
+
+static int
+music_switch(const char *f)
+{
+ 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;
+ }
+
+ if (m != NULL)
+ Mix_FreeMusic(m);
+ if (str != NULL)
+ free(str);
+
+ if (Mix_PlayMusic(music, -1) != 0) {
+ (void) fprintf(stderr, "music_switch() - Mix_PlayMusic() - %s",
+ Mix_GetError());
+ ret = -1;
+ }
+
+ return ret;
+}
+
+void
+frame_draw(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);
+
+ (void) SDL_Flip(screen_surface);
+}