main.cpp 19.4 KB
Newer Older
1
2
#include <stdio.h>
#include <string.h>
Gargaj's avatar
Gargaj committed
3
#include <assert.h>
Gargaj's avatar
Gargaj committed
4

5
6
7
8
9
10
11
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd
#else
#include <unistd.h>
#endif

Gargaj's avatar
Gargaj committed
12
#include "ShaderEditor.h"
Gargaj's avatar
add SDL    
Gargaj committed
13
#include "Renderer.h"
14
#include "FFT.h"
Gargaj's avatar
Gargaj committed
15
#include "MIDI.h"
Gargaj's avatar
Gargaj committed
16
#include "Timer.h"
17
#include "Misc.h"
18
19
#include "UniConversion.h"
#include "jsonxx.h"
20
#include "Capture.h"
21

22
23
24
25
26
27
28
29
void ReplaceTokens( std::string &sDefShader, const char * sTokenBegin, const char * sTokenName, const char * sTokenEnd, std::vector<std::string> &tokens )
{
  if (sDefShader.find(sTokenBegin) != std::string::npos
    && sDefShader.find(sTokenName) != std::string::npos
    && sDefShader.find(sTokenEnd) != std::string::npos
    && sDefShader.find(sTokenBegin) < sDefShader.find(sTokenName)
    && sDefShader.find(sTokenName) < sDefShader.find(sTokenEnd))
  {
30
    int nTokenStart = (int)(sDefShader.find(sTokenBegin) + strlen(sTokenBegin));
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    std::string sTextureToken = sDefShader.substr( nTokenStart, sDefShader.find(sTokenEnd) - nTokenStart );

    std::string sFinalShader;
    sFinalShader = sDefShader.substr( 0, sDefShader.find(sTokenBegin) );

    //for (std::map<std::string, Renderer::Texture*>::iterator it = tokens.begin(); it != tokens.end(); it++)
    for (int i=0; i < tokens.size(); i++)
    {
      std::string s = sTextureToken;
      while (s.find(sTokenName) != std::string::npos)
      {
        s.replace( s.find(sTokenName), strlen(sTokenName), tokens[i], 0, std::string::npos );
      }
      sFinalShader += s;
    }
    sFinalShader += sDefShader.substr( sDefShader.find(sTokenEnd) + strlen(sTokenEnd), std::string::npos );
    sDefShader = sFinalShader;
  }
}

