Modern-iGraphics

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

Modern-iGraphics-main
โ”œโ”€โ”€ MINGW
โ”‚   โ”œโ”€โ”€ bin
โ”‚   โ”œโ”€โ”€ include
โ”‚   โ”œโ”€โ”€ lib
โ”‚   โ”œโ”€โ”€ ....
โ”‚   โ””โ”€โ”€ share
โ”œโ”€โ”€ OpenGL
โ”œโ”€โ”€ assets
โ”œโ”€โ”€ bin
โ”œโ”€โ”€ obj
โ”œโ”€โ”€ examples
โ”œโ”€โ”€ iGraphics.h
โ”œโ”€โ”€ iGraphics.cbp
โ”œโ”€โ”€ iMain.cpp
โ”œโ”€โ”€ ....

๐Ÿงฑ Setup in Code::Blocks

Change the compiler path of Code::Blocks as following: Settings โ†’ Compiler โ†’ 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:

.\runner.bat examples\BallDemo.cpp
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
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:


๐Ÿ“ฆ Release Guideline (Windows)


๐Ÿ‘จโ€๐Ÿ’ป 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'
        iCloseWindow();
        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
    iOpenWindow(400, 400, "iGraphics");
    // Execution will continue from here once iCloseWindow() is called.
    return 0;
}

๐Ÿ› ๏ธ Functions in iGraphics.h

๐Ÿ–ผ๏ธ Graphics Functions

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

void iCloseWindow()

void iClear()

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

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

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

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

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

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

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

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

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

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

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

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

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

void iSetLineWidth(float width)

void iShowSpeed(double x, double y)

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

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

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

โฑ๏ธ Animation and Timer

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

void iPauseTimer(int index)

void iResumeTimer(int index)

void iDelay(int sec)

๐Ÿ–ฑ๏ธ Mouse Functions

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

void iMouseMove(int mx, int my)

void iMouseDrag(int mx, int my)

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

void iShowCursor()

void iHideCursor()

โŒจ๏ธ Keyboard Functions

void iKeyPress(unsigned char key)

void iSpecialKeyPress(unsigned char key)

void iKeyRelease(unsigned char key)

void iSpecialKeyRelease(unsigned char key)

int isKeyPressed(unsigned char key)

int isSpecialKeyPressed(unsigned char key)

๐Ÿ”‰ 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)

void iPauseSound(int channel)

void iResumeSound(int channel)

void iStopSound(int channel)

void iStopAllSounds()

void iSetVolume(int channel, int volume)

void iIncreaseVolume(int channel, int amount)

void iDecreaseVolume(int channel, int amount)

๐Ÿ…ฐ๏ธ 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)

๐Ÿ–ผ๏ธ Image Functions

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

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

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

void iScaleImage(Image* img, double scale)

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

void iMirrorImage(Image* img, MirrorState state)

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

void iIgnorePixels(Image* img, int ignoreColor)

void iFreeImage(Image* img)

๐Ÿงฉ 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)

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

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

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

void iShowSprite(Sprite* s)

void iAnimateSprite(Sprite* s)

void iScaleSprite(Sprite* s, double scale)

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

void iMirrorSprite(Sprite* s, MirrorState state)

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

void iFreeSprite(Sprite* s)

int iCheckCollision(Sprite* s1, Sprite* s2)

int iGetVisiblePixelsCount(Sprite* s1)

๐Ÿงฐ Miscellaneous

void iEnterFullscreen()

void iLeaveFullscreen()


๐Ÿ“„ License

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

๐Ÿ™ Acknowledgements

๐Ÿ“š Libraries