How to do graphics in visual c++. Is it possible to run a graphics program in c++?

Recommended Answers

All 9 Replies

There are a number of graphics libraries available depending on your needs. Can you be more specific?

Any code using <graphics.h> is too far outdated to be useful. The <graphics.h> header was originally an implementation of BGI for Borland Turbo C on MS-DOS (real DOS, not that Windows hosted faux-DOS you see these days). There have been attempts to port it to other compilers, but Visual C++ isn't one of them to the best of my knowledge, and even then you'd be wise to look for a modern graphics library rather than one that's 20 years old and targeted to a dead operating system.

Which one do you prefer for modern graphics? Actually I dont have any Idea about it.
I have done a project on game "Baloon Shooting" in c. I used for the game TC(Turbo C).
TC(Turbo C) supports "graphics.h>. But I dont know about C++.

You still haven't specified what you want to do. Are you writing a game? Do you just want a GUI? What kind of graphics? 2D? 3D?

Which one do you prefer for modern graphics?

I don't do graphics. My expertise is in back-end development.

I don't do graphics. My expertise is in back-end development.

Like this?

I don't do graphics.

Why not? Doing graphics can be so much fun! I do graphics all the time! :D

Here's something I've been working on lately -> I saw this flash game where the sprite animations where created by applying a series of transformations (scalings, rotations, shearings, translations... etc) on the original sprite. I liked the idea and I thought I'd play around with it a bit. Pretty soon, though, I realized that the library I was using (SFML) didn't directly support any but some trivial transformations, so, I had to manually create the ones I wanted using some rather low level interface (sf::Image::GetPixel / sf::Image::SetPixel). However, the end result was very satisfying. Here's a console version of what I did (sorry about the flickering :/)

#define _WIN32_WINNT 0x0500

#include <windows.h>

#include <iostream>
#include <vector>

#include <cstdlib>
#include <ctime>
#include <cmath>

typedef void (*Callback)(double[2][2], double[2], int, int, int, int, int, int);

struct Color
{
    static const int Default = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
    static const int White   = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
    static const int Red     = FOREGROUND_RED |                                      FOREGROUND_INTENSITY;
    static const int Green   =                  FOREGROUND_GREEN |                   FOREGROUND_INTENSITY;
    static const int Blue    =                                     FOREGROUND_BLUE | FOREGROUND_INTENSITY;
};

struct Bitmap
{
    int width;
    int height;

    std::vector<int> data;

    Bitmap(int width_ = 1, int height_ = 1, int color_ = Color::White) :
        width(width_), height(height_), data(width_ * height_, color_) {}

    int & operator()(int x, int y)       { return data[x * height + y]; }
    int   operator()(int x, int y) const { return data[x * height + y]; }
};

Bitmap RandomBitmap(int width, int height)
{
    Bitmap bmp(width, height);

    struct Local
    {
        static void RandomColorDrops(Bitmap & bmp, int color)
        {
            int x, y;

            const int max_drops = 1 + (bmp.width + bmp.height) / 2;

            for (int count = 0; count < max_drops; ++count)
            {
                x = rand() % (bmp.width  - 6) + 3;
                y = rand() % (bmp.height - 6) + 3;

                for (int i = -1; i <= 1; ++i)
                    for (int j = -1; j <= 1; ++j)
                        bmp(x + i, y + j) = color;
            }
        }
    };

    Local::RandomColorDrops(bmp, Color::Red);
    Local::RandomColorDrops(bmp, Color::Green);
    Local::RandomColorDrops(bmp, Color::Blue);

    return bmp;
}

struct Console
{
    static void Resize(int x, int y)
    {
        COORD size = { x, y };

        SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), size);
    }

    static void GotoXY(int x, int y)
    {
        COORD pos = { x, y };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
    }

    static void Clear()
    {
        DWORD written;
        COORD start = { 0, 0 };

        CONSOLE_SCREEN_BUFFER_INFO csbi;

        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

        FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),
            ' ', csbi.dwSize.X * csbi.dwSize.Y , start, &written);

        GotoXY(0, 0);
    }

    static void SetColor(int color)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
    }

    static void Draw(const Bitmap & bmp)
    {
        GotoXY(0, 0);

        for (int y = 0; y < bmp.height; ++y)
        {
            for (int x = 0; x < bmp.width; ++x)
            {
                SetColor(bmp(x, y));

                std::cout << '#';
            }

            std::cout << std::endl;
        }
    }
};

void Invert(double a[2][2], double b[2])
{
    double det = a[0][0] * a[1][1] - a[0][1] * a[1][0];

    double temp = a[0][0];

    a[0][0] = a[1][1];
    a[1][1] = temp;

    a[0][1] = - a[0][1];
    a[1][0] = - a[1][0];

    a[0][0] /= det;
    a[0][1] /= det;
    a[1][0] /= det;
    a[1][1] /= det;

    temp = b[0];

    b[0] = - (a[0][0] * b[0] + a[0][1] * b[1]);
    b[1] = - (a[1][0] * temp + a[1][1] * b[1]);
}

