Skip to content
 
 

Repository files navigation

Modern iGraphics Library

🎮 A Comprehensive C++ Graphics Library for Beginners

GitHub repo GitHub stars GitHub forks Last Commit Latest Release

iGraphics Banner

iGraphics.h header file contains some drawing functions that can be used to draw basic graphical shapes in C++. These functions are implemented in OpenGL. Users of iGraphics do not need any knowledge of OpenGL to use it. Simply calling the drawing functions a user can draw any 2D shape on screen. This library also provides easy ways for animation, keyboard and mouse event handling.

It was originally created by Shahriar Nirjon on 2009 with limited functionalities and only for Windows. This is an extended version of the original iGraphics library with support for multiple image formats, custom fonts, sound engine, sprite management, collision detection and advanced mouse-keyboard control. The library is now cross-platform and works on both Windows and Linux. Updates will be added incrementally based on requests.

🧱 Original iGraphics vs. ⚙️ Modern iGraphics

Feature Original iGraphics (2009) Modern iGraphics (2025)
Cross-Platform Support ❌ Windows only ✅ Windows & Linux
Image Formats ❌ BMP only ✅ Multiple formats
Audio Formats ❌ WAV only ✅ Multiple formats
Font Support ❌ Limited bitmap fonts ✅ Custom TTF fonts
Sound Integration ❌ Single Channel ✅ Multi-Channel
Input Handling ❌ Basic ✅ Enhanced controls
Transparency Support ❌ Not available ✅ Full RGBA color support
Image Manipulation ❌ No transformations ✅ Rotate/Scale/Flip/Wrap
Image rendering ❌ Slow ✅ Fast (Texture-based + Caching)
Sprite Management ❌ Manual implementation ✅ Built-in sprite system
Collision Detection ❌ Not available ✅ Pixel-perfect collision

🎥 Example Games

You can find executable games here


Necessary Files

  • Download the Modern-iGraphics-1.0.0.zip file from here and extract it.
  • Download MINGW.zip file from here and extract it.
  • Copy the MINGW folder to the extracted Modern-iGraphics-1.0.0 folder.
  • The final folder structure should look like this:
Modern-iGraphics-1.0.0
├── MINGW
│   ├── bin
│   ├── include
│   ├── lib
│   ├── ....
│   └── share
├── OpenGL
├── assets
├── bin
├── examples
├── iGraphics.h
├── iGraphics.cbp
├── iMain.cpp
├── ....

🧱 Setup in Code::Blocks

Change the compiler path of Code::Blocks as following: SettingsCompiler → Go to Toolchain executables tab → Change the Compiler's installation directory to the MINGW directory in the iGraphics folder. You can do that by clicking the three dots (...) on right. After you change the compiler, clear the .o files inside obj folder (If there is any).

Open iGraphics.cbp in Code::Blocks. The project is already configured with all the necessary settings. You can directly run the project. By default, the main file is iMain.cpp. You can remove it and add a different file if you want.

You can find the slides with step-by-step screenshots here.


⚙️ Setup in Terminal

Download the Library: Clone or download the iGraphics library from the repository.

git clone https://github.com/mahirlabibdihan/Modern-iGraphics
cd Modern-iGraphics

Alternatively, you can download the ZIP file from here and extract it.

Running the Example: Ensure that g++ is installed on your system and available in your PATH. Then, run the following command to compile and execute the example program:

  • Windows
.\runner.bat examples\BallDemo.cpp
  • Linux
sudo apt install libglu1-mesa-dev freeglut3-dev mesa-common-dev
sudo apt install libsdl2-dev libsdl2-mixer-dev
sudo apt install libfreetype6-dev
./runner.sh examples/BallDemo.cpp
  • MSYS2-MINGW64
pacman -S mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_mixer
pacman -S mingw-w64-x86_64-freeglut mingw-w64-x86_64-gcc
pacman -S mingw-w64-x86_64-freetype
./runner.sh examples/BallDemo.cpp

🚀 Quick Start

🛠️ Project Creation Tools

For quick project setup, use the provided project creation scripts:

Windows:

.\create_project.bat MyGameName

Linux/Unix:

./create_project.sh MyGameName

These scripts will automatically generate a complete starter template with:

  • ✅ All necessary iGraphics function stubs
  • ✅ Example drawing code (text + red circle)
  • ✅ Mouse and keyboard handlers
  • ✅ Animation timer setup (commented)
  • ✅ Comprehensive comments and documentation

📦 Release Guideline (Windows)

  • Organize Your Assets

    • Ensure all game assets (images, sprites, sounds, level designs, etc.) are inside the assets folder.
    • Remove any unnecessary or default assets to save space.
  • Save Files

    • Place any saved game data in the saves folder.
  • Run the Release Script

    • Double-click release.bat or run it from the terminal using: .\release.bat iMain.cpp
  • Check the Output

    • After execution, a release folder will be created. Navigate to: release → windows → x86
    • Inside, you’ll find the necessary files to run your game. Please test game.exe to ensure your game runs properly.

👨‍💻 Description of iMain.cpp

Users of iGraphics only have to edit, compile and run iMain.cpp. See the listing of iMain.cpp below:

#include "iGraphics.h"

/*
function iDraw() is called again and again by the system.
*/
void iDraw()
{
    //place your drawing codes here
    iClear();
}

/*
function iMouseClick() is called when the user presses/releases the mouse.
(mx, my) is the position where the mouse pointer is.
*/
void iMouseClick(int button, int state, int mx, int my)
{
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        //place your codes here
    }
    if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    {
        //place your codes here
    }
}

/*
function iMouseMove() is called when the user moves the mouse.
(mx, my) is the position where the mouse pointer is.
*/
void iMouseMove(int mx, int my)
{
    //place your codes here
}

/*
function iKeyPress() is called whenever the user hits a key in keyboard.
key- holds the ASCII value of the key pressed.
*/
void iKeyPress(unsigned char key)
{
    switch (key)
    {
    case 'q':
        // do something with 'q'
        iExitMainLoop();
        break;
    // place your codes for other keys here
    default:
        break;
    }
}

/*
function iSpecialKeyPress() is called whenver user hits special keys likefunction
keys, home, end, pg up, pg down, arraows etc. you have to use
appropriate constants to detect them. A list is:
GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11,
GLUT_KEY_F12, GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN,
GLUT_KEY_PAGE_UP, GLUT_KEY_PAGE_DOWN, GLUT_KEY_HOME, GLUT_KEY_END,
GLUT_KEY_INSERT */
void iSpecialKeyPress(unsigned char key)
{
    switch (key)
    {
    case GLUT_KEY_END:
        // do something
        break;
    // place your codes for other keys here
    default:
        break;
    }
}

int main(int argc, char *argv[])
{
    // Initialization code before opening the window
    iWindowedMode(400, 400, "iGraphics");
    iStartMainLoop();
    // Execution will continue from here once iExitMainLoop() is called or window is closed.
    return 0;
}

🛠️ Functions in iGraphics.h

🖼️ Graphics Functions

void iWindowedMode(int width=500, int height=500, char* title="iGraphics")

  • Description: Creates a window of specified size and title.
  • Parameters:
    • width: Width of the window.
    • height: Height of the window.
    • title: Title of the window.
  • Example:
    iWindowedMode(300, 300, "iGraphics");

void iGameMode(const char *gameModeStr = W800_X_H600)

  • Description: Sets the game mode for fullscreen display.
  • Parameters:
    • gameModeStr: String representing the game mode.
      • Available modes include:
        • W800_X_H600: 800x600 resolution.
        • W1024_X_H768: 1024x768 resolution.
        • W1280_X_H720: 1280x720 resolution.
        • W1920_X_H1080: 1920x1080 resolution.
    • Example:
    iGameMode(W1280_X_H720);

void iStartMainLoop()

  • Description: Starts the main loop of the application.
  • Parameters: None
  • Note: This function contains an infinite loop that keeps the application running until iExitMainLoop() is called.
  • Example:
    iStartMainLoop();

void iExitMainLoop()

  • Description: Exits the main loop and return from iStartMainLoop().
  • Parameters: None
  • Example:
    void iKeyPress(unsigned char key)
    {
        switch (key)
        {
        case 'q':
            iExitMainLoop();
            break;
        default:
            break;
        }
    }

