fork download
  1. #include<windows.h>
  2. #include<iostream>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. /****************************************************************************
  7. 定義関連
  8. ****************************************************************************/
  9. enum KEY
  10. {
  11. VK_Z=0x5a,//キーボード「Z」
  12. };
  13.  
  14. struct Player//自機構造体
  15. {
  16. float x,y;//自機の座標
  17. int width,height;//自機の幅と高さ
  18. int shotInterval;//弾の発射間隔を制御するためのカウンター
  19. };
  20.  
  21. const float PAI=3.14159265358979323846f;//π
  22. #define DegToRadian(x) ((double)(x*PAI/180))
  23. HINSTANCE hInst;//インスタンスのハンドル
  24. HWND hWnd;//ウィンドウのハンドル
  25. Player player;//自機
  26.  
  27. HDC hBackDC;//裏画面用デバイスコンテキスト
  28. HBITMAP hBackBitmap;//裏画面
  29. int fps;
  30.  
  31. LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
  32. void GameInit();//初期化
  33. void GameEnd();//終了処理
  34. void GameLoop();//ゲームループ
  35. void GameDraw();//描画処理
  36. void Stage();//ステージ処理関連
  37. void MovePlayer();//自機移動等
  38. void DrawPlayer(const HDC &hDC);//自機描画
  39.  
  40. /****************************************************************************
  41. Window処理関連
  42. ****************************************************************************/
  43. //ウィンドウプロシージャ
  44. LRESULT CALLBACK WndProc(HWND hWnd,
  45. UINT uMsg,
  46. WPARAM wParam,
  47. LPARAM lParam)
  48. {
  49. switch (uMsg)
  50. {
  51. case WM_CLOSE://窓を閉じたら行う処理
  52. DestroyWindow(hWnd);
  53. return FALSE;
  54.  
  55. case WM_DESTROY://ウィンドウが破壊されたときに行う処理
  56. PostQuitMessage(0);
  57. return FALSE;
  58.  
  59. default:
  60. return DefWindowProc(hWnd,uMsg,wParam,lParam);
  61. }
  62. }
  63.  
  64. //ウィンドウ作成関数
  65. void CreateGameWindow()
  66. {
  67. //どんなウィンドウを作成するか記述する
  68. WNDCLASSEX wcex={sizeof(WNDCLASSEX)};//構造体を全部0で初期化
  69. wcex.cbSize=sizeof(WNDCLASSEX);
  70. wcex.lpfnWndProc=(WNDPROC)WndProc;
  71. wcex.hInstance=hInst;
  72. wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+2);
  73. wcex.lpszClassName="STGSample1";
  74. wcex.hCursor = (HCURSOR)LoadImage( // マウスカーソル
  75. NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR,
  76. 0, 0, LR_DEFAULTSIZE | LR_SHARED
  77. );
  78. RegisterClassEx(&wcex);
  79. hWnd=CreateWindow(wcex.lpszClassName,
  80. "--- stg ---",
  81. WS_OVERLAPPEDWINDOW-WS_SIZEBOX-WS_MAXIMIZEBOX,
  82. 0,0,384,448,
  83. NULL,NULL,hInst,NULL);
  84. ShowWindow(hWnd,SW_SHOW);
  85. UpdateWindow(hWnd);
  86. }
  87.  
  88. int APIENTRY WinMain(HINSTANCE hInstance,
  89. HINSTANCE hPrevInstance,
  90. LPSTR lpCmdLine,
  91. int nCmdShow )
  92. {
  93. MSG msg;
  94. hInst=hInstance;
  95. nCmdShow;lpCmdLine;hPrevInstance;hInstance;
  96. //ウィンドウ初期化
  97. CreateGameWindow();
  98.  
  99. //変数等初期化
  100. GameInit();
  101.  
  102. //メッセージループ
  103. while(TRUE)
  104. {
  105. if(PeekMessage(&msg,0,0,0,PM_NOREMOVE))
  106. {
  107. if(!GetMessage(&msg,NULL,0,0))break;
  108.  
  109. TranslateMessage(&msg);
  110. DispatchMessage(&msg);
  111. }
  112. else
  113. {
  114. if(GetKeyState(VK_ESCAPE)&0x8000) exit(0);
  115. //ここにゲームループをかく
  116. GameLoop();
  117. }
  118. }
  119. return msg.wParam;
  120. }
  121.  
  122.  
  123.  
  124. /****************************************************************************
  125. ゲーム処理関連
  126. ****************************************************************************/
  127. //ゲーム初期化
  128. void GameInit()
  129. {
  130. timeBeginPeriod(1);
  131. ZeroMemory(&player,sizeof(Player));
  132. //自機変数初期化
  133. player.x=192;
  134. player.y=224;
  135. player.width=16;
  136. player.height=16;
  137.  
  138. //裏画面初期化
  139. HDC hDC;
  140. hDC=GetDC(hWnd);//表画面のデバイスコンテキストの内容を取得
  141. hBackDC=CreateCompatibleDC(hDC);//表画面と同じの裏画面用のデバイスコンテキストを生成
  142. hBackBitmap=CreateCompatibleBitmap(hDC,384,448);//表画面と同じ画面生成
  143. SelectObject(hBackDC,hBackBitmap); //デバイスコンテキストと画面本体を関連付ける
  144. ReleaseDC(hWnd,hDC);
  145. }
  146.  
  147. //ゲーム終了処理
  148. void GameEnd()
  149. {
  150.  
  151. }
  152.  
  153. //ゲームループ
  154. void GameLoop()
  155. {
  156. Stage();
  157. MovePlayer();
  158. GameDraw();
  159. Sleep(16);
  160. }
  161.  
  162.  
  163.  
  164. int GetFps()
  165. {
  166. return fps;
  167. }
  168.  
  169. //描画処理
  170. void GameDraw()
  171. {
  172. HDC hDC;
  173. RECT rcWnd;
  174. GetClientRect(hWnd,&rcWnd);//クライアント領域の大きさを取得
  175.  
  176. //裏画面を黒でクリア
  177. FillRect(hBackDC,&rcWnd,(HBRUSH)GetStockObject(BLACK_BRUSH));
  178. SetBkMode(hBackDC,TRANSPARENT);
  179. //裏画面に描画
  180. DrawPlayer(hBackDC);
  181.  
  182. //SCORE
  183. SetTextColor(hBackDC,RGB(255,255,255));
  184. char buf[256];
  185. sprintf(buf,"--Test--");
  186. TextOut(hBackDC,0,0,buf,lstrlen(buf));//spf表示
  187.  
  188. //表画面に裏画面をコピー
  189. hDC=GetDC(hWnd);//デバイスコンテキストを取得
  190. BitBlt(hDC,0,0,384,448,hBackDC,0,0,SRCCOPY);//裏画面→表画面にコピー
  191. ReleaseDC(hWnd,hDC);//デバイスコンテキスト開放
  192. }
  193.  
  194. /****************************************************************************
  195. ステージ処理関連
  196. ****************************************************************************/
  197. void Stage()
  198. {
  199.  
  200. }
  201.  
  202. /****************************************************************************
  203. 自機処理関連
  204. ****************************************************************************/
  205. //自機移動等
  206. void MovePlayer()
  207. {
  208. RECT rcWnd;
  209. GetClientRect(hWnd,&rcWnd);//クライアント領域の大きさを取得
  210. //自機移動
  211. if(GetKeyState(VK_LEFT)&0x8000)player.x-=3; //←
  212. else if(GetKeyState(VK_RIGHT)&0x8000)player.x+=3; //→
  213. if(GetKeyState(VK_UP)&0x8000)player.y-=3; //↑
  214. else if(GetKeyState(VK_DOWN)&0x8000)player.y+=3; //↓
  215. //移動後画面端を越えないようにする処理
  216. if(player.x<0)player.x=0;
  217. else if(player.x>rcWnd.right-player.width)player.x=rcWnd.right-player.width;
  218. if(player.y<0)player.y=0;
  219. else if(player.y>rcWnd.bottom-player.height)player.y=rcWnd.bottom-player.height;
  220.  
  221. //自機弾発射
  222. if(GetKeyState(VK_Z)&0x8000)
  223. {
  224. // player.shotInterval++;//弾発射間隔を制御
  225. // if(player.shotInterval%6==0)
  226. // CreateShot(player.x+player.width/2-8/2,player.y,8,8,8,270,STRAIGHT,PLAYER);
  227. }
  228. else player.shotInterval=0;
  229. }
  230.  
  231. //自機描画
  232. void DrawPlayer(const HDC &hDC)
  233. {
  234. HBRUSH hBrush, hOldBrush;
  235. hBrush=CreateSolidBrush(RGB(64,64,212));
  236. hOldBrush=(HBRUSH)SelectObject(hDC,hBrush);
  237. Rectangle(hDC,player.x,player.y,player.x+player.width,player.y+player.height);
  238. SelectObject(hDC,hOldBrush);
  239. DeleteObject(hBrush);
  240. }
  241.  
  242.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:20: error: windows.h: No such file or directory
