33,311
社区成员
发帖
与我相关
我的任务
分享
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;
typedef struct
{
short type;
unsigned char bfSize[4];//unsigned int bfSize???
short bfReserved1;
short bfReserved2;
unsigned short offset;
short bi;//???
}BMPFileHeader;
typedef struct{
short type;//2byte
unsigned int fSize;//4byte
short reserved1;//2byte
short reserved2;//2byte
int offset;//4byte
}BMPFileHeader; //++
/*#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;
}
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;
}
}
#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()
};
#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;
}
int main(){
BMPFileHeader *file;
cout<<(*file).type<<endl; //注意括号()
system("pause");
}
int main(){
string* s=new string("hello");
cout<<(*s).length();
system("pause");
}
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;
//++
#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");
}