void iClear()

  • Description: Clears the screen.
  • Parameters: None
  • Example:
    iClear();

void iSetColor(int r, int g, int b)

  • Description: Sets current drawing color.
  • Parameters:
    • r: Red component of color (0-255).
    • g: Green component of color (0-255).
    • b: Blue component of color (0-255).
  • Example:
    iSetColor(255, 0, 0); // Red

void iSetTransparentColor(int r, int g, int b, double a)

  • Description: Sets current drawing color with transparency.
  • Parameters:
    • r, g, b: RGB values (0-255).
    • a: Alpha value (0.0 to 1.0).
  • Example:
      iSetTransparentColor(255, 0, 0, 0.5); // Semi-transparent red

void iGetPixelColor(int x, int y, int rgb[])

  • Description: Gets pixel color at coordinate (x, y).
  • Parameters:
    • x, y: Coordinates of the pixel.
    • rgb[]: Array to store RGB values.
  • Example:
    iGetPixelColor(100, 120, array);

void iPoint(double x, double y, int size=0)

  • Description: Draws a point at (x, y) using current color.
  • Parameters:
    • x, y: Coordinates.
    • size: Optional size.
  • Example:
    iPoint(10, 20);

void iLine(double x1, double y1, double x2, double y2)

  • Description: Draws a line between two points.
  • Parameters:
    • x1, y1: One end.
    • x2, y2: Other end.
  • Example:
    iLine(10, 20, 100, 120);

void iCircle(double x, double y, double r, int slices=100)

  • Description: Draws a circle.
  • Parameters:
    • x, y: Center.
    • r: Radius.
    • slices: Segments to draw.
  • Example:
    iCircle(10, 20, 10);

void iFilledCircle(double x, double y, double r, int slices=100)

  • Description: Draws a filled circle.
  • Parameters:
    • Same as iCircle.
  • Example:
    iFilledCircle(10, 20, 10);

void iEllipse(double x, double y, double a, double b, int slices=100)

  • Description: Draws an ellipse.
  • Parameters:
    • x, y: Center.
    • a, b: Axes lengths.
    • slices: Segments to draw.
  • Example:
    iEllipse(10, 20, 10, 5);

void iFilledEllipse(double x, double y, double a, double b, int slices=100)

  • Description: Draws a filled ellipse.
  • Parameters: Same as iEllipse.
  • Example:
    iFilledEllipse(10, 20, 10, 5);

void iRectangle(double left, double bottom, double dx, double dy)

  • Description: Draws a rectangle.
  • Parameters:
    • left: x-coordinate of bottom-left.
    • bottom: y-coordinate of bottom-left.
    • dx: Width.
    • dy: Height.
  • Example:
    iRectangle(10, 20, 10, 5);

void iFilledRectangle(double left, double bottom, double dx, double dy)

  • Description: Draws a filled-rectangle on the screen with current color.
  • Parameters: Same as iRectangle.
  • Example:
    iFilledRectangle(10, 20, 10, 5);

void iPolygon(double x[], double y[], int n)

  • Description: Draws a polygon on the screen with current color.
  • Parameters:
    • x, y: Arrays of coordinates of vertices.
    • n: Number of vertices.
  • Example:
    double xa[] = {0, 10, 5};
    double ya[] = {0, 0, 10};
    iPolygon(xa, ya, 3);

void iFilledPolygon(double x[], double y[], int n)

  • Description: Draws a filled-polygon on the screen with current color.
  • Parameters: Same as iPolygon.
  • Example:
    double xa[] = {0, 10, 5};
    double ya[] = {0, 0, 10};
    iFilledPolygon(xa, ya, 3);

void iSetLineWidth(float width)

  • Description: Sets the width of lines to be drawn.
  • Parameters: width - Line width in pixels.
  • Example:
    iSetLineWidth(3.0); // Set line width to 3 pixels
    iLine(0, 0, 100, 100); // Draw a thick line

