1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7. using System.Net;
  8.  
  9. using SdlDotNet;
  10. using SdlDotNet.Core;
  11. using SdlDotNet.Graphics;
  12. using SdlDotNet.Graphics.Sprites;
  13. using SdlDotNet.Input;
  14. using SdlDotNet.Audio;
  15.  
  16. using Newtonsoft.Json;
  17.  
  18. namespace ColorCycling
  19. {
  20.     class Scene
  21.     {
  22.         public Window window;
  23.         Bitmap scenebmp;
  24.         SceneList sceneList;
  25.         System.Drawing.Bitmap bmp;
  26.         public bool blend = false;
  27.  
  28.         Surface sceneSurface;
  29.  
  30.         int currentScene;
  31.         long sceneStartTicks;
  32.         int updateTimer = 0;
  33.  
  34.         public Scene(Window w, SceneList s)
  35.         {
  36.             window = w;
  37.             sceneList = s;
  38.             bmp = new System.Drawing.Bitmap(640, 480);
  39.         }
  40.  
  41.         public void LoadScene(int sceneindex)
  42.         {
  43.             currentScene = sceneindex;
  44.  
  45.             string str = "";
  46.  
  47.             if (File.Exists("scenes/" + sceneList.scenes[sceneindex].jsonpath + ".LBM.JSON"))
  48.             {
  49.                 str = File.ReadAllText("scenes/" + sceneList.scenes[sceneindex].jsonpath + ".LBM.JSON");
  50.             }
  51.             else
  52.             {
  53.                 string response = GetHTTP("http://www.effectgames.com/demos/canvascycle/image.php?file=" + sceneList.scenes[sceneindex].jsonpath);
  54.                 str = response.Substring(1, response.Length - 4);
  55.                 File.WriteAllText("scenes/" + sceneList.scenes[sceneindex].jsonpath + ".LBM.JSON", str);
  56.             }
  57.  
  58.             Music m = new Music("audio/" + sceneList.scenes[sceneindex].soundpath + ".ogg");
  59.             m.Play(true);
  60.  
  61.             JSONBitmap j = JsonConvert.DeserializeObject<JSONBitmap>(str);
  62.             scenebmp = new Bitmap(this, j);
  63.  
  64.             sceneStartTicks = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  65.  
  66.             scenebmp.render(bmp, false);
  67.             sceneSurface = new Surface(bmp);
  68.         }
  69.  
  70.         public void NextScene()
  71.         {
  72.             if (currentScene < 34)
  73.             {
  74.                 currentScene++;
  75.             }
  76.             else
  77.             {
  78.                 currentScene = 0;
  79.             }
  80.  
  81.             LoadScene(currentScene);
  82.         }
  83.  
  84.         public void PreviousScene()
  85.         {
  86.             if (currentScene > 0)
  87.             {
  88.                 currentScene--;
  89.             }
  90.             else
  91.             {
  92.                 currentScene = 34;
  93.             }
  94.  
  95.             LoadScene(currentScene);
  96.         }
  97.  
  98.         string GetHTTP(string requeststr)
  99.         {
  100.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requeststr);
  101.             request.KeepAlive = false;
  102.             request.Method = "GET";
  103.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  104.             StreamReader sr = new StreamReader(response.GetResponseStream());
  105.             return sr.ReadToEnd();
  106.         }
  107.  
  108.         public void Update()
  109.         {
  110.             updateTimer++;
  111.             if (updateTimer > 2)
  112.             {
  113.                 scenebmp.palette.cycle(scenebmp.palette.baseColors, GetTickCount(), 1, blend);
  114.                 scenebmp.render(bmp, false);
  115.                 Draw();
  116.                 updateTimer = 0;
  117.             }
  118.             else
  119.             {
  120.                
  121.             }
  122.         }
  123.  
  124.         long GetTickCount()
  125.         {
  126.             long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  127.             return milliseconds - sceneStartTicks;
  128.         }
  129.  
  130.         void Draw()
  131.         {
  132.             sceneSurface = new Surface(bmp);
  133.             window.BlitToScreen(sceneSurface);
  134.             sceneSurface.Dispose();
  135.         }
  136.     }
  137. }