Jump to content
  • Advertisement
Sign in to follow this  
sas1992

DIRECTX HELP

This topic is 3490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

// DX1.cpp : 

#include <stdafx.h>


#include <windowsx.h>
#include <d3d9.h>
#include <string>
using namespace std;


// define the screen resolution and keyboard macros
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")

// global declarations
LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL;    // the pointer to the vertex buffer
LPDIRECT3DVERTEXBUFFER9 t_bufferc= NULL,t_bufferh=NULL;
VOID* pVoid;    // a void pointer

// function prototypes
void initD3D(HWND hWnd);    // sets up and initializes Direct3D
void render_frame(void);    // renders a single frame
void cleanD3D(void);    // closes Direct3D and releases memory
void init_graphics(void);    // 3D declarations

struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

CUSTOMVERTEX t_vert[] =
    {   //tri 1
        { 0.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//0
      { 26.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//1
      { 13.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//2
        //tri 2
      { 27.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//3
      { 53.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//4
      { 40.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//5
      //tri 3
      { 54.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//6
      { 80.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//7
      { 67.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//8
      //tri 4
      { 81.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//9
      { 107.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//10
      { 94.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//11
      //tri 5
      { 108.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//12
      { 134.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//13
      { 121.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//14
      //tri 6
      { 135.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//15
      { 161.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//16
      { 148.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//17
      //tri 7
      { 162.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//18
      { 188.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//19
      { 175.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//20
      //tri 8
      { 189.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//21
      { 215.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//22
      { 201.5f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//23
      //tri 9
      { 216.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//24
      { 242.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//25
      { 229.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//26
      //tri 10
      { 243.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//27
      { 269.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//28
      { 256.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//29
      //tri 11
      { 270.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//30
      { 296.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//31
      { 283.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//32
      //tri 12
      { 297.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//33
      { 323.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//34
      { 310.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//35
      //tri 13
      { 324.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//36
      { 350.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//37
      { 337.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//38
      //tri 14
      { 351.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//39
      { 377.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//40
      { 364.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//41
      //tri 15
      { 378.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//42
      { 404.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//43
      { 390.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//44
      //tri 16
      { 405.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//45
      { 431.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//46
      { 418.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//47
      //tri 17
      { 432.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//48
      { 458.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//49
      { 445.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//50
      //tri 18
      { 459.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//51
      { 485.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//52
      { 472.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//53
      //tri 19
      { 486.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//54
      { 512.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//55
      { 499.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//56
      //tri 20
      { 513.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//57
      { 539.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//58
      { 526.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//59
      //tri 21
      { 540.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//60
      { 566.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//61
      { 553.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//62
      //tri 22
      { 567.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//63
      { 593.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//64
      { 580.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//65
      //tri 23
      { 594.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//66
      { 620.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//67
      { 607.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//68
      //tri 24
      { 621.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },//69
      { 647.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },//70
      { 634.0f, 30.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },//71
};

CUSTOMVERTEX com_vert[] =
{   
        { 13.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//0
      { 13.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//1
      
      { 40.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//2
      { 40.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//3

      { 67.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//4
      { 67.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//5

      { 94.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//6
      { 94.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//7

      { 121.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//8
      { 121.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//9

      { 148.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//10
      { 148.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//11
      
      { 175.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//12
      { 175.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//13

      { 201.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//14
      { 201.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//15

      { 229.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//16
      { 229.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//17

      { 256.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//18
      { 256.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//19
      
      { 283.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//20
      { 283.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//21

      { 310.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//22
      { 310.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//23

      { 337.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//24
      { 337.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//25

      { 364.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//26
      { 364.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//27

      { 390.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//28
      { 390.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//29

      { 418.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//30
      { 418.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//31

      { 445.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//32
      { 445.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//33

      { 472.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//34
      { 472.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//35

      { 499.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//36
      { 499.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//37

      { 526.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//38
      { 526.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//39

      { 553.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//40
      { 553.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//41

      { 580.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//42
      { 580.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//43

      { 607.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//44
      { 607.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//45

      { 634.0f, 31.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//46
      { 634.0f, 51.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },//47

}; 
CUSTOMVERTEX hum_vert[]={

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//0    
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//1    

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//2    
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//3    

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//4    
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//5    

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//6
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//7

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//8
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//9

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//10
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//11

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//12
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//13

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//14
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//15

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//16
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//17

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//18
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//19

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//20
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//21

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//22
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//23

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//24
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//25

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//26
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//27

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//28
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//29

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//30
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//31

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//32
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//33

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//34
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//35

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//36
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//37

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//38
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//39

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//40
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//41

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//42
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//43

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//44
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//45

      { 0.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//46
      { 0.0f, 429.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(100, 100, 255), },//47

      { 304.0f, 479.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(1, 100, 255), },//48
      { 337.0f, 479.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(1, 100, 255), },//49
      { 320.0f, 449.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(1, 100, 255), },//50
};

int ctr=0;
int com_ammo[24]={0};
int com_stat[24]={0};
int hum_ammo[24]={0};
int hum_pos[24]={0};

void upgrade()
{
   for(int x=0;x<24;++x)
   {
      if(com_ammo[x]==1)
      {
         //com_vert[x*2].Y+=4.0f;
         //com_vert[(x*2)+1].Y+=4.0f;
      }
      if(hum_ammo[x]==1)
      {
         hum_vert[(x*2)].Y-=2.0f;
         hum_vert[(x*2)+1].Y-=2.0f;
      }
   }
}

void hum_shot()
{
      for(ctr=0;ctr<24;ctr++)
         if(hum_ammo[ctr]==0)
            break;

      hum_ammo[ctr]=1;
      hum_vert[(ctr*2)].X=320.0f;
      hum_vert[(ctr*2)+1].X=320.0f;
}

void com_shot()
{
      int s=0;
      for(int i=0;i<24;++i,++s)
                if(hum_pos==1)
                      break;
      int ctr=1;
      com_ammo[s]=1;
      for(int j=1;ctr<14;++j)               
      {             
                  if(s-j>-1)
                   {               
                     com_ammo[s-j]=1;
                      ctr++;
                   }
                  if(s+j<24)
                   {
                     com_ammo[s+j]=1;
                        ctr++;

                   }
      }
}

void clean()
{
   for(int x=0;x<24;++x)
   {
      if(com_vert[x*2].Y>480.0f)
      {
         com_vert[x*2].Y=31.0f;
         com_vert[(x*2)+1].Y=51.0f;
         com_ammo[x]=0;
      }
      if(hum_vert[x*2].Y<0.0f)
      {
         hum_vert[x*2].Y=449.0f;
         hum_vert[(x*2)+1].Y=429.0f;
         hum_ammo[x]=0;
      }
   }
}

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    wc.lpszClassName = "WindowClass";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          "WindowClass",
                          "Our Direct3D Program",
                          WS_EX_TOPMOST | WS_POPUP,    // fullscreen values
                          0, 0,    // the starting x and y positions should be 0
                          SCREEN_WIDTH, SCREEN_HEIGHT,    // set the window to 640 x 480
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D
    initD3D(hWnd);

    // enter the main loop:

    MSG msg;

    while(TRUE)
    {
        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
      hum_pos[0]=1;
      clean();
      com_shot();
        render_frame();
      upgrade();

        // check the 'escape' key
         if(KEY_DOWN(VK_ESCAPE))
            PostMessage(hWnd, WM_DESTROY, 0, 0);
       if(KEY_DOWN(VK_SPACE))
          hum_shot();
       if(KEY_DOWN(VK_RIGHT))
       {
          for(int x=48;x<51;++x)
            hum_vert[x].X+=2.0f;
       }
       
       if(KEY_DOWN(VK_LEFT))
       {
          for(int x=48;x<51;++x)
            hum_vert[x].X-=2.0f;
       }
        while ((GetTickCount() - starting_point) < 10);
      
    }

    // clean up DirectX and COM
    cleanD3D();

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

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


// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;

    // create a device class using this information and the info from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

   d3ddev->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
    init_graphics();    // call the function to initialize the graphics

    return;
}


// this is the function used to render a single frame
void render_frame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

   t_bufferc->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, com_vert, sizeof(com_vert));
    t_bufferc->Unlock();

   t_bufferh->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, hum_vert, sizeof(hum_vert));
    t_bufferh->Unlock();

   // select which vertex format we are using   
        d3ddev->SetFVF(CUSTOMFVF);

        // select the vertex buffer to display
        d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

        // copy the vertex buffer to the back buffer
      for(DWORD x=0;x<72;x=x+3)
         d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, x, 1);

      d3ddev->SetStreamSource(0, t_bufferc, 0, sizeof(CUSTOMVERTEX));

        // copy the vertex buffer to the back buffer
      for(DWORD y=0;y<24;++y)
         if(com_ammo[y]==1)
            d3ddev->DrawPrimitive(D3DPT_LINELIST, (y*2), 1);
      
      d3ddev->SetStreamSource(0, t_bufferh, 0, sizeof(CUSTOMVERTEX));
      
      d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 48, 1);
      
      for(DWORD z=0;z<24;++z)
         if(hum_ammo[z]==1)
            d3ddev->DrawPrimitive(D3DPT_LINELIST, (z*2), 1);

    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
}


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
    t_buffer->Release();    // close and release the vertex buffer
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D

    return;
}


// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
   

    // create a vertex buffer interface called t_buffer
    d3ddev->CreateVertexBuffer(sizeof(t_vert),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_buffer,
                               NULL);

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));
    t_buffer->Unlock();

    // create a vertex buffer interface called t_buffer
    d3ddev->CreateVertexBuffer(sizeof(com_vert),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_bufferc,
                               NULL);
   d3ddev->CreateVertexBuffer(sizeof(hum_vert),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_bufferh,
                               NULL);
    return;
}
Pls tell me why does bottom triangle disappear after pressin spacebar few times. Every help will be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
You based your code on DirectXTutorial.com code, didn't you? Please don't. It's a terrible site.

Problems I see with your code:
  • You don't check any return values anywhere. It's a wonder your app doesn't just crash as soon as you run it
  • You cast your WndProc function pointer when registering the window class - that's an unbelievably bad idea. If it doesn't compile without the cast, you have a serious problem.
  • You only grab one window message before rendering a frame, that'll mean your message queue will gradually fill up and cause lag and possibly loss of messages.
  • I'd be inclined to use the WM_KEYDOWN message in your window proc instead of using GetAsyncKeyState. It's cleaner, and avoids some "interesting" bugs with GetAsyncKeyState().
  • PostMessage(WM_DESTROY) is not the correct way to destroy a window - that's completely undefined behaviour, since WM_DESTROY is only valid to be sent by the OS. You want DestroyWindow() or PostQuitMessage().
  • A busy while() loop like that is a great way to chew up 100% of the CPU time doing nothing. And you have a 60% margin of error there - GetTickCount() is only accurate to ~16ms.
  • Drawing one triangle several times is incredibly inefficient. Draw them all in one call instead of one at a time in 24 goes.
  • Locking a static vertex buffer every frame has a severe performance penalty associated with it, and locking two is even worse. Either create the buffer as dynamic, or don't lock it every frame.
  • Your vertex buffers contain a single vertex in them, so you have a rather huge buffer overflow. Again, it's a wonder your app doesn't crash instantly. I can also tell you're not using the debug runtimes, because they wouldn't let you get away with such a massive bug.

    As for the actual problem, I'd guess it's because you're writing into random memory with your Lock() calls, or you're moving the vertices offscreen or in some way to form an invalid triangle. Any output from the debug runtimes? When you look at the vertex arrays in the debugger, do they all look valid?

    Share this post


    Link to post
    Share on other sites
    Pls tell me how to use dynamic buffers and using up the CPU time whch is wasted doing nothing and how to handle more messages in one iteration.


    Also reccommend a good learning site.

    Thanks for your help.

    Share this post


    Link to post
    Share on other sites
    Hi!

    You might find the Tutorials and Samples from the DirectX SDK interesting. They should be quite state of the art. I dont know if they are really suitable for beginners. I guess the Win API programming part is a bit short, but from my point the turorials should be sufficient. I usually suggest them to people who ask me. What do you (as beginner) or other people think about that?

    All the samples and tutorial have a main loop that processes the window messages.

    In the SDK there is also an article called "Performance Optimization" there is a section "Using Dynamic Vertex and Index Buffers", I dont wanna summarize it now, you best read it, try it yourself, and then ask questions ;)

    greets, Lui

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by sas1992
    Pls tell me how to use dynamic buffers and using up the CPU time whch is wasted doing nothing and how to handle more messages in one iteration.


    Also reccommend a good learning site.

    Thanks for your help.

    1. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) should be while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)), and you'll need to change your WM_QUIT handler slightly to break out of both loops.
    2. while ((GetTickCount() - starting_point) < 10); should be while ((GetTickCount() - starting_point) < 10) Sleep(1); or similar, but that won't solve your precision issue. You'll need to use another timer like timeGetTime() if you want any sort of accuracy.
    3.As Lui said.

    For learning sites, take a look at the forum FAQ. I'm also in the process of writing up some tutorials, but they're nowhere near complete (There's currently One in my Journal covering initial setup code).

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!