由于这个问题积累了好长时间 ,
也是我最希望得到各位帮助的一个问题:
我相信解决问题对我们都是提高!
为方便大家共同解决 我也尽力:
1.把代码压缩到最短
2. 附带源文件(春节假期不能上传资源 如果问题没解决 我会在2月1之后上传)
3. 把编译成功的发上去 问题出现的地方(加上那句include之后编译不过去)注释上去
出现的问题是我经典的error LNK2005 ~~
由于我学c++是非专业途径 代码也比较非专业 不好意思 呵呵 (关于vc编译的书像“模式”那种太深 看不懂 而对于业余 新手的教材少之又少)
我想求有经验的同志帮我解决这个老问题 感激不尽T ~ T
以下是四个源文件(可以编译通过 ,但加了head2.h里面那一句就编译不过去了):
1.cpp:
#include "Head2.h"
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <dinput.h>//dinput
#include <fstream>//导入位图
#include <cmath>//for HSI
DO::I_Will_Do()
{
}
2.cpp:
#include "Head1.h"
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <dinput.h>//dinput
#include <fstream>//导入位图
#include <cmath>//for HSI
using namespace std;
void FLYsh::DDRAW_INIT_STRUCT(DDSURFACEDESC2 & ddstruct)
{
memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct);
}
FLYsh::FLYsh()
{
lpdd_ddraw_1234567=NULL;
lpdd_surface_1234567=NULL;
//
Next_screen=new RGB*[SCREEN_WIDTH];
for (int sinit=0 ; sinit<SCREEN_WIDTH ; sinit++)
{
Next_screen[sinit]= new RGB[SCREEN_HEIGHT];
}
should_close_all=false;
}
void FLYsh::FLYsh_Init()
{
DirectDrawCreateEx(NULL, (void **)&lpdd_ddraw_1234567, IID_IDirectDraw7, NULL);
lpdd_ddraw_1234567->SetCooperativeLevel(main_window_handle,
DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
lpdd_ddraw_1234567->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0);
DDRAW_INIT_STRUCT(ddsd_surface_desc_12);
ddsd_surface_desc_12.dwFlags = DDSD_CAPS;
ddsd_surface_desc_12.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
lpdd_ddraw_1234567->CreateSurface(&ddsd_surface_desc_12, &lpdd_surface_1234567, NULL);
/////////////
//
DirectInput8Create (WinMain_hinstance,DIRECTINPUT_VERSION,IID_IDirectInput8, (void **)&lpdi,NULL);//dinput
lpdi->CreateDevice(GUID_SysKeyboard,&lpdikey, NULL);//dinput
lpdikey->SetCooperativeLevel(main_window_handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);//dinput
lpdikey->SetDataFormat(&c_dfDIKeyboard);//dinput
lpdikey->Acquire();//dinput
//
for(int n1=0;n1<SCREEN_WIDTH;n1++)
{
for(int n2=0;n2<SCREEN_HEIGHT;n2++)
{
Next_screen[n1][n2].b=0;
Next_screen[n1][n2].a=0;
Next_screen[n1][n2].g=0;
Next_screen[n1][n2].r=0;
}
}
////
}
void FLYsh::FLYsh_Main()
{
if(should_close_all){SendMessage(main_window_handle,WM_CLOSE,0,0);}
if(should_close_all){return;}
/////////////////////////////////////////////
///////////////onEnterFrame//////////////////////////
if (KEYDOWN(VK_ESCAPE))
{
should_close_all=true;
}
Next_screen[100][50].r=255;
Next_screen[100][50].g=255;
Next_screen[100][50].b=255;
///////////////onEnterFrame//////////////////////////
/////////////////////////////////////////////
DDRAW_INIT_STRUCT(ddsd_surface_desc_12); //清内存
if (FAILED(lpdd_surface_1234567->Lock(NULL, &ddsd_surface_desc_12,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL)))//锁
{return;} // end if
unsigned int *This_screen=(unsigned int *)ddsd_surface_desc_12.lpSurface;
int lpitch32=(int)(ddsd_surface_desc_12.lPitch >> 2);
for (int y=0;y< SCREEN_HEIGHT;y++)
{
for(int x=0;x<SCREEN_WIDTH ;x++)
{
unsigned int pixel =(Next_screen[x][y].b)+(Next_screen[x][y].g<<8)+(Next_screen[x][y].r<<16)+(Next_screen[x][y].a<<24);
This_screen[x + y*lpitch32] = pixel;
}
}
if (FAILED(lpdd_surface_1234567->Unlock(NULL)))//锁
{return ;}
lpdikey->GetDeviceState(256, (LPVOID)keyboard_state);//dinput
} // end_Main
void FLYsh::FLYsh_Shutdown()
{
lpdd_surface_1234567->Release();
lpdd_surface_1234567 = NULL;
//
lpdd_ddraw_1234567->Release();
lpdd_ddraw_1234567 = NULL;
//以相反顺序销毁dx对象
//
for (int delS = 0; delS <SCREEN_WIDTH ; delS++)
{
delete [] Next_screen[delS];
Next_screen[delS]=NULL;
}
delete [] Next_screen;
Next_screen=NULL;
//
lpdikey->Unacquire();//dinput
lpdikey->Release();//dinput
lpdi->Release();//dinput
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
int WINAPI WinMain( HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // graphics device context
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "WINCLASS1";
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
WinMain_hinstance = hinstance;
if (!RegisterClassEx(&winclass))
return(0);
if (!(hwnd = CreateWindowEx(NULL,"WINCLASS1","FLYsh_stereo",WS_POPUP|WS_VISIBLE,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,NULL,NULL,hinstance,NULL)))
return(0);
main_window_handle = hwnd;// save main window handle
mygame1.FLYsh_Init();// (入口1)
while(TRUE)
{if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
if (msg.message == WM_QUIT)
{break;}
TranslateMessage(&msg);
DispatchMessage(&msg);
} // end if
mygame1.FLYsh_Main(); //(入口2)
} // end while
mygame1.FLYsh_Shutdown();// (入口3)
return(msg.wParam);
} // end WinMain
/////////////////////////////////////WindowProc///////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
char buffer[80]; // used to print strings
switch(msg){
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;
} // end switch
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
////////////////////////
2 个解决方案
#1
head1.h:
#ifndef __head_H__
#define __head_H__
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#define INITGUID // make sure directX guids are included
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <dinput.h>//dinput
#include <fstream>//导入位图
#include <cmath>//for HSI
HWND main_window_handle = NULL; // 只用于win main 和game class的沟通
HINSTANCE WinMain_hinstance = NULL; //用于win main 和WindowProc的沟通 和game class(input)的沟通
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);//提前声明WindowProc
const short SCREEN_WIDTH=1000 ;// size of screen
const short SCREEN_HEIGHT =800;
const short SCREEN_BPP =32 ;// bits per pixel
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
class FLYsh
{
private:
LPDIRECTDRAW7 lpdd_ddraw_1234567;//即 lpdd
LPDIRECTDRAWSURFACE7 lpdd_surface_1234567; //即 lpddsprimary
DDSURFACEDESC2 ddsd_surface_desc_12; // 即ddsd
DDSCAPS2 ddscaps; // 即ddscaps
bool should_close_all;
LPDIRECTINPUT8 lpdi;//dinput
LPDIRECTINPUTDEVICE8 lpdikey;//dinput
unsigned char keyboard_state[256];//dinput
public:
struct RGB
{
int r,g,b,a;
};
RGB **Next_screen;
void DDRAW_INIT_STRUCT(DDSURFACEDESC2 & ddstruct);
FLYsh();
void FLYsh_Init();
void FLYsh_Main();
void FLYsh_Shutdown();
}mygame1;
#endif
Head2.h:
#ifndef __head_2__
#define __head_2__
//#include "Head1.h" //就是这一句 加了他就报错:error LNK2005 为什么?谢谢大家帮忙!
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <dinput.h>//dinput
#include <fstream>//导入位图
#include <cmath>//for HSI
class DO
{
public:
I_Will_Do();
};
#endif
我想求有经验的同志帮我解决这个老问题 感激不尽T ~ T
以上是四个源文件(可以编译通过 ,但加了head2.h里面那一句就编译不过去了)
#2
如果朋友们想试着编译 别忘了下载dx sdk
link这些: ddraw.lib dxguid.lib dinput.lib dinput8.lib winmm.lib
我知道阅读代码很烦 谢谢各位!
link这些: ddraw.lib dxguid.lib dinput.lib dinput8.lib winmm.lib
我知道阅读代码很烦 谢谢各位!
#1
head1.h:
#ifndef __head_H__
#define __head_H__
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#define INITGUID // make sure directX guids are included
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <dinput.h>//dinput
#include <fstream>//导入位图
#include <cmath>//for HSI
HWND main_window_handle = NULL; // 只用于win main 和game class的沟通
HINSTANCE WinMain_hinstance = NULL; //用于win main 和WindowProc的沟通 和game class(input)的沟通
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);//提前声明WindowProc
const short SCREEN_WIDTH=1000 ;// size of screen
const short SCREEN_HEIGHT =800;
const short SCREEN_BPP =32 ;// bits per pixel
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
class FLYsh
{
private:
LPDIRECTDRAW7 lpdd_ddraw_1234567;//即 lpdd
LPDIRECTDRAWSURFACE7 lpdd_surface_1234567; //即 lpddsprimary
DDSURFACEDESC2 ddsd_surface_desc_12; // 即ddsd
DDSCAPS2 ddscaps; // 即ddscaps
bool should_close_all;
LPDIRECTINPUT8 lpdi;//dinput
LPDIRECTINPUTDEVICE8 lpdikey;//dinput
unsigned char keyboard_state[256];//dinput
public:
struct RGB
{
int r,g,b,a;
};
RGB **Next_screen;
void DDRAW_INIT_STRUCT(DDSURFACEDESC2 & ddstruct);
FLYsh();
void FLYsh_Init();
void FLYsh_Main();
void FLYsh_Shutdown();
}mygame1;
#endif
Head2.h:
#ifndef __head_2__
#define __head_2__
//#include "Head1.h" //就是这一句 加了他就报错:error LNK2005 为什么?谢谢大家帮忙!
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <dinput.h>//dinput
#include <fstream>//导入位图
#include <cmath>//for HSI
class DO
{
public:
I_Will_Do();
};
#endif
我想求有经验的同志帮我解决这个老问题 感激不尽T ~ T
以上是四个源文件(可以编译通过 ,但加了head2.h里面那一句就编译不过去了)
#2
如果朋友们想试着编译 别忘了下载dx sdk
link这些: ddraw.lib dxguid.lib dinput.lib dinput8.lib winmm.lib
我知道阅读代码很烦 谢谢各位!
link这些: ddraw.lib dxguid.lib dinput.lib dinput8.lib winmm.lib
我知道阅读代码很烦 谢谢各位!