void iShowSpeed(double x, double y)

  • Description: Displays the current FPS (Frames Per Second) at specified coordinates.
  • Parameters:
    • x, y: Coordinates where FPS will be displayed.
  • Example:
    iShowSpeed(10, 10); // Show FPS counter at top-left corner

void iText(double x, double y, char *str, void* font=GLUT_BITMAP_8_BY_13)

  • Description: Displays a string on screen.
  • Parameters:
    • x, y: Coordinates of the first character.
    • str: The text to display.
    • font: (Optional) Font type. Available fonts include:
      • GLUT_BITMAP_8_BY_13
      • GLUT_BITMAP_9_BY_15
      • GLUT_BITMAP_TIMES_ROMAN_10
      • GLUT_BITMAP_TIMES_ROMAN_24
      • GLUT_BITMAP_HELVETICA_10
      • GLUT_BITMAP_HELVETICA_12
      • GLUT_BITMAP_HELVETICA_18
  • Example: iText(50, 60, "This is a text", GLUT_BITMAP_TIMES_ROMAN_10);

void iTextBold(double x, double y, char *str, void* font=GLUT_BITMAP_8_BY_13)

  • Description: Displays a bold string on screen.

void iTextAdvanced(double x, double y, const char *str, float scale = 0.3, float weight = 1.0, void *font = GLUT_STROKE_ROMAN)

  • Description: Displays a string on screen with specified scale and weight.
  • Parameters:
    • x, y: Coordinates of the first character.
    • str: The text to display.
    • scale: Scale factor for the text.
    • weight: Weight of the text (1.0 for normal, 2.0 for bold).
    • font: Font type (default is GLUT_STROKE_ROMAN).
  • Example: iTextAdvanced(50, 60, "This is a text", 0.5, 2.0);

void iRotate(double x, double y, double degree)

  • Description: Rotates the coordinate system around a point.
  • Parameters:
    • x, y: Coordinates of the point to rotate around.
    • degree: Angle in degrees to rotate.
  • Example:
    iRotate(100, 100, 45); // Rotate around point (100, 100) by 45 degrees
    iShowImage(50, 50, "image.png"); // This image will be rotated
    iUnRotate();

void iScale(double x, double y, double scaleX, double scaleY)

  • Description: Scales the coordinate system around a point.
  • Parameters:
    • x, y: Coordinates of the point to scale around.
    • scaleX: Scaling factor in the x-direction.
    • scaleY: Scaling factor in the y-direction.
    • Example:
    iScale(0,0, 2.0, 1.5); // Scale around the origin (0, 0) by 2.0 in x and 1.5 in y
    // This will affect all subsequent drawing operations
    iUnScale();

⏱️ Animation and Timer

int iSetTimer(int msec, void (*f)(void))

  • Description: Repeatedly executes a function at specified time intervals.

  • Parameters:

    • msec: Time interval in milliseconds.
    • f: Function to be executed.
  • Returns: Timer index.

  • Example:

    void func() {
        //code of the task that will be repeated.
    }
    
    int main(int argc, char *argv[])
    {
        ...
        int t = iSetTimer(100, func); // //call it inside main() before iWindowedMode();
        ...
        iWindowedMode(400, 400, "iGraphics");
    }

void iPauseTimer(int index)

  • Description: Pauses the timer.
  • Parameters: index of the timer.
  • Example: iPauseTimer(t);

void iResumeTimer(int index)

  • Description: Resumes the timer.
  • Parameters: index of the timer.
  • Example: iResumeTimer(t);

void iDelay(int sec)

  • Description: Pauses execution for a given duration.
  • Parameters: sec in seconds.
  • Example:
    iDelay(5); // Pauses for 5 seconds

🖱️ Mouse Functions

void iMouseClick(int button, int state, int mx, int my)

  • Description: Called when a mouse button is pressed or released.
  • Parameters:
    • button: Button pressed (GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, GLUT_MIDDLE_BUTTON).
    • state: State of the button (GLUT_DOWN or GLUT_UP).
    • mx, my: Coordinates of the mouse pointer.
  • Note: This function should be defined in the main file.

void iMouseMove(int mx, int my)

  • Description: Called when the mouse moves.
  • Parameters: mx, my: Coordinates of the mouse pointer.
  • Note: This function should be defined in the main file.

