/* scivi - visualization plugin for XMMS * Copyright (C) 2003 Vitaly V. Bursov * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef HAVE_CONFIG_H #include #endif /*#ifdef HAVE_LIBSDL_TTF*/ #if 0 #include #include #include #include #include #include #include #include #include #include #include "common.h" #include "gui.h" #include #define LISTS_COUNT 3 #define FPS_LIST (gui->list_ids) #define FPS_FONT (gui->fonts[0]) #define PRESET_LIST (gui->list_ids+1) #define PRESET_FONT (gui->fonts[1]) #define TITLE_LIST (gui->list_ids+2) #define TITLE_FONT (gui->fonts[2]) void gui_set_fps(Gui gui, float fps) { gui->fps = fps; gui->fps_changed = 1; } void gui_set_presetinfo(Gui gui, PresetInfo *preset_info) { gui->preset_info = preset_info; gui->preset_info_changed = 1; } void gui_set_dimensions(Gui gui, int width, int height) { gui->width = width; gui->height = height; } /* * SDL_ttf2 segfaults ... * */ TTF_Font *my_TTF_OpenFont(char *f, int s) { FILE *fd; fd = fopen(f, "rb"); if (!fd){ eprintf("Can not open file %s: %s\n", f, strerror(errno)); return NULL; } else fclose(fd); return TTF_OpenFont(f, s); } Gui gui_setup(char *fontfilename) { Gui gui = (Gui) malloc(sizeof(Gui_t)); #ifdef TTF_FONT_NAME char *fontpath = TTF_FONT_NAME; #else char *fontpath = NULL; #endif if (fontfilename) fontpath = fontfilename; if (!gui){ eprintf("Failed malloc %d bytes for Gui struct\n", sizeof(Gui_t)); return NULL; } gui->max_fonts = 3; gui->fonts = (TTF_Font**)malloc(sizeof(TTF_Font*)*gui->max_fonts); if (!gui->fonts){ eprintf("Failed malloc %d bytes for Gui->fonts struct\n", sizeof(TTF_Font*)*gui->max_fonts); free(gui); return NULL; } gui->initialized = 0; #define LOAD_FONT(r, fn, s) { \ dprintf("Loading font file: %s, size: %d\n", (fn), (s)); \ (r) = my_TTF_OpenFont((fn), (s)); \ if ((r) == NULL){ \ eprintf("Failed to load font\n"); \ } \ } if (fontpath){ LOAD_FONT(TITLE_FONT, fontpath, 20) LOAD_FONT(FPS_FONT, fontpath, 10) LOAD_FONT(PRESET_FONT, fontpath, 12) } else { PRESET_FONT = NULL; FPS_FONT = NULL; TITLE_FONT = NULL; } #ifdef TTF_FONT_NAME if (!TITLE_FONT) LOAD_FONT(TITLE_FONT, TTF_FONT_NAME, 20) if (!FPS_FONT) LOAD_FONT(FPS_FONT, TTF_FONT_NAME, 10) if (!PRESET_FONT) LOAD_FONT(PRESET_FONT, TTF_FONT_NAME, 12) #endif #undef LOAD_FONT gui->initialized = 1; gui->song_title = NULL; gui->fps = 0; gui->fps_changed = 1; /* redraw needed */ gui->preset_info = NULL; gui->preset_info_changed = 1; gui->list_ids = glGenLists(LISTS_COUNT); return gui; } void gui_finit(Gui gui) { int i; for (i=0;imax_fonts;i++){ if (gui->fonts[i]) { TTF_CloseFont(gui->fonts[i]); gui->fonts[i] = NULL; } } free(gui->fonts); if (gui->song_title){ g_free(gui->song_title); } glDeleteLists(gui->list_ids, LISTS_COUNT); free(gui); } int gui_process_event(Gui gui, SDL_Event *event) { return 0; } static void draw_title(Gui gui) { int pos; char *title; int title_refresh; SDL_Surface *text = NULL; SDL_Color color; if (!PRESET_FONT){ glNewList(TITLE_LIST, GL_COMPILE); /* do nothing */ glEndList(); return; } pos = xmms_remote_get_playlist_pos(SES); title = xmms_remote_get_playlist_title(SES, pos); title_refresh = 0; if (!gui->song_title && title){ gui->song_title = title; title_refresh = 1; } else if (gui->song_title && title){ title_refresh = strcmp(gui->song_title, title); if (title_refresh){ g_free(gui->song_title); gui->song_title = title; } } else if (gui->song_title){ g_free(gui->song_title); gui->song_title = NULL; glNewList(TITLE_LIST, GL_COMPILE); /* do nothing */ glEndList(); } if (title_refresh){ color.r = 0x00; color.g = 0x00; color.b = 0x00; text = TTF_RenderText_Blended(TITLE_FONT, gui->song_title, color); glNewList(TITLE_LIST, GL_COMPILE); if (text){ int i,j; glRasterPos2f(-0.95, 0.95); glPixelZoom(1.0f, -1.0f); glDrawPixels(text->w, text->h, GL_BGRA, GL_UNSIGNED_BYTE, text->pixels); for (i=0;ih;i++){ unsigned int *scanline = text->pixels+i*text->pitch; for (j=0;jw;j++){ if ((scanline[j] & 0xffffff) == 0x000000 ){ scanline[j] = (scanline[j] & 0xff000000) | 0xffffff; } } } glRasterPos2f(-0.96, 0.96); glDrawPixels(text->w, text->h, GL_BGRA, GL_UNSIGNED_BYTE, text->pixels); SDL_FreeSurface(text); } glEndList(); } } static void draw_fps(Gui gui) { SDL_Surface *text = NULL; SDL_Color color; if (gui->fps_changed && FPS_FONT){ char fps_str[32]; sprintf(fps_str, "FPS: %.2f", gui->fps); color.r = 0xf0; color.g = 0xf0; color.b = 0xf0; text = TTF_RenderText_Blended(FPS_FONT, fps_str, color); if (text){ glNewList(FPS_LIST, GL_COMPILE); glRasterPos2f(1.0f-(2.0f*text->w/gui->width), 1.0); glPixelZoom(1.0f, -1.0f); glDrawPixels(text->w, text->h, GL_BGRA, GL_UNSIGNED_BYTE, text->pixels); glEndList(); SDL_FreeSurface(text); gui->fps_changed = 0; } } if (gui->fps_changed == 1){ glNewList(FPS_LIST, GL_COMPILE); /* do nothing */ glEndList(); gui->fps_changed = 0; } } static void draw_preset(Gui gui) { SDL_Surface *text = NULL; SDL_Color color; if (gui->preset_info_changed && PRESET_FONT){ if (gui->preset_info){ char *buf; color.r = 0xf0; color.g = 0xf0; color.b = 0xf0; text = NULL; if (gui->preset_info->name && gui->preset_info->author){ int l = strlen(gui->preset_info->name)+ strlen(gui->preset_info->author)+3+1; buf = malloc(l); if (!buf){ eprintf("Failed to allocate %d bytes\n", l); text = TTF_RenderText_Blended(PRESET_FONT, gui->preset_info->name, color); } else { /* note: remember to update l above! */ sprintf(buf, "%s - %s", gui->preset_info->author, gui->preset_info->name); text = TTF_RenderText_Blended(PRESET_FONT, buf, color); free(buf); } } else { if (gui->preset_info->name){ text = TTF_RenderText_Blended(PRESET_FONT, gui->preset_info->name, color); } else if (gui->preset_info->author){ int l = strlen(gui->preset_info->author)+3+1; buf = malloc(l); if (!buf){ eprintf("Failed to allocate %d bytes\n", l); text = TTF_RenderText_Blended(PRESET_FONT, gui->preset_info->author, color); } else { /* note: remember to update l above! */ sprintf(buf, "by %s", gui->preset_info->author); text = TTF_RenderText_Blended(PRESET_FONT, buf, color); free(buf); } } } if (text){ glNewList(PRESET_LIST, GL_COMPILE); glRasterPos2f(-0.95f, 1.0); glPixelZoom(1.0f, -1.0f); glDrawPixels(text->w, text->h, GL_BGRA, GL_UNSIGNED_BYTE, text->pixels); glEndList(); SDL_FreeSurface(text); gui->preset_info_changed = 0; } } } if (gui->preset_info_changed == 1){ glNewList(PRESET_LIST, GL_COMPILE); /* do nothing */ glEndList(); gui->preset_info_changed = 0; } } void gui_draw(Gui gui) { unsigned int listi; SDL_Surface *text = NULL; SDL_Color color; if (!gui || !gui->initialized) return; draw_title(gui); draw_fps(gui); draw_preset(gui); /* execute all lists */ for (listi=0;listilist_ids+listi); } #endif /* HAVE_LIBSDL_TTF */