Advertisement
TheBirkIsReal

Untitled

Aug 8th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.20 KB | None | 0 0
  1. package sandbox
  2.  
  3. import "core:fmt"
  4. import "core:os"
  5. import "shared:odin-gl"
  6. import "shared:odin-glfw"
  7.  
  8. KEY_UP    :: glfw.KEY_UP;
  9. KEY_DOWN  :: glfw.KEY_DOWN;
  10. KEY_LEFT  :: glfw.KEY_LEFT;
  11. KEY_RIGHT :: glfw.KEY_RIGHT;
  12.  
  13. Sandbox :: struct {
  14.     width, height: int,
  15.     pixels: []u32,
  16.     vao: u32,
  17.     vbo: u32,
  18.     gltexture: u32,
  19.     program: u32,
  20.     window: glfw.Window_Handle,
  21.     fullscreen: bool,
  22.     old_x, old_y: i32,
  23.  
  24.     last_frame_start: f64,
  25.     last_frame_time: f64,
  26. }
  27.  
  28. vert_shader := `
  29. #version 330 core
  30.  
  31. layout(location=0) in vec2 pos;
  32. layout(location=1) in vec2 in_uv;
  33.  
  34. out vec2 uv;
  35.  
  36. void main() {
  37.     uv = in_uv;
  38.     gl_Position = vec4(pos, 0, 1);
  39. }
  40. `;
  41.  
  42. frag_shader := `
  43. #version 330 core
  44.  
  45. in vec2 uv;
  46. out vec4 color;
  47.  
  48. uniform sampler2D tex;
  49.  
  50. void main() {
  51.     color = texture(tex, uv);
  52. }
  53. `;
  54.  
  55. key_callback :: proc "c" (window: glfw.Window_Handle, key, scancode, action, mods: i32) {
  56.     sb := cast(^Sandbox) glfw.GetWindowUserPointer(window);
  57.     if action == glfw.PRESS {
  58.         if key == glfw.KEY_ESCAPE {
  59.             os.exit(0);
  60.         } else if key == glfw.KEY_F11 {
  61.             sb.fullscreen = !sb.fullscreen;
  62.             monitor := glfw.GetPrimaryMonitor();
  63.             mode := glfw.GetVideoMode(monitor);
  64.  
  65.             if sb.fullscreen {
  66.                 sb.old_x, sb.old_y = glfw.GetWindowPos(sb.window);
  67.                 glfw.SetWindowMonitor(sb.window, monitor, 0, 0, mode.width, mode.height, mode.refresh_rate);
  68.             } else {
  69.                 //glfw.SetWindowMonitor(sb.window, nil, i32(int(mode.width)/2 - sb.width/2), i32(int(mode.height)/2 - sb.height/2), i32(sb.width), i32(sb.height), -1);
  70.                 glfw.SetWindowMonitor(sb.window, nil, sb.old_x, sb.old_y, i32(sb.width), i32(sb.height), -1);
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. framebuffer_size_callback :: proc "c" (window: glfw.Window_Handle, width, height: i32) {
  77.     gl.Viewport(0, 0, width, height);
  78. }
  79.  
  80. make_sandbox :: proc(width, height: int, title: string) -> ^Sandbox {
  81.     sandbox := new(Sandbox);
  82.  
  83.     sandbox.width = width;
  84.     sandbox.height = height;
  85.     sandbox.pixels = make([]u32, width*height);
  86.  
  87.     assert(glfw.Init() == glfw.TRUE, "Failed to init glfw3!");
  88.  
  89.     glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3);
  90.     glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3);
  91.     glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE );
  92.     glfw.WindowHint(glfw.RESIZABLE, 1);
  93.     glfw.WindowHint(glfw.AUTO_ICONIFY, 0);
  94.     sandbox.window = glfw.CreateWindow(i32(width), i32(height), title, nil, nil);
  95.     //sandbox.window = glfw.CreateWindow(i32(width), i32(height), title, glfw.GetPrimaryMonitor(), nil);
  96.     assert(sandbox.window != nil, "Failed to create window!");
  97.  
  98.     glfw.SetWindowUserPointer(sandbox.window, rawptr(sandbox));
  99.     glfw.SetKeyCallback(sandbox.window, key_callback);
  100.     glfw.SetFramebufferSizeCallback(sandbox.window, framebuffer_size_callback);
  101.  
  102.     glfw.MakeContextCurrent(sandbox.window);
  103.     glfw.SwapInterval(1);
  104.  
  105.     gl.load_up_to(3, 3, glfw.set_proc_address);
  106.  
  107.     gl.Viewport(0, 0, i32(width), i32(height));
  108.     gl.ClearColor(1, 0, 1, 1);
  109.  
  110.     gl.GenVertexArrays(1, &sandbox.vao);
  111.     gl.BindVertexArray(sandbox.vao);
  112.  
  113.     ok: bool;
  114.     sandbox.program, ok = gl.load_shaders_source(vert_shader, frag_shader);
  115.     if !ok {
  116.         panic("Failed to compile shaders");
  117.     }
  118.     gl.UseProgram(sandbox.program);
  119.  
  120.     verts := [?]f32 {
  121.         -1,  1, 0, 0,
  122.          1, -1, 1, 1,
  123.         -1, -1, 0, 1,
  124.  
  125.         -1,  1, 0, 0,
  126.          1,  1, 1, 0,
  127.          1, -1, 1, 1,
  128.     };
  129.     gl.GenBuffers(1, &sandbox.vbo);
  130.     gl.BindBuffer(gl.ARRAY_BUFFER, sandbox.vbo);
  131.     gl.BufferData(gl.ARRAY_BUFFER, 4*4*6, &verts[0], gl.STATIC_DRAW);
  132.  
  133.     gl.EnableVertexAttribArray(0);
  134.     gl.EnableVertexAttribArray(1);
  135.     gl.VertexAttribPointer(0, 2, gl.FLOAT, gl.FALSE, 4*4, nil);
  136.     gl.VertexAttribPointer(1, 2, gl.FLOAT, gl.FALSE, 4*4, rawptr(uintptr(2*4)));
  137.  
  138.     gl.GenTextures(1, &sandbox.gltexture);
  139.     gl.BindTexture(gl.TEXTURE_2D, sandbox.gltexture);
  140.     gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, i32(width), i32(height), 0, gl.BGRA, gl.UNSIGNED_BYTE, &sandbox.pixels[0]);
  141.     gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  142.     gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  143.  
  144.     sandbox.last_frame_start = glfw.GetTime();
  145.  
  146.     return sandbox;
  147. }
  148.  
  149. should_close :: proc(sandbox: ^Sandbox) -> bool {
  150.     return glfw.WindowShouldClose(sandbox.window);
  151. }
  152.  
  153. set_cursor :: proc(sb: ^Sandbox, w,h: int, pixels: ^u8) {
  154.     cur: glfw.Image;
  155.     cur.width = i32(w);
  156.     cur.height = i32(h);
  157.     cur.pixels = pixels;
  158.     cursor := glfw.CreateCursor(&cur, 0, 0);
  159.     glfw.SetCursor(sb.window, cursor);
  160. }
  161.  
  162. set_title :: proc(sandbox: ^Sandbox, fmt: string, vars: ..any) {
  163.     glfw.SetWindowTitle(sandbox.window, fmt, ..vars);
  164. }
  165.  
  166. set_vsync :: proc(sandbox: ^Sandbox, vsync: bool) {
  167.     glfw.SwapInterval(vsync ? 1 : 0);
  168. }
  169.  
  170. set_size :: proc(sandbox: ^Sandbox, width, height: int) {
  171.     glfw.SetWindowSize(sandbox.window, i32(width), i32(height));
  172.     gl.Viewport(0, 0, i32(width), i32(height));
  173. }
  174.  
  175. get_key :: proc(sandbox: ^Sandbox, key: i32) -> bool {
  176.     return glfw.GetKey(sandbox.window, key);
  177. }
  178.  
  179. update :: proc(using sandbox: ^Sandbox) {
  180.     gl.Clear(gl.COLOR_BUFFER_BIT);
  181.  
  182.     gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, i32(width), i32(height), gl.BGRA, gl.UNSIGNED_BYTE, &pixels[0]);
  183.     gl.DrawArrays(gl.TRIANGLES, 0, 6);
  184.  
  185.     glfw.SwapBuffers(window);
  186.     glfw.PollEvents();
  187.  
  188.     now := glfw.GetTime();
  189.     sandbox.last_frame_time = now - sandbox.last_frame_start;
  190.     sandbox.last_frame_start = now;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement