Skip to content

Loading…

OpenGL for video/input #2

Merged
merged 5 commits into from

2 participants

@wmbest2
Collaborator

OpenGL for video/input

@scottferg scottferg was assigned
@scottferg scottferg merged commit 2268999 into scottferg:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 8, 2012
  1. @wmbest2

    opengl

    wmbest2 committed
  2. @wmbest2

    Merge branch 'master' of github.com:wmbest2/Fergulator

    wmbest2 committed
    Conflicts:
    	video.go
  3. Fixed Opengl

    William Best committed
  4. Removed roms

    William Best committed
  5. Added input

    William Best committed
Showing with 157 additions and 119 deletions.
  1. +41 −47 controller.go
  2. +0 −1 machine.go
  3. +1 −1 palette.go
  4. +7 −7 ppu.go
  5. +0 −63 video.go
  6. +108 −0 video_gl.go
View
88 controller.go
@@ -1,7 +1,21 @@
package main
import (
- "github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
+ "github.com/jteeuwen/glfw"
+)
+
+const (
+ A = 90
+ B = 88
+ SELECT = glfw.KeyRshift
+ START = glfw.KeyEnter
+ UP = glfw.KeyUp
+ LEFT = glfw.KeyLeft
+ DOWN = glfw.KeyDown
+ RIGHT = glfw.KeyRight
+ RESET = 82
+ SAVE = 83
+ LOAD = 76
)
type Controller struct {
@@ -10,32 +24,32 @@ type Controller struct {
LastWrite Word
}
-func (c *Controller) SetButtonState(k sdl.KeyboardEvent, v Word) {
- switch k.Keysym.Sym {
- case sdl.K_z: // A
+func (c *Controller) SetButtonState(k int, v Word) {
+ switch (k) {
+ case A: // A
c.ButtonState[0] = v
- case sdl.K_x: // B
+ case B: // B
c.ButtonState[1] = v
- case sdl.K_RSHIFT: // Select
+ case SELECT: // Select
c.ButtonState[2] = v
- case sdl.K_RETURN: // Start
+ case START: // Start
c.ButtonState[3] = v
- case sdl.K_UP: // Up
+ case UP: // Up
c.ButtonState[4] = v
- case sdl.K_DOWN: // Down
+ case DOWN: // Down
c.ButtonState[5] = v
- case sdl.K_LEFT: // Left
+ case LEFT: // Left
c.ButtonState[6] = v
- case sdl.K_RIGHT: // Right
+ case RIGHT: // Right
c.ButtonState[7] = v
}
}
-func (c *Controller) KeyDown(e sdl.KeyboardEvent) {
+func (c *Controller) KeyDown(e int) {
c.SetButtonState(e, 0x41)
}
-func (c *Controller) KeyUp(e sdl.KeyboardEvent) {
+func (c *Controller) KeyUp(e int) {
c.SetButtonState(e, 0x40)
}
@@ -71,41 +85,21 @@ func (c *Controller) Init() {
}
}
-func JoypadListen() {
- for {
- select {
- case ev := <-sdl.Events:
- switch e := ev.(type) {
- case sdl.QuitEvent:
+func KeyListener(key, state int) {
+ if (state == glfw.KeyPress) {
+ switch key {
+ case glfw.KeyEsc:
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()
- }
- }
-
- switch e.Type {
- case sdl.KEYDOWN:
- controller.KeyDown(e)
- case sdl.KEYUP:
- controller.KeyUp(e)
- }
- }
+ case RESET:
+ cpu.RequestInterrupt(InterruptReset)
+ case LOAD:
+ LoadState()
+ case SAVE:
+ SaveState()
+ default:
+ controller.KeyDown(key)
}
+ } else {
+ controller.KeyUp(key)
}
}
View
1 machine.go
@@ -209,7 +209,6 @@ func main() {
video.Init(v, d, gamename)
defer video.Close()
- go JoypadListen()
go video.Render()
for running {
View
2 palette.go
@@ -1,7 +1,7 @@
package main
var (
- PaletteRgb = []int{
+ PaletteRgb = []uint32{
0x7C7C7C, 0x0000FC, 0x0000BC, 0x4428BC, 0x940084,
0xA80020, 0xA81000, 0x881400, 0x503000, 0x007800,
0x006800, 0x005800, 0x004058, 0x000000, 0x000000,
View
14 ppu.go
@@ -28,7 +28,7 @@ type Flags struct {
}
type Pixel struct {
- Color int
+ Color uint32
Value int
}
@@ -71,9 +71,9 @@ type Ppu struct {
AttributeShift [0x400]uint
Palettebuffer []Pixel
- Framebuffer []int
+ Framebuffer []uint32
- Output chan []int
+ Output chan []uint32
Cycle int
Scanline int
Timestamp int
@@ -85,9 +85,9 @@ type Ppu struct {
SuppressVbl bool
}
-func (p *Ppu) Init() (chan []int, chan []int) {
+func (p *Ppu) Init() (chan []uint32, chan []uint32) {
p.WriteLatch = true
- p.Output = make(chan []int)
+ p.Output = make(chan []uint32)
p.Cycle = 0
p.Scanline = -1
@@ -110,7 +110,7 @@ func (p *Ppu) Init() (chan []int, chan []int) {
}
p.Palettebuffer = make([]Pixel, 0xF000)
- p.Framebuffer = make([]int, 0xF000)
+ p.Framebuffer = make([]uint32, 0xF000)
return p.Output, nil
}
@@ -169,7 +169,7 @@ func (p *Ppu) raster() {
y := int(math.Floor(float64(i / 256)))
x := i - (y * 256)
- var color int
+ var color uint32
color = p.Palettebuffer[i].Color
p.Framebuffer[(y*256)+x] = color
p.Palettebuffer[i].Value = 0
View
63 video.go
@@ -1,63 +0,0 @@
-package main
-
-import (
- "fmt"
- "github.com/0xe2-0x9a-0x9b/Go-SDL/gfx"
- "github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
- "log"
-)
-
-type Video struct {
- screen *sdl.Surface
- fpsmanager *gfx.FPSmanager
- tick <-chan []int
- debug <-chan []int
-}
-
-func (v *Video) Init(t <-chan []int, d <-chan []int, n string) {
- if sdl.Init(sdl.INIT_VIDEO) != 0 {
- log.Fatal(sdl.GetError())
- }
-
- v.screen = sdl.SetVideoMode(512, 480, 32, 0)
-
- if v.screen == nil {
- log.Fatal(sdl.GetError())
- }
-
- sdl.WM_SetCaption(fmt.Sprintf("Fergulator - %s", n), "")
-
- v.fpsmanager = gfx.NewFramerate()
- v.fpsmanager.SetFramerate(70)
-
- v.tick = t
- v.debug = d
-}
-
-func (v *Video) Render() {
- buf := (*[512 * 480]int32)(v.screen.Pixels)[:]
- for {
- select {
- case val := <-v.tick:
- for i := len(val) - 1; i >= 0; i-- {
- y := i >> 8
- x := i - (y * 256)
-
- y *= 2
- x *= 2
-
- buf[(y*512)+x] = int32(val[i])
- buf[((y+1)*512)+x] = int32(val[i])
- buf[(y*512)+(x+1)] = int32(val[i])
- buf[((y+1)*512)+(x+1)] = int32(val[i])
- }
-
- v.screen.Flip()
- v.fpsmanager.FramerateDelay()
- }
- }
-}
-
-func (v *Video) Close() {
- sdl.Quit()
-}
View
108 video_gl.go
@@ -0,0 +1,108 @@
+package main
+
+import (
+ "fmt"
+ "github.com/banthar/gl"
+ "github.com/jteeuwen/glfw"
+ "os"
+)
+
+type Video struct {
+ tick <-chan []uint32
+ debug <-chan []uint32
+ tex gl.Texture
+}
+
+func reshape(width int, height int) {
+
+ gl.Viewport(0, 0, 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) Init(t <-chan []uint32, d <-chan []uint32, n string) {
+ v.tick = t
+ v.debug = d
+
+ if err := glfw.Init(); err != nil {
+ fmt.Fprintf(os.Stderr, "[e] %v\n", err)
+ return
+ }
+
+ if err := glfw.OpenWindow(512, 480, 0, 0, 0, 0, 0, 0, glfw.Windowed); err != nil {
+ fmt.Fprintf(os.Stderr, "[e] %v\n", err)
+ return
+ }
+
+ if gl.Init() != 0 {
+ panic("gl error")
+ }
+
+ gl.Enable(gl.TEXTURE_2D)
+
+ glfw.SetWindowTitle("FergulatorGL")
+ glfw.SetWindowSizeCallback(reshape)
+ glfw.SetWindowCloseCallback(quit_event)
+ glfw.SetKeyCallback(KeyListener)
+ reshape(512, 480)
+
+ v.tex = gl.GenTexture()
+
+}
+
+func (v *Video) Render() {
+ for {
+ select {
+ /*case d := <-v.debug:*/
+ // 60hz
+ // time.Sleep(16000000 * time.Nanosecond)
+ // time.Sleep(12000000 * time.Nanosecond)
+ case val := <-v.tick:
+ // 60hz
+ // time.Sleep(16000000 * time.Nanosecond)
+ // time.Sleep(4000000 * time.Nanosecond)
+
+ 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);
+ 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()
+
+ glfw.SwapBuffers()
+ }
+ }
+}
+
+func (v *Video) Close() {
+ glfw.CloseWindow()
+ glfw.Terminate()
+
+}
Something went wrong with that request. Please try again.