[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / visualizations / Goom / Main.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21
22 /*
23
24 Goom Visualization Interface for XBMC
25 - Team XBMC
26
27 */
28
29 #define __STDC_LIMIT_MACROS
30
31 #include "../../addons/include/xbmc_vis_dll.h"
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <string>
36 extern "C" {
37 #include "goom.h"
38 }
39 #include "goom_config.h"
40 #include <GL/glew.h>
41
42 extern int  preset_index;
43 char        g_visName[512];
44 PluginInfo* g_goom  = NULL;
45
46 int g_tex_width     = GOOM_TEXTURE_WIDTH;
47 int g_tex_height    = GOOM_TEXTURE_HEIGHT;
48 int g_window_width  = 512;
49 int g_window_height = 512;
50 int g_window_xpos   = 0;
51 int g_window_ypos   = 0;
52
53 GLuint         g_texid       = 0;
54 unsigned char* g_goom_buffer = NULL;
55 short          g_audio_data[2][512];
56 std::string    g_configFile;
57
58 using namespace std;
59
60 //-- Create -------------------------------------------------------------------
61 // Called once when the visualisation is created by XBMC. Do any setup here.
62 //-----------------------------------------------------------------------------
63 extern "C" ADDON_STATUS ADDON_Create(void* hdl, void* props)
64 {
65   if (!props)
66     return ADDON_STATUS_UNKNOWN;
67
68   VIS_PROPS* visprops = (VIS_PROPS*)props;
69
70   strcpy(g_visName, visprops->name);
71   g_configFile = string(visprops->profile) + string("/goom.conf");
72   std::string presetsDir = string(visprops->presets) + string("/resources");
73
74   /** Initialise Goom */
75   if (g_goom)
76   {
77     goom_close( g_goom );
78     g_goom = NULL;
79   }
80
81   g_goom = goom_init(g_tex_width, g_tex_height);
82   if (!g_goom)
83     return ADDON_STATUS_UNKNOWN;
84
85   g_goom_buffer = (unsigned char*)malloc(g_tex_width * g_tex_height * 4);
86   goom_set_screenbuffer( g_goom, g_goom_buffer );
87   memset( g_audio_data, 0, sizeof(g_audio_data) );
88   g_window_width = visprops->width;
89   g_window_height = visprops->height;
90   g_window_xpos = visprops->x;
91   g_window_ypos = visprops->y;
92
93   return ADDON_STATUS_OK;
94 }
95
96 //-- Destroy -------------------------------------------------------------------
97 // Do everything before unload of this add-on
98 // !!! Add-on master function !!!
99 //-----------------------------------------------------------------------------
100 extern "C" void ADDON_Destroy()
101 {
102   if ( g_goom )
103   {
104     goom_close( g_goom );
105     g_goom = NULL;
106   }
107   if ( g_goom_buffer )
108   {
109     free( g_goom_buffer );
110     g_goom_buffer = NULL;
111   }
112 }
113
114 //-- Start --------------------------------------------------------------------
115 // Called when a new soundtrack is played
116 //-----------------------------------------------------------------------------
117 extern "C" void Start(int iChannels, int iSamplesPerSec, int iBitsPerSample, const char* szSongName)
118 {
119   if ( g_goom )
120   {
121     goom_update( g_goom, g_audio_data, 0, 0, (char*)szSongName, (char*)"XBMC" );
122   }
123 }
124
125 //-- Stop ---------------------------------------------------------------------
126 // Called when the visualisation is closed by XBMC
127 //-----------------------------------------------------------------------------
128 extern "C" void ADDON_Stop()
129 {
130   if (g_texid)
131   {
132     glDeleteTextures( 1, &g_texid );
133     g_texid = 0;
134   }
135 }
136
137 //-- Audiodata ----------------------------------------------------------------
138 // Called by XBMC to pass new audio data to the vis
139 //-----------------------------------------------------------------------------
140 extern "C" void AudioData(const float* pAudioData, int iAudioDataLength, float *pFreqData, int iFreqDataLength)
141 {
142   int copysize = iAudioDataLength < (int)sizeof( g_audio_data ) >> 1 ? iAudioDataLength : (int)sizeof( g_audio_data ) >> 1;
143   int ipos, i;
144   for(ipos = 0, i = 0; i < copysize; i += 2, ++ipos)
145   {
146     g_audio_data[0][ipos] = (int)(pAudioData[i  ] * (INT16_MAX+.5f));
147     g_audio_data[1][ipos] = (int)(pAudioData[i+1] * (INT16_MAX+.5f));
148   }
149 }
150
151
152 //-- Render -------------------------------------------------------------------
153 // Called once per frame. Do all rendering here.
154 //-----------------------------------------------------------------------------
155 extern "C" void Render()
156 {
157   if ( g_goom )
158   {
159     goom_set_screenbuffer( g_goom, g_goom_buffer );
160     if (!g_texid)
161     {
162       // initialize the texture we'll be using
163       glGenTextures( 1, &g_texid );
164       if (!g_texid)
165         return;
166       goom_update( g_goom, g_audio_data, 0, 0, NULL, (char*)"XBMC" );
167       glEnable(GL_TEXTURE_2D);
168       glBindTexture( GL_TEXTURE_2D, g_texid );
169       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
170       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
171       glTexImage2D( GL_TEXTURE_2D, 0, 4, g_tex_width, g_tex_height, 0,
172                     GL_RGBA, GL_UNSIGNED_BYTE, g_goom_buffer );
173       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
174     }
175     else
176     {
177       // update goom frame and copy to our texture
178       goom_update( g_goom, g_audio_data, 0, 0, NULL, (char*)"XBMC" );
179       glEnable(GL_TEXTURE_2D);
180       glBindTexture( GL_TEXTURE_2D, g_texid );
181       glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, g_tex_width, g_tex_height,
182                        GL_RGBA, GL_UNSIGNED_BYTE, g_goom_buffer );
183       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
184     }
185
186     glDisable(GL_BLEND);
187     glBegin( GL_QUADS );
188     {
189       glColor3f( 1.0, 1.0, 1.0 );
190       glTexCoord2f( 0.0, 0.0 );
191       glVertex2f( g_window_xpos, g_window_ypos );
192
193       glTexCoord2f( 0.0, 1.0 );
194       glVertex2f( g_window_xpos, g_window_ypos + g_window_height );
195
196       glTexCoord2f( 1.0, 1.0 );
197       glVertex2f( g_window_xpos + g_window_width, g_window_ypos + g_window_height );
198
199       glTexCoord2f( 1.0, 0.0 );
200       glVertex2f( g_window_xpos + g_window_width, g_window_ypos );
201     }
202     glEnd();
203     glDisable( GL_TEXTURE_2D );
204     glEnable(GL_BLEND);
205   }
206 }
207
208 //-- GetInfo ------------------------------------------------------------------
209 // Tell XBMC our requirements
210 //-----------------------------------------------------------------------------
211 extern "C" void GetInfo(VIS_INFO* pInfo)
212 {
213   pInfo->bWantsFreq = false;
214   pInfo->iSyncDelay = 0;
215 }
216
217 //-- OnAction -----------------------------------------------------------------
218 // Handle XBMC actions such as next preset, lock preset, album art changed etc
219 //-----------------------------------------------------------------------------
220 extern "C" bool OnAction(long flags, const void *param)
221 {
222   bool ret = false;
223   return ret;
224 }
225
226 //-- GetPresets ---------------------------------------------------------------
227 // Return a list of presets to XBMC for display
228 //-----------------------------------------------------------------------------
229 extern "C" unsigned int GetPresets(char ***presets)
230 {
231   return 0;
232 }
233
234 //-- GetPreset ----------------------------------------------------------------
235 // Return the index of the current playing preset
236 //-----------------------------------------------------------------------------
237 extern "C" unsigned GetPreset()
238 {
239   return 0;
240 }
241
242 //-- IsLocked -----------------------------------------------------------------
243 // Returns true if this add-on use settings
244 //-----------------------------------------------------------------------------
245 extern "C" bool IsLocked()
246 {
247   return false;
248 }
249
250 //-- GetSubModules ------------------------------------------------------------
251 // Return any sub modules supported by this vis
252 //-----------------------------------------------------------------------------
253 extern "C" unsigned int GetSubModules(char ***names)
254 {
255   return 0; // this vis supports 0 sub modules
256 }
257
258 //-- HasSettings --------------------------------------------------------------
259 // Returns true if this add-on use settings
260 // !!! Add-on master function !!!
261 //-----------------------------------------------------------------------------
262 extern "C" bool ADDON_HasSettings()
263 {
264   return false;
265 }
266
267 //-- GetStatus ---------------------------------------------------------------
268 // Returns the current Status of this visualisation
269 // !!! Add-on master function !!!
270 //-----------------------------------------------------------------------------
271 extern "C" ADDON_STATUS ADDON_GetStatus()
272 {
273   return ADDON_STATUS_OK;
274 }
275
276 //-- GetSettings --------------------------------------------------------------
277 // Return the settings for XBMC to display
278 // !!! Add-on master function !!!
279 //-----------------------------------------------------------------------------
280 extern "C" unsigned int ADDON_GetSettings(ADDON_StructSetting ***sSet)
281 {
282   return 0;
283 }
284
285 //-- FreeSettings --------------------------------------------------------------
286 // Free the settings struct passed from XBMC
287 // !!! Add-on master function !!!
288 //-----------------------------------------------------------------------------
289
290 extern "C" void ADDON_FreeSettings()
291 {
292 }
293
294 //-- SetSetting ---------------------------------------------------------------
295 // Set a specific Setting value (called from XBMC)
296 // !!! Add-on master function !!!
297 //-----------------------------------------------------------------------------
298 extern "C" ADDON_STATUS ADDON_SetSetting(const char *strSetting, const void* value)
299 {
300   return ADDON_STATUS_OK;
301 }