Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows.forms and redrawing bitmaps

Tags:

forms

graphics

f#

I'm implementing a gameboy emulator as so many before me.

I'm trying to implement the PPU and to do this I'm using a class representing the screen.

// needed because VS can't find it as dependency
#r "nuget: System.Windows.Forms"
open System
open System.Windows.Forms
open System.Drawing

type Screen(title, width : int, height : int) as screen =
    inherit Form()
    
    let mutable canvas = new Bitmap(width, height)

    do
        // set some attributes of the screen
        screen.Size <- new Size(width, height)
        screen.Text <- title

    interface IDisposable with
        member S.Dispose() = (canvas :> IDisposable).Dispose()

    override S.OnPaint e =
        e.Graphics.DrawImage(canvas,0,0) // here
        base.OnPaint(e)

    member S.Item
        with get (w, h) = canvas.GetPixel(w,h)
        and set (w,h) pixel = canvas.SetPixel(w,h,pixel)

But I can't get it to update the screen after I have redrawing the bitmap, it does not show the redrawn image.

the redraw

let main () =
    let screen = new Screen("gameboy",800,600)
    Application.Run(screen)
    // test example
    for i in 0 .. 300 do
        screen.[i,i] <- Drawing.Color.Black
    (screen :> Form).Refresh()

i.e. How do I make it to redraw after update of the bitmap?

like image 355
kam Avatar asked Jul 01 '26 11:07

kam


1 Answers

You can't do anything graphical after Application.Run has been called, because it doesn't finish until the user closes the main form. Instead, you can create an event handler that is called once the main form is loaded, like this:

let main argv =
    let screen = new Screen("gameboy",800,600)
    screen.Load.Add(fun _ ->
        for i in 0 .. 300 do
            screen.[i,i] <- Drawing.Color.Black)
    Application.Run(screen)
    0
like image 53
Brian Berns Avatar answered Jul 03 '26 11:07

Brian Berns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!