void iMouseDrag(int mx, int my)

  • Description: Called when the mouse is dragged.
  • Parameters: mx, my: Coordinates of the mouse pointer.
  • Note: This function should be defined in the main file.

void iMouseWheel(int dir, int mx, int my)

  • Description: Called when the mouse wheel is scrolled.
  • Parameters:
    • dir: Direction of scroll (1 for up, -1 for down).
    • mx, my: Coordinates of the mouse pointer.
  • Note: This function should be defined in the main file.

void iShowCursor()

  • Description: Shows the mouse cursor.
  • Parameters: None
  • Example:
    iShowCursor();

void iHideCursor()

  • Description: Hides the mouse cursor.
  • Parameters: None
  • Example:
    iHideCursor();

⌨️ Keyboard Functions

void iKeyPress(unsigned char key)

  • Description: Called when a key is pressed.
  • Parameters:
    • key: ASCII value of the key pressed.
  • Note: This function should be defined in the main file.

void iSpecialKeyPress(unsigned char key)

  • Description: Called when a special key is pressed.
  • Parameters:
    • key: Special key value (e.g., GLUT_KEY_LEFT, GLUT_KEY_RIGHT, etc.).
  • Note: This function should be defined in the main file.

void iKeyRelease(unsigned char key)

  • Description: Called when a key is released.
  • Parameters:
  • key: ASCII value of the key released.
  • Note: This function should be defined in the main file.

void iSpecialKeyRelease(unsigned char key)

  • Description: Called when a special key is released.
  • Parameters:
  • key: Special key value (e.g., GLUT_KEY_LEFT, GLUT_KEY_RIGHT, etc.).
  • Note: This function should be defined in the main file.

int isKeyPressed(unsigned char key)

  • Description: Checks if a key is being pressed (Not yet released).
  • Parameters: key to check.
  • Returns: 1 if pressed, 0 otherwise.
  • Example:
    if (isKeyPressed('a')) {
        // 'a' key is pressed
    }

int isSpecialKeyPressed(unsigned char key)

  • Description: Checks if a special key is being pressed (Not yet released).
  • Parameters: key to check.
  • Returns: 1 if pressed, 0 otherwise.
  • Example:
    if (isSpecialKeyPressed(GLUT_KEY_LEFT)) {
        // Left arrow key is pressed
    }

🔉 Sound Functions

iGraphics was originally designed for graphical applications, but it has been extended to support sound playback using the SDL2 library. The sound functions are available in iSound.h and are shown below:

int iPlaySound(const char *filename, int loop = 0, int volume = 100)

  • Description: Plays a sound from file with optional looping and volume control.

  • Parameters:

    • filename: Path to the sound file.
    • loop: true for continuous play, false for one-time play.
    • volume: Volume level (0-100).
  • Returns: Channel where the sound is played (-1 if failed).

  • Note: You can't play more than 8 sounds simultaneously.

  • Supported Formats: WAV, MP3, OGG, FLAC, and more.

  • Example:

    #include "iSound.h" // Include the sound header
    ...
    int channel = iPlaySound("background.wav", true, 80);

void iPauseSound(int channel)

  • Description: Pauses the sound specified by channel.
  • Parameters: channel of the sound.
  • Example:
    iPauseSound(channel);

void iResumeSound(int channel)

  • Description: Resumes the sound specified by channel.
  • Parameters: channel of the sound.
  • Example:
    iResumeSound(channel);

void iStopSound(int channel)

  • Description: Stops the sound specified by channel. Frees the channel for other sounds.
  • Parameters: channel of the sound.
  • Example:
    iStopSound(channel);

void iStopAllSounds()

  • Description: Stops all currently playing sounds. Frees all channels.

void iSetVolume(int channel, int volume)

  • Description: Sets the volume for a specific sound.
  • Parameters:
    • index: Index of the sound.
    • volume: Volume level (0-100).

void iIncreaseVolume(int channel, int amount)

  • Description: Increases the volume of a specific sound by a specified amount.
  • Parameters:
    • index: Index of the sound.
    • amount: Amount to increase the volume by (0-100).

void iDecreaseVolume(int channel, int amount)

  • Description: Decreases the volume of a specific sound by a specified amount.
  • Parameters:
    • index: Index of the sound.
    • amount: Amount to decrease the volume by (0-100).

🅰️ Text Functions

Custom font rendering is supported using TrueType fonts. freetype library is used to render text. The new text functions are available in iFont.h and are shown below:

void iShowText(double x, double y, const char *text, const char *fontPath, int fontSize = 48)

  • Description: Displays a string on screen using TrueType font.

  • Parameters:

    • x, y: Coordinates of the first character.
    • text: The text to display.
    • fontPath: Path to the TrueType font file (e.g., "assets/fonts/arial.ttf").
    • fontSize: Size of the font (default is 48).
  • Example:

    #include "iFont.h" // Include the font header
    ...
    iShowText(50, 60, "This is a text", "assets/fonts/arial.ttf", 48);

🖼️ Image Functions

void iShowImage(int x, int y, const char *filename)

  • Description: Displays an image at specified coordinates.

  • Parameters:

    • x, y: Coordinates where the image will be displayed.
    • filename: Path to the image file.
  • Example:

    iShowImage(100, 200, "image.png");

int iLoadImage(Image* img, const char filename[])

  • Description: Loads an image from file. Supports multiple image formats (BMP, PNG, JPG, GIF) with the help of the stb_image library.

  • Parameters:

    • img: Pointer to an Image structure.
    • filename: Path to the image file.
  • Returns: true if successful, false otherwise.

  • Example:

    Image img;
    if (iLoadImage(&img, "image.png")) {
        // Image loaded successfully
    } else {
        // Failed to load image
    }
  • Image Structure

    typedef struct
    {
        unsigned char *data;
        int width, height, channels;
        GLuint textureID; // OpenGL texture ID
    } Image;

void iShowLoadedImage(int x, int y, Image* img)

  • Description: Displays an already loaded image at specified coordinates. Image should be loaded using iLoadImage.

  • Parameters:

    • x, y: Coordinates where the image will be displayed.
    • img: Pointer to the loaded Image structure.
  • Example:

    Image img;
    iLoadImage(&img, "image.png");
    iShowLoadedImage(100, 200, &img);

void iScaleImage(Image* img, double scale)

  • Description: Scales the image by a specified factor.

  • Parameters:

    • img: Pointer to the loaded Image structure.
    • scale: Scaling factor (e.g., 2.0 for double size).
  • Example: iScaleImage(&img, 2.0);

void iResizeImage(Image* img, int width, int height)

  • Description: Resizes the image to specified dimensions.

  • Parameters:

    • img: Pointer to the loaded Image structure.
    • width: New width of the image.
    • height: New height of the image.
  • Example:

    iResizeImage(&img, 200, 100); // Resize to 200x100 pixels

void iMirrorImage(Image* img, MirrorState state)

  • Description: Mirrors the image either horizontally or vertically.

  • Parameters:

    • img: Pointer to the loaded Image structure.
    • state: HORIZONTAL or VERTICAL. Here, MirrorState is an enum.
  • Example:

    iMirrorImage(&img, HORIZONTAL); // Mirror horizontally

void iWrapImage(Image* img, int dx = 0, int dy = 0)

  • Description: Wraps the image around the screen by dx pixels horizontally and dy pixels vertically. This function is useful for creating infinite scrolling backgrounds.

  • Parameters:

    • img: Pointer to the loaded Image structure.
    • dx: Horizontal shift in pixels (default is 0).
      • A positive value of dx shifts the image to the right.
      • A negative value of dx shifts the image to the left.
    • dy: Vertical shift in pixels (default is 0).
      • A positive value of dy shifts the image down.
      • A negative value of dy shifts the image up.
  • Example:

    iWrapImage(&img, 50); // Wrap the image by 50 pixels to the right
    iWrapImage(&img, -50); // Wrap the image by 50 pixels to the left
    iWrapImage(&img, 0, 30); // Wrap the image by 30 pixels down
    iWrapImage(&img, 0, -30); // Wrap the image by 30 pixels up

