Skip to content

Commit

Permalink
First unoptimized, buggy and ugly attempt to implement Issue #43. BTW…
Browse files Browse the repository at this point in the history
…, it does demonstrate that it can be done.
  • Loading branch information
Andrea Fazzi committed Jun 30, 2011
1 parent 49e42b7 commit fe3c1f6
Show file tree
Hide file tree
Showing 21 changed files with 622 additions and 212 deletions.
234 changes: 125 additions & 109 deletions src/gospeccy.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/output/GOAM.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Package("spectrum/output")
DisableGoFmt("sdl_display.go")
1 change: 1 addition & 0 deletions src/output/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package output
2 changes: 2 additions & 0 deletions src/output/sdl/GOAM.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package("spectrum/output/sdl")
DisableGoFmt("sdl_display.go")
68 changes: 34 additions & 34 deletions src/output/sdl_compositing.go → src/output/sdl/sdl_compositing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* except for usages in immoral contexts.
*/

package output
package sdl

import (
"rand"
"⚛sdl"
"spectrum"
"unsafe"
sdllib "⚛sdl"
)

// Composes multiple SDL surfaces into a single surface
Expand All @@ -24,16 +24,16 @@ type SDLSurfaceComposer struct {
inputs []*input_surface_t

// The surface where to put the composited image
output_orNil *sdl.Surface
output_orNil *sdllib.Surface

commandChannel chan interface{}

showPaintedRegions bool
}

