
Шустрый

Профиль
Группа: Участник
Сообщений: 66
Регистрация: 13.9.2009
Где: Дом
Репутация: нет Всего: нет
|
Добрый день. На днях с помощью одной книги написал игру Arkanoid,писал с помощью DirectDraw.Вот код: Arkonoid.cppКод | #define WIN32_LEAN_AND_MEAN #define INITGUID #include <windows.h> #include <windowsx.h> #include <mmsystem.h> #include <iostream> #include <conio.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <string.h> #include <stdarg.h> #include <stdio.h> #include <math.h> #include <io.h> #include <fcntl.h> #include <ddraw.h> #include "box.h" #define WINDOW_CLASS_NAME "WIN3DCLASS" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 #define GAME_STATE_INIT 0 #define GAME_STATE_START_LEVEL 1 #define GAME_STATE_RUN 2 #define GAME_STATE_SHUTDOWN 3 #define GAME_STATE_EXIT 4 #define NUM_BLOCK_ROWS 6 #define NUM_BLOCK_COLUMNS 8 #define BLOCK_WIDTH 64 #define BLOCK_HEIGHT 16 #define BLOCK_ORIGIN_X 8 #define BLOCK_ORIGIN_Y 8 #define BLOCK_X_GAP 80 #define BLOCK_Y_GAP 32 #define PADDLE_START_X (SCREEN_WIDTH/2 - 16) #define PADDLE_START_Y (SCREEN_HEIGHT - 32) #define PADDLE_WIDTH 32 #define PADDLE_HEIGHT 8 #define PADDLE_COLOR 191 #define BALL_START_Y (SCREEN_HEIGHT/2) #define BALL_SIZE 4 int Game_Init(void *parms = NULL); int Game_Shutdown(void *parms = NULL); int Game_Main(void * parms = NULL); HWND main_window_handle = NULL; HINSTANCE main_instance = NULL; int game_state = GAME_STATE_INIT; int paddle_x = 0, paddle_y = 0; int ball_x = 0, ball_y = 0; int ball_dx = 0, ball_dy = 0; int score = 0; int level = 1; int blocks_hit = 0; UCHAR blocks[NUM_BLOCK_ROWS][NUM_BLOCK_COLUMNS]; LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg, WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; HDC hdc; 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; } return (DefWindowProc(hwnd, msg, wparam, lparam)); } int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASS winclass; HWND hwnd; MSG msg; 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; if(!RegisterClass(&winclass)) return(0); if(!(hwnd = CreateWindow(WINDOW_CLASS_NAME, "FREAKOUT", WS_POPUP | WS_VISIBLE, 0,0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hinstance, NULL))) return(0); ShowCursor(FALSE); main_window_handle = hwnd; main_instance = hinstance; Game_Init(); while(1) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } Game_Main(); } Game_Shutdown(); ShowCursor(TRUE); return(msg.wParam); } int Game_Init(void *param) { return(1); } int Game_Shutdown(void *params) { return(1); } void Init_Blocks(void) { for (int row = 0; row < NUM_BLOCK_ROWS; row++) for (int col = 0; col < NUM_BLOCK_COLUMNS; col++) blocks[row][col] = row*16+col*3+16; } void Draw_Blocks(void) { int x1 = BLOCK_ORIGIN_X, y1 = BLOCK_ORIGIN_Y; for(int row=0; row < NUM_BLOCK_ROWS; row++) { x1 = BLOCK_ORIGIN_X; for(int col = 0; col < NUM_BLOCK_COLUMNS; col++) { if(blocks[row][col] != 0) { Draw_Rectangle(x1 - 4, y1 + 4, x1+ BLOCK_WIDTH - 4, y1 +BLOCK_HEIGHT + 4, 0); Draw_Rectangle(x1,y1,x1 + BLOCK_WIDTH,y1 + BLOCK_HEIGHT , blocks[row][col]); } x1 += BLOCK_X_GAP; } y1 += BLOCK_Y_GAP; } } void Process_Ball(void) { int x1 = BLOCK_ORIGIN_X, y1 = BLOCK_ORIGIN_Y; int ball_cx = ball_x = (BALL_SIZE / 2), ball_cy = ball_y = (BALL_SIZE / 2); if(ball_y > (SCREEN_HEIGHT / 2) && ball_dy > 0) { int x = ball_x+(BALL_SIZE/2); int y = ball_y+(BALL_SIZE/2); if((x >= paddle_x && x <= paddle_x + PADDLE_WIDTH) && (y >= paddle_y && y <= paddle_y + PADDLE_HEIGHT)) { ball_dy =- ball_dy; ball_y += ball_dy; if(KEY_DOWN(VK_RIGHT)) ball_dx-=(rand()%3); else if (KEY_DOWN(VK_LEFT)) ball_dx+=(rand()%3); else ball_dx+=(-1+rand()%3); if(blocks_hit >= (NUM_BLOCK_ROWS * NUM_BLOCK_COLUMNS)) { game_state = GAME_STATE_START_LEVEL; level++; } MessageBeep(MB_OK); return; } } for(int row = 0; row < NUM_BLOCK_ROWS; row++) { x1 = BLOCK_ORIGIN_X; for(int col = 0; col < NUM_BLOCK_COLUMNS; col++) { if(blocks[row][col]!= 0) { if ((ball_cx > x1) && (ball_cx < x1+BLOCK_WIDTH) && (ball_cy > y1) && (ball_cy < y1+BLOCK_HEIGHT)) { blocks[row][col] = 0; blocks_hit++; ball_dy=-ball_dy; ball_dx+=(-1+rand()%3); MessageBeep(MB_OK); score+=5*(level+(abs(ball_dx))); return; } } x1 += BLOCK_X_GAP; } y1 += BLOCK_X_GAP; } } int Game_Main(void *params) { char buffer[80]; if (game_state == GAME_STATE_INIT) { DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP); srand(Star_Clock()); paddle_x = PADDLE_START_X; paddle_y = PADDLE_START_Y; ball_x = 8+rand()%(SCREEN_WIDTH-16); ball_y = BALL_START_Y; ball_dx = -4 + rand()%(8+1); ball_dy = 6 + rand()%2; game_state = GAME_STATE_START_LEVEL; } else if(game_state == GAME_STATE_START_LEVEL) { Init_Blocks(); blocks_hit = 0; game_state = GAME_STATE_RUN; } else if(game_state == GAME_STATE_RUN) { Star_Clock(); Draw_Rectangle(0,0,SCREEN_WIDTH-1, SCREEN_HEIGHT-1, 200); if(KEY_DOWN(VK_RIGHT)) { paddle_x += 8; if(paddle_x > (SCREEN_WIDTH - PADDLE_WIDTH)) paddle_x = SCREEN_WIDTH-PADDLE_WIDTH; } else if(KEY_DOWN(VK_LEFT)) { paddle_x -= 8; if(paddle_x < 0) paddle_x = 0; } Draw_Blocks(); ball_x+=ball_dx; ball_y+=ball_dy; if(ball_x > (SCREEN_WIDTH - BALL_SIZE) || ball_x < 0) { ball_dx =- ball_dx; ball_x += ball_dx; } if(ball_y < 0) { ball_dy =- ball_dy; ball_y += ball_dy; } else if (ball_y > (SCREEN_HEIGHT - BALL_SIZE)) { ball_dy=-ball_dy; ball_y+=ball_dy; score-=100; } if(ball_dx > 8) ball_dx = 8; else if(ball_dx < -8) ball_dx =-8; Process_Ball(); Draw_Rectangle(paddle_x-8, paddle_y+8, paddle_x+PADDLE_WIDTH-8, paddle_y+PADDLE_HEIGHT+8,0); Draw_Rectangle(paddle_x, paddle_y, paddle_x+PADDLE_WIDTH, paddle_y+PADDLE_HEIGHT,PADDLE_COLOR); Draw_Rectangle(ball_x-4, ball_y+4, ball_x+BALL_SIZE-4, ball_y+BALL_SIZE+4, 0); Draw_Rectangle(ball_x, ball_y, ball_x+BALL_SIZE, ball_y+BALL_SIZE, 255); sprintf(buffer,"F R E A K O U T Score %d Level %d",score,level); Draw_Text_GDI(buffer, 8,SCREEN_HEIGHT-16, 127); DD_Flip(); Wait_Clock(30); if (KEY_DOWN(VK_ESCAPE)) { PostMessage(main_window_handle, WM_DESTROY,0,0); game_state = GAME_STATE_SHUTDOWN; } } else if (game_state == GAME_STATE_SHUTDOWN) { DD_Shutdown(); game_state = GAME_STATE_EXIT; } return(1); }
|
box.cppКод | #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> #include <mmsystem.h> #include <iostream> #include <conio.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <string.h> #include <stdarg.h> #include <stdio.h> #include <math.h> #include <io.h> #include <fcntl.h> #include <ddraw.h> #include "box.h" extern HWND main_window_handle; extern HINSTANCE main_instance; LPDIRECTDRAW7 lpdd = NULL; LPDIRECTDRAWSURFACE7 lpddsprimary = NULL; LPDIRECTDRAWSURFACE7 lpddsback = NULL; LPDIRECTDRAWPALETTE lpddpal = NULL; LPDIRECTDRAWCLIPPER lpddclipper = NULL; PALETTEENTRY palette[256]; PALETTEENTRY save_palette[256]; DDSURFACEDESC2 ddsd; DDBLTFX ddbltfx; DDSCAPS2 ddscaps; HRESULT ddrval; DWORD start_clock_count = 0; int min_clip_x = 0, max_clip_x = SCREEN_WIDTH-1, min_clip_y = 0, max_clip_y = SCREEN_HEIGHT-1; int screen_width = SCREEN_WIDTH, screen_height = SCREEN_HEIGHT, screen_bpp = SCREEN_BPP; int DD_Init(int width, int height, int bpp) { int index; if(DirectDrawCreateEx(NULL, (void**) &lpdd, IID_IDirectDraw7, 0) != DD_OK) return(0); if(lpdd->SetCooperativeLevel(main_window_handle,DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!= DD_OK ) return(0); screen_height = height; screen_width = width; screen_bpp = bpp; memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL); ddscaps.dwCaps = DDSCAPS_BACKBUFFER; lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback); memset(palette,0,256 * sizeof(PALETTEENTRY)); for (index=0; index < 256; index++) { if(index < 64) palette[index].peRed = index * 4; else if(index >= 64 && index < 128) palette[index].peGreen = (index - 64) * 4; else if(index >= 128 && index < 192) palette[index].peBlue = (index - 128) * 4; else if (index >= 192 && index < 256) palette[index].peRed = palette[index].peGreen = palette[index].peBlue = (index-192)*4; palette[index].peFlags = PC_NOCOLLAPSE; } if (lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_INITIALIZE | DDPCAPS_ALLOW256, palette,&lpddpal,NULL)!=DD_OK) return(0); if (lpddsprimary->SetPalette(lpddpal)!=DD_OK) return(0); DD_Fill_Surface(lpddsprimary,0); DD_Fill_Surface(lpddsback,0); RECT screen_rect = {0,0,screen_width,screen_height}; lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect); return(1); } int DD_Shutdown(void) { if (lpddclipper) lpddclipper->Release(); if (lpddpal) lpddpal->Release(); if (lpddsback) lpddsback->Release(); if (lpddsprimary) lpddsprimary->Release(); if (lpdd) lpdd->Release(); return(1); } LPDIRECTDRAWCLIPPER DD_Attach_Clipper(LPDIRECTDRAWSURFACE7 lpdds,int num_rects, LPRECT clip_list) { int index; LPDIRECTDRAWCLIPPER lpddclipper; LPRGNDATA region_data; if((lpdd->CreateClipper(0,&lpddclipper,0))!=DD_OK) return(0); region_data = (LPRGNDATA)malloc(sizeof(RGNDATAHEADER)+num_rects*sizeof(RECT)); memcpy(region_data->Buffer, clip_list, sizeof(RECT)*num_rects); region_data->rdh.dwSize = sizeof(RGNDATAHEADER); region_data->rdh.iType = RDH_RECTANGLES; region_data->rdh.nCount = num_rects; region_data->rdh.nRgnSize = num_rects*sizeof(RECT); region_data->rdh.rcBound.left = 64000; region_data->rdh.rcBound.top = 64000; region_data->rdh.rcBound.right = -64000; region_data->rdh.rcBound.bottom = -6400; for(index = 0; index < num_rects; index++) { if(clip_list[index].left < region_data->rdh.rcBound.left) region_data->rdh.rcBound.left = clip_list[index].left; if(clip_list[index].right > region_data->rdh.rcBound.right) region_data->rdh.rcBound.right = clip_list[index].right; if(clip_list[index].top < region_data->rdh.rcBound.top) region_data->rdh.rcBound.top = clip_list[index].top; if(clip_list[index].bottom > region_data->rdh.rcBound.bottom) region_data->rdh.rcBound.bottom = clip_list[index].top; } if ((lpddclipper->SetClipList(region_data, 0))!=DD_OK) { free(region_data); return(NULL); } if ((lpdds->SetClipper(lpddclipper))!=DD_OK) { free(region_data); return(NULL); } free(region_data); return(lpddclipper); } int DD_Flip(void) { return(1); } DWORD Star_Clock(void) { return(start_clock_count = Get_Clock()); } DWORD Get_Clock(void) { return(GetTickCount()); } DWORD Wait_Clock(DWORD count) { while((Get_Clock() - start_clock_count) < count); return (Get_Clock()); } int DD_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds , int color) { DDBLTFX ddbltfx; DD_INIT_STRUCT(ddbltfx); ddbltfx.dwFillColor = color; lpdds->Blt(0,0,0, DDBLT_COLORFILL | DDBLT_WAIT | DDBLT_ASYNC, &ddbltfx); return(1); } int Draw_Rectangle(int x1, int y1, int x2, int y2, int color, LPDIRECTDRAWSURFACE7 lpdds) { DDBLTFX ddbltfx; RECT fill_area; DD_INIT_STRUCT( ddbltfx); DD_INIT_STRUCT( ddbltfx); ddbltfx.dwFillColor = color; fill_area.top = y1; fill_area.left = x1; fill_area.top = y2 + 1; fill_area.right = x2 + 1; lpdds->Blt(&fill_area,0,0,DDBLT_COLORFILL | DDBLT_WAIT | DDBLT_ASYNC, &ddbltfx); return(1); } int Draw_Text_GDI(char *text, int x,int y,int color, LPDIRECTDRAWSURFACE7 lpdds) { HDC xdc; if(lpdds->GetDC(&xdc) != DD_OK) return(0); SetTextColor(xdc,RGB(palette[color].peRed,palette[color].peGreen,palette[color].peBlue)); SetBkMode(xdc, TRANSPARENT); TextOut(xdc,x,y,text,strlen(text)); lpdds->ReleaseDC(xdc); return(1); }
|
Но возникла ошибка:"1>box.obj : error LNK2019: ссылка на неразрешенный внешний символ _DirectDrawCreateEx@16 в функции "int __cdecl DD_Init(int,int,int)" (?DD_Init@@YAHHHH@Z)"Перерыл весь код не чего найти не могу , кто может помогите пожалуйста! Заранее благодарен!
|