sdl2 icon indicating copy to clipboard operation
sdl2 copied to clipboard

getRenderer missing from high level functions

Open Lokathor opened this issue 8 years ago • 3 comments

This seems like an easy one to be added in. It's in the raw functions already

getRenderer :: MonadIO m => Window -> m Renderer

However, those are the raw Window and Renderer types, Ptr (). Since SDL events usually give the Window in the event data, it'd obviously be nice to be able to get the Renderer for that Window and do things.

Edit: It should be as simple to do as this:

niceGetRenderer :: MonadIO m => SDL.Window -> m SDL.Renderer
niceGetRenderer (SDLInternal.Window w) = SDLInternal.Renderer <$> SDLRaw.getRenderer w

Lokathor avatar Dec 18 '16 20:12 Lokathor

The idea is that you use the Renderer given by createRenderer and thus shouldn't have a need to acquire a reference to it at run-time from the sdl2 library. Also, keep in mind that a Window may not have a Renderer if one was never created, your function needs to take that into consideration.

polarina avatar Dec 19 '16 18:12 polarina

That works if you have a single Window only. However, it's possible to have more than one Window in your program. In that case, you'd need to sort out which Renderer to use during your event loop if you get an event in a particular window. Without getRenderer you'd have to maintain a [(Window,Renderer)] or some such and then check through it, which is obviously silly when SDL already provides the correct function for this case.

Even in a single Window context, not having the possibility of the getRenderer function means you have to pass around your Renderer reference once you create it just in case one part of your event switch ever needs it, which is fairly silly.

Though, you do have a point about the null. The high level type should thus be a m (Maybe Renderer) result where a check has been made for null.

Lokathor avatar Dec 19 '16 19:12 Lokathor

I'd be ok with adding this function if it returned Maybe SDL.Renderer

ocharles avatar Dec 22 '16 17:12 ocharles