Bitmap Transform(const Bitmap & input, Callback matrix_setup, int frame, int max_frame)
{
    double matrix_a[2][2] = { 1, 0, 0, 1 };
    double matrix_b[2]    = { 0, 0 };

    const int old_width  = input.width;
    const int old_height = input.height;

    int max_new_x = matrix_b[0];
    int min_new_x = matrix_b[0];
    int max_new_y = matrix_b[1];
    int min_new_y = matrix_b[1];

    int new_x, new_y;
    int old_x, old_y;

    for (int old_x = 0; old_x < old_width; ++old_x)
    {
        for (int old_y = 0; old_y < old_height; ++old_y)
        {
            matrix_setup(matrix_a, matrix_b, old_x, old_y, old_width, old_height, frame, max_frame);

            new_x = matrix_a[0][0] * old_x + matrix_a[0][1] * old_y + matrix_b[0];
            new_y = matrix_a[1][0] * old_x + matrix_a[1][1] * old_y + matrix_b[1];

            if (new_x < min_new_x) min_new_x = new_x;
            if (new_x > max_new_x) max_new_x = new_x;

            if (new_y < min_new_y) min_new_y = new_y;
            if (new_y > max_new_y) max_new_y = new_y;
        }
    }

    const int new_width  = max_new_x - min_new_x + 1;
    const int new_height = max_new_y - min_new_y + 1;

    Bitmap output(new_width, new_height);

    for (int new_x = 0; new_x < new_width; ++new_x)
    {
        for (int new_y = 0; new_y < new_height; ++new_y)
        {
            matrix_setup(matrix_a, matrix_b, new_x, new_y, new_width, new_height, frame, max_frame);

            Invert(matrix_a, matrix_b);

            old_x = matrix_a[0][0] * new_x + matrix_a[0][1] * new_y + matrix_b[0];
            old_y = matrix_a[1][0] * new_x + matrix_a[1][1] * new_y + matrix_b[1];

            if (old_x >= 0 && old_x < old_width && old_y >=0 && old_y < old_height)
                output(new_x, new_y) = input(old_x, old_y);
        }
    }

    return output;
}

void shear(double a[2][2], double b[2], int x, int y, int max_x, int max_y, int frame, int max_frame)
{
    a[0][1] = frame * 2.0 / max_frame;
}

void grow_top(double a[2][2], double b[2], int x, int y, int max_x, int max_y, int frame, int max_frame)
{
    a[0][0] = 1 + (1 - y * 1.0 / max_y) * frame * 3.0 / max_frame;
}

void shrink_left(double a[2][2], double b[2], int x, int y, int max_x, int max_y, int frame, int max_frame)
{
    a[1][1] = 1 - (1 - x * 1.0 / max_x) * frame * 0.67 / max_frame;
}

void magnifying_glass(double a[2][2], double b[2], int x, int y, int max_x, int max_y, int frame, int max_frame)
{
    a[0][0] = 1;
    a[1][1] = 1;

    b[0] = 0;
    b[1] = 0;

    if ( (x - (15 + frame * 15.0 / max_frame )) * (x - (15 + frame * 15.0 / max_frame ))
         + (y - (20 + frame * 15.0 / max_frame )) * (y - (20 + frame * 15.0 / max_frame )) < 225)
    {
        a[0][0] = 2;
        a[1][1] = 2;

        b[0] =  (- 10 - frame * 35.0 / max_frame);
        b[1] =  (- 10 - frame * 35.0 / max_frame);
    }
}

void Animate(const Bitmap & bmp, Callback transformation)
{
    const int max_frame = 30;
    const int delay = 50;

    Bitmap animation[max_frame + 1];

    for (int i = 0; i <= max_frame; ++i)
        animation[i] = Transform(bmp, transformation, i, max_frame);

    for (int i = 0; i <= max_frame; ++i)
    {
        Console::Clear();

        Console::Draw(animation[i]);

        Sleep(delay);
    }

    for (int i = max_frame; i >=0; --i)
    {
        Console::Clear();

        Console::Draw(animation[i]);

        Sleep(delay);
    }
}

int main()
{
    srand(time(0));

    Console::Resize(100, 100);

    ShowWindow(GetConsoleWindow(), SW_SHOWMAXIMIZED);

    Bitmap small_bmp = RandomBitmap(25, 25);
    Bitmap   big_bmp = RandomBitmap(50, 50);

    Animate(small_bmp, shear);
    Animate(small_bmp, grow_top);

    Animate(big_bmp, shrink_left);
    Animate(big_bmp, magnifying_glass);

    Console::Draw(big_bmp);

    Console::SetColor(Color::Default);
}

Hey m4ster_r0shi
I can not run your program also .I use Visual C++. How to run it?

What do you mean you can not run it? Create an empty project, add a source file, paste the code and then
build the project. Also, try this version instead -> http://pastebin.com/1Bn12dnS (I fixed the flickering problem).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.