type input_surface_t struct {
surface *sdl.Surface
updatedRectsCh <-chan []sdl.Rect
surface *sdllib.Surface
updatedRectsCh <-chan []sdllib.Rect
forwarderLoop *spectrum.EventLoop
x, y int
}
Expand All @@ -58,14 +58,14 @@ func NewSDLSurfaceComposer(app *spectrum.Application) *SDLSurfaceComposer {
// The order of surfaces in the mentioned list defines the compositing order.
// The first surface will visually appear at the bottom,
// while the last surface will visually appear at the top.
func (composer *SDLSurfaceComposer) AddInputSurface(surface *sdl.Surface, x, y int, updatedRectsCh <-chan []sdl.Rect) {
func (composer *SDLSurfaceComposer) AddInputSurface(surface *sdllib.Surface, x, y int, updatedRectsCh <-chan []sdllib.Rect) {
composer.commandChannel <- cmd_add{surface, x, y, updatedRectsCh}
}

// Enqueues a command that will remove the specified surface
// from [the list of input surfaces of 'composer'].
// The returned channel will receive a single value when the command completes.
func (composer *SDLSurfaceComposer) RemoveInputSurface(surface *sdl.Surface) <-chan byte {
func (composer *SDLSurfaceComposer) RemoveInputSurface(surface *sdllib.Surface) <-chan byte {
done := make(chan byte)
composer.commandChannel <- cmd_remove{surface, done}
return done
Expand All @@ -80,13 +80,13 @@ func (composer *SDLSurfaceComposer) RemoveAllInputSurfaces() <-chan byte {
}

// Enqueues a command that will move the specified surface to a new position
func (composer *SDLSurfaceComposer) SetPosition(surface *sdl.Surface, x, y int) {
func (composer *SDLSurfaceComposer) SetPosition(surface *sdllib.Surface, x, y int) {
composer.commandChannel <- cmd_setPosition{surface, x, y}
}

// Enqueues a command that will replace the output surface.
// The returned channel will receive a single value when the command completes.
func (composer *SDLSurfaceComposer) ReplaceOutputSurface(surface_orNil *sdl.Surface) <-chan byte {
func (composer *SDLSurfaceComposer) ReplaceOutputSurface(surface_orNil *sdllib.Surface) <-chan byte {
done := make(chan byte)
composer.commandChannel <- cmd_replaceOutputSurface{surface_orNil, done}
return done
Expand All @@ -101,13 +101,13 @@ func (composer *SDLSurfaceComposer) ShowPaintedRegions(enable bool) {


type cmd_add struct {
surface *sdl.Surface
surface *sdllib.Surface
x, y int
updatedRectsCh <-chan []sdl.Rect
updatedRectsCh <-chan []sdllib.Rect
}

type cmd_remove struct {
surface *sdl.Surface
surface *sdllib.Surface
done chan<- byte
}

Expand All @@ -116,12 +116,12 @@ type cmd_removeAll struct {
}

type cmd_setPosition struct {
surface *sdl.Surface
surface *sdllib.Surface
x, y int
}

type cmd_replaceOutputSurface struct {
surface_orNil *sdl.Surface
surface_orNil *sdllib.Surface
done chan<- byte
}

Expand All @@ -131,7 +131,7 @@ type cmd_showPaintedRegions struct {

type cmd_update struct {
surface *input_surface_t
rects []sdl.Rect
rects []sdllib.Rect
}

// The composer's command loop.
Expand Down Expand Up @@ -217,7 +217,7 @@ func (composer *SDLSurfaceComposer) forwarderLoop(s *input_surface_t) {
}
}

func (composer *SDLSurfaceComposer) indexOf(surface *sdl.Surface) int {
func (composer *SDLSurfaceComposer) indexOf(surface *sdllib.Surface) int {
n := len(composer.inputs)
for i := 0; i < n; i++ {
if composer.inputs[i].surface == surface {
Expand All @@ -228,7 +228,7 @@ func (composer *SDLSurfaceComposer) indexOf(surface *sdl.Surface) int {
panic("no such surface")
}

func (composer *SDLSurfaceComposer) add(app *spectrum.Application, surface *sdl.Surface, x, y int, updatedRectsCh <-chan []sdl.Rect) {
func (composer *SDLSurfaceComposer) add(app *spectrum.Application, surface *sdllib.Surface, x, y int, updatedRectsCh <-chan []sdllib.Rect) {
newInput := &input_surface_t{
surface: surface,
updatedRectsCh: updatedRectsCh,
Expand All @@ -238,33 +238,33 @@ func (composer *SDLSurfaceComposer) add(app *spectrum.Application, surface *sdl.
}
composer.inputs = append(composer.inputs, newInput)

updateRect := sdl.Rect{
updateRect := sdllib.Rect{
X: int16(0),
Y: int16(0),
W: uint16(newInput.surface.W),
H: uint16(newInput.surface.H),
}

composer.performCompositing(x, y, []sdl.Rect{updateRect})
composer.performCompositing(x, y, []sdllib.Rect{updateRect})

go composer.forwarderLoop(newInput)
}

func (composer *SDLSurfaceComposer) remove(surface *sdl.Surface, done chan<- byte) {
func (composer *SDLSurfaceComposer) remove(surface *sdllib.Surface, done chan<- byte) {
i := composer.indexOf(surface)
input := composer.inputs[i]

// Remove the i-th element from 'composer.inputs'
copy(composer.inputs[i:], composer.inputs[i+1:])
composer.inputs = composer.inputs[0 : len(composer.inputs)-1]

updateRect := sdl.Rect{
updateRect := sdllib.Rect{
X: int16(0),
Y: int16(0),
W: uint16(input.surface.W),
H: uint16(input.surface.H),
}
composer.performCompositing(input.x, input.y, []sdl.Rect{updateRect})
composer.performCompositing(input.x, input.y, []sdllib.Rect{updateRect})

go func() {
deleted := input.forwarderLoop.Delete()
Expand All @@ -290,7 +290,7 @@ func (composer *SDLSurfaceComposer) removeAll(done chan<- byte) {
}()
}

func (composer *SDLSurfaceComposer) setPosition(surface *sdl.Surface, newX, newY int) {
func (composer *SDLSurfaceComposer) setPosition(surface *sdllib.Surface, newX, newY int) {
input := composer.inputs[composer.indexOf(surface)]

oldX, oldY := input.x, input.y
Expand All @@ -317,27 +317,27 @@ func (composer *SDLSurfaceComposer) setPosition(surface *sdl.Surface, newX, newY
absDeltaY = uint(oldY - newY)
}

updateRect := sdl.Rect{
updateRect := sdllib.Rect{
X: int16(x - newX),
Y: int16(y - newY),
W: uint16(uint(input.surface.W) + absDeltaX),
H: uint16(uint(input.surface.H) + absDeltaY),
}

// Repaint
composer.performCompositing(input.x, input.y, []sdl.Rect{updateRect})
composer.performCompositing(input.x, input.y, []sdllib.Rect{updateRect})
}
}

func (composer *SDLSurfaceComposer) repaintTheWholeOutputSurface() {
if composer.output_orNil != nil {
updateRect := sdl.Rect{
updateRect := sdllib.Rect{
X: int16(0),
Y: int16(0),
W: uint16(composer.output_orNil.W),
H: uint16(composer.output_orNil.H),
}
composer.performCompositing(0, 0, []sdl.Rect{updateRect})
composer.performCompositing(0, 0, []sdllib.Rect{updateRect})
}
}

Expand All @@ -351,11 +351,11 @@ var rnd *rand.Rand = rand.New(rand.NewSource(0))
// ofsX, ofsY: The translation to be applied to each element of 'rects'.
// After the translation, the position of each rectangle is relative
// to the coordinate system of the output surface.
func (composer *SDLSurfaceComposer) performCompositing(ofsX, ofsY int, rects []sdl.Rect) {
func (composer *SDLSurfaceComposer) performCompositing(ofsX, ofsY int, rects []sdllib.Rect) {
if composer.output_orNil != nil {
output := composer.output_orNil

updateRects := make([]sdl.Rect, 0)
updateRects := make([]sdllib.Rect, 0)

for inputIndex, input := range composer.inputs {
for _, rect := range rects {
Expand Down Expand Up @@ -395,7 +395,7 @@ func (composer *SDLSurfaceComposer) performCompositing(ofsX, ofsY int, rects []s
//
// The value of the alpha channel is from 0 to 256 (including 256).
// The value 256 maps to an alpha value of 1.0.
func fillRect(surface *SDLSurface, r sdl.Rect, RGB uint32, A uint32) {
func fillRect(surface *SDLSurface, r sdllib.Rect, RGB uint32, A uint32) {
R := uint32((RGB >> 16) & 0xFF)
G := uint32((RGB >> 8) & 0xFF)
B := uint32((RGB >> 0) & 0xFF)
Expand Down Expand Up @@ -432,17 +432,17 @@ func fillRect(surface *SDLSurface, r sdl.Rect, RGB uint32, A uint32) {

// Clips 'rect' to the dimensions of 'surface'.
// Returns the clipped rectangle.
func clip(rect sdl.Rect, surface *sdl.Surface) sdl.Rect {
func clip(rect sdllib.Rect, surface *sdllib.Surface) sdllib.Rect {
if (rect.X >= int16(surface.W)) || (rect.Y >= int16(surface.H)) {
return sdl.Rect{}
return sdllib.Rect{}
}

clippedRect := rect

if clippedRect.X < 0 {
w := int16(clippedRect.W) - (-clippedRect.X)
if w <= 0 {
return sdl.Rect{}
return sdllib.Rect{}
}

clippedRect.X = 0
Expand All @@ -451,7 +451,7 @@ func clip(rect sdl.Rect, surface *sdl.Surface) sdl.Rect {
if clippedRect.Y < 0 {
h := int16(clippedRect.H) - (-clippedRect.Y)
if h <= 0 {
return sdl.Rect{}
return sdllib.Rect{}
}

clippedRect.Y = 0
Expand Down
Loading

0 comments on commit fe3c1f6

Please sign in to comment.