回 帖 发 新 帖 刷新版面

主题:请各位帮我看看那里错了  谢谢了。

我是自学的  太痛苦了  瞪了半天 没看出原因..请各位高手帮忙看看..
运行的时候 出现0x00401520 引用 的0x00000000 不能为read
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <math.h>
#include "my.h"
#include "F:\SDK\DXF\DXSDK\include\ddraw.h"



#define SCREEN_WIDE 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 8
#define WINDOW_NAME "WINDOWCLASS1"

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000)?1:0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000)?0:1)

typedef struct VERTEX2DF_TYP
{
    float x;
    float y;
}VERTEX2DF,*VERTEX2DF_PTR;

typedef struct POLYGON2D_TYP
{
    int state;
    int num_vertice;
    int x0;
    int y0;
    int vx;
    int vy;
    VERTEX2DF *vlist;
}POLYGON2D,*POLYGON2D_PTR;

float sin_look[360];
float cos_look[360];
HWND window_main_handle=NULL;
POLYGON2D polygon2d;


LPDIRECTDRAW7 lpdd7=NULL;
LPDIRECTDRAWSURFACE7 lpddsprimary=NULL;
LPDIRECTDRAWSURFACE7 lpddsback=NULL;
DDSURFACEDESC2 ddsd;
PALETTEENTRY palette[256];
LPDIRECTDRAWPALETTE lppalette=NULL;
DDBLTFX ddbltfx;

////////////////////////////////////////////////////////////////////
int Draw_polygon(POLYGON2D_PTR polygon,DWORD color,unsigned char *dest_buff,int lpitch)
{
    int index;

    if(polygon->state)
    {
        for(index=0;index<polygon->num_vertice-1;index++)
        {
            Draw_Clip_Line(polygon->x0+polygon->vlist[index].x,
                            polygon->y0+polygon->vlist[index].y,
                            polygon->x0+polygon->vlist[index+1].x,
                            polygon->y0+polygon->vlist[index+1].y,
                            color,
                            dest_buff,
                            lpitch);
        }
        
        Draw_Clip_Line(polygon->x0+polygon->vlist[0].x,
                        polygon->y0+polygon->vlist[0].y,
                        polygon->x0+polygon->vlist[index].x,
                        polygon->y0+polygon->vlist[index].y,
                        color,
                        dest_buff,
                        lpitch);

        return 1;
    }
    return 0;
}
///////////////////////////////////////////////////////////////////////
int Polygon_Move(POLYGON2D_PTR polygon,int dx,int dy)
{
    if(polygon->state)
    {
        for(int index=0;index<polygon->num_vertice;index++)
        {
            polygon->vlist[index].x+=dx;
            polygon->vlist[index].y+=dy;
        }
        return 1;
    }
    return 0;
}
//////////////////////////////////////////////////////////////////////
int Polygon_Retroflexion(POLYGON2D_PTR polygon,int angle)
{
    double xr,yr;
    if(polygon->state)
    {
        for(int index=0;index<polygon->num_vertice;index++)
        {
            xr=polygon->vlist[index].x*cos_look[angle]-polygon->vlist[index].y*sin_look[angle];
            yr=polygon->vlist[index].x*sin_look[angle]+polygon->vlist[index].y*cos_look[angle];

            polygon->vlist[index].x=xr;
            polygon->vlist[index].y=yr;
        }
        return 1;
    }
    return 0;
}
/////////////////////////////////////////////////////////////////////
int Polygon_Enlarge(POLYGON2D_PTR polygon,float multiple)
{
    if(polygon->state)
    {
        for(int index=0;index<polygon->num_vertice;index++)
        {
            polygon->vlist[index].x*=multiple;
            polygon->vlist[index].y*=multiple;
        }
        return 1;
    }
    return 0;
}
//////////////////////////////////////////////////////////////////
int DDraw_Blt(LPDIRECTDRAWSURFACE7 lpdds,int color)
{

    memset(&ddbltfx,0,sizeof(ddbltfx));
    ddbltfx.dwSize=sizeof(ddbltfx);
    

    ddbltfx.dwFillColor = color; 
    

    lpdds->Blt(NULL,      
        NULL,               
        NULL,      
        DDBLT_COLORFILL | DDBLT_WAIT,                  
        &ddbltfx); 
    

    return(1);
}
/////////////////////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;

    switch(uMsg)
    {
    case WM_CREATE:
        {
            return 0;
        }break;

    case WM_PAINT:
        {
            hdc=BeginPaint(hwnd,&ps);
            EndPaint(hwnd,&ps);
            return 0;
        }break;

    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }break;

        default:break;
    }

    return (DefWindowProc(hwnd,uMsg,wParam,lParam));
}
/////////////////////////////////////////////////////////////////////////
int game_init(void *parm=NULL,int num_parms=0)
{
    if (FAILED(DirectDrawCreateEx(NULL,(void **)&lpdd7, IID_IDirectDraw7, NULL)))
        return(0);

    if(FALSE(lpdd7->SetCooperativeLevel(window_main_handle,DDSCL_ALLOWREBOOT|DDSCL_EXCLUSIVE|
                                        DDSCL_FULLSCREEN|DDSCL_ALLOWMODEX)))
        return 0;

    if(FAILED(lpdd7->SetDisplayMode(SCREEN_WIDE,
                                    SCREEN_HEIGHT,
                                    SCREEN_BPP,
                                    0,0)))
        return 0;

    memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize=sizeof(ddsd);
    ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps=DDSCAPS_COMPLEX|DDSCAPS_FLIP|DDSCAPS_PRIMARYSURFACE;
    ddsd.dwBackBufferCount=1;

    if(FAILED(lpdd7->CreateSurface(&ddsd,&lpddsprimary,NULL)))
        return 0;

    ddsd.ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;
    
    if(FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsback)))
        return 0;
    

    for(int color=1;color<255;color++)
    {
        palette[color].peRed=rand()%256;
        palette[color].peGreen=rand()%256;
        palette[color].peBlue=rand()%256;
        palette[color].peFlags=PC_NOCOLLAPSE;
    }

    palette[0].peRed=0;
    palette[0].peGreen=0;
    palette[0].peBlue=0;
    palette[0].peFlags=PC_NOCOLLAPSE;

    palette[255].peRed=255;
    palette[255].peGreen=255;
    palette[255].peBlue=255;
    palette[255].peFlags=PC_NOCOLLAPSE;

    if(FAILED(lpdd7->CreatePalette(DDPCAPS_8BIT|DDPCAPS_ALLOW256,
                                    palette,
                                    &lppalette,
                                    NULL)))
        return 0;

    if(FAILED(lpddsprimary->SetPalette(lppalette)))
        return 0;

    DDraw_Blt(lpddsprimary,0);
    DDraw_Blt(lpddsback,0);



    VERTEX2DF vertex[8]={33,-3, 9,-18, -12,-9, -21,-12, -9,6, -15,15, -3,27, 21,21};

    polygon2d.num_vertice=8;
    polygon2d.state=1;
    polygon2d.vx=0;
    polygon2d.vy=0;
    polygon2d.x0=SCREEN_WIDE/2;
    polygon2d.y0=SCREEN_HEIGHT/2;
    polygon2d.vlist=(VERTEX2DF_PTR)malloc(10*sizeof(VERTEX2DF));

    for(int index=0;index<8;index++)
    {
        polygon2d.vlist[index]=vertex[index];
    }
    
    float temp;

    for(int ang=0;ang<360;ang++)
    {
        temp=(float)ang*3.141592653/(float)180;
        
        sin_look[ang]=sin(temp);
        cos_look[ang]=cos(temp);
    }
    return 1;
}

