Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,793 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,596 people online right now. Registration is fast and FREE... Join Now!




C graphics programming

 
Reply to this topicStart new topic

C graphics programming

p@ddy
post 11 Oct, 2008 - 11:12 AM
Post #1


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 2

Hi,

Plz can anybody help me out in the C graphics programming
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 11 Oct, 2008 - 11:25 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,980



Thanked 77 times

Dream Kudos: 1175
My Contributions


Moved to C
User is offlineProfile CardPM

Go to the top of the page

David W
post 11 Oct, 2008 - 12:20 PM
Post #3


D.I.C Regular

Group Icon
Joined: 20 Sep, 2008
Posts: 304



Thanked 15 times

Dream Kudos: 250
My Contributions


I not sure if this is the kind of thing that you are looking for ... ?

If you compile it ... remember to turn the 'windows' compile option 'on'.

Here is some code that may give some ideas and/or serve as a template ?

Shalom,

David
http://developers-heaven.net/forum/index.php/topic,46.0.html

CODE
/*
    This program draws graph of 'running average'
    And in the center, a big circle that also has
    2 sets of 33 smaller circles ...
*/                

#include <windows.h>
#include <math.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HPEN hPen;
HBRUSH hBrush;

int width  = 600;   /* number of dots in width of Window */
int height = 500;   /* number of dots in height of Window */


/* draws a Circle; need to pass in handle, centerX, centerY, radius ... */
void Circle( HDC hdc, int cPointX, int cPointY, int radius );


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR szCmdLine, int nShowCmd)
{
    static char name[] = "My Application";
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    /* Step 1: Registering the Window Class */
    wc.cbSize         = sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    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) (COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = name;
    wc.hIconSm         = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Registration Failure",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    /* Step 2: Creating the Window */
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        name,
        "Spme simple math functions, a graph and some graphics demonstrated ...  ",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, width, height,
        NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nShowCmd);
    UpdateWindow(hwnd);

    /*  Step 3: The Message Loop */
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return (int)Msg.wParam;
}



