Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 212 lines (172 sloc) 6.133 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/

/*
This package holds a virtual display screen and keyboard input handler.
*/
package device

import (
"os"
"gl"
"github.com/jteeuwen/glfw"
)

const (
ScreenWidth = 256
ScreenHeight = 256
InputSize = 1 // Size of input buffer in 16-bit words.
VideoSize = (ScreenWidth * ScreenHeight) >> 1 // Size of video buffer in 16-bit words.
Scanline = ScreenWidth >> 1 // Width of a single row of pixels in 16-bit words.
)

var (
disp_colour uint16 // 16-bit base colour.
disp_tex gl.Texture // Opengl texture we render to.
disp_size [2]float32 // window size
disp_rgb [4]uint8 // Separate rgb components.
disp_mem []uint16 // Frame buffer.
)

// Open opens and initializes a new display with the given base colour.
// The supplied memory slice is the memory-mapped region from where this display
// reads its pixel data.
func Open(colour uint16, vsync bool, vidmem []uint16) (err os.Error) {
if err = glfw.Init(); err != nil {
return
}

glfw.OpenWindowHint(glfw.WindowNoResize, 1)

if err = glfw.OpenWindow(ScreenWidth<<1, ScreenHeight<<1, 8, 8, 8, 8, 0, 0, glfw.Windowed); err != nil {
return
}

glfw.SetWindowTitle("rvm")

if vsync {
glfw.SetSwapInterval(1)
} else {
glfw.SetSwapInterval(0)
}

glfw.Disable(glfw.AutoPollEvents)
glfw.SetWindowSizeCallback(func(w, h int) {
disp_size[0] = float32(w)
disp_size[1] = float32(h)

gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Viewport(0, 0, w, h)
gl.Ortho(0, float64(w), float64(h), 0, -1.0, 1.0)

gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
})
glfw.SetWindowRefreshCallback(Swap)

disp_mem = vidmem

gl.Init()
gl.Enable(gl.TEXTURE_2D)
gl.Enable(gl.BLEND)
gl.Disable(gl.DEPTH_TEST)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE)
gl.ShadeModel(gl.FLAT)
gl.ClearColor(0, 0, 0, 0)
gl.DepthFunc(gl.LEQUAL)
gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)

SetColour(colour)

disp_tex = gl.GenTexture()
disp_tex.Bind(gl.TEXTURE_2D)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, ScreenWidth, ScreenHeight, 0,
gl.ALPHA, gl.UNSIGNED_BYTE, make([]byte, ScreenWidth*ScreenHeight))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
disp_tex.Unbind(gl.TEXTURE_2D)
return
}

// Swap draws the pixel data to the backbuffer. It then swaps the
// front and back buffers to actually display the data.
func Swap() {
renderScene()
displayScene()
glfw.SwapBuffers()
}

/* Uncomment this version if the fancy fade edition below is too slow.
The fade version takes about 121 microseconds to execute.
This one about 16 microseconds.

var zero_mem [VideoSize]uint16

// Clear clears video memory by setting every pixel to zero.
func Clear() { copy(disp_mem, zero_mem[:]) }
*/

// Clear clears video memory by fading each pixel to 0 luminosity.
// This more or less simulates the old CRT monitor afterglow.
func Clear() {
for p := 0; p < len(disp_mem); p++ {
if disp_mem[p] == 0 {
continue
}

disp_mem[p] = ((disp_mem[p] & 0xff) >> 1) |
(((disp_mem[p] >> 8) >> 1) << 8)
}
}

func displayScene() {
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.LoadIdentity()

disp_tex.Bind(gl.TEXTURE_2D)

w := float32(ScreenWidth << 1)
h := float32(ScreenHeight << 1)
x := (disp_size[0] * 0.5) - (w * 0.5)
y := (disp_size[1] * 0.5) - (h * 0.5)

disp_rgb[3] = 255
gl.Color4ubv(disp_rgb[:])

gl.Begin(gl.QUADS)
gl.TexCoord2f(0, 0)
gl.Vertex2f(x, y)
gl.TexCoord2f(1, 0)
gl.Vertex2f(x+w, y)
gl.TexCoord2f(1, 1)
gl.Vertex2f(x+w, y+h)
gl.TexCoord2f(0, 1)
gl.Vertex2f(x, y+h)
gl.End()

disp_tex.Unbind(gl.TEXTURE_2D)
}

// renderScene Draws the video memory data to a texture.
//
// FIXME(jimt): We have to redraw the full video memory chunk every single time.
// If there is a more efficient way to do this at all, by all means let me know.
func renderScene() {
gl.PushAttrib(gl.VIEWPORT_BIT | gl.ENABLE_BIT)
gl.Viewport(0, 0, ScreenWidth, ScreenHeight)

gl.Clear(gl.COLOR_BUFFER_BIT)
gl.DrawPixels(ScreenWidth, ScreenHeight, gl.LUMINANCE, gl.UNSIGNED_BYTE, disp_mem)

disp_tex.Bind(gl.TEXTURE_2D)
gl.CopyTexImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, 0, 0, ScreenWidth, ScreenHeight, 0)
disp_tex.Unbind(gl.TEXTURE_2D)

gl.PopAttrib()
}

// KeyState returns the current state of the given key. This will be either
// 0 for 'released' or 1 for 'pressed'.
func KeyState(key uint16) uint16 { return uint16(glfw.Key(int(key))) }

// Update calls the message pump in order to process window and input events.
// This should be called once per loop. Otherwise the screen/input will
// freeze and become unresponsive.
func Update() { glfw.PollEvents() }

// Close shuts down the display and input handler.
// It also removes its reference to the memory mapped regions.
func Close() {
disp_mem = nil
disp_tex.Delete()
glfw.CloseWindow()
glfw.Terminate()
}

// SetColourRGB sets the monochromatic base-colour for the display.
// A single colour value is a 16 bit word with bit layout [rrrrrggggggbbbbb].
// Values for red and blue are 0-31 and 0-63 for green.
func SetColourRGB(r, g, b uint8) {
SetColour(uint16(r)<<11 | uint16(g)<<5 | uint16(b))
}

// SetColour sets the monochromatic base-colour for the display.
// A single colour value is a 16 bit word with bit layout [rrrrrggggggbbbbb]
// Values for red and blue are 0-31 and 0-63 for green.
func SetColour(c uint16) {
disp_colour = c

// Some precalc needed later on.
// Split RGB components and convert to 0-255 byte range.
disp_rgb[0] = uint8((float32(c>>11) / 0.31) * 2.55)
disp_rgb[1] = uint8((float32((c>>5)&63) / 0.63) * 2.55)
disp_rgb[2] = uint8((float32(c&31) / 0.31) * 2.55)
}

// Colour returns the current monochromatic base-colour for the display.
func Colour() uint16 { return disp_colour }
Something went wrong with that request. Please try again.