Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sandbox
- import "core:fmt"
- import "core:os"
- import "shared:odin-gl"
- import "shared:odin-glfw"
- KEY_UP :: glfw.KEY_UP;
- KEY_DOWN :: glfw.KEY_DOWN;
- KEY_LEFT :: glfw.KEY_LEFT;
- KEY_RIGHT :: glfw.KEY_RIGHT;
- Sandbox :: struct {
- width, height: int,
- pixels: []u32,
- vao: u32,
- vbo: u32,
- gltexture: u32,
- program: u32,
- window: glfw.Window_Handle,
- fullscreen: bool,
- old_x, old_y: i32,
- last_frame_start: f64,
- last_frame_time: f64,
- }
- vert_shader := `
- #version 330 core
- layout(location=0) in vec2 pos;
- layout(location=1) in vec2 in_uv;
- out vec2 uv;
- void main() {
- uv = in_uv;
- gl_Position = vec4(pos, 0, 1);
- }
- `;
- frag_shader := `
- #version 330 core
- in vec2 uv;
- out vec4 color;
- uniform sampler2D tex;
- void main() {
- color = texture(tex, uv);
- }
- `;
- key_callback :: proc "c" (window: glfw.Window_Handle, key, scancode, action, mods: i32) {
- sb := cast(^Sandbox) glfw.GetWindowUserPointer(window);
- if action == glfw.PRESS {
- if key == glfw.KEY_ESCAPE {
- os.exit(0);
- } else if key == glfw.KEY_F11 {
- sb.fullscreen = !sb.fullscreen;
- monitor := glfw.GetPrimaryMonitor();
- mode := glfw.GetVideoMode(monitor);
- if sb.fullscreen {
- sb.old_x, sb.old_y = glfw.GetWindowPos(sb.window);
- glfw.SetWindowMonitor(sb.window, monitor, 0, 0, mode.width, mode.height, mode.refresh_rate);
- } else {
- //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);
- glfw.SetWindowMonitor(sb.window, nil, sb.old_x, sb.old_y, i32(sb.width), i32(sb.height), -1);
- }
- }
- }
- }
- framebuffer_size_callback :: proc "c" (window: glfw.Window_Handle, width, height: i32) {
- gl.Viewport(0, 0, width, height);
- }
- make_sandbox :: proc(width, height: int, title: string) -> ^Sandbox {
- sandbox := new(Sandbox);
- sandbox.width = width;
- sandbox.height = height;
- sandbox.pixels = make([]u32, width*height);
- assert(glfw.Init() == glfw.TRUE, "Failed to init glfw3!");
- glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3);
- glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3);
- glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE );
- glfw.WindowHint(glfw.RESIZABLE, 1);
- glfw.WindowHint(glfw.AUTO_ICONIFY, 0);
- sandbox.window = glfw.CreateWindow(i32(width), i32(height), title, nil, nil);
- //sandbox.window = glfw.CreateWindow(i32(width), i32(height), title, glfw.GetPrimaryMonitor(), nil);
- assert(sandbox.window != nil, "Failed to create window!");
- glfw.SetWindowUserPointer(sandbox.window, rawptr(sandbox));
- glfw.SetKeyCallback(sandbox.window, key_callback);
- glfw.SetFramebufferSizeCallback(sandbox.window, framebuffer_size_callback);
- glfw.MakeContextCurrent(sandbox.window);
- glfw.SwapInterval(1);
- gl.load_up_to(3, 3, glfw.set_proc_address);
- gl.Viewport(0, 0, i32(width), i32(height));
- gl.ClearColor(1, 0, 1, 1);
- gl.GenVertexArrays(1, &sandbox.vao);
- gl.BindVertexArray(sandbox.vao);
- ok: bool;
- sandbox.program, ok = gl.load_shaders_source(vert_shader, frag_shader);
- if !ok {
- panic("Failed to compile shaders");
- }
- gl.UseProgram(sandbox.program);
- verts := [?]f32 {
- -1, 1, 0, 0,
- 1, -1, 1, 1,
- -1, -1, 0, 1,
- -1, 1, 0, 0,
- 1, 1, 1, 0,
- 1, -1, 1, 1,
- };
- gl.GenBuffers(1, &sandbox.vbo);
- gl.BindBuffer(gl.ARRAY_BUFFER, sandbox.vbo);
- gl.BufferData(gl.ARRAY_BUFFER, 4*4*6, &verts[0], gl.STATIC_DRAW);
- gl.EnableVertexAttribArray(0);
- gl.EnableVertexAttribArray(1);
- gl.VertexAttribPointer(0, 2, gl.FLOAT, gl.FALSE, 4*4, nil);
- gl.VertexAttribPointer(1, 2, gl.FLOAT, gl.FALSE, 4*4, rawptr(uintptr(2*4)));
- gl.GenTextures(1, &sandbox.gltexture);
- gl.BindTexture(gl.TEXTURE_2D, sandbox.gltexture);
- gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, i32(width), i32(height), 0, gl.BGRA, gl.UNSIGNED_BYTE, &sandbox.pixels[0]);
- gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
- gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
- sandbox.last_frame_start = glfw.GetTime();
- return sandbox;
- }
- should_close :: proc(sandbox: ^Sandbox) -> bool {
- return glfw.WindowShouldClose(sandbox.window);
- }
- set_cursor :: proc(sb: ^Sandbox, w,h: int, pixels: ^u8) {
- cur: glfw.Image;
- cur.width = i32(w);
- cur.height = i32(h);
- cur.pixels = pixels;
- cursor := glfw.CreateCursor(&cur, 0, 0);
- glfw.SetCursor(sb.window, cursor);
- }
- set_title :: proc(sandbox: ^Sandbox, fmt: string, vars: ..any) {
- glfw.SetWindowTitle(sandbox.window, fmt, ..vars);
- }
- set_vsync :: proc(sandbox: ^Sandbox, vsync: bool) {
- glfw.SwapInterval(vsync ? 1 : 0);
- }
- set_size :: proc(sandbox: ^Sandbox, width, height: int) {
- glfw.SetWindowSize(sandbox.window, i32(width), i32(height));
- gl.Viewport(0, 0, i32(width), i32(height));
- }
- get_key :: proc(sandbox: ^Sandbox, key: i32) -> bool {
- return glfw.GetKey(sandbox.window, key);
- }
- update :: proc(using sandbox: ^Sandbox) {
- gl.Clear(gl.COLOR_BUFFER_BIT);
- gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, i32(width), i32(height), gl.BGRA, gl.UNSIGNED_BYTE, &pixels[0]);
- gl.DrawArrays(gl.TRIANGLES, 0, 6);
- glfw.SwapBuffers(window);
- glfw.PollEvents();
- now := glfw.GetTime();
- sandbox.last_frame_time = now - sandbox.last_frame_start;
- sandbox.last_frame_start = now;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement