Skip to content
This repository
tree: f2f90da81e
Fetching contributors…

Cannot retrieve contributors at this time

file 195 lines (168 sloc) 4.296 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
package main

import (
"fmt"
"github.com/0xe2-0x9a-0x9b/Go-SDL/gfx"
"github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
"github.com/banthar/gl"
"log"
"math"
)

type Video struct {
tick <-chan []uint32
debug <-chan []uint32
fpsmanager *gfx.FPSmanager
screen *sdl.Surface
tex gl.Texture
}

func (v *Video) Init(t <-chan []uint32, d <-chan []uint32, n string) {
v.tick = t
v.debug = d

if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
log.Fatal(sdl.GetError())
}

v.screen = sdl.SetVideoMode(512, 480, 32, sdl.OPENGL|sdl.RESIZABLE)

if v.screen == nil {
log.Fatal(sdl.GetError())
}

sdl.WM_SetCaption(fmt.Sprintf("Fergulator - %s", n), "")

if gl.Init() != 0 {
panic("gl error")
}

gl.Enable(gl.TEXTURE_2D)
v.Reshape(int(v.screen.W), int(v.screen.H))

v.tex = gl.GenTexture()

v.fpsmanager = gfx.NewFramerate()
v.fpsmanager.SetFramerate(70)
}

func (v *Video) ResizeEvent(w, h int) {
v.screen = sdl.SetVideoMode(w, h, 32, sdl.OPENGL|sdl.RESIZABLE)
v.Reshape(w, h)
}

func (v *Video) Reshape(width int, height int) {
x_offset := 0
y_offset := 0

r := ((float64)(height)) / ((float64)(width))

if r > 0.9375 { // Height taller than ratio
h := (int)(math.Floor((float64)(0.9375 * (float64)(width))))
y_offset = (height - h) / 2
height = h
} else if r < 0.9375 { // Width wider
var scrW, scrH float64
if ppu.OverscanEnabled {
scrW = 240.0
scrH = 224.0
} else {
scrW = 256.0
scrH = 240.0
}

w := (int)(math.Floor((float64)((scrH / scrW) * (float64)(height))))
x_offset = (width - w) / 2
width = w
}

gl.Viewport(x_offset, y_offset, width, height)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(-1, 1, -1, 1, -1, 1)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.Disable(gl.DEPTH_TEST)
}

func quit_event() int {
running = false
return 0
}

func (v *Video) Render() {
for running {
select {
case ev := <-sdl.Events:
// TODO: Should see if there's a way to do this
// from another goroutine. Had to move it here for
// the ResizeEvent
switch e := ev.(type) {
case sdl.ResizeEvent:
v.ResizeEvent(int(e.W), int(e.H))
case sdl.QuitEvent:
running = false
case sdl.KeyboardEvent:
switch e.Keysym.Sym {
case sdl.K_ESCAPE:
running = false
case sdl.K_r:
// Trigger reset interrupt
if e.Type == sdl.KEYDOWN {
cpu.RequestInterrupt(InterruptReset)
}
case sdl.K_l:
if e.Type == sdl.KEYDOWN {
// Trigger reset interrupt
LoadState()
}
case sdl.K_s:
if e.Type == sdl.KEYDOWN {
// Trigger reset interrupt
SaveState()
}
case sdl.K_o:
if e.Type == sdl.KEYDOWN {
ppu.OverscanEnabled = !ppu.OverscanEnabled
}
case sdl.K_1:
if e.Type == sdl.KEYDOWN {
v.ResizeEvent(256, 240)
}
case sdl.K_2:
if e.Type == sdl.KEYDOWN {
v.ResizeEvent(512, 480)
}
case sdl.K_3:
if e.Type == sdl.KEYDOWN {
v.ResizeEvent(768, 720)
}
case sdl.K_4:
if e.Type == sdl.KEYDOWN {
v.ResizeEvent(1024, 960)
}
}

switch e.Type {
case sdl.KEYDOWN:
controller.KeyDown(e)
case sdl.KEYUP:
controller.KeyUp(e)
}
}
case val := <-v.tick:
slice := make([]uint8, len(val)*3)
for i := 0; i < len(val); i = i + 1 {
slice[i*3+0] = (uint8)((val[i] >> 16) & 0xff)
slice[i*3+1] = (uint8)((val[i] >> 8) & 0xff)
slice[i*3+2] = (uint8)((val[i]) & 0xff)
}

gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

v.tex.Bind(gl.TEXTURE_2D)

if ppu.OverscanEnabled {
gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 240, 224, 0, gl.RGB, gl.UNSIGNED_BYTE, slice)
} else {
gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 256, 240, 0, gl.RGB, gl.UNSIGNED_BYTE, slice)
}

gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

gl.Begin(gl.QUADS)
gl.TexCoord2f(0.0, 1.0)
gl.Vertex3f(-1.0, -1.0, 0.0)
gl.TexCoord2f(1.0, 1.0)
gl.Vertex3f(1.0, -1.0, 0.0)
gl.TexCoord2f(1.0, 0.0)
gl.Vertex3f(1.0, 1.0, 0.0)
gl.TexCoord2f(0.0, 0.0)
gl.Vertex3f(-1.0, 1.0, 0.0)
gl.End()

if v.screen != nil {
sdl.GL_SwapBuffers()
v.fpsmanager.FramerateDelay()
}
}
}
}

func (v *Video) Close() {
sdl.Quit()
}
Something went wrong with that request. Please try again.