void iIgnorePixels(Image* img, int ignoreColor)

  • Description: Makes specific colored pixels transparent in the image.
  • Parameters:
    • img: Pointer to the loaded Image structure.
    • ignoreColor: Color to ignore in 0xRRGGBB format (e.g., 0xFF0000 for red).
  • Example:
    iIgnorePixels(&img, 0xFFFFFF); // Make white pixels transparent

void iFreeImage(Image* img)

  • Description: Frees the memory allocated for the image.

🧩 Sprite Functions

Free sprite resources: https://craftpix.net/freebies/\ Online sprite cutter: https://ezgif.com/sprite-cutter

Structure Definitions

FrameSet Structure

typedef struct
{
    Image *frames;
    int count;
} FrameSet;

Sprite Structure

typedef struct
{
    int x, y;
    FrameSet frameSet;
    int currentFrame;
    ....
} Sprite;

int iLoadFramesFromFolder(FrameSet *frames, const char *folderPath)

  • Description: Loads frames from a folder containing multiple images.

  • Parameters:

    • frames: Pointer to a FrameSet structure.
    • folderPath: Path to the folder containing images.
  • Returns: Number of frames loaded. -1 if failed to load any images.

  • Example:

    FrameSet frames;
    iLoadFramesFromFolder(&frames, "sprites/"); // Load images from "sprites/" folder

int iLoadFramesFromSheet(FrameSet *frames, const char *filename, int rows, int cols)

  • Description: Loads frames from a sprite sheet.

  • Parameters:

    • frames: Pointer to a FrameSet structure.
    • filename: Path to the sprite sheet image.
    • rows: Number of rows in the sprite sheet.
    • cols: Number of columns in the sprite sheet.
  • Returns: Number of frames loaded. -1 if failed to load the sprite sheet.

  • Example:

    FrameSet frames;
    iLoadFramesFromSheet(&frames, "spritesheet.png", 4, 4); // Load images frames a sprite sheet with 4 rows and 4 columns

void iChangeSpriteFrames(Sprite *s, const FrameSet* frames)

  • Description: Changes the frames of a sprite.

  • Parameters:

    • s: Pointer to a Sprite structure.
    • frames: Pointer to a FrameSet structure representing the new frames.
  • Example:

    FrameSet frames;
    iLoadFramesFromFolder(&frames, "sprites/"); // Load images from a folder
    Sprite s;
    iChangeSpriteFrames(&s, &frames);

void iSetSpritePosition(Sprite* s, int x, int y)

  • Description: Sets the position of the sprite.
  • Parameters:
    • s: Pointer to a Sprite structure.
    • x, y: New coordinates for the sprite.
  • Example:
    iSetSpritePosition(&s, 100, 200); // Set sprite position to (100, 200)

void iShowSprite(Sprite* s)

  • Description: Displays the sprite on the screen.
  • Parameters:
    • s: Pointer to a Sprite structure.

void iAnimateSprite(Sprite* s)

  • Description: Animates the sprite by cycling through its frames.
  • Parameters:
    • s: Pointer to a Sprite structure.
  • Example:
    iAnimateSprite(&s); // Animate the sprite

void iScaleSprite(Sprite* s, double scale)

  • Description: Scales the sprite by a specified factor.
  • Parameters:
    • s: Pointer to a Sprite structure.
    • scale: Scaling factor (e.g., 2.0 for double size).

void iResizeSprite(Sprite* s, int width, int height)

  • Description: Resizes the sprite to specified dimensions.
  • Parameters:
    • s: Pointer to a Sprite structure.
    • width: New width of the sprite.
    • height: New height of the sprite.

void iMirrorSprite(Sprite* s, MirrorState state)

  • Description: Mirrors the sprite either horizontally or vertically.
  • Parameters:
    • s: Pointer to a Sprite structure.
    • state: HORIZONTAL or VERTICAL.

void iRotateSprite(Sprite* s, double x, double y, double degree)

  • Description: Rotates the sprite around a point (x, y) by a specified angle in degrees.
  • Parameters:
    • s: Pointer to a Sprite structure.
    • x, y: Coordinates of the point to rotate around.
    • degree: Angle in degrees to rotate.
  • Example:
    iRotateSprite(&s, 100, 100, 45); // Rotate sprite around point (100, 100) by 45 degrees

