谢天谢地 总算可以问问题了

ga6840 2008-08-19 09:03:37
 
BMPFileHeader file;
du_ru.read((char*)&file,sizeof(BMPFileHeader));
cout<<file.type<<endl;

BMPFileHeader *file;
du_ru.read((char*)file,sizeof(BMPFileHeader));
cout<<(*file).type<<endl;


请问第二个为什么编译不过去啊
应该是一样的啊
(这个问题的名字叫问题1)

typedef struct
{
short type;
unsigned char bfSize[4];//unsigned int bfSize???
short bfReserved1;
short bfReserved2;
unsigned short offset;
short bi;//???
}BMPFileHeader;

还有还有~
上面的位图头 把unsigned char bfSize[4];改为unsigned int bfSize为啥就不行了呢?? (这个问题的名字叫问题2)
 
typedef struct{
short type;//2byte
unsigned int fSize;//4byte
short reserved1;//2byte
short reserved2;//2byte
int offset;//4byte
}BMPFileHeader; //++

非常之奇怪啊 我参考的写位图文件(include fstream)写位图他用的是上面的头结构
而读位图用的是上上面那个头结构(换了 之后读数就不对了)
写的时候为什么多了个 short bi ??(这个问题的名字叫问题3)


/*#include<iostream>
using namespace std;
bool momo=true;
void main()
{
int a;
if(momo)
{
a=88;
}
cout<<a<<endl;
}*/
//为什么上面的可以而以下的编译不行? (这个问题的名字叫问题4)
#include<iostream>
using namespace std;
bool momo=true;
void main()
{
if(momo)
{
int a=88;
}
cout<<a<<endl;
}



我还想问~~~~~~~
学生问题很多的


我不知道为什么附件传不上来
我就把问题描述一下 希望好心人能帮帮我
我写了个dx的接口类 编译后无论画点还是导入位图 图像会闪烁?而~~~~且位图导入失真,颜色有些改变?(这个问题的名字最好听 叫问题5)
我会把源码复制再发一个帖子(字数限制)


各位高人 我把全部希望都寄托到这了
你能回答一个就回答一个
不管哪个
我都会格外感激 大家辛苦了

...全文
129 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
ga6840 2008-08-20
  • 打赏
  • 举报
回复
还有:


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 = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hinstance_app = hinstance;
if (!RegisterClassEx(&winclass))
return(0);
if (!(hwnd = CreateWindowEx(NULL,WINDOW_CLASS_NAME,"FLYsh_game",WS_POPUP|WS_VISIBLE,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,NULL,NULL,hinstance,NULL)))
return(0);////////////////////////////////////////// ^----FLYsh_game(′°?úname)
main_window_handle = hwnd;// save main window handle
FLYsh mygame1(main_window_handle);
mygame1.FLYsh_Init();// initialize game here (ó??·è??ú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(); // main game processing goes here (ó??·è??ú2)
} // end while
mygame1.FLYsh_Shutdown();// closedown game here (ó??·è??ú3)
return(msg.wParam);
} // end WinMain
/////////////////////////////////////WindowProc///////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
省略~
} // end WinProc
/////////////////////////////////////////////A . I/////////////////////////////////////////////////
void FLYsh::FLYsh_onEnterFrame()
{




static bool is_firs_run=true;


//FLYsh_rectangle(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,255,255,255);


if(is_firs_run)
{
for (int j=0;j<100;j++)
{
FLYsh_plot(156,200,70,500+j,500+j);
}
FLYsh_load_bitmap();


is_firs_run=false;
}

if (KEYDOWN(VK_ESCAPE)){
should_close_all=true;
}
}



ga6840 2008-08-20
  • 打赏
  • 举报
回复
是啊 我把第五个问题 就回在这啦:
这是那个源代码



