This is an enhanced version of the iGraphics library, now with support for multiple image formats and new features. Updates will be added incrementally based on requests. Original iGraphics by S. M. Shahriar Nirjon.
See the following files for example usage of the new features.
imageDemo.cppSpriteDemo.cppMouseDemo.cpp
-
The
iPassiveMouseMove(int mx, int my)function is called whenever the mouse moves on the window. Themxandmyparameters are the current x and y coordinates of the mouse cursor on the window. This function can be used to get the coordinate of the mouse cursor on the window without having to click the mouse. -
Define new Image structure to store image data. This structure is used to load the images in memory and display them on the screen without the need of reading from disk every time.
typedef struct{
unsigned char* data;
int width, height, channels;
} Image;- Load image from file. Supports multiple image formats (BMP, PNG, JPG, GIF) with the help of the stb_image library.
void iLoadImage(Image* img, const char filename[]);- Show image at position (x, y):
void iShowImage(int x, int y, Image* img);- Wrap an image around the window by
dxpixels. This function is useful for creating infinite scrolling backgrounds.:
void iWrapImage(Image* img, int dx);- Resize image:
void iResizeImage(Image* img, int width, int height);- Mirror image: (
MirrorState = HORIZONTAL or VERTICAL)
void iMirrorImage(Image* img, MirrorState state);- Free image:
void iFreeImage(Image* img);- Define new Sprite struct to store image, position and collision mask. The functionalities of the Sprite struct are similar to the Image struct with additional features like setting positions and collision detection.
typedef struct{
int x, y;
Image img;
unsigned char* collisionMask;
int ignoreColor;
} Sprite;- The main difference with loading with the
Spritestruct is that anignorecolorparameter is passed during loading. Set this to-1to read the whole image, or0xRRGGBBto ignore the colorRRGGBBwhile loading the image. The ignored part will be transparent. The collision mask is also generated during loading (iLoadSpriteinternally calls theiUpdateCollisionMaskfunction), so there is no need to set it manually unless you want to change it.
void iLoadSprite(Sprite* s, const char* filename, int ignoreColor);
void iSetSpritePosition(Sprite* s, int x, int y);
void iFreeSprite(Sprite* s);
void iShowSprite(Sprite* s);
void iWrapSprite(Sprite* s, int dx);
void iResizeSprite(Sprite* s, int width, int height);
void iMirrorSprite(Sprite* s, MirrorState state);- The collision detection is handled by the
int iCheckCollision(Sprite* s1, Sprite* s2)function. Returns1or0depending on whether the two sprites are colliding or not. If the bounding box of two images do not overlap, this has a time complexity ofO(1). Otherwise, it has a time complexity ofO(wh), wherewandhare the width and height of the overlapping area of the two images. Internally, whenever a sprite is created, it automatically creates a collision mask, which is a boolean 2D array of the same size as the image.
- Include the
iGraphics.hheader file in your project. - Change line 11 in runner.bat to the path of MinGW directory in your system.
- Run
runner.bat your_file_name.cppto compile and run your project. - Executable will be created in the
execdirectory.