Marco Foco's avatar
Marco Foco committed
51
int main(int argc, const char *argv[])
Gargaj's avatar
add SDL    
Gargaj committed
52
{
Gargaj's avatar
Gargaj committed
53
  Misc::PlatformStartup();
54

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  const char * configFile = "config.json";
  if ( argc > 1 )
  {
    configFile = argv[ 1 ];
    printf( "Loading config file '%s'...\n", configFile );
  }
  else
  {
    char configPath[ 256 ] = { 0 };
    if ( getcwd( configPath, 255 ) )
    {
      printf( "Looking for config.json in '%s'...\n", configPath );
    }
  }

70
  jsonxx::Object options;
71
  FILE * fConf = fopen( configFile, "rb" );
72
73
74
75
76
77
  if (fConf)
  {
    printf("Config file found, parsing...\n");

    char szConfig[65535];
    memset( szConfig, 0, 65535 );
78
    fread( szConfig, 1, 65535, fConf );
79
80
81
82
83
    fclose(fConf);

    options.parse( szConfig );
  }

Gargaj's avatar
add SDL    
Gargaj committed
84
  RENDERER_SETTINGS settings;
Gargaj's avatar
Gargaj committed
85
  settings.bVsync = false;
Gargaj's avatar
Gargaj committed
86
#ifdef _DEBUG
Gargaj's avatar
add SDL    
Gargaj committed
87
88
89
  settings.nWidth = 1280;
  settings.nHeight = 720;
  settings.windowMode = RENDERER_WINDOWMODE_WINDOWED;
Gargaj's avatar
Gargaj committed
90
91
92
93
#else
  settings.nWidth = 1920;
  settings.nHeight = 1080;
  settings.windowMode = RENDERER_WINDOWMODE_FULLSCREEN;
94
95
96
97
98
99
100
101
102
  if (options.has<jsonxx::Object>("window"))
  {
    if (options.get<jsonxx::Object>("window").has<jsonxx::Number>("width"))
      settings.nWidth = options.get<jsonxx::Object>("window").get<jsonxx::Number>("width");
    if (options.get<jsonxx::Object>("window").has<jsonxx::Number>("height"))
      settings.nHeight = options.get<jsonxx::Object>("window").get<jsonxx::Number>("height");
    if (options.get<jsonxx::Object>("window").has<jsonxx::Boolean>("fullscreen"))
      settings.windowMode = options.get<jsonxx::Object>("window").get<jsonxx::Boolean>("fullscreen") ? RENDERER_WINDOWMODE_FULLSCREEN : RENDERER_WINDOWMODE_WINDOWED;
  }
Gargaj's avatar
Gargaj committed
103
  if (!Renderer::OpenSetupDialog( &settings ))
104
    return -1;
Gargaj's avatar
Gargaj committed
105
#endif
Gargaj's avatar
add SDL    
Gargaj committed
106

Gargaj's avatar
add FFT    
Gargaj committed
107
  if (!Renderer::Open( &settings ))
Gargaj's avatar
Gargaj committed
108
109
  {
    printf("Renderer::Open failed\n");
110
    return -1;
Gargaj's avatar
Gargaj committed
111
  }
Gargaj's avatar
add FFT    
Gargaj committed
112
113

  if (!FFT::Open())
Gargaj's avatar
Gargaj committed
114
  {
115
116
    printf("FFT::Open() failed, continuing anyway...\n");
    //return -1;
Gargaj's avatar
Gargaj committed
117
  }
Gargaj's avatar
add SDL    
Gargaj committed
118

Gargaj's avatar
Gargaj committed
119
  if (!MIDI::Open())
Gargaj's avatar
Gargaj committed
120
  {
121
122
    printf("MIDI::Open() failed, continuing anyway...\n");
    //return -1;
Gargaj's avatar
Gargaj committed
123
  }
Gargaj's avatar
Gargaj committed
124

Gargaj's avatar
Gargaj committed
125
  std::map<std::string,Renderer::Texture*> textures;
Gargaj's avatar
Gargaj committed
126
  std::map<int,std::string> midiRoutes;
Gargaj's avatar
Gargaj committed
127

128
129
  const char * szDefaultFontPath = Misc::GetDefaultFontPath();

130
131
  SHADEREDITOR_OPTIONS editorOptions;
  editorOptions.nFontSize = 16;
132
133
134
135
136
137
138
139
  if ( !szDefaultFontPath )
  {
    printf( "Misc::GetDefaultFontPath couldn't find ANY default fonts!\n" );
  }
  else
  {
    editorOptions.sFontPath = szDefaultFontPath;
  }
140
141
142
143
  editorOptions.nOpacity = 0xC0;
  editorOptions.bUseSpacesForTabs = true;
  editorOptions.nTabSize = 2;
  editorOptions.bVisibleWhitespace = false;
144
  editorOptions.eAutoIndent = aitSmart;
Gargaj's avatar
Gargaj committed
145

Gargaj's avatar
Gargaj committed
146
  int nDebugOutputHeight = 200;
Gargaj's avatar
Gargaj committed
147
  int nTexPreviewWidth = 64;
Gargaj's avatar
Gargaj committed
148
  float fFFTSmoothingFactor = 0.9f; // higher value, smoother FFT
cupe's avatar
cupe committed
149
  float fFFTSlightSmoothingFactor = 0.6f; // higher value, smoother FFT
150

151
152
  std::string sPostExitCmd;

153
  if (!options.empty())
Gargaj's avatar
Gargaj committed
154
  {
155
    if (options.has<jsonxx::Object>("rendering"))
Gargaj's avatar
Gargaj committed
156
    {
157
158
      if (options.get<jsonxx::Object>("rendering").has<jsonxx::Number>("fftSmoothFactor"))
        fFFTSmoothingFactor = options.get<jsonxx::Object>("rendering").get<jsonxx::Number>("fftSmoothFactor");
159
160
      if (options.get<jsonxx::Object>("rendering").has<jsonxx::Number>("fftAmplification"))
        FFT::fAmplification = options.get<jsonxx::Object>("rendering").get<jsonxx::Number>("fftAmplification");
Gargaj's avatar
Gargaj committed
161
162
    }

163
    if (options.has<jsonxx::Object>("textures"))
Gargaj's avatar
Gargaj committed
164
165
    {
      printf("Loading textures...\n");
166
      std::map<std::string, jsonxx::Value*> tex = options.get<jsonxx::Object>("textures").kv_map();
Gargaj's avatar
Gargaj committed
167
168
      for (std::map<std::string, jsonxx::Value*>::iterator it = tex.begin(); it != tex.end(); it++)
      {
Marco Foco's avatar
Marco Foco committed
169
        const char * fn = it->second->string_value_->c_str();
Gargaj's avatar
Gargaj committed
170
171
        printf("* %s...\n",fn);
        Renderer::Texture * tex = Renderer::CreateRGBA8TextureFromFile( fn );
Gargaj's avatar
Gargaj committed
172
173
174
175
176
        if (!tex)
        {
          printf("Renderer::CreateRGBA8TextureFromFile(%s) failed\n",fn);
          return -1;
        }
Marco Foco's avatar
Marco Foco committed
177
        textures[it->first] = tex;
Gargaj's avatar
Gargaj committed
178
179
      }
    }
180
    if (options.has<jsonxx::Object>("font"))
Gargaj's avatar
Gargaj committed
181
    {
182
183
184
      if (options.get<jsonxx::Object>("font").has<jsonxx::Number>("size"))
        editorOptions.nFontSize = options.get<jsonxx::Object>("font").get<jsonxx::Number>("size");
      if (options.get<jsonxx::Object>("font").has<jsonxx::String>("file"))
185
186
      {
        std::string fontpath = options.get<jsonxx::Object>("font").get<jsonxx::String>("file");
187
        if (!Misc::FileExists(fontpath.c_str()))
188
        {
189
190
          printf("Font path (%s) is invalid!\n", fontpath.c_str());
          return -1;
191
        }
192
        editorOptions.sFontPath = fontpath;
193
      }
Gargaj's avatar
Gargaj committed
194
    }
195
    if (options.has<jsonxx::Object>("gui"))
Gargaj's avatar
Gargaj committed
196
    {
197
198
199
200
201
202
203
204
205
206
207
208
      if (options.get<jsonxx::Object>("gui").has<jsonxx::Number>("outputHeight"))
        nDebugOutputHeight = options.get<jsonxx::Object>("gui").get<jsonxx::Number>("outputHeight");
      if (options.get<jsonxx::Object>("gui").has<jsonxx::Number>("texturePreviewWidth"))
        nTexPreviewWidth = options.get<jsonxx::Object>("gui").get<jsonxx::Number>("texturePreviewWidth");
      if (options.get<jsonxx::Object>("gui").has<jsonxx::Number>("opacity"))
        editorOptions.nOpacity = options.get<jsonxx::Object>("gui").get<jsonxx::Number>("opacity");
      if (options.get<jsonxx::Object>("gui").has<jsonxx::Boolean>("spacesForTabs"))
        editorOptions.bUseSpacesForTabs = options.get<jsonxx::Object>("gui").get<jsonxx::Boolean>("spacesForTabs");
      if (options.get<jsonxx::Object>("gui").has<jsonxx::Number>("tabSize"))
        editorOptions.nTabSize = options.get<jsonxx::Object>("gui").get<jsonxx::Number>("tabSize");
      if (options.get<jsonxx::Object>("gui").has<jsonxx::Boolean>("visibleWhitespace"))
        editorOptions.bVisibleWhitespace = options.get<jsonxx::Object>("gui").get<jsonxx::Boolean>("visibleWhitespace");
209
210
211
212
213
214
215
216
217
218
219
      if (options.get<jsonxx::Object>("gui").has<jsonxx::String>("autoIndent"))
      {
        std::string autoIndent = options.get<jsonxx::Object>("gui").get<jsonxx::String>("autoIndent");
        if (autoIndent == "smart") {
          editorOptions.eAutoIndent = aitSmart;
        } else if (autoIndent == "preserve") {
          editorOptions.eAutoIndent = aitPreserve;
        } else {
          editorOptions.eAutoIndent = aitNone;
        }
      }
Gargaj's avatar
Gargaj committed
220
    }
221
    if (options.has<jsonxx::Object>("midi"))
Gargaj's avatar
Gargaj committed
222
    {
223
      std::map<std::string, jsonxx::Value*> tex = options.get<jsonxx::Object>("midi").kv_map();
Gargaj's avatar
Gargaj committed
224
225
      for (std::map<std::string, jsonxx::Value*>::iterator it = tex.begin(); it != tex.end(); it++)
      {
Marco Foco's avatar
Marco Foco committed
226
        midiRoutes[it->second->number_value_] = it->first;
Gargaj's avatar
Gargaj committed
227
228
      }
    }
229
230
231
232
    if (options.has<jsonxx::String>("postExitCmd"))
    {
      sPostExitCmd = options.get<jsonxx::String>("postExitCmd");
    }
233
    Capture::LoadSettings( options );
234
  }
235
  if (!editorOptions.sFontPath.size())
236
237
238
239
  {
    printf("Couldn't find any of the default fonts. Please specify one in config.json\n");
    return -1;
  }
240
  if (!Capture::Open(settings))
241
242
243
  {
    printf("Initializing capture system failed!\n");
    return 0;
Gargaj's avatar
Gargaj committed
244
  }
Gargaj's avatar
Gargaj committed
245
246

  Renderer::Texture * texFFT = Renderer::Create1DR32Texture( FFT_SIZE );
Gargaj's avatar
Gargaj committed
247
  Renderer::Texture * texFFTSmoothed = Renderer::Create1DR32Texture( FFT_SIZE );
cupe's avatar
cupe committed
248
  Renderer::Texture * texFFTIntegrated = Renderer::Create1DR32Texture( FFT_SIZE );
Gargaj's avatar
Gargaj committed
249

Gargaj's avatar
Gargaj committed
250
251
  bool shaderInitSuccessful = false;
  char szShader[65535];
252
  char szError[4096];
253
  FILE * f = fopen(Renderer::defaultShaderFilename,"rb");
Gargaj's avatar
Gargaj committed
254
  if (f)
255
  {
256
257
    printf("Loading last shader...\n");

Gargaj's avatar
Gargaj committed
258
    memset( szShader, 0, 65535 );
259
    fread( szShader, 1, 65535, f );
Gargaj's avatar
Gargaj committed
260
    fclose(f);
261
    if (Renderer::ReloadShader( szShader, (int)strlen(szShader), szError, 4096 ))
Gargaj's avatar
Gargaj committed
262
    {
263
      printf("Last shader works fine.\n");
Gargaj's avatar
Gargaj committed
264
265
      shaderInitSuccessful = true;
    }
266
267
268
    else {
      printf("Shader error:\n%s\n", szError);
    }
Gargaj's avatar
Gargaj committed
269
270
271
  }
  if (!shaderInitSuccessful)
  {
272
273
    printf("No valid last shader found, falling back to default...\n");

274
275
276
277
278
279
280
281
282
283
284
285
286
    std::string sDefShader = Renderer::defaultShader;

    std::vector<std::string> tokens;
    for (std::map<std::string, Renderer::Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
      tokens.push_back(it->first);
    ReplaceTokens(sDefShader, "{%textures:begin%}", "{%textures:name%}", "{%textures:end%}", tokens);

    tokens.clear();
    for (std::map<int,std::string>::iterator it = midiRoutes.begin(); it != midiRoutes.end(); it++)
      tokens.push_back(it->second);
    ReplaceTokens(sDefShader, "{%midi:begin%}", "{%midi:name%}", "{%midi:end%}", tokens);

    strncpy( szShader, sDefShader.c_str(), 65535 );
287
    if (!Renderer::ReloadShader( szShader, (int)strlen(szShader), szError, 4096 ))
Gargaj's avatar
Gargaj committed
288
    {
289
      printf("Default shader compile failed:\n");
Gargaj's avatar
Gargaj committed
290
      puts(szError);
Gargaj's avatar
Gargaj committed
291
292
      assert(0);
    }
293
294
  }

295
296
  Misc::InitKeymaps();

Gargaj's avatar
Gargaj committed
297
#ifdef SCI_LEXER
Gargaj's avatar
Gargaj committed
298
  Scintilla_LinkLexers();
Gargaj's avatar
Gargaj committed
299
#endif
300
  Scintilla::Surface * surface = Scintilla::Surface::Allocate( SC_TECHNOLOGY_DEFAULT );
Gargaj's avatar
Gargaj committed
301
  surface->Init( NULL );
302

Gargaj's avatar
Gargaj committed
303
304
  int nMargin = 20;

Gargaj's avatar
Gargaj committed
305
306
  bool bTexPreviewVisible = true;

307
  editorOptions.rect = Scintilla::PRectangle( nMargin, nMargin, settings.nWidth - nMargin - nTexPreviewWidth - nMargin, settings.nHeight - nMargin * 2 - nDebugOutputHeight );
308
  ShaderEditor mShaderEditor( surface );
309
  mShaderEditor.Initialise( editorOptions );
Gargaj's avatar
Gargaj committed
310
  mShaderEditor.SetText( szShader );
Gargaj's avatar
Gargaj committed
311

312
  editorOptions.rect = Scintilla::PRectangle( nMargin, settings.nHeight - nMargin - nDebugOutputHeight, settings.nWidth - nMargin - nTexPreviewWidth - nMargin, settings.nHeight - nMargin );
Gargaj's avatar
Gargaj committed
313
  ShaderEditor mDebugOutput( surface );
314
  mDebugOutput.Initialise( editorOptions );
Gargaj's avatar
Gargaj committed
315
316
317
  mDebugOutput.SetText( "" );
  mDebugOutput.SetReadOnly(true);

Gargaj's avatar
Gargaj committed
318
319
320
321
322
  static float fftData[FFT_SIZE];
  memset(fftData, 0, sizeof(float) * FFT_SIZE);
  static float fftDataSmoothed[FFT_SIZE];
  memset(fftDataSmoothed, 0, sizeof(float) * FFT_SIZE);

Gargaj's avatar
Gargaj committed
323

cupe's avatar
cupe committed
324
325
326
327
328
  static float fftDataSlightlySmoothed[FFT_SIZE];
  memset(fftDataSlightlySmoothed, 0, sizeof(float) * FFT_SIZE);
  static float fftDataIntegrated[FFT_SIZE];
  memset(fftDataIntegrated, 0, sizeof(float) * FFT_SIZE);

Gargaj's avatar
Gargaj committed
329
  bool bShowGui = true;
Gargaj's avatar
Gargaj committed
330
  Timer::Start();
Marco Foco's avatar
Marco Foco committed
331
  float fNextTick = 0.1f;
Gargaj's avatar
add SDL    
Gargaj committed
332
333
  while (!Renderer::WantsToQuit())
  {
334
    bool newShader = false;
Gargaj's avatar
Gargaj committed
335
    float time = Timer::GetTime() / 1000.0; // seconds
Gargaj's avatar
add SDL    
Gargaj committed
336
    Renderer::StartFrame();
Gargaj's avatar
add FFT    
Gargaj committed
337

Gargaj's avatar
Gargaj committed
338
339
    for(int i=0; i<Renderer::mouseEventBufferCount; i++)
    {
340
      if (bShowGui)
341
342
343
344
345
346
347
348
349
350
351
352
      {
        switch (Renderer::mouseEventBuffer[i].eventType)
        {
          case Renderer::MOUSEEVENTTYPE_MOVE:
            mShaderEditor.ButtonMovePublic( Scintilla::Point( Renderer::mouseEventBuffer[i].x, Renderer::mouseEventBuffer[i].y ) );
            break;
          case Renderer::MOUSEEVENTTYPE_DOWN:
            mShaderEditor.ButtonDown( Scintilla::Point( Renderer::mouseEventBuffer[i].x, Renderer::mouseEventBuffer[i].y ), time * 1000, false, false, false );
            break;
          case Renderer::MOUSEEVENTTYPE_UP:
            mShaderEditor.ButtonUp( Scintilla::Point( Renderer::mouseEventBuffer[i].x, Renderer::mouseEventBuffer[i].y ), time * 1000, false );
            break;
mathieu _alkama_ m's avatar
mathieu _alkama_ m committed
353
354
355
          case Renderer::MOUSEEVENTTYPE_SCROLL:
            mShaderEditor.WndProc( SCI_LINESCROLL, -Renderer::mouseEventBuffer[i].x, -Renderer::mouseEventBuffer[i].y);
            break;
356
357
        }
      }
Gargaj's avatar
Gargaj committed
358
359
360
    }
    Renderer::mouseEventBufferCount = 0;

Gargaj's avatar
Gargaj committed
361
362
    for(int i=0; i<Renderer::keyEventBufferCount; i++)
    {
Gargaj's avatar
Gargaj committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
      if (Renderer::keyEventBuffer[i].scanCode == 283) // F2
      {
        if (bTexPreviewVisible)
        {
          mShaderEditor.SetPosition( Scintilla::PRectangle( nMargin, nMargin, settings.nWidth - nMargin, settings.nHeight - nMargin * 2 - nDebugOutputHeight ) );
          mDebugOutput .SetPosition( Scintilla::PRectangle( nMargin, settings.nHeight - nMargin - nDebugOutputHeight, settings.nWidth - nMargin, settings.nHeight - nMargin ) );
          bTexPreviewVisible = false;
        }
        else
        {
          mShaderEditor.SetPosition( Scintilla::PRectangle( nMargin, nMargin, settings.nWidth - nMargin - nTexPreviewWidth - nMargin, settings.nHeight - nMargin * 2 - nDebugOutputHeight ) );
          mDebugOutput .SetPosition( Scintilla::PRectangle( nMargin, settings.nHeight - nMargin - nDebugOutputHeight, settings.nWidth - nMargin - nTexPreviewWidth - nMargin, settings.nHeight - nMargin ) );
          bTexPreviewVisible = true;
        }
      }
Gargaj's avatar
Gargaj committed
378
      else if (Renderer::keyEventBuffer[i].scanCode == 286 || (Renderer::keyEventBuffer[i].ctrl && Renderer::keyEventBuffer[i].scanCode == 'r')) // F5
Gargaj's avatar
Gargaj committed
379
380
      {
        mShaderEditor.GetText(szShader,65535);
381
        if (Renderer::ReloadShader( szShader, (int)strlen(szShader), szError, 4096 ))
Gargaj's avatar
Gargaj committed
382
        {
383
384
385
          // Shader compilation successful; we set a flag to save if the frame render was successful
          // (If there is a driver crash, don't save.)
          newShader = true;
Gargaj's avatar
Gargaj committed
386
387
388
        }
        else
        {
Gargaj's avatar
Gargaj committed
389
          mDebugOutput.SetText( szError );
Gargaj's avatar
Gargaj committed
390
391
        }
      }
392
      else if (Renderer::keyEventBuffer[i].scanCode == 292 || (Renderer::keyEventBuffer[i].ctrl && Renderer::keyEventBuffer[i].scanCode == 'f')) // F11 or Ctrl/Cmd-f  
Gargaj's avatar
Gargaj committed
393
394
395
      {
        bShowGui = !bShowGui;
      }
396
      else if (bShowGui)
Gargaj's avatar
Gargaj committed
397
398
      {
        bool consumed = false;
Gargaj's avatar
Gargaj committed
399
400
        if (Renderer::keyEventBuffer[i].scanCode)
        {
401
402
403
404
405
406
          mShaderEditor.KeyDown(
            iswalpha(Renderer::keyEventBuffer[i].scanCode) ? towupper(Renderer::keyEventBuffer[i].scanCode) : Renderer::keyEventBuffer[i].scanCode,
            Renderer::keyEventBuffer[i].shift,
            Renderer::keyEventBuffer[i].ctrl,
            Renderer::keyEventBuffer[i].alt,
            &consumed);
Gargaj's avatar
Gargaj committed
407
408
        }
        if (!consumed && Renderer::keyEventBuffer[i].character)
Gargaj's avatar
Gargaj committed
409
410
411
412
        {
          char    utf8[5] = {0,0,0,0,0};
          wchar_t utf16[2] = {Renderer::keyEventBuffer[i].character, 0};
          Scintilla::UTF8FromUTF16(utf16, 1, utf8, 4 * sizeof(char));
413
          mShaderEditor.AddCharUTF(utf8, (unsigned int)strlen(utf8));
Gargaj's avatar
Gargaj committed
414
415
416
417
418
419
        }

      }
    }
    Renderer::keyEventBufferCount = 0;

Gargaj's avatar
Gargaj committed
420
421
422
    Renderer::SetShaderConstant( "fGlobalTime", time );
    Renderer::SetShaderConstant( "v2Resolution", settings.nWidth, settings.nHeight );

Gargaj's avatar
Gargaj committed
423
424
    for (std::map<int,std::string>::iterator it = midiRoutes.begin(); it != midiRoutes.end(); it++)
    {
Marco Foco's avatar
Marco Foco committed
425
      Renderer::SetShaderConstant( it->second.c_str(), MIDI::GetCCValue( it->first ) );
Gargaj's avatar
Gargaj committed
426
427
    }

Gargaj's avatar
Gargaj committed
428

Gargaj's avatar
Gargaj committed
429
    if (FFT::GetFFT(fftData))
Gargaj's avatar
Gargaj committed
430
    {
Gargaj's avatar
Gargaj committed
431
      Renderer::UpdateR32Texture( texFFT, fftData );
cupe's avatar
cupe committed
432
433

      const static float maxIntegralValue = 1024.0f;
Gargaj's avatar
Gargaj committed
434
435
436
      for ( int i = 0; i < FFT_SIZE; i++ )
      {
        fftDataSmoothed[i] = fftDataSmoothed[i] * fFFTSmoothingFactor + (1 - fFFTSmoothingFactor) * fftData[i];
cupe's avatar
cupe committed
437
438
439
440
441
442

        fftDataSlightlySmoothed[i] = fftDataSlightlySmoothed[i] * fFFTSlightSmoothingFactor + (1 - fFFTSlightSmoothingFactor) * fftData[i];
        fftDataIntegrated[i] = fftDataIntegrated[i] + fftDataSlightlySmoothed[i];
        if (fftDataIntegrated[i] > maxIntegralValue) {
          fftDataIntegrated[i] -= maxIntegralValue;
        }
Gargaj's avatar
Gargaj committed
443
      }
cupe's avatar
cupe committed
444

Gargaj's avatar
Gargaj committed
445
      Renderer::UpdateR32Texture( texFFTSmoothed, fftDataSmoothed );
cupe's avatar
cupe committed
446
      Renderer::UpdateR32Texture( texFFTIntegrated, fftDataIntegrated );
Gargaj's avatar
Gargaj committed
447
    }
Gargaj's avatar
Gargaj committed
448
449

    Renderer::SetShaderTexture( "texFFT", texFFT );
Gargaj's avatar
Gargaj committed
450
    Renderer::SetShaderTexture( "texFFTSmoothed", texFFTSmoothed );
cupe's avatar
cupe committed
451
    Renderer::SetShaderTexture( "texFFTIntegrated", texFFTIntegrated );
Gargaj's avatar
add FFT    
Gargaj committed
452

Gargaj's avatar
Gargaj committed
453
    for (std::map<std::string, Renderer::Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
Gargaj's avatar
Gargaj committed
454
    {
Marco Foco's avatar
Marco Foco committed
455
      Renderer::SetShaderTexture( it->first.c_str(), it->second );
Gargaj's avatar
Gargaj committed
456
457
    }

458
459
    Renderer::RenderFullscreenQuad();

Gargaj's avatar
Gargaj committed
460
461
    Renderer::StartTextRendering();

Gargaj's avatar
Gargaj committed
462
    if (bShowGui)
463
    {
Gargaj's avatar
Gargaj committed
464
465
466
      if (time > fNextTick)
      {
        mShaderEditor.Tick();
Gargaj's avatar
Gargaj committed
467
        mDebugOutput.Tick();
Gargaj's avatar
Gargaj committed
468
469
        fNextTick = time + 0.1;
      }
470

Gargaj's avatar
Gargaj committed
471
472
      mShaderEditor.Paint();
      mDebugOutput.Paint();
Gargaj's avatar
Gargaj committed
473
474

      Renderer::SetTextRenderingViewport( Scintilla::PRectangle(0,0,Renderer::nWidth,Renderer::nHeight) );
Gargaj's avatar
Gargaj committed
475
476
477
478
479
480
481
482

      if (bTexPreviewVisible)
      {
        int y1 = nMargin;
        int x1 = settings.nWidth - nMargin - nTexPreviewWidth;
        int x2 = settings.nWidth - nMargin;
        for (std::map<std::string, Renderer::Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
        {
Gargaj's avatar
Gargaj committed
483
          int y2 = y1 + nTexPreviewWidth * (it->second->height / (float)it->second->width);
Gargaj's avatar
Gargaj committed
484
485
          Renderer::BindTexture( it->second );
          Renderer::RenderQuad(
486
487
488
489
            Renderer::Vertex( x1, y1, 0xccFFFFFF, 0.0, 0.0 ),
            Renderer::Vertex( x2, y1, 0xccFFFFFF, 1.0, 0.0 ),
            Renderer::Vertex( x2, y2, 0xccFFFFFF, 1.0, 1.0 ),
            Renderer::Vertex( x1, y2, 0xccFFFFFF, 0.0, 1.0 )
Gargaj's avatar
Gargaj committed
490
          );
491
          surface->DrawTextNoClip( Scintilla::PRectangle(x1,y1,x2,y2), *mShaderEditor.GetTextFont(), y2 - 5.0, it->first.c_str(), (int)it->first.length(), 0xffFFFFFF, 0x00000000);
Gargaj's avatar
Gargaj committed
492
493
494
495
          y1 = y2 + nMargin;
        }
      }

496
497
      char szLayout[255];
      Misc::GetKeymapName(szLayout);
Gargaj's avatar
Gargaj committed
498
      std::string sHelp = "F2 - toggle texture preview   F5 or Ctrl-R - recompile shader   F11 - hide GUI   Current keymap: ";
499
      sHelp += szLayout;
500
      surface->DrawTextNoClip( Scintilla::PRectangle(20,Renderer::nHeight - 20,100,Renderer::nHeight), *mShaderEditor.GetTextFont(), Renderer::nHeight - 5.0, sHelp.c_str(), (int)sHelp.length(), 0x80FFFFFF, 0x00000000);
Gargaj's avatar
Gargaj committed
501
    }
502

Gargaj's avatar
Gargaj committed
503

Gargaj's avatar
Gargaj committed
504
505
    Renderer::EndTextRendering();

Gargaj's avatar
add SDL    
Gargaj committed
506
    Renderer::EndFrame();
507

508
    Capture::CaptureFrame();
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

    if (newShader)
    {
      // Frame render successful, save shader
      FILE * f = fopen(Renderer::defaultShaderFilename,"wb");
      if (f)
      {
        fwrite( szShader, strlen(szShader), 1, f );
        fclose(f);
        mDebugOutput.SetText( "" );
      }
      else
      {
        mDebugOutput.SetText( "Unable to save shader! Your work will be lost when you quit!" );
      }
    }
Gargaj's avatar
add SDL    
Gargaj committed
525
526
  }

Gargaj's avatar
Gargaj committed
527

528
529
  delete surface;

Gargaj's avatar
Gargaj committed
530
  MIDI::Close();
Gargaj's avatar
add FFT    
Gargaj committed
531
532
  FFT::Close();

Gargaj's avatar
Gargaj committed
533
  Renderer::ReleaseTexture( texFFT );
Gargaj's avatar
Gargaj committed
534
  Renderer::ReleaseTexture( texFFTSmoothed );
Gargaj's avatar
Gargaj committed
535
536
537
538
539
  for (std::map<std::string, Renderer::Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
  {
    Renderer::ReleaseTexture( it->second );
  }

Gargaj's avatar
add SDL    
Gargaj committed
540
  Renderer::Close();
541
542
543

  if ( !sPostExitCmd.empty() )
  {
Marco Foco's avatar
Marco Foco committed
544
    Misc::ExecuteCommand( sPostExitCmd.c_str(), Renderer::defaultShaderFilename );
545
546
  }

Gargaj's avatar
Gargaj committed
547
548
  Misc::PlatformShutdown();

549
550
  return 0;
}