Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: xteraco on May 16, 2006

Title: rotating logo
Post by: xteraco on May 16, 2006
blech, its not finished, but i *had* to be the 1st person to post c++ code :P  w00t!
compiles in Devc++ btw...

Code: [Select]
#include <windows.h>
#include <gl/gl.h>

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);

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;

    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);

    hWnd = CreateWindow (
      "GLSample", "OpenGL Sample",
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 800, 600,
      NULL, NULL, hInstance, NULL);

    EnableOpenGL (hWnd, &hDC, &hRC);

    while (!bQuit)
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {

            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_TRIANGLE_FAN);
            glVertex3f (-1.9f, 0.3f, 0);
        glVertex3f (-1.7f, 0.2f, 0);
        glVertex3f (-1.9f, 0.8f, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-1.7, 0.2, 0);
        glVertex3f (-1.5, 0.8, 0);
        glVertex3f (-1.5, 0.4, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-1.7, 0.2, 0);
        glVertex3f (-1.9, 0, 0);
        glVertex3f (-1.9, -0.4, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-1.7, 0.2, 0);
        glVertex3f (-1.6, -0.4, 0);
        glVertex3f (-1.5, 0, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-1.4, 0.7, 0);
        glVertex3f (-0.6, 0.7, 0);
        glVertex3f (-1.1, 0.6, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-1.2, 0.7, 0);
        glVertex3f (-1, 0.7, 0);
        glVertex3f (-1.2, -0.3, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-0.9, 0.5, 0);
        glVertex3f (-1, -0.3, 0);
        glVertex3f (-0.7, 0.5, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-0.7, 0.5, 0);
        glVertex3f (-0.4, 0.5, 0);
        glVertex3f (-0.8, 0.3, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-1, -0.3, 0);
        glVertex3f (-0.7, -0.3, 0);
        glVertex3f (-0.9, 0, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-0.9, -0.1, 0);
        glVertex3f (-0.7, 0.1, 0);
        glVertex3f (-0.8, 0.2, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-0.5, -0.3, 0);
        glVertex3f (-0.5, 0.4, 0);
        glVertex3f (-0.2, 0.6, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-0.4, 0.1, 0);
        glVertex3f (0, 0.2, 0);
        glVertex3f (-0.3, 0.3, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-0.2, 0.6, 0);
        glVertex3f (0, 0.2, 0);
        glVertex3f (0, 0.6, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (-0.3, 0.1, 0);
        glVertex3f (-0.1, 0.2, 0);
        glVertex3f (0, -0.3, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (0.1, -0.3, 0);
        glVertex3f (0.3, 0.6, 0);
        glVertex3f (0.4, 0, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (0.4, 0, 0);
        glVertex3f (0.7, 0, 0);
        glVertex3f (0.3, 0.2, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (0.3, 0.6, 0);
        glVertex3f (0.7, 0, 0);
        glVertex3f (0.7, 0.4, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (0.7, 0.4, 0);
        glVertex3f (0.9, -0.2, 0);
        glVertex3f (0.7, 0, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (1, -0.2, 0);
        glVertex3f (0.8, 0.6, 0);
        glVertex3f (1, 0.8, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (1, 0.8, 0);
        glVertex3f (1.3, 0.8, 0);
        glVertex3f (0.9, 0.5, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (1, -0.2, 0);
        glVertex3f (1.3, -0.2, 0);
        glVertex3f (1, 0.2, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (1.4, 0.6, 0);
        glVertex3f (1.2, 0.1, 0);
        glVertex3f (1.4, -0.2, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (1.4, -0.2, 0);
        glVertex3f (1.6, 0.4, 0);
        glVertex3f (1.7, 0, 0);
    glEnd ();

    glBegin (GL_TRIANGLE_FAN);
        glVertex3f (1.6, 0.4, 0);
        glVertex3f (1.4, 0.6, 0);
        glVertex3f (1.6, 0.6, 0);
glEnd ();

           
            glEnd ();
            glPopMatrix ();

            SwapBuffers (hDC);

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

    DisableOpenGL (hWnd, hDC, hRC);

    DestroyWindow (hWnd);

    return msg.wParam;
}


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);
    }
}

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

    *hDC = GetDC (hWnd);

    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);

    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}



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


runs waaay to fast, and needs to be zoom'd out a lil, but like i said, its a WIP
Title: Re: rotating logo
Post by: Stonemonkey on May 16, 2006
I've not run this btw but why draw single triangles with GL_TRIANGLE_FAN, If you just use GL_TRIANGLES you won't need all the GLbegins and GLends in between as for every 3 vertices sent results in a tri being drawn.

Code: [Select]
    glBegin (GL_TRIANGLES);
        glVertex3f (-1.7, 0.2, 0);
        glVertex3f (-1.5, 0.8, 0);
        glVertex3f (-1.5, 0.4, 0);

        glVertex3f (-1.7, 0.2, 0);
        glVertex3f (-1.9, 0, 0);
        glVertex3f (-1.9, -0.4, 0);

        glVertex3f (-1.7, 0.2, 0);
        glVertex3f (-1.6, -0.4, 0);
        glVertex3f (-1.5, 0, 0);
    glEnd ();

will draw 3 triangles.
Title: Re: rotating logo
Post by: xteraco on May 16, 2006
hey, thnx for the advice, i knew about gl_triangle .... dunno why i used triangle fan tho...
Title: Re: rotating logo
Post by: CoDeMaN on May 17, 2006
Installed dev-c++ run the code and got lots of errors

Title: Re: rotating logo
Post by: Clyde on May 17, 2006
You need to find and download the external libs and files etc dude.
As you dont seem to have OGL, Glu, etc.

Should be something in the options to download updates etc, if i remember right, unless someone else knows where to get them.
Title: Re: rotating logo
Post by: CoDeMaN on May 18, 2006
D/l all updates and still same error

Cant seem to find anything help on opengl setting ect either :(
Title: Re: rotating logo
Post by: CoDeMaN on May 18, 2006
At last i finally got it running :)

From the help file

To link a library with your project, just add in Project options, Further option files :
-lopengl32

Trouble is the Projects options is not available to use.. its not selectable.
only way i got around it wa to load an opengl sample and then delete the code to add the above code then it worked.

Hope you know what i mean :)

Well its 5:30am and spose its time for some sleep ...