prog.cpp:23: error: ‘HINSTANCE’ does not name a type
prog.cpp:24: error: ‘HWND’ does not name a type
prog.cpp:27: error: ‘HDC’ does not name a type
prog.cpp:28: error: ‘HBITMAP’ does not name a type
prog.cpp:31: error: ‘LRESULT’ does not name a type
prog.cpp:38: error: expected ‘,’ or ‘...’ before ‘&’ token
prog.cpp:38: error: ISO C++ forbids declaration of ‘HDC’ with no type
prog.cpp:44: error: ‘LRESULT’ does not name a type
prog.cpp: In function ‘void CreateGameWindow()’:
prog.cpp:68: error: ‘WNDCLASSEX’ was not declared in this scope
prog.cpp:68: error: expected `;' before ‘wcex’
prog.cpp:69: error: ‘wcex’ was not declared in this scope
prog.cpp:70: error: ‘WNDPROC’ was not declared in this scope
prog.cpp:70: error: expected `;' before ‘WndProc’
prog.cpp:71: error: ‘hInst’ was not declared in this scope
prog.cpp:72: error: ‘HBRUSH’ was not declared in this scope
prog.cpp:72: error: ‘COLOR_WINDOW’ was not declared in this scope
prog.cpp:74: error: ‘HCURSOR’ was not declared in this scope
prog.cpp:74: error: expected `;' before ‘LoadImage’
prog.cpp:78: error: ‘RegisterClassEx’ was not declared in this scope
prog.cpp:79: error: ‘hWnd’ was not declared in this scope
prog.cpp:81: error: ‘WS_OVERLAPPEDWINDOW’ was not declared in this scope
prog.cpp:81: error: ‘WS_SIZEBOX’ was not declared in this scope
prog.cpp:81: error: ‘WS_MAXIMIZEBOX’ was not declared in this scope
prog.cpp:83: error: ‘CreateWindow’ was not declared in this scope
prog.cpp:84: error: ‘SW_SHOW’ was not declared in this scope
prog.cpp:84: error: ‘ShowWindow’ was not declared in this scope
prog.cpp:85: error: ‘UpdateWindow’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:88: error: expected initializer before ‘WinMain’
stdout
Standard output is empty