Click here to Skip to main content
15,887,846 members
Articles / Multimedia / DirectX

A DirectX Wrapper

Rate me:
Please Sign up or sign in to vote.
4.69/5 (20 votes)
18 Jul 200333 min read 181.1K   4.3K   66  
A DirectX Wrapper
/*

  testing for DXSmithIO lib

*/

/*

  copyright 2001, Alex Russell, all rights reserved

  April 7, 2001 AJR initial file

*/

//#include "stdafx.h"

#include <stdio.h>
#include <conio.h>


#include "..\DXSmithIO.h"

int main(int argc, char *argv[])
{
	int ret=0, i, j;
    CgsdxIO dx;
    CString errstr, tempStr;
    HFONT font=NULL;
    SIZE size;
    char temp[100];
    char temp2[100], *t1;
    DWORD mask;
    gsdxMode_t *modeInfo;

    D(dx.m_slog.PrintFile(TRUE));
    D(dx.m_slog.PrintLine(TRUE));
    D(dx.m_slog.StatusOut("This is a test of the stat log, %s", argv[0]));

    dx.InitResource();
    dx.AddResourcePath("c:\\temp");

    dx.GetModes();

    i=dx.GetLastError(&errstr);
    printf("Last error %d %s\n", i, (LPCSTR )errstr);
    
    j=dx.GetNumGraphicModes();
    if ( j > 0 )
        {
        for ( i=0; i < j; i++ )
            {
            modeInfo=dx.GetGraphicModeInfo(i);
            printf("width: %d height %d color %d\n",
                modeInfo->width,
                modeInfo->height,
                modeInfo->colorBits);
            }
        }
    else
        {
        printf("Error getting modes\n");
        }

    getch();

    if ( dx.InitGraphics(1024, 768, 32, GSDX_FULL_SCREEN|GSDX_CONSOLE_MODE) )
        {
        TRACE("Failed to init graphics %d\n");
        printf("failed to init graphics\n");
        }
    else
        {
        CPoint dest;
        DWORD soundID;
        DWORD soundID2;
        DWORD color;
        RECT r;
        gsdxBitmap_t *bitmap2;
        gsdxBitmap_t *bitmap;
        gsdxBitmap_t *topLeft;
        gsdxBitmap_t *topRight;
        gsdxBitmap_t *bottomLeft;
        gsdxBitmap_t *bottomRight;
        gsdxBitmap_t *centre;
        gsdxBitmap_t *topLeft2;
        gsdxBitmap_t *topRight2;
        gsdxBitmap_t *bottomLeft2;
        gsdxBitmap_t *bottomRight2;
        int done;

        bitmap=dx.LoadBitmap("test1.bmp", NULL);
        r.top=20;
        r.bottom=50;
        r.left=30;
        r.right=50;
        bitmap2=dx.LoadBitmap("test1.bmp", &r); // uses previously loaded bitmap

        r.left=0;
        r.right=320;
        r.top=0;
        r.bottom=240;
        topLeft=dx.LoadBitmap("square4.bmp", &r);
        r.left=320;
        r.right=640;
        r.top=0;
        r.bottom=240;
        topRight=dx.LoadBitmap("square4.bmp", &r);
        r.left=0;
        r.right=320;
        r.top=240;
        r.bottom=480;
        bottomLeft=dx.LoadBitmap("square4.bmp", &r);
        r.left=320;
        r.right=640;
        r.top=240;
        r.bottom=480;
        bottomRight=dx.LoadBitmap("square4.bmp", &r);
        r.left=160;
        r.right=480;
        r.top=120;
        r.bottom=360;
        centre=dx.LoadBitmap("square4.bmp", &r);


        r.left=0;
        r.right=320;
        r.top=0;
        r.bottom=240;
        topLeft2=dx.LoadBitmap("defaultSprite.bmp", &r);
        r.left=320;
        r.right=640;
        r.top=0;
        r.bottom=240;
        topRight2=dx.LoadBitmap("defaultSprite.bmp", &r);
        r.left=0;
        r.right=320;
        r.top=240;
        r.bottom=480;
        bottomLeft2=dx.LoadBitmap("defaultSprite.bmp", &r);
        r.left=320;
        r.right=640;
        r.top=240;
        r.bottom=480;
        bottomRight2=dx.LoadBitmap("defaultSprite.bmp", &r);


        font=dx.CreateFont(24, FW_MEDIUM, 0, 1);

        dx.InitSound();

        dx.LoadSound("horn.wav", &soundID);
        dx.LoadSound("1125SC1.WAV", &soundID2);
        dx.PlaySound(soundID);
        getch();

        dx.PlaySound(soundID2);
        dx.PlayMusic("Meteor Blast 4.mid", 1);
        ZeroMemory(&r, sizeof(RECT));


        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, topLeft);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, topRight);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, bottomLeft);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, bottomRight);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, centre);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, topLeft2);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, topRight2);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, bottomLeft2);
        dx.Flip();
        getch();

        dx.RectFill(NULL, 0);
        dest.x=0;
        dest.y=0;
        dx.Blit(&dest, bottomRight2);
        dx.Flip();
        getch();


        TRACE("main blit baitmap\n");
        dest.x=0;
        dest.y=0;
        dx.RectFill(NULL, 0);
        dx.Blit(&dest, bitmap);

        dx.Line(0, 10, 400, 10, 10, RGB(255, 0,0));
        dx.Line(0, 20, 400, 20, 10, RGB(0, 255,0));
        dx.Line(0, 30, 400, 30, 10, RGB(0, 0,255));
        dx.Line(0, 40, 400, 40, 10, RGB(100, 100, 100));

        dx.Line(0, 60, 400, 60, 10, dx.ConvertRGB(RGB(255, 0,0)));
        dx.Line(0, 70, 400, 70, 10, dx.ConvertRGB(RGB(0, 255,0)));
        dx.Line(0, 80, 400, 80, 10, dx.ConvertRGB(RGB(0, 0,255)));
        dx.Line(0, 90, 400, 90, 10, dx.ConvertRGB(RGB(100, 100, 100)));

        dx.EllipseFill(200, 90, 300, 190, dx.ConvertRGB(RGB(100, 100, 100)));
        dx.PieFill(300, 90, 400, 190, 90, 300, 400, 190, dx.ConvertRGB(RGB(100, 100, 100)));

        r.left=500;
        r.right=600;
        r.top=0;
        r.bottom=300;
        dx.RectFill(&r, RGB(255, 0,0));
        r.left+=100;
        r.right+=100;
        dx.RectFill(&r, RGB(0, 255,0));
        r.left+=100;
        r.right+=100;
        dx.RectFill(&r, RGB(0, 0, 255));
        r.left+=100;
        r.right+=100;
        dx.RectFill(&r, RGB(100, 100, 100));

        r.left=500;
        r.right=600;
        r.top=350;
        r.bottom=650;
        dx.RectFill(&r, dx.ConvertRGB(RGB(255, 0,0)));
        r.left+=100;
        r.right+=100;
        dx.RectFill(&r, dx.ConvertRGB(RGB(0, 255,0)));
        r.left+=100;
        r.right+=100;
        dx.RectFill(&r, dx.ConvertRGB(RGB(0, 0, 255)));
        r.left+=100;
        r.right+=100;
        dx.RectFill(&r, dx.ConvertRGB(RGB(100, 100, 100)));




        dx.Flip();
        getch();

        TRACE("bitmap 2\n");
        dx.RectFill(NULL, 0);
        dest.x=200;
        dest.y=300;
        dx.Blit(&dest, bitmap2);
        for ( i=0; i < 9; i++ )
            {
            dest.x+=75;
            dx.Blit(&dest, bitmap2, i%2);
            }
        dx.Flip();
        getch();

        if ( dx.InitIO() )
            {
            TRACE("initIO failed\n");
            dx.DrawText(font, 50, 230, RGB(255, 255,255), 0, "InitIO failed");
            dx.Flip();
            getch();
            }
        else
            {
            gsdxEvent_t event;
            gsdxJoystickState_t joyState;

            TRACE("init IO OK\n");
            dx.DrawText(font, 50, 230, RGB(255, 255,255), 0, "InitIO OK, press a key to test");
            dx.GetTextSize(font, "a", &size);
            sprintf(temp, "24 is font creation height, cy = %d", size.cy);
            dx.DrawText(font, 50, 255, RGB(255, 255,255), 0, temp);
            dx.Flip();
            getch();

            done=0;
            while ( !done )
                {
                dx.RectFill(NULL, 0);
                if ( dx.IsKeyDown(DIK_ESCAPE) )
                    done=1;
                if ( dx.GetEvent(&event) )
                    {
                    if ( event.type == GSDX_IO_TYPE_KEY )
                        {
                        sprintf(temp, "scan %d ascii %d %c %s CAP %s NUM %s SCROLL %s",
                            event.event.key.scanCode,
                            event.event.key.ascii,
                            event.event.key.ascii,
                            event.event.key.isDown ? "DOWN" : "UP",
                            dx.GetCapState() ? "ON" : "OFF",
                            dx.GetNumState() ? "ON" : "OFF",
                            dx.GetScrollState() ? "ON" : "OFF");
                        dx.DrawText(font, 50, 230, RGB(255, 255,255), 0, temp);
                        if ( event.event.key.scanCode == DIK_ESCAPE )
                            done=1;
                        }
                    else
                        {
                        if ( event.type == GSDX_IO_TYPE_MOUSE )
                            {
                            sprintf(temp, "mouse %04d:%04d wheel %04d",
                                event.x,
                                event.y,
                                event.z);
                            if ( event.event.mouse.sub_type == GSDX_IO_SUBTYPE_BUTTON_DOWN )
                                {
                                sprintf(temp2, " button down num %d\n", event.event.mouse.index);
                                strcat(temp, temp2);
                                }
                            else
                                {
                                if ( event.event.mouse.sub_type == GSDX_IO_SUBTYPE_BUTTON_UP )
                                    {
                                    sprintf(temp2, " button up num %d\n", event.event.mouse.index);
                                    strcat(temp, temp2);
                                    }
                                else
                                    strcat(temp, " mouse move");
                                }
                            dx.DrawText(font, 50, 250, RGB(255, 255,255), 0, temp);
                            }
                        else
                            {
                            if ( event.type == GSDX_IO_TYPE_JOYSTICK )
                                {
                                sprintf(temp, "JOY %05d %05d ", 
                                    event.x,
                                    event.y);
                                switch ( event.event.joy.sub_type )
                                    {
                                    case GSDX_IO_SUBTYPE_JOYSTICK_X:
                                        strcat(temp, "X ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_Y:
                                        strcat(temp, "Y ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_Z:
                                        strcat(temp, "Z ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_R:
                                        strcat(temp, "R ");
                                        break;
                                    case GSDX_IO_SUBTYPE_BUTTON_UP:
                                        strcat(temp, "B UP ");
                                        sprintf(temp2, "%02d ", event.event.joy.index);
                                        strcat(temp, temp2);
                                        break;
                                    case GSDX_IO_SUBTYPE_BUTTON_DOWN:
                                        strcat(temp, "B DN ");
                                        sprintf(temp2, "%02d ", event.event.joy.index);
                                        strcat(temp, temp2);
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_POV:
                                        strcat(temp, "POV ");
                                        break;
                                    }

                                switch ( event.event.joy.sub_sub_type )
                                    {
                                    case GSDX_IO_SUBTYPE_JOYSTICK_CENTRE:
                                        strcat(temp, "TO CENTRE      ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_LEFT:
                                        strcat(temp, "TO LEFT        ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_RIGHT:
                                        strcat(temp, "TO RIGHT       ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_UP:
                                        strcat(temp, "TO UP          ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_DOWN:
                                        strcat(temp, "TO DOWN        ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_DOWN_LEFT:
                                        strcat(temp, "TO DOWN  LEFT      ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_DOWN_RIGHT:
                                        strcat(temp, "TO DOWN  RIGHT      ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_UP_LEFT:
                                        strcat(temp, "TO UP  LEFT      ");
                                        break;
                                    case GSDX_IO_SUBTYPE_JOYSTICK_UP_RIGHT:
                                        strcat(temp, "TO UP  RIGHT      ");
                                        break;
                                    default:
                                        strcat(temp, "               ");
                                        break;
                                    }
                                dx.DrawText(font, 50, 250, RGB(255, 255,255), 0, temp);
                                }
                            else
                                dx.DrawText(font, 50, 230, RGB(255, 255,255), 0, "not KEY type >>>>");
                            }
                        }

                    }

                dx.GetJoystickState(&joyState);
                sprintf(temp, "x %05d y %05d z %05d pov %d %d %d %d Rudders x %05d y %05d z %05d",
                    joyState.x,
                    joyState.y,
                    joyState.z,
                    joyState.pov[0],
                    joyState.pov[1],
                    joyState.pov[2],
                    joyState.pov[3],
                    joyState.rx,
                    joyState.ry,
                    joyState.rz);
                t1=temp2;
                *t1='>';
                t1++;
                for ( mask=1,i=0; i < 10; i++, mask<<=1 )
                    {
                    TRACE("mask %d mask & button %d\n", mask, mask & joyState.buttons);
                    if ( (mask & joyState.buttons) )
                        *t1='*';
                    else
                        *t1=' ';
                    t1++;
                    }
                *t1='<';
                t1++;
                *t1=0;
                dx.DrawText(font, 50, 270, RGB(255, 255,255), 0, temp);
                dx.DrawText(font, 50, 290, RGB(255, 255,255), 0, temp2);

                if ( event.type == GSDX_IO_TYPE_JOYSTICK )
                    Sleep(1000);
                dx.Flip();
                }


            } // initIO ok


        dx.PlaySound(soundID2);
        dx.RectFill(NULL, 0);
        r.left=0;
        r.right=bitmap->bmRect.right - bitmap->bmRect.left;
        r.top=0;
        r.bottom=bitmap->bmRect.bottom - bitmap->bmRect.top;
        for ( i=0; i < 7; i++ )
            {
            dx.Blit(&r, bitmap);
            r.left+=120;
            r.right+=150;
            r.top+=50;
            r.bottom+=80;
            }
        dx.Flip();
        getch();

        dx.RectFill(NULL, RGB(0,255,0));
        dx.Flip();
        getch();

        r.left=100;
        r.right=200;
        r.top=100;
        r.bottom=200;
        dx.RectFill(NULL, 0);
        dx.RectFill(&r, RGB(255, 0,0));
        dx.Line(10, 10, 200, 200, 10, RGB(255, 0,0));
        dx.Line(10, 10, 300, 200, 10, RGB(255, 0,0));
        dx.Line(10, 10, 400, 250, 10, RGB(255, 0,0));
        dx.Line(10, 10, 500, 300, 10, RGB(255, 0,0));
        dx.DrawText(font, 10, 100, RGB(255, 0,0), 0, "Hi World");
        dx.DrawText(font, 20, 130, RGB(0, 0,255), 0, "Hi World");
        dx.DrawText(font, 50, 160, RGB(0, 255,0), 0, "Hi World");
        dx.DrawText(font, 50, 190, RGB(255, 255,255), GSDX_TRANSPARENT_TEXT, "Hi World", &size);
        sprintf(temp, "size is width %d height  %d",
            size.cx, size.cy);
        dx.DrawText(font, 50, 230, RGB(255, 255,255), GSDX_TRANSPARENT_TEXT, temp);
        dx.Flip();
        getch();

        r.left=100;
        r.right=900;
        r.top=100;
        r.bottom=500;
        dx.RectFill(NULL, 0);
        dx.Flip();
        dx.RectFill(NULL, 0);
        dx.Flip();
        if ( !dx.SetClipping(&r) )   // default 1 rect
            {
            dx.EnableClipping();
            dx.RectFill(NULL, RGB(100, 0,100));
            dx.EllipseFill(200, 90, 300, 190, dx.ConvertRGB(RGB(100, 100, 100)));
            dx.PieFill(300, 90, 400, 190, 90, 300, 400, 190, dx.ConvertRGB(RGB(100, 100, 100)));
            dx.Flip();
            dx.RectFill(NULL, RGB(100, 0,100));
            dx.EllipseFill(200, 90, 300, 190, dx.ConvertRGB(RGB(100, 100, 100)));
            dx.PieFill(300, 90, 400, 190, 90, 300, 400, 190, dx.ConvertRGB(RGB(100, 100, 100)));
            dx.Flip();
            dest.x=-10;
            dest.y=-11;
            TRACE("Clipped, full size blits");
            dx.Blit(&dest, bitmap, 1);
            for ( i=0; i < 7; i++ )
                {
                //sprintf(temp,"i = %d, x = %d y = %d", i, dest.x, dest.y);
                //dx.DrawText(font, 10, 10, RGB(255, 0,0), 0, temp);
                dx.Line(10+i*10, 10, 200, 200, 10, RGB(255, 0,0));
                dx.Blit(&dest, bitmap);
                dest.x+=100;
                dest.y+=20;
                dx.Flip();
                getch();
                }

            r.left=0; // change to above zero to fix !!
            r.right=bitmap->bmRect.right - bitmap->bmRect.left ;
            r.top=0;  // change to above zero to fix !!
            r.bottom=bitmap->bmRect.bottom - bitmap->bmRect.top;
            for ( i=0; i < 7; i++ )
                {
                sprintf(temp,"i = %d, x = %d y = %d", i, r.left, r.top);
                //dx.DrawText(font, 10, 10, RGB(255, 0,0), 0, temp);
                dx.Blit(&r, bitmap, 0);
                r.left+=120;
                r.right+=150;
                r.top+=50;
                r.bottom+=80;
               
                TRACE("i = %d left %d right %d top %d bottom %d\n", i,
                    r.left,
                    r.right,
                    r.top,
                    r.bottom
                    );
                dx.Flip();
                getch();
                }

            }

        // draw all bitmaps full size
        dx.RectFill(NULL, 0);
        dx.Flip();
        dx.RectFill(NULL, 0);
        dx.DrawText(font, 0,0, RGB(255, 255, 255), 0, "Draw all bitmaps");
        dx.Flip();
        getch();

        dx.EnableClipping(0);
        j=dx.GetNumBitmaps();
        dest.x=0;
        dest.y=0;
        for ( i=0; i < j; i++ )
            {
            sprintf(temp, "Bitmap %d of %d", i+1, j);
            dx.RectFill(NULL, 0);
            dx.Blit(&dest, dx.GetBaseBitmap(i));
            dx.DrawText(font, 0, 300, RGB(255, 255, 255), 0, temp);
            dx.Flip();
            getch();
            }

        delete bitmap;
        delete bitmap2;
        delete topLeft;
        delete topRight;
        delete bottomLeft;
        delete bottomRight;
        delete centre;
        delete topLeft2;
        delete topRight2;
        delete bottomLeft2;
        delete bottomRight2;
        }

	return ret;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Canada Canada
Professional Programmer living in Beautiful Vancouver, BC, Canada.

Comments and Discussions