#define WIN32_LEAN_AND_MEAN // just say no to MFC
#define WINDOW_CLASS_NAME "WINCLASS1"// defines for windows
#define INITGUID // make sure directX guids are included
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <fstream>
using namespace std;//òò?a°üo?á?<fstream>?a?ùμ?"á÷?a"
HWND main_window_handle = NULL; // ??ó?óúwin main oígame classμ?1μí¨
HINSTANCE hinstance_app = NULL; // ??ó?óúwin main oíWindowProcμ?1μí¨
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);//ìá?°éù?÷WindowProc
#define SCREEN_WIDTH 1024 // size of screen
#define SCREEN_HEIGHT 768
#define SCREEN_BPP 16 // bits per pixel
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))

// this builds a 16 bit color value in 5.6.5 format (green dominate mode)
#define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
#define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }
////////////
class FLYsh
{
private:
LPDIRECTDRAW7 lpdd_ddraw_1234567;//?′ lpdd
LPDIRECTDRAWSURFACE7 lpdd_surface_1234567; //?′ lpddsprimary

DDSURFACEDESC2 ddsd_surface_desc_12; // ?′ddsd
DDSCAPS2 ddscaps; // ?′ddscaps
LPDIRECTDRAWSURFACE7 lpddsback; //oó±??o3? ó?êéí???
HWND main_window_handle;//dxμ?ó??·ààó?windows????μ??¨ò?1μí¨
bool should_close_all;//1?±?è?2?μ???±£??
void FLYsh_onEnterFrame();//?òμ?AI?ó?ú
DDBLTFX blt; //?′ddbltfx?′??D??÷


BITMAPFILEHEADER strHead;
BITMAPINFOHEADER strInfo;
char * Raster;//?áè?bitmap?ó
int height_s_abs,Width,BytesPerRow;//?a?áè?bitmap

public:
FLYsh(HWND &window_handle_t)
{ lpdd_ddraw_1234567=NULL;
lpdd_surface_1234567=NULL;
main_window_handle=window_handle_t;
lpddsback=NULL;
should_close_all=false;
}
void 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_BACKBUFFERCOUNT;
ddsd_surface_desc_12.dwBackBufferCount =1;
ddsd_surface_desc_12.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
lpdd_ddraw_1234567->CreateSurface(&ddsd_surface_desc_12, &lpdd_surface_1234567, NULL);
ddsd_surface_desc_12.ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;
lpdd_surface_1234567->GetAttachedSurface(&ddsd_surface_desc_12.ddsCaps,&lpddsback);
}

void FLYsh_Shutdown()
{
lpdd_surface_1234567->Release();
lpdd_surface_1234567 = NULL;

lpdd_ddraw_1234567->Release();
lpdd_ddraw_1234567 = NULL;

}

