graphics in C++

Can someone please tell me what kind of graphics are available in c++,.....some examples........
I do not think C++ itself can do graphics, however using an API you can handle graphics, such as OpenGL.

The following is the default Dev-C++ OpenGL program, you need to link the library "opengl32" for this to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**************************
 * Includes
 *
 **************************/

#include <windows.h>
#include <gl/gl.h>


/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);


/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
      "GLSample", "OpenGL Sample", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 256, 256,
      NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */

            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);

            glPushMatrix ();
            glRotatef (theta, 0.0f, 0.0f, 1.0f);
            glBegin (GL_TRIANGLES);
            glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
            glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
            glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
            glEnd ();
            glPopMatrix ();

            SwapBuffers (hDC);

            theta += 1.0f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{

    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}


This makes a colourful triangle that rotates... using OpenGL you can do pretty much anything - it is very good for creating 3D worlds.
Last edited on
Can you please explain what API,BGI ...all these terms actually mean..........


i have a project..........in c++......and i wanted to learn about graphics......
Last edited on
Google and wikipedia are good friends:
http://en.wikipedia.org/wiki/Api
http://en.wikipedia.org/wiki/Borland_Graphics_Interface

A free tutorial for OpenGl programming:
http://nehe.gamedev.net/

And because it uses windows programming, a free tutorial for windows programming:
http://www.zetcode.com/tutorials/winapi/

Good luck
Thank you................
http://www.zetcode.com/tutorials/winapi/

What ever is given over here .......will they work in c++( i am not sure if i am allowed use VC++ in the project..................
Everything stated in this thread, from OpenGL to Windows works in C++ just takes your gusto to put this information into action. Jus take your time to learn some of it if its what your really wanting to do.
Thanks a lot.......that was a relief.........
i am still not getting Graphics ....
@grey127: If you have a concern or question, please post your own topic for it to be answered instead of hijacking another question.
@Zaita the word hijacking is not proper use some sensible words ....
well, ur right i will post me own question ... but think before u write ......
Right. I've always preferred "pulling a WTC", or "the Jewish Double Stranglehold Maneuver".
I will take a brief moment to remind everyone that we should take steps against The Jewish Problem so that tragedies such as the WTC never happen again.
For the example given by chris(and many programmes i got from the net i get a error....


Unresolved external '_main' referenced from c:\bcc\c0x32.obj


i searched for corrections online.........someone told the the folllowing

'This kind of error is usually caused by some mix-up between the intended project type and the actual project type. For instance, if you chose File, New, Console Wizard, checking 'Console Application', but proceeded to write a windows-style application, with a WinMain entrypoint, this is exactly the error you would receive. To remedy the problem, you would create a new project, of the correct type, and add your source files to the new project.



If compiling from the command line, the "target" options are analogous to the project type, and must likewise, be consistent with the application you're writing. The following list shows the BCC32.EXE options and their intended targets:
-W Target is Windows application ("WinMain" entrypoint)
-TC Target is console application ("main" entrypoint)
-WD Target is a DLL ("DllMain" entrypoint) '

the problem is that it is in command line tools.......and i use borland c++......


Someone please give me a remedy to the problem

Download an other compiler, eg MinGW: http://mingw.org/ (this compiler comes with dev-cpp: http://www.bloodshed.net/). I have never used borland, but from what I've heard it isn't very good. You can control MinGw with cmd.
thanks for telling....................but if someone finds out about borland c++....please tell me................In the school we don't have dev c++
Last edited on
Topic archived. No new replies allowed.