Skip to content

Commit

Permalink
Example: Working chip8 display
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Jun 4, 2020
1 parent 98934c4 commit 1a2e9bb
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions arcade/experimental/examples/chip8_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from arcade import get_projection

# Do the math to figure out our screen dimensions
SCREEN_WIDTH = 64 * 10
SCREEN_HEIGHT = 32 * 10
SCREEN_WIDTH = 64 * 20
SCREEN_HEIGHT = 32 * 20
SCREEN_TITLE = "CHIP-8 Screen"


Expand All @@ -31,38 +31,84 @@ def __init__(self, width, height, title):
self.program = self.ctx.program(
vertex_shader="""
#version 330
uniform mat4 projection;
in vec2 in_vert;
in vec2 in_uv;
out vec2 v_uv;
void main() {
gl_Position = projection * vec4(in_vert, 0.0, 1.0);
v_uv = in_uv;
}
""",
fragment_shader="""
#version 330
// Unsigned integer sampler for reading uint data from texture
uniform usampler2D screen;
in vec2 v_uv;
out vec4 out_color;
void main() {
out_color = vec4(vec3(texture(screen, v_uv * vec2(1.0, -1.0)).r), 1.0);
// Calculate the bit position on the x axis
uint bit_pos = uint(round((v_uv.x * 64) - 0.5)) % 8u;
// Create bit mask we can AND the fragment with to extract the pixel value
uint flag = uint(pow(2u, 7u - bit_pos));
// Read the fragment value (We reverse the y axis here as well)
uint frag = texture(screen, v_uv * vec2(1.0, -1.0)).r;
// Write the pixel value. Values above 1 will be clamped to 1.
out_color = vec4(vec3(frag & flag), 1.0);
}
"""
)
# 8 x 4
self.program['projection'] = get_projection().flatten()
self.program['screen'] = 0
self.quad = geometry.screen_rectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
self.texture = self.ctx.texture((8, 4), components=1, dtype='i1')
b = 0 # border to test scale
self.quad = geometry.screen_rectangle(b, b, SCREEN_WIDTH - b * 2, SCREEN_HEIGHT - b * 2)
self.texture = self.ctx.texture((8, 32), components=1, dtype='i1')
self.texture.write(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 0xAA, 0xCC, 0x99, 0xbd, 0x63, 0x20, 0x00,
0x55, 0x55, 0xCC, 0x99, 0xbd, 0x63, 0x10, 0x00,
0xAA, 0xAA, 0xCC, 0x99, 0xbd, 0x63, 0x08, 0x00,
0x55, 0x55, 0xCC, 0x99, 0xbd, 0x63, 0x04, 0x00,
0xAA, 0xAA, 0xCC, 0x99, 0xbd, 0x63, 0x02, 0x00,
0x55, 0x55, 0xCC, 0x99, 0xbd, 0x63, 0x01, 0x00,

0xAA, 0xAA, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x80,
0x55, 0x55, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x40,
0xAA, 0xAA, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x20,
0x55, 0x55, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x10,
0xAA, 0xAA, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x08,
0x55, 0x55, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x04,
0xAA, 0xAA, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x02,
0x55, 0x55, 0xCC, 0x99, 0xbd, 0x63, 0x00, 0x01,
]
).tobytes())

Expand Down

0 comments on commit 1a2e9bb

Please sign in to comment.