From 725d17436b6b3d2c1f98aa1ef9bb1c1aaa8c46ad Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 12 Nov 2013 20:16:53 -0500 Subject: [PATCH] add build system and template source file --- .gitignore | 3 ++ Gemfile | 3 ++ Gemfile.lock | 10 +++++ Rakefile.rb | 35 +++++++++++++++ src/gss.d | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile.rb create mode 100644 src/gss.d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c7fef5e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.rsconscache +/gss +/gss.exe diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2c6fd29 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source "http://rubygems.org" + +gem "rscons" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..fa3c726 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,10 @@ +GEM + remote: http://rubygems.org/ + specs: + rscons (0.2.1) + +PLATFORMS + ruby + +DEPENDENCIES + rscons diff --git a/Rakefile.rb b/Rakefile.rb new file mode 100644 index 0000000..183a9fa --- /dev/null +++ b/Rakefile.rb @@ -0,0 +1,35 @@ +require "bundler" +begin + Bundler.setup(:default, :development) +rescue Bundler::BundlerError => e + raise "Unable to Bundler.setup(): You probably need to run `bundle install`: " + e.message +end +require "rscons" + +desc "Build ScreenSaver" +task :default do + Rscons::Environment.new do |env| + sources = [] + dirs = [ + "src", + "modules/DerelictUtil/source", + "modules/DerelictSDL2/source", + "modules/DerelictGL3/source", + "modules/gl3n/gl3n", + "modules/glamour/glamour", + ].each do |dir| + sources += Dir["#{dir}/**/*.d"] + end + env["D_IMPORT_PATH"] += [ + "modules/DerelictUtil/source", + "modules/DerelictSDL2/source", + "modules/DerelictGL3/source", + "modules/gl3n", + "modules/glamour", + ] + env["DFLAGS"] += ["-fversion=Derelict3", "-fversion=gl3n", "-fversion=SDLImage"] + env["LDFLAGS"] += ["-static-libgcc"] + env.build_root = "build" + env.Program("gss", sources) + end +end diff --git a/src/gss.d b/src/gss.d new file mode 100644 index 0000000..61b59a3 --- /dev/null +++ b/src/gss.d @@ -0,0 +1,125 @@ +import std.stdio; +import derelict.sdl2.sdl; +import derelict.opengl3.gl3; +import glamour.vao; +import glamour.shader; +import glamour.vbo; +import gl3n.linalg; + +enum int WIDTH = 800; +enum int HEIGHT = 600; + +GLint position_idx; +GLint color_idx; +GLint view_idx; +mat4 view_matrix; + +void init() +{ + glClearColor (1.0, 0.7, 0.0, 0.0); + glViewport(0, 0, WIDTH, HEIGHT); + immutable string shader_src = ` +vertex: + uniform mat4 view; + in vec2 position; + in vec3 color; + out vec3 color_i; + void main(void) + { + gl_Position = view * vec4(position, 0.0, 1.0); + color_i = color; + } +fragment: + in vec3 color_i; + void main(void) + { + gl_FragColor = vec4(color_i, 1.0); + } + `; + VAO vao = new VAO(); + Shader program = new Shader("program", shader_src); + program.bind(); + position_idx = program.get_attrib_location("position"); + color_idx = program.get_attrib_location("color"); + float[] vertices = [0.4, 0.4, -0.4, 0.4, -0.4, -0.4, 0.4, -0.4]; + float[] colors = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]; + ushort[] indices = [0, 1, 2, 3]; + Buffer vbo = new Buffer(vertices); + vbo.bind(); + glEnableVertexAttribArray(position_idx); + glVertexAttribPointer(position_idx, 2, GL_FLOAT, GL_FALSE, 0, null); + Buffer cbo = new Buffer(colors); + cbo.bind(); + glEnableVertexAttribArray(color_idx); + glVertexAttribPointer(color_idx, 3, GL_FLOAT, GL_FALSE, 0, null); + ElementBuffer ibo = new ElementBuffer(indices); + ibo.bind(); + view_idx = program.get_uniform_location("view"); +} + +void display(SDL_Window * window) +{ + glClear(GL_COLOR_BUFFER_BIT); + + view_matrix.make_identity(); + view_matrix.rotatez(SDL_GetTicks() / 500.0); + view_matrix.scale(HEIGHT / cast(float)WIDTH, 1.0, 1.0); + glUniformMatrix4fv(view_idx, 1, GL_TRUE, view_matrix.value_ptr); + glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, null); + + SDL_GL_SwapWindow(window); +} + +int main() +{ + DerelictSDL2.load(); + + DerelictGL3.load(); + + if (SDL_Init(SDL_INIT_EVERYTHING)) + { + writeln("Failed to initialize SDL!"); + return 1; + } + + SDL_Window * window = SDL_CreateWindow("d-dub-derelict-sdl2-gl3-demo", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WIDTH, + HEIGHT, + SDL_WINDOW_OPENGL); + SDL_GLContext context = SDL_GL_CreateContext(window); + + if (window == null) + { + writeln("Failed to create SDL window!"); + return 1; + } + + DerelictGL3.reload(); + + init(); + SDL_Event event; + for (;;) + { + if (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + break; + else if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) + break; + } + } + display(window); + } + + SDL_GL_DeleteContext(context); + + SDL_DestroyWindow(window); + + SDL_Quit(); + + return 0; +}