/*
    The first thing I did was to create a 2 (1 now) pixel, solid, red pen,
    and a solid green brush.  Then I began the painting.  The brush
    and the pen won't take effect unless you tell the DC to use them.  
    You do so by using the function SelectObject.  This function selects
    all kinds of objects into the DC.  But for right now we need it to
    select the new brush and pen.  Now that we've selected the brush and
    pen, we are ready to begin drawing.  What I drew is a rectangle between
    the points (50, 50) and (200, 100) just because I like the size of it,
    and it's easy to deal with numbers.  Then I moved the beginning point
    to (50, 100) (where the bottom left hand corner of the rectangle is).  
    Then I drew a few lines creating something that looks like a graph.
    Then I set the text color to blue with the function SetTextColor.  
    Then finally drew the text, with the first letter beginning at (95, 100).  
    And I ended the painting.

    Play around with the brushes, pens, and drawing until you get the hang of it.
*/


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc; /* a handle to a device context */
    PAINTSTRUCT ps;
    RECT rect;

    switch(msg)
    {
    case WM_PAINT:
        
    /*
        hdc = BeginPaint(hwnd, &ps);
        
        GetClientRect(hwnd, &rect);
        // now draw rectangle ( handle, upLeftX, upLeftY, lowRightX, lowRightY );
        Rectangle(hdc, rect.right/2-133, rect.bottom/2-33, rect.right/2+133, rect.bottom/2+33);

        DrawText(hdc, "Hello Matthew David John Zavitz", -1, &rect, DT_SINGLELINE |  DT_CENTER | DT_VCENTER );
        
        EndPaint(hwnd, &ps);
        break;
    */

    /*  
        Declared AT TOP the pen and the brush ... HPEN and HBRUSH
        ... and changed WM_PAINT message handling to this ...
    */
    
        hPen = CreatePen(BS_SOLID, 1, RGB(255, 0, 0));/* Defined HPEN hPen i.e. RED*/
        hBrush = CreateSolidBrush(RGB(0, 255, 0));/*Defined HBRUSH hBrush i.e. GREEN*/
        
    /*
        The first thing I did was to create a 1 pixel, solid, red pen,
        and a solid green brush.  Then I began the painting.  The brush
        and the pen won't take effect unless you tell the DC to use them.  
        You do so by using the function SelectObject.
    */
        hdc = BeginPaint(hwnd, &ps);   /* --------- Begin Painting ---------- */
            
        SelectObject(hdc, hPen);       /*Load the pen into the DC*/
        SelectObject(hdc, hBrush);     /*Load the brush into the DC*/
    
        Rectangle(hdc, 50,50, 200,100);/*Make a rectangle, will use the brush and the pen*/
            
        MoveToEx(hdc, 50, 100, NULL);  /*Set beginning point at (50,100)*/
        LineTo(hdc, 70, 60);           /*Make a line from (50, 100) to (70, 60)*/
        LineTo(hdc, 90, 85);           /*Make a line from (70, 60) to (90, 85)*/
        LineTo(hdc, 110, 55);          /*Make a line from (90, 85) to (110, 55)*/
        LineTo(hdc, 120, 60);          /*Make a line from (110, 55) to (120, 60)*/
        LineTo(hdc, 140, 95);          /*Make a line from (120, 60) to (140, 95)*/
        LineTo(hdc, 180, 53);          /*Make a line from (140, 95 to (180, 53)*/
            
        SetTextColor(hdc, RGB(0, 0, 255));             /*Set the Text color i.e. B*/
        TextOut(hdc, 55, 105, "2005 06 07 08 ...",17); /*Draw the text*/
        TextOut(hdc, 55, 25, "Running Average ...",19);/*Draw the text*/
        TextOut(hdc, 25, 50, "80 -",4);                /*Draw the text*/
        TextOut(hdc, 25, 70, "70 -",4);                /*Draw the text*/
        TextOut(hdc, 25, 90, "60 -",4);                /*Draw the text*/  
        
            
        /*Circle( int cPointX, int cPointY, int radius ) */
        /* in the middle of 600 x 400 dot window */
        
        int x = width/2, y = height/2, r =height/3;
        Circle( hdc, x, y, r );
        
        int x2,y2;
        int n;
        double theta;
        
        /* Now ... draw 33 little circles ... theta from 0..2PI */
        /* new_center = old_center(x, y) + ( r*cos(theta), r*sin(theta) )*/
        for( n = 0; n < 33; ++n)
        {
            theta = n*2*3.14159/33;
            x2 = x + r*cos(theta);
            y2 = y + r*sin(theta);  
            Circle( hdc, x2, y2, r/10 );
        }
        
        SetTextColor(hdc, RGB(0, 0, 255));/*Set the Text color i.e. Blue*/
        TextOut(hdc, width/2-70, 0.88*height, "33 Circles on a Circle.", 23);/*Draw the text*/
        
        
        
        /* Now ... draw 33 decreasing circles, in a funnel, UP... to the right */
        /* new_center = old_center(x, y) + ( r*n/33, r*n/33 )*/
        for( n = 0; n < 33; ++n)
        {
            x2 = x + r*n/33;
            y2 = y - r*n/33;  
            Circle( hdc, x2, y2, r/(n+1.33 ) );
        }    
        
        EndPaint(hwnd, &ps);           /* ---------- End Painting ----------- */
        break;
    
        
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
        
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}
/*  
    r^2 = x^2 + y^2
    x = sqrt( r^ - y^ )
*/

/* pass in handleToWindow, centerX, centerY, radius ... */
void Circle( HDC hdc, int cPointX, int cPointY, int radius )
{  
       int x= cPointX,
        y =cPointY-radius;
        
       MoveToEx(hdc, x, y, NULL); /* Starting position ... */
       
       /* for y from top to bottom (i.e. cPointY-radius to cPointY+radius) do ... */
       for(; y<=cPointY+radius; ++y )
       {
        x = sqrt( radius*radius - (y-cPointY)*(y-cPointY) );
        LineTo(hdc, (cPointX+x), y);
    }

    /* ... and back ... to top again ... */
    for(; y>=cPointY-radius; --y )
       {
        x = sqrt( radius*radius - (y-cPointY)*(y-cPointY) );
        LineTo(hdc, (cPointX-x), y);
    }  
}


This post has been edited by David W: 12 Oct, 2008 - 10:21 AM
User is offlineProfile CardPM

Go to the top of the page

schnalf
post 11 Oct, 2008 - 02:10 PM
Post #4


D.I.C Head

**
Joined: 9 Feb, 2008
Posts: 124



Thanked 2 times
My Contributions


if you want to make os independent or more effective applications you can use sdl (http://www.libsdl.org)
User is offlineProfile CardPM

Go to the top of the page

Bill Hsu
post 12 Oct, 2008 - 02:51 AM
Post #5


New D.I.C Head

*
Joined: 12 Oct, 2008
Posts: 1


My Contributions


Try OpenGl

This post has been edited by Bill Hsu: 12 Oct, 2008 - 02:52 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 14 Oct, 2008 - 04:32 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,397



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


What type of graphics?

Games? Software?

Do you want it to be cross platform?

Please don't say the graphics header with Borland sleep.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 04:45PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month