C++ DirectX

Hey everyone I'm new to the forums. But I'm trying to make an image move up and down on the screen slowly but I'm unsure how to do it, can anyone help me please? Thanks in Advance!
here's the code:
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
#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;

const string APPTITLE = "Game Loop";
HWND window;
HDC device;
bool gameover = false;


/**
 ** Loads and draws a bitmap from a file and then frees the memory
 **/
void DrawBitmap(char *filename, int x, int y)
{
    //load the bitmap image
    HBITMAP image = (HBITMAP)LoadImage(0,"c.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

    //read the bitmap's properties
    BITMAP bm;
    GetObject(image, sizeof(BITMAP), &bm);

    //create a device context for the bitmap
    HDC hdcImage = CreateCompatibleDC(device);
    SelectObject(hdcImage, image);

    //draw the bitmap to the window (bit block transfer)
    BitBlt( 
        device,                  //destination device context
        x, y,                    //x,y location on destination
        bm.bmWidth, bm.bmHeight, //width,height of source bitmap
        hdcImage,                  //source bitmap device context
        0, 0,                    //start x,y on source bitmap
        SRCCOPY);                //blit method

    //delete the device context and bitmap
    DeleteDC(hdcImage);
    DeleteObject((HBITMAP)image);
}

/**
 ** Startup and loading code goes here
 **/
bool Game_Init()
{
    //start up the random number generator
    srand(time(NULL));

    return 1;
}

/**
 ** Update function called from inside game loop
 **/
void Game_Run()
{
    if (gameover == true) return;

    //get the drawing surface
    RECT rect;
    GetClientRect(window, &rect);

    //draw bitmap at random location
	//int x = rand() % (rect.right - rect.left);
	//int y = rand() % (rect.bottom - rect.top);
    //DrawBitmap("c.bmp", x, y);

	//Draw Bitmap at given location
    int x = 100;
    int y = 100;
    DrawBitmap("c.bmp", x, y);
}

/**
 ** Shutdown code
 **/
void Game_End()
{
    //free the device
    ReleaseDC(window, device);
}

/**
 ** Window callback function
 **/
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
        case WM_DESTROY:
            gameover = true;
            PostQuitMessage(0);
            break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

/**
 ** MyRegiserClass function sets program window properties
 **/
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX); 

    //fill the struct with info
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC)WinProc;
    wc.cbClsExtra     = 0;
    wc.cbWndExtra     = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = APPTITLE.c_str();
    wc.hIconSm       = NULL;

    //set up the window with the class info
    return RegisterClassEx(&wc);
}

/**
 ** Helper function to create the window and refresh it
 **/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    //create a new window
    window = CreateWindow(
       APPTITLE.c_str(),              //window class
       APPTITLE.c_str(),              //title bar
       WS_OVERLAPPEDWINDOW,   //window style
       CW_USEDEFAULT,         //x position of window
       CW_USEDEFAULT,         //y position of window
       640,                   //width of the window
       480,                   //height of the window
       NULL,                  //parent window
       NULL,                  //menu
       hInstance,             //application instance
       NULL);                 //window parameters

    //was there an error creating the window?
    if (window == 0) return 0;

    //display the window
    ShowWindow(window, nCmdShow);
    UpdateWindow(window);

    //get device context for drawing
    device = GetDC(window);

    return 1;
}

/**
 ** Entry point function
 **/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;

    //create window
    MyRegisterClass(hInstance);
    if (!InitInstance (hInstance, nCmdShow)) return 0;

    //initialize the game
    if (!Game_Init()) return 0;

    // main message loop
    while (!gameover)
    {
        //process Windows events
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        //process game loop
        Game_Run();
    }

    //free game resources
    Game_End();

    return msg.wParam;
}
The topic is a bit misleading... I was expecting a DirectX question =P.

Anyway... personally I would recommend you avoid WinAPI for this and deal with a gaming lib (like SFML) instead. It makes this much simpler.

http://www.sfml-dev.org

Doing graphics (and particularly graphics suitable for a game) with WinGDI is painful. Very complex, and very slow.
I'm a little confused. I'm using Visual Studio 2008 with the DirectX SDK and am just trying to make the image(a picture) move up and down the screen that's all..
I'm guessing it has something to do with this part of the code to make it move up and down from the location its set to.
1
2
3
4
5
	//Draw Bitmap at given location
    int x = 100;
    int y = 100;
    DrawBitmap("c.bmp", x, y);
}
I'm using Visual Studio 2008 with the DirectX SDK


You're actually not using the DirectX SDK at all. Everything you're doing is with WinAPI.

What you're doing now is not suitable for a game. I really strongly recommend you stop and restart with a different API. If you want to go with DirectX, that's better, but really there are other libs that are much easier (and portable).

I already linked to SFML. I highly recommend you download and install that library and use it instead of what you're doing now.


WinAPI and especially DirectX are extremely convoluted. Look how it took you nearly 200 lines of code just to have a window with a picture in it. In other APIs this could be done in something like 20 lines.


I'm guessing it has something to do with this part of the code to make it move up and down from the location its set to.


You're correct. You're drawing the picture at the same location every time (100,100). If you want the picture to move, you need to draw it at different coords every time.

But note you're doing other things that are unoptimal. Such as loading the bitmap every time you want to draw it. Typically you load it once and keep it loaded until you're done with it. Loading an image is a very expensive operation.
Topic archived. No new replies allowed.