A simple 2D graphics library.
Project description
SimplGraphics
A Simple 2D Graphics Library accelerated by OpenGL.
Installation
pip install SimplGraphics
Basic Usage
First, the window must be created, this is done by using the init function with the signature (title: str, width: int, height: int, vsync: bool=5, msaa: int=0).
To handle the running loop, the update function should be used. It takes no parameters and will return a bool depending on whether it should be running or not. It is recommended to have the main logic loop in the following format while SimplGraphics.update() and not externalShouldClose:.
Finally, once the application has ended and should be closed, the exit function should be used. It takes no arguments.
Drawing Shapes
The drawing mechanism in SimplGraphics is buffer based and whenever a draw function is called, it will add necessary values to a buffer and all items in the buffer will be rendered when the renderScreen function is called.
The screen will be automatically cleared every time renderScreen is called with a clearColour value. This value can be changed with the setClearColour function which just takes in the colour value to set the clear colour to.
All functions taking in a colour value with use the proprietary Colour object. This can be created using the rgbToColour and hexToColour functions.
There are several shape drawing function in SimplGraphics. Below is a table of all of them.
| Function Name | Function Signature | Description |
|---|---|---|
| drawRectangleTL | def drawRectangleTL(pos: tuple[int, int], size: tuple[int, int], col: Colour, borderRadius: int=0): | Draw a rectangle with pos being the coordinate of the top-left corner |
| drawRectangle | def drawRectangle(pos: tuple[int, int], size: tuple[int, int], col: Colour, borderRadius: int=0, rot: float=0.0): | Draw a rectangle with pos being the coordinate of the centre |
| drawCircle | def drawCircle(pos: tuple[int, int], radius: float, col: Colour): | Draw a circle with pos being the coordinate of the centre |
Drawing Images and Text
In order to draw images, first you need to load it using the loadImage which will take the filepath of the image, load and and create an OpenGL texture with it. The function will return the id of the image, you will need it to draw the image.
To draw the image once it is loaded, there are convenient drawImage and drawImageTL function which define a rectangle shape the same as drawRectangle and drawRectangleTL with a near-identical signature but instead of giving a colour, you will give the image id from the loaded image.
In order to draw text, it first needs to be laoded, to do that there is a loadFont function which will take in either a standard font name such as "arial" and "calibri" or the filepath to a .ttf or .ttc file. The function will also need the size of the font to be loaded in pixels. This will then add the font and the size to a unique key in a font atlas.
To draw the text, there are the drawText and drawTextTL functions where you specify the position, colour and optionally rotation like when drawing a rectangle but instead of giving a cornerRadius or a size, you will give the string used to load the font, the size used to load the font and the string of the text to be drawn. the signatures are as following: def drawText(pos: tuple[int, int], font: str, size: int, text: str, col: Colour, rot:float=0.0): and def drawTextTL(pos: tuple[int, int], font: str, size: int, text: str, col: Colour):
Input
Most of the input is build directly on top of the input from glfw, the window library used by SimplGraphics, but there are a few slight differences. The key and mouse button constants are the same but they will instead be given to the bool returning function keyPressed, keyDown, keyReleased, mouseButtonPressed, mouseButtonDown and mouseButtonReleased with pressed being the change from the key/button transitioning from not pressed to pressed and released the opposite.
The function getMousePos will return a tuple of the mouse position and the function getMouseInWindow will return a bool depending on whether the cursor is currently over the window or not.
The function getScrollChangeX and getScrollChangeY will return the change in mouse scroll since the last time update was called.
getTime will return of float of the time since init was called in seconds.
Finally, the clipboard functions getClipboard and setClipboard will get the string value of the current clipboard and set the string value of the clipboard.
Window Control
There are a few function to have some control over the window. They are stated below.
setWindowIcon requried the user to specify the path of an icon but will then set the icon of the window to the image.
setCursorLock will set wether the cursor is locked in the screen or not. It is useful for creating first-person games.
setCursorShow will set wether the cursor is visible or not. Useful if you have a custom non-conventional cursor as you can hide the default one and draw a custom cursor where the cursor would be.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simplgraphics-1.0.0-py3-none-any.whl.
File metadata
- Download URL: simplgraphics-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b1b6b7cfc5e7f27b7607bae51a93927e8b3cfa2fed9716d33fee723e30e5911
|
|
| MD5 |
ef420947fc673d08c5e8a7bc5f655e88
|
|
| BLAKE2b-256 |
133df4b04e93536be5794a079cce51822fa70b4cf52f650e04cf1aa922a6e129
|