int game_main(void *parm=NULL,int num_parms=0)
{
    if(KEYDOWN(VK_ESCAPE))
    {
        SendMessage(window_main_handle,WM_CLOSE,0,0);
        return 0;
    }

    DDraw_Blt(lpddsback,0);
    

    if(lpddsback->Lock(NULL,
                       &ddsd,
                       DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT,
                       NULL))
        return 0;

    if(KEYDOWN('A'))
    {
        Polygon_Enlarge(&polygon2d,1.1);
    }
    if(KEYDOWN('S'))
    {
        Polygon_Enlarge(&polygon2d,0.9);
    }
    
    Polygon_Retroflexion(&polygon2d,10);

    Draw_polygon(&polygon2d,255,(unsigned char *)ddsd.lpSurface,ddsd.lPitch);
    
    if(FAILED(lpddsback->Unlock(0)))
        return 0;

    while(FAILED(lpddsprimary->Flip(NULL,DDFLIP_WAIT)));

    Sleep(33);

    return 1;


}

int game_down(void *parm=NULL,int parms=0)
{
    
    if(lpddsback)
    {
        lpddsback->Release();
        lpddsback=NULL;
    }
    if(lppalette)
    {
        lppalette->Release();
        lppalette=NULL;
    }
    if(lpddsprimary)
    {
        lpddsprimary->Release();
        lpddsprimary=NULL;
    }
    if(lpdd7)
    {
        lpdd7->Release();
        lpdd7=NULL;
    }
    
    return 1;
}
////////////////////////////////////////////////////////////////    
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
    WNDCLASSEX winclass;

    winclass.cbClsExtra=0;
    winclass.cbSize=sizeof(WNDCLASSEX);
    winclass.cbWndExtra=0;
    winclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    winclass.hCursor=LoadCursor(hInstance,"wincur");
    winclass.hIcon=LoadIcon(hInstance,"winico");
    winclass.hIconSm=LoadIcon(hInstance,"winico");
    winclass.hInstance=hInstance;
    winclass.lpfnWndProc=WindowProc;
    winclass.lpszClassName=WINDOW_NAME;
    winclass.lpszMenuName=0;
    winclass.style=CS_DBLCLKS|CS_HREDRAW|CS_OWNDC|CS_VREDRAW;

    if(!(RegisterClassEx(&winclass)))
        return 0;
    HWND hwnd;
    if(!(hwnd=CreateWindowEx(NULL,
                             WINDOW_NAME,
                             "my window",
                             WS_VISIBLE|WS_POPUP,
                             0,0,
                             SCREEN_WIDE,SCREEN_HEIGHT,
                             0,0,
                             hInstance,
                             0)))
        return 0;

    window_main_handle=hwnd;

    MSG msg;

    game_init();

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

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        game_main();
    }
    game_down();

    return (msg.wParam);
}

回复列表 (共2个回复)

沙发

看都看晕了,直接编译这段代码也没意义,根据报错,应该是有指针访问错误,检查一下有没有用到野指针,未正确初始化的指针,以及指针访问越界的情况吧

板凳

谢谢  已经解决了

我来回复

您尚未登录,请登录后再回复。点此登录或注册