function for getting window width and height with haskell gloss - haskell

Is there a function, similar to JS screen.width and screen.height in Haskell with the gloss graphic library, which returns the screen width and height?

There is the function is in Graphics.Gloss.Interface.Environment called getScreenSize it return a IO (Int,Int) the first value is width, the second is height.

I don't think gloss itself exports this capability, but you can use these calls from the GLFW package to determine the screen resolution:
import Graphics.UI.GLFW
main = do
initialize
desktopMode >>= print
putStrLn "all video modes:"
videoModes >>= mapM_ print
Note that gloss may be compiled to use either GLUT or GLFW. If gloss uses GLFW as its interface to Open GL it will call GLFW's initialize function when you create a window, and it's possible there is an issue with calling initialize twice in the same process but I kinda doubt it.
You can then use these dimensions to set the drawable area when creating a gloss window with the FullScreen constructor.

Related

Making a Map in Gloss

In PL class we were asked to make a pacman clone in Gloss, nevertheless I got stuck at making the map.
My approach was taking a .png of the classical pacman first level, and paste it in the render function so I don't need to draw everything by hand.
Nevertheless by doing so, the games lags horribly, I'm assuming that it's because the game renders the map in every step.
Is there some way to just render the map as a background a single time, or avoid the huge lag? Or am I taking a bad approach and it would it be better if I draw the components by hand using the Picture module?
I append the render function just in case I'm wiring it badly:
render :: PacmanGame -> IO Picture
render game = do
sprite <- fmap (head) (playerSprites $ player game)
let playerOne = uncurry translate (playerPos $ player game) $ sprite
map' <- pacmanMap
return $ pictures [map', playerOne]
Where pacmanMap :: IO Picture
It looks like you’re reloading the file in every call to render. You need to run the pacmanMap :: IO Picture action once, for example at startup in main; then you can just return the resulting static Picture from your render function. For example, you might store the reference to the current background image in the PacmanGame, or pass it as an additional argument to render.

PyQt: Obtain all pixels inside QPolygon

In PyQt 5, is there a way to obtain all pixel positions that would be modified by a call to QPainter.drawPolygon for a QPainter object constructed with some QImage as an argument without actually drawing the polygon? Ideally I would like to obtain separate sets of pixel positions for the polygon's border and for all pixels inside the polygon.
Just like #ekhumoro said, QPolygon is a subclass of QVector (that is, a QList). However, in Pyqt this is a Python array and not a QList. I got runtime errors when trying to iterate over this list, because it was inside the QPolygon object and there was no getter. In this case, in PyQt the solution is not very efficient. You need to iterate over each pixel of the image, creating a QPoint with pixel coordinates and checking if the QPolygon contains this point through the containsPoint method. There aren't many implementation details, but consider the following code snippet.
array_qpoints = [] # this array will have all the QPoints
polygon = QPolygon([
QPoint(140,234),
QPoint(126,362),
QPoint(282,409),
QPoint(307,273),
QPoint(307,233),
])
# let's consider a 640x480 image
for x in range(640):
for y in range(480):
point = QPoint(x, y)
if polygon.containsPoint(point, Qt.FillRule.OddEvenFill):
array_qpoints.append(point)
You can get the coordinates of each pixel by calling the x() and y() methods for each element in array_qpoints.
for point in array_qpoints:
x = point.x()
y = point.y()
# do what you want with the information
I'm posting this answer for others who visit this question and are looking for a solution by code. Since it's been several years, if you've found a better solution, please post :)

Haskell Gloss : Making zoom, pan, and all that kind of effects for module Graphics.Gloss.Interface.Pure.Game

Recently I have been working on making a game with Haskell, and we have been using the library Gloss in order to complete the graphic part. To make a game with Gloss, as you probably know, you have to use Graphics.Gloss.Interface.Pure.Game (or IO game, that's not the main thing to focus here). My doubt is simple. As they say on their description of this module : "Pressing ESC will still abort the program, but you don't get automatic pan and zoom controls like with displayInWindow.". I have been trying to build those two effects but with no sucess. I made an accumulator in my state, called v, that gets the value of 1 when 'reactEvent (EventKey (MouseButton WheelUp) Down _ _) ((px,py),p,mapas,i,players,b,c,d,v) = ((px,py),p,mapas,i,players,b,c,d,1)' and then the function that makes the picture appear would turn it back to 0 after applying the effect needed, creating a cicle. What's the mistake in my logic?
Edit : Added the prints, that's what I am trying to get to work. Also, if anyone has a clue on how to make different camera angles, I would also appreciate some help.
https://imgur.com/3PAqO2x
https://imgur.com/jk93lzQ

How to flip a sprite among X or Y axis in cocos2d-python

I´m starting with cocos2d for python and would like to flip a sprite among its x (or y) axis. From what I gather this should be possible with the underlying pyglet lib but I couldn´t figure out how. I tried it like this:
class Ninja(cocos.sprite.Sprite):
def __init__(self):
super(Ninja, self).__init__("Idle__000.png")
self.flip_x = True
I think there should be a flip() or transform() function somewhere, but couldn´t find anything going through cocos2d-python and pyglets sources.
How can I flip a sprite after instantiation?
Alternative approach: If I can´t flip a sprite programmatically, I´d
try to just swap out the picture with an already flipped version. How
would I do this then?
Hi if there is not flip method on Sprite try set property scale_x or scale_y to -1. Or make Ninja Sprite with scale parameter. There is list of parameters for sprite initialization.
http://python.cocos2d.org/doc/api/cocos.sprite.html?highlight=cocos.sprite.sprite#cocos.sprite.Sprite

Cannot get (->) r monad to work with SDL2 rendering

I have a small working SDL2 sample (a straightforward port of a C++ tutorial) and am trying to refactor the rendering code into using the (->) r monad. Here's the working part of the sample without the refactoring
renderClear renderer
renderCopy renderer texture nullPtr nullPtr
renderPresent renderer
Make & run - the texture is there. Both
renderClear renderer
flip ($) renderer $ do
copy texture nullPtr nullPtr
renderPresent renderer
and
renderClear renderer
renderCopy renderer texture nullPtr nullPtr
flip ($) renderer $ do
present
work fine too. Yet
renderClear renderer
flip ($) renderer $ do
copy texture nullPtr nullPtr
present
just draws the black window and that's it. present is an alias of renderPresent and copy is renderCopy with first argument shifted to the end. Do it have something to do with these functions working with IO? How can I fix this?
When you combine it into
flip ($) renderer $ do
copy texture nullPtr nullPtr
present
the whole do block is in the (->) r monad, and so the actions are not sequenced by IO or whatever underlying monad you have, instead using essentially
(f >> g) x = g x
leaving only present in the combination.
The way to fix this, I think, is to use a ReaderT monad transformer which knows how to use the underlying monad when sequencing, turning it into something like (you'll need to redefine copy and present appropriately)
flip runReaderT renderer $ do
copy texture nullPtr nullPtr
present

Resources