void FLYsh_Main()
{

if(should_close_all){SendMessage(main_window_handle,WM_CLOSE,0,0);}
if(should_close_all){return;}

memset(&blt,0,sizeof(blt));
blt.dwSize = sizeof(blt);
DDRAW_INIT_STRUCT(ddsd_surface_desc_12);
if (FAILED(lpddsback->Lock(NULL, &ddsd_surface_desc_12,
DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
NULL)))
{return;}
FLYsh_onEnterFrame();
if (FAILED(lpddsback->Unlock(NULL)))
{return ;}

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

Sleep(30);
//*/

} // end Game_Main
inline void FLYsh_plot(int b,int g,int r,int x,int y)
{
if(x>0 && x<SCREEN_WIDTH && y>0 && y< SCREEN_HEIGHT)
{
unsigned short *video_buffer=(unsigned short *)ddsd_surface_desc_12.lpSurface;
int lpitch16=(int)(ddsd_surface_desc_12.lPitch >> 1);
unsigned short pixel = _RGB16BIT565(r,g,b);
video_buffer[x + y*lpitch16] = pixel;
}
}
void FLYsh_load_bitmap()
{
ifstream infile;
infile.open("bitmap24.bmp",ios::in|ios::binary);

infile.read((char *)&strHead,sizeof(strHead));
infile.read((char *)&strInfo,sizeof(strInfo));

height_s_abs =(strInfo.biHeight>0) ? strInfo.biHeight : -strInfo.biHeight;
Width=strInfo.biWidth;

BytesPerRow = Width * strInfo.biBitCount / 8;
BytesPerRow += (4-BytesPerRow%4) % 4;

infile.seekg (strHead.bfOffBits,ios::beg);

Raster= new char[BytesPerRow*height_s_abs];

if (strInfo.biHeight>0)
{
for (int n=height_s_abs-1;n>=0;n--)
infile.read (Raster+BytesPerRow*n,BytesPerRow);
}else
{
infile.read (Raster,BytesPerRow*height_s_abs);
}

infile.close();

for (int index_y = 0; index_y < height_s_abs +1; index_y++)
{
for (int index_x = 0; index_x < Width +1; index_x++)
{

char blue = (Raster[index_y*Width*3 + index_x*3 + 0]) >> 3,/////////////////////////// u char
green = (Raster[index_y*Width*3 + index_x*3 + 1]) >> 3,
red = (Raster[index_y*Width*3 + index_x*3 + 2]) >> 3;

unsigned int pixel = _RGB16BIT565(red,green,blue);//_RGB16BIT555
unsigned short *video_buffer=(unsigned short *)ddsd_surface_desc_12.lpSurface;

video_buffer[index_x + (index_y*ddsd_surface_desc_12.lPitch >> 1)] = pixel;



} // end for index_x
} // end for index_y

}//end for FLYsh_load_bitmap()


};



ga6840 2008-08-20
  • 打赏
  • 举报
回复
我用的是 dx7sdk 只用fstream逐字节倒入位图 我没用双缓存 我用windows游戏编程大师技巧 推荐的后备缓冲

另外 第一个问题不是括号的问题 那个括号时发帖时改的
这是我截的一部分:

 
#include<fstream>
#include<iostream>
using namespace std;
typedef struct
{
short type;
unsigned char bfSize[4];
short bfReserved1;
short bfReserved2;
unsigned short offset;
short bi;
}BMPFileHeader;

void main()
{
char b_nam[25];
cin.getline(b_nam,25);
ifstream du_ru(b_nam);
if(!du_ru)
return;
BMPFileHeader *file;
du_ru.read((char*)file,sizeof(BMPFileHeader));


du_ru.close();
cout<<"type:"<<(*file).type<<endl;
cout<<"bfSize:"<<(*file).bfSize<<endl;
cout<<"bfReserved1:"<<(*file).bfReserved1<<endl;
cout<<"bfReserved2:"<<(*file).bfReserved2<<endl;
cout<<"offset:"<<(*file).offset<<endl;
cout<<"bi:"<<(*file).bi<<endl;
//
cout<<endl;
}


执行时会发送错误报告
我也不知道为啥
而改为第一种就行了
ga6840 2008-08-20
  • 打赏
  • 举报
回复
freshui
说的对 但你可不可以告诉我
unsigned char bfSize[4]
为什么
占6个字节, 每个char不是一节马?
加起来是4节 阿?

谢谢啦
e_sharp 2008-08-20
  • 打赏
  • 举报
回复
路过,看题目以为散分贴呢
太乙 2008-08-20
  • 打赏
  • 举报
回复
5嘛:

闪烁?lz用的什么画图?怎么导的位图?

如果是mfc的话,画图用了双缓存了没?导入的图是多少色的?16的?
太乙 2008-08-20
  • 打赏
  • 举报
回复
问题四:不用我说了吧?
太乙 2008-08-20
  • 打赏
  • 举报
回复
问题3应该是字对齐~
太乙 2008-08-20
  • 打赏
  • 举报
回复
问题2:

什么不行??

编译通不过么?

我想应该是用的时候出错吧,不怪struct的定义~
太乙 2008-08-20
  • 打赏
  • 举报
回复


int main(){

BMPFileHeader *file;
cout<<(*file).type<<endl; //注意括号()
system("pause");
}

