diff --git a/source/app.d b/source/app.d index 44ded8d..3fa1d07 100644 --- a/source/app.d +++ b/source/app.d @@ -159,8 +159,8 @@ struct PixelBuffer { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //linearly interpolate between pixels, MIN if texture is too small for drawing area, MAG if drawing area is smaller than texture - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //texture type, level, format to store as, width, height, border, format loaded in glTexImage2D(GL_TEXTURE_2D, 0, input_format_, width_, height_, 0, output_format_, data_type_, pixels); @@ -205,7 +205,7 @@ struct PixelBuffer { void update(void[] pixels, size_t offset = 0) { bind(0); - glBufferSubData(GL_ARRAY_BUFFER, cast(GLintptr)offset, pixels.length, pixels.ptr); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, input_format_, data_type_, pixels.ptr); unbind(); } // update @@ -241,13 +241,14 @@ struct PixelBuffer { glBindBuffer(GL_ARRAY_BUFFER, vbo_); glBufferData(GL_ARRAY_BUFFER, vertices.length * vertices[0].sizeof, vertices.ptr, draw_type); + // pos glEnableVertexAttribArray(0); glVertexAttribPointer(0, - vertices[0].length, + 2, GL_FLOAT, GL_FALSE, - vertices[0].sizeof, - vertices.ptr + float.sizeof * 2, + null ); glBindVertexArray(0); @@ -280,13 +281,12 @@ struct PixelBuffer { uniform vec2 screen_size; layout(location = 0) in vec2 pos; - layout(location = 1) in vec2 uv; out vec2 frag_uv; void main() { - frag_uv = uv; - gl_Position = vec4(pos, 0.0, 0.0); + gl_Position = vec4(pos, 0.0, 1.0); + frag_uv = clamp(pos, vec2(0.0, 0.0), vec2(1.0, 1.0)); } }; @@ -301,7 +301,9 @@ struct PixelBuffer { out vec4 out_col; void main() { - out_col = texture(tex, frag_uv.st); + float v = texture(tex, frag_uv.st).r; + out_col = vec4(v, v, v, 1.0); + // out_col = vec4(1.0, 0.0, 0.0, 1.0); } }; @@ -313,17 +315,17 @@ struct PixelBuffer { void create(void* pixels, int w, int h) { float[2][6] rect = [ - [0.0f, 0.0f], // top left - [1.0f, 0.0f], // top right + [-1.0f, -1.0f], // top left + [1.0f, -1.0f], // top right [1.0f, 1.0f], // bottom right - [0.0f, 0.0f], // top left - [0.0f, 1.0f], // bottom left + [-1.0f, -1.0f], // top left + [-1.0f, 1.0f], // bottom left [1.0f, 1.0f], // bottom right ]; vao.create(rect[]); - tex.create(pixels, w, h); + tex.create(pixels, w, h, GL_RED, GL_RED); shader.compile(&vs_shader, &fs_shader); } // create @@ -338,6 +340,11 @@ struct PixelBuffer { } // draw + nothrow @nogc + void update(void[] pixels, size_t offset = 0) { + tex.update(pixels, offset); + } // update + } // PixelBuffer struct Chip8Status { @@ -637,7 +644,7 @@ struct Chip8 { switch (cpu.opcode & 0x0FFF) { case 0x00E0: // 0x00E0 Clears the screen. - // screen_buf[0..$] = 0; + screen_buf[0..$] = 0; draw_flag = true; break; @@ -1005,11 +1012,14 @@ struct Emulator { // setup debug ui status.initialize(&this, &emu); - // set up pixel buffer to poke at - emu.screen_data[0][] = 255; - buf.create(emu.screen_data.ptr, 64, 32); + // debug data + emu.screen_buf[0] = 255; + emu.screen_buf[64 - 1] = 255; + emu.screen_buf[64*32 - 64] = 255; + emu.screen_buf[64*32 - 1] = 255; - emu.screen_buf[] = 255; + // set up pixel buffer to poke a + buf.create(emu.screen_buf.ptr, 64, 32); } @@ -1061,6 +1071,10 @@ struct Emulator { int w, h; window.windowSize(w, h); + if (emu.draw_flag) { + buf.update(emu.screen_buf); + } + buf.draw(w, h); imgui.newFrame(window);