Modern-iGraphics

Modern iGraphics Library

πŸ’» Code β€’ 🏠 Homepage


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, sound engine, sprite management, collision detection and advanced mouse control. The library is now cross-platform and works on both Windows and Linux. Updates will be added incrementally based on requests.


iGraphics Banner

πŸŽ₯ Example Games


🧱 Setup in Code::Blocks

Download the ZIP file from here and extract it.

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.

In some versions of Code::Blocks, you may need to change the compiler path 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.

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 # Install SDL2 and SDL2_mixer if not already installed
./runner.sh examples/BallDemo.cpp

πŸ“¦ 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 iMouse() is called when the user presses/releases the mouse.
(mx, my) is the position where the mouse pointer is.
*/
void iMouse(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 iMouseDrag() is called when the user presses and drags the mouse.
(mx, my) is the position where the mouse pointer is.
*/
void iMouseDrag(int mx, int my)
{
    //place your codes here
}

/*
function iMouseWheel() is called when the user scrolls the mouse wheel.
dir = 1 for up, -1 for down.
*/
void iMouseWheel(int dir, int mx, int my)
{
    // place your code here
}

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

/*
function iSpecialKeyboard() 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 iSpecialKeyboard(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[])
{
    glutInit(&argc, argv);
    iInitialize(400, 400, "demooo");
    return 0;
}

πŸ› οΈ Functions in iGraphics.h

πŸ–ΌοΈ Graphics Functions

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

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 iText(double x, double y, char *str, void* font=GLUT_BITMAP_8_BY_13)

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

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

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

⏱️ 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 iMouse(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)

⌨️ Keyboard Functions

void iKeyboard(unsigned char key)

void iSpecialKeyboard(unsigned char key)

bool isKeyPressed(unsigned char key)

bool 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, bool loop = false, 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)

πŸ–ΌοΈ Image Functions

void iShowImage(int x, int y, const char *filename, int width = -1, int height = -1, MirrorState mirror = NO_MIRROR, int ignoreColor = -1)

bool iLoadImage(Image* img, const char filename[], int ignoreColor = -1)

void iShowLoadedImage(int x, int y, Image* img, int width = -1, int height = -1, MirrorState mirror = NO_MIRROR)

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 iFreeImage(Image* img)

🧩 Sprite Functions

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

void iInitSprite(Sprite *s)

void iLoadFramesFromFolder(Image *frames, const char *folderPath, int ignoreColor = -1)

void iLoadFramesFromSheet(Image *frames, const char *filename, int rows, int cols, int ignoreColor = -1)

void iChangeSpriteFrames(Sprite *s, const Image *frames, int totalFrames)

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)

🧰 Miscellaneous

void iToggleFullscreen()


✨ Contributors

Shahriar Nirjon Mahir Labib Dihan Anwarul Bashar Shuaib Ashrafur Rahman Khan
Shahriar Nirjon Mahir Labib Dihan Anwarul Bashar Shuaib Md. Ashrafur Rahman Khan

πŸ“„ License

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