太乙 2008-08-20
  • 打赏
  • 举报
回复
汗~~我知道了,第一个问题,lz那个括号,改成英文的!
太乙 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 ga6840 的帖子:]
C/C++ code
BMPFileHeader file;
du_ru.read((char*)&file,sizeof(BMPFileHeader));
cout<<file.type<<endl;

BMPFileHeader *file;
du_ru.read((char*)file,sizeof(BMPFileHeader));
cout<<(*file).type<<endl;



请问第二个为什么编译不过去啊
应该是一样的啊
(这个问题的名字叫问题1)


C/C++ code
typedef struct
{
short type;
unsigned char bfSize[4];//unsigned int bfSize???

[/Quote]


第一个问题:报什么错?

vs2008上可以这样~



int main(){
string* s=new string("hello");
cout<<(*s).length();
system("pause");
}

guzhilei1986 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 freedomzcd 的回复:]
question 4: int a 放到if(){}外面去,否则就是if语句块变量.

o(∩_∩)o...


[/Quote]
雪鹰翔天 2008-08-20
  • 打赏
  • 举报
回复
question 4: int a 放到if(){}外面去,否则就是if语句块变量.

o(∩_∩)o...

ga6840 2008-08-20
  • 打赏
  • 举报
回复
十分感谢freshui
问题解决了
freshui 2008-08-20
  • 打赏
  • 举报
回复
可能我说的不清楚,
不是unsigned char bfSize[4] 占6个字节
而是
short type;
unsigned char bfSize[4] 一共占6个字节

short type;
unsigned int fSize;
一共占8个字节

typedef struct
{
short type; //2B
unsigned char bfSize[4]; //4B ==> 2+4 = 6B
short bfReserved1; //2B
short bfReserved2; //2B
unsigned short offset; //2B
}BMPFileHeader; //12B



还有还有~
上面的位图头 把unsigned char bfSize[4];改为unsigned int bfSize为啥就不行了呢?? (这个问题的名字叫问题2)

C/C++ code
typedef struct{
short type; //2byte
//因为对齐, 这里 2B
unsigned int fSize; //4B => 2+2+4 = 8B
short reserved1; //2byte
short reserved2; //2byte
int offset; //4byte
}BMPFileHeader;

//++


ga6840 2008-08-20
  • 打赏
  • 举报
回复
谢谢hqin6
我知道了
我会给你分的

回20楼:分不是问题

我只想问1:
unsigned char bfSize[4]
为什么
占6个字节, 每个char不是一节马?
加起来是4节阿?

2:谁有这方面的经验告诉我dx使用后备缓冲为啥会闪烁?
太乙 2008-08-20
  • 打赏
  • 举报
回复
第一题:



#include<fstream>
#include<iostream>
using namespace std;
typedef struct
{
short type;
unsigned char bfSize[4];
short bfReserved1;
short bfReserved2;
unsigned short offset;
short bi;
}BMPFileHeader;

void main()
{
char b_nam[25];
cin.getline(b_nam,25);
ifstream du_ru(b_nam);
if(!du_ru)
return;
BMPFileHeader *file = new BMPFileHeader;//分配内存
du_ru.read((char*)file,sizeof(BMPFileHeader));


du_ru.close();
cout<<"type:"<<(*file).type<<endl;
cout<<"bfSize:"<<(*file).bfSize<<endl;
cout<<"bfReserved1:"<<(*file).bfReserved1<<endl;
cout<<"bfReserved2:"<<(*file).bfReserved2<<endl;
cout<<"offset:"<<(*file).offset<<endl;
cout<<"bi:"<<(*file).bi<<endl;
//
cout<<endl;

system("pause");
}


200303148 2008-08-20
  • 打赏
  • 举报
回复
接分。
ga6840 2008-08-19
  • 打赏
  • 举报
回复
真的非常感谢各位
我会在明天有时间做修改
还有 我想听听更多人的意见
所以 我想明天再回大家
加载更多回复(4)

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