Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Theta::IO.*;
- import Theta::Web.*;
- module Theta::Program;
- // @summary A class capable of generation OpenGL for Theta.
- public static prototype Generator {
- fields
- {
- // @summary The main workspace that holds the project.
- public static String GLWorkspaceName = "Khronos.TEA";
- // @summary The project where files will be placed.
- public static String GLTargetProjectName = "OpenGL.TEA";
- // @summary The gl target path.
- public static mut String GLTargetPath;
- // @summary The project to generate code in.
- public static mut String GLTargetSourcesPath;
- // @summary The path to where to download / get OpenGL specs.
- public static mut String GLTargetSpecs;
- }
- structures
- {
- private type SpecURL<String> =
- {
- EGL("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/egl.xml"),
- GL("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/gl.xml"),
- GLX("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/glx.xml"),
- WGL("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/wgl.xml"),
- README("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/readme.pdf"),
- }
- private prototype CodeWriter
- {
- def CodeWriter<consructor>(IO::TextWriter writer)
- {
- m_writer = writer;
- }
- def CodeWriter<deconsructor>()
- {
- }
- structures
- {
- private TextWriter m_writer;
- private mut Int m_tabCount;
- private mut String m_tabString = String.Empty;
- public Int TabCount
- {
- get
- {
- return m_tabCount;
- }
- set
- {
- m_tabCount = Math.Max(value, 0);
- m_tabString = new string(' ', m_tabCount * 4);
- }
- }
- }
- methods
- {
- def WriteLine<public, void>()
- {
- m_writer.WriteLine();
- }
- def WriteLine<public, void>(String line)
- {
- m_writer.Write(m_tabString);
- m_writer.WriteLine(line);
- }
- def WriteLine<public, void>(String format, Object... args)
- {
- m_writer.Write(m_tabString);
- m_writer.WriteLine(format, args);
- }
- def WriteOpenBraceAndIndent<public, void>()
- {
- WriteLine("{");
- TabCount++;
- }
- def WriteCloseBraceAndDedent<public, void>()
- {
- TabCount--;
- WriteLine("}");
- }
- }
- }
- }
- methods
- {
- def UpdateDirPaths<private, static, void>()
- {
- IO::DirectoryInfo directoryRoot = new IO::DirectoryInfo(IO::Directory.GetCurrentDirectory());
- // Walk up to the solution file so we can then go into the project and write to the C# files directly.
- while (directoryRoot != null && !IO::File.Exists(IO::Path.Combine(directoryRoot.FullName, glWorkspaceName + ".sln")))
- {
- directoryRoot = directoryRoot.Parent;
- }
- // If the solution wasn't found (maybe its not not running from the bin directory), just write out to the current directory.
- if (directoryRoot == null)
- {
- directoryRoot = new IO::DirectoryInfo(IO::Directory.GetCurrentDirectory());
- }
- // Sets the specified gl paths.
- GLTargetPath = IO::Path.Combine(directoryRoot.FullName, glTargetProjectName);
- GLTargetSourcesPath = glTargetPath + "/" + "Sources";
- GLTargetSpecs = glTargetPath + "/" + "GLSpecs";
- }
- def DownloadSpecs<private, static, void>()
- {
- // Creates the specs path directory if it does not exist!
- if (!IO::Directory.Exists(glTargetSpecs))
- {
- IO::Directory.CreateDirectory(glTargetSpecs);
- }
- // Downloads the specs files.
- foreach (SpecURL url : SpecURL.ToList)
- {
- String[] spilit = url.Value.Split('/');
- String p = "";
- if (!IO::File.Exists((p = IO::Path.Combine(glTargetSpecs, spilit[spilit.Length - 1]))))
- {
- if (Web::HasInternet)
- {
- IO::StdOut.PrintLn("Downloading the required {0} document!", spilit[spilit.Length - 1]);
- Web::WebClient glClient = new Web::WebClient();
- glClient.DownloadFile(url.Value, p);
- }
- else
- {
- IO::StdOut.PrintLn("Could not download {0}, no internet connection!", url.Name);
- }
- }
- }
- }
- def Generate<private, static, void>(String xmlName, String dllPath)
- {
- var rootPath = "";
- var source = IO::XDocument.Load(GLTargetSpecs + "/" + xmlName + ".xml");
- var sourceTypes = XDocument.Load(GLTargetSpecs + "/type.xml");
- IO::Directory.CreateDirectory(rootPath = GLTargetSourcesPath + "/Graphics/");
- // MOR CRAP:
- CreateInitFile(Configs.glTargetSourcesPath + "/Graphics/", "OpenGLInit");
- }
- // MOR FUNCTIONS:
- def CreateInitFile<private, static, void>(String path, String initClassName)
- {
- using (IO::StreamWriter writer = new IO::StreamWriter(path + "/" + initClassName + ".tea"))
- {
- var code = new CodeWriter(writer);
- GenerateHeader(code, initClassName);
- code.WriteLine("fields");
- code.WriteOpenBraceAndIndent();
- code.WriteLine("internal static mut Func<string, IntPtr> GetProcAddress;");
- code.WriteCloseBraceAndDedent();
- code.WriteLine();
- code.WriteLine("structures");
- code.WriteOpenBraceAndIndent();
- code.WriteCloseBraceAndDedent();
- code.WriteLine();
- code.WriteLine("methods");
- code.WriteOpenBraceAndIndent();
- code.WriteLine("def Init<public, static, void>(Func<string, IntPtr> @procAddress)");
- code.WriteOpenBraceAndIndent();
- code.WriteLine("GetProcAddress = @procAddress;");
- code.WriteCloseBraceAndDedent();
- code.WriteCloseBraceAndDedent();
- CloseFile(code);
- }
- }
- def GenerateHeader<private, static, void>(CodeWriter code, String className)
- {
- Console.WriteLine("Generating class: {0}", className);
- code.WriteLine("module Theta::OpenGL");
- code.WriteLine();
- code.WriteLine("public static prototype {0}", className);
- code.WriteOpenBraceAndIndent();
- }
- def CloseFile<private, static, void>(CodeWriter code)
- {
- code.WriteCloseBraceAndDedent();
- }
- def FindCommand<private, static, GLCommand>(Command commands, string name)
- {
- foreach (var command : commands.Commands)
- {
- if (command.Name.Trim() == name.Trim())
- {
- return command;
- }
- }
- return null;
- }
- def FindEnumeration<private, static, GLEnum>(Enumeration enumeration, String name)
- {
- foreach (var enums : enumeration.Enums)
- {
- foreach (var enum2 : enums.Enums)
- {
- if (enum2.Name.Trim() == name.Trim())
- {
- return enum2;
- }
- }
- }
- return null;
- }
- def FindFeature<private, static, GLFeature>(Feature features, String name)
- {
- foreach (var feature : features.Features)
- {
- if (feature.Name.Trim() == name.Trim())
- {
- return feature;
- }
- }
- return null;
- }
- // @summary Entry point to the program, can be in any prototype.
- // @params args: Arguements provided to the program.
- // @returns int: Status code of the program.
- def Main<entry, Int>(String[] args)
- {
- // Sets up the console.
- IO::StdOut.Title = "TEA OpenGL";
- IO::StdOut.BackgroundColor = IO::ConsoleColour.Black;
- IO::StdOut.ForegroundColor = IO::ConsoleColour.Green;
- // Initalize files and paths.
- UpdateDirPaths();
- DownloadSpecs();
- // Generate files! TODO: OpenAL, OpenCL & GLFS
- Generate("glfw", "");
- Generate("gl", "");
- // Generate("wgl", "");
- // Generate("glx", "");
- // Generate("egl", "");
- // Wait for the user to end the program.
- IO::StdOut.PrintLn("Press any key to exit...");
- IO::StdOut.Pause();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement