I've been trying to render an OpenGL context using a gl-bitmap and then displaying that in a snip (to enable user input). I extended the image snip class and overrode the basic methods to draw, get-extent, etc.
When I try to display my snip, I see nothing, but when I highlight over the snip with my cursor I can see the shape underneath! It's clearly rendering, and I can render the gl-bitmap by itself fine when it is not connected to a snip. What could possibly be making my snip appear white?
My two hypotheses are:
1) I didn't set up the snip-class% correctly. I have no clue what the purpose of this is (the documentation is challenging to follow here), and I don't know what argument to provide to `(set-classname "(lib ...)")` (what should replace the ...?)
#lang racket
(require racket/gui/base
ffi/unsafe
ffi/unsafe/define)
;; ffi code
(define my-ffi (ffi-lib "/Users/kshitijsachan/Documents/geopipe/builddir/lib/Debug/libgldraw"))
(define-ffi-definer define-gltest my-ffi)
;; type def
(define _GLTest-pointer (_cpointer 'GLTest))
;; functions
(define-gltest makeGLTest (_fun _int _int -> _GLTest-pointer))
(define-gltest drawAFrame (_fun _GLTest-pointer -> _void))
;; global variables
(define screen-width 100)
(define screen-height 100)
;; gl code
(define gl-config (new gl-config%))
(send gl-config set-double-buffered #f)
(send gl-config set-legacy? #f)
(define gl-bitmap (make-gl-bitmap screen-width screen-height gl-config))
(define gl-dc (new bitmap-dc% [bitmap gl-bitmap]))
(define gl-context (send gl-dc get-gl-context))
(define opengl-snip (make-object image-snip% gl-bitmap))
;; bitmap
(define my-bitmap (make-bitmap screen-width screen-height #t))
(define my-dc (new bitmap-dc% [bitmap my-bitmap]))
(send my-dc set-brush "red" 'solid)
(send my-dc set-pen "black" 3 'solid)
(send my-dc draw-ellipse 1 1 98 98)
;; snip-class (THIS IS WHAT I HAVE A QUESTION ON)
(define scroll-snip-class%
(class snip-class%
(inherit set-classname)
(super-new)
(set-classname "(lib test-snip)")
;; not sure if necessary
(define/override (read f)
(define width-b (box 0.0))
(define height-b (box 0.0))
(send f get width-b)
(send f get height-b)
(new scroll-snip% [snip-width (unbox width-b)] [snip-height (unbox height-b)]))
))
(define scroll-snip-class (new scroll-snip-class%))
;; custom image-snip (THIS IS ALSO WHAT I HAVE A QUESTION ON)
(define scroll-snip%
(class image-snip%
;; set up fields
(init bit)
(define snip-bitmap bit)
(init-field [snip-width screen-width] [snip-height screen-height])
;; make image-snip
(super-make-object snip-bitmap)
;; add ability to handle events
(inherit/super set-flags get-admin set-snipclass)
(set-snipclass scroll-snip-class)
(set-flags (list 'handles-events))
;; override methods
(define/override (get-extent dc x y w h descent space lspace rspace)
(define (maybe-set-box! b v) (when b (set-box! b v)))
(maybe-set-box! w snip-width)
(maybe-set-box! h snip-height)
(maybe-set-box! descent 1.0)
(maybe-set-box! space 1.0)
(maybe-set-box! lspace 1.0)
(maybe-set-box! rspace 1.0))
;; there seems to be a problem when copying (snip looks white but can be seen when highlighted)
(define/override (copy)
(new scroll-snip% [bit snip-bitmap]))
(define/override (draw dc x y left top right bottom dx dy draw-caret)
(send gl-context call-as-current
(lambda () (drawAFrame (makeGLTest screen-width screen-height)) gl-bitmap))
(super draw dc x y left top right bottom dx dy draw-caret)
)
(define/override (on-char dc x y editorx editory event)
(cond
[(equal? (send event get-key-code) 'up)
(define admin (get-admin))
(set! snip-width (+ snip-width 15))
(set! snip-height (+ snip-height 15))
(when admin
(send admin resized this #t))]
[(equal? (send event get-key-code) 'down)
(define admin (get-admin))
(set! snip-width (- snip-width 20))
(set! snip-height (- snip-height 20))
(when admin
(send admin resized this #t))]))
))
(new scroll-snip% [bit gl-bitmap])