void iFreeSprite(Sprite* s)

  • Description: Frees the memory allocated for the sprite.
  • Parameters:
    • s: Pointer to a Sprite structure.

int iGetVisiblePixelsCount(Sprite* s1)

  • Description: Counts the number of visible pixels in a sprite. This is useful for collision detection.
  • Parameters:
    • s1: Pointer to the Sprite structure.
    • Returns: Number of visible pixels in the sprite.
  • Example:
    Sprite s1;
    ...
    int visiblePixels = iGetVisiblePixelsCount(&s1);

FrameSet iCreateFrameSet(Image *frames, int count)

  • Description: Creates a FrameSet from an array of images.

  • Parameters:

    • frames: Pointer to an array of Image structures.
    • count: Number of frames in the array.
    • Returns: A FrameSet structure containing the frames.
  • Example:

    Image frames[4];
    ...
    FrameSet frameSet = iCreateFrameSet(frames, 4); // Create a FrameSet from the array of images
    Image singleImg;
    ...
    FrameSet frameSet = iCreateFrameSet(&singleImg, 1); // Create a FrameSet from a single image

<!-- Collision Detection -->

### 🛡️ Collision Detection

#### `int iCheckImageCollision(int x1, int y1, Image *img1, int x2, int y2, Image *img2)`

- **Description:** Checks for pixel-level collision between two images.
- **Parameters:**
  - `x1`, `y1`: Coordinates of the first image.
  - `img1`: Pointer to the first `Image` structure.
  - `x2`, `y2`: Coordinates of the second image.
  - `img2`: Pointer to the second `Image` structure.
- **Returns:** Number of overlapping pixels. `>= 1` if collision is detected, `0` otherwise.
- **Note:** This function doesn't consider image rotation (Using `iRotate` and `iUnRotate`). Use `iCheckSpriteCollision` for such cases.
- **Example:**

  ```cpp
  Image img1, img2;
  iLoadImage(&img1, "image1.png");
  iLoadImage(&img2, "image2.png");

  ....
  // Check for collision between img1 at (100, 200) and img2 at (150, 250)
  if (iCheckImageCollision(100, 200, &img1, 150, 250, &img2)) {
      // Collision detected
  }

int iCheckSpriteCollision(Sprite* s1, Sprite* s2)

  • Description: Checks for pixel-level collision between two sprites.

  • Parameters:

    • s1: Pointer to the first Sprite structure.
    • s2: Pointer to the second Sprite structure.
  • Returns: Number of overlapping pixels. >= 1 if collision is detected, 0 otherwise.

  • Example:

    Sprite s1, s2;
    ....
    if (iCheckSpriteCollision(&s1, &s2)) {
        // Collision detected
    }

int iCheckImageSpriteCollision(int x1, int y1, Image *img, Sprite *s)

  • Description: Checks for pixel-level collision between an image and a sprite.
  • Parameters:
    • x1, y1: Coordinates of the image.
    • img: Pointer to the Image structure.
    • s: Pointer to the Sprite structure.
    • Returns: Number of overlapping pixels. >= 1 if collision is detected, 0 otherwise.
    • Note: This function doesn't consider image rotation (Using iRotate and iUnRotate). Use iCheckSpriteCollision for such cases.
  • Example:
    Image img;
    Sprite s;
    iLoadImage(&img, "image.png");
    ....
    if (iCheckImageSpriteCollision(100, 200, &img, &s)) {
        // Collision detected between image and sprite
    }

🧰 Miscellaneous

void iEnterFullscreen()

  • Description: Enters fullscreen mode.
  • Example:
    iEnterFullscreen(); // Enter fullscreen mode

void iLeaveFullscreen()

  • Description: Exits fullscreen mode and returns to windowed mode.
  • Example:
    iLeaveFullscreen(); // Exit fullscreen mode

📄 License

This library is for educational purposes and is typically used in academic or hobbyist OpenGL projects.

🙏 Acknowledgements

📚 Libraries

Releases

Packages

Contributors

Languages