Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.14 KB | None | 0 0
  1. #include <windows.h>
  2. #include <gl/gl.h>
  3. #include <gl/glu.h>
  4. #include <gl/glaux.h>
  5.  
  6. #include "us_types.h"
  7.  
  8. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  9. WPARAM wParam, LPARAM lParam);
  10. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
  11. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
  12.  
  13. HWND hWnd;
  14. u8 bQuit = 0;
  15.  
  16. int i2 = 0;
  17. GLuint texture[1];
  18. u8 blockb[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  19. float color_red = 0,color_green = 0,color_blue = 0;
  20. struct {
  21.     int x;
  22.     int y;
  23. } cursor_koor;
  24. float cursor_x;
  25. struct {
  26.     int left;
  27.     int top;
  28.     int right;
  29.     int bottom;
  30. } window_koor;
  31. float ball_x = 1,ball_y = -1;
  32. float speed_x = 0,speed_y = -0.0009375f;
  33. u8 param1 = 1,i1 = 0;
  34.  
  35. u8 texture1[] = {0x4c,0xb1,0x22,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,
  36. 0xff,0x0,0x0,0xff,0x0,0xff,0x0,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xc8,0x7f,0x7f,0xc8,0x7f,0x7f,0x0,0xff,
  37. 0x0,0xea,0xd9,0x99,0xea,0xd9,0x99,0xea,0xd9,0x99,0xea,0xd9,0x99,0x0,0xff,0x0,0xc8,0x7f,0x7f,0xc8,0x7f,0x7f,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0xff,
  38. 0x0,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,
  39. 0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x4c,0xb1,0x22,0x4c,0xb1,0x22,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,
  40. 0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0x0,0x0,0xff,0xff,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,
  41. 0xff,0x0,0x0,0xff,0x0,0xc8,0x7f,0x7f,0x0,0xff,0x0,0x85,0xe9,0x5f,0xb1,0xe7,0x61,0xb1,0xe7,0x61,0x85,0xe9,0x5f,0x0,0xff,0x0,0xc8,0x7f,0x7f,0x0,0xff,0x0,0x0,0xff,0x0,
  42. 0x0,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0x0,0x0,0x0,0xff,0xff,0x0,0x0,0xff,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0xff,
  43. 0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x4c,0xb1,0x22,0x4c,0xb1,0x22,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,
  44. 0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,
  45. 0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x85,0xe9,0x5f,0xb1,0xe7,0x61,0xb1,0xe7,0x61,0x85,0xe9,0x5f,0x0,0xff,0x0,
  46. 0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,
  47. 0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x4c,0xb1,0x22,0x4c,0xb1,0x22,0x0,0xff,
  48. 0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x0,
  49. 0xff,0x0,0x0,0x0,0x0,0xff,0xff,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xc8,0x7f,0x7f,0x0,0xff,0x0,0x85,0xe9,0x5f,0xb1,0xe7,0x61,0xb1,
  50. 0xe7,0x61,0x85,0xe9,0x5f,0x0,0xff,0x0,0xc8,0x7f,0x7f,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0x0,0x0,0x0,0xff,0xff,0x0,0x0,0xff,0x0,
  51. 0x0,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,
  52. 0x4c,0xb1,0x22,0x4c,0xb1,0x22,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,
  53. 0xff,0x0,0x0,0xff,0x0,0xff,0x0,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0x0,0xff,0x0,0xc8,0x7f,0x7f,0xc8,0x7f,0x7f,0x0,0xff,
  54. 0x0,0xea,0xd9,0x99,0xea,0xd9,0x99,0xea,0xd9,0x99,0xea,0xd9,0x99,0x0,0xff,0x0,0xc8,0x7f,0x7f,0xc8,0x7f,0x7f,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0xff,0x0,0xff,
  55. 0x0,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0xff,0x0,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,
  56. 0x0,0xff,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x4c,0xb1,0x22,};
  57.  
  58.  
  59.  
  60. void risovat() {
  61.     glClear (GL_COLOR_BUFFER_BIT);
  62.    
  63.     float blockx = 0;
  64.     float blocky = 0;
  65.    
  66.    
  67.     glBegin(GL_QUADS);
  68.     while (i1 < 50) {
  69.         switch (i1/10) {
  70.             case 0: {
  71.                 color_red = 0.51f;
  72.                 color_green = 0.42f;
  73.                 color_blue = 0.627f;
  74.                 break;
  75.             }
  76.             case 1: {
  77.                 color_red = 0.345f;
  78.                 color_green = 0.659f;
  79.                 color_blue = 0.706f;
  80.                 break;
  81.             }
  82.             case 2: {
  83.                 color_red = 1.0f;
  84.                 color_green = 0.71f;
  85.                 color_blue = 0.102f;
  86.                 break;
  87.             }
  88.             case 3: {
  89.                 color_red = 0.102f;
  90.                 color_green = 1.0f;
  91.                 color_blue = 0.482f;
  92.                 break;
  93.             }
  94.             case 4: {
  95.                 color_red = 1.0f;
  96.                 color_green = 0;
  97.                 color_blue = 0;
  98.                 break;
  99.             }
  100.         }      
  101.         if (blockb[i1]) {
  102.             glColor3f(color_red,color_green,color_blue);
  103.             if (i1 == 18) {
  104.                 color_red = 1.0f; color_green = 0; color_blue = 0;
  105.             }
  106.             if (i1 == 21) {
  107.                 color_red = 1.0f; color_green = 1.0f; color_blue = 0;
  108.             }
  109.             if (i1 == 45) {
  110.                 color_red = 0.5f; color_green = 0; color_blue = 0.2f;
  111.             }
  112.             glVertex2f(blockx,blocky);
  113.            
  114.             glColor3f(color_red,color_green,color_blue);
  115.             if (i1 == 18) {
  116.                 color_red = 1.0f; color_green = 1.0f; color_blue = 1.0f;
  117.             }
  118.             if (i1 == 21) {
  119.                 color_red = 1.0f; color_green = 1.0f; color_blue = 0.8f;
  120.             }
  121.             if (i1 == 45) {
  122.                 color_red = 0.2f; color_green = 0.3; color_blue = 0.5;
  123.             }
  124.             glVertex2f(blockx + 0.2f,blocky);
  125.            
  126.             glColor3f(color_red,color_green,color_blue);
  127.             if (i1 == 18) {
  128.                 color_red = 1.0f; color_green = 0; color_blue = 1.0;
  129.             }
  130.             if (i1 == 21) {
  131.                 color_red = 1.0f; color_green = 0; color_blue = 0.5f;
  132.             }
  133.             if (i1 == 45) {
  134.                 color_red = 0.8f; color_green = 1.0f; color_blue = 0;
  135.             }
  136.             glVertex2f(blockx + 0.2f,blocky - 0.05f);
  137.            
  138.             glColor3f(color_red,color_green,color_blue);
  139.             if (i1 == 18) {
  140.                 color_red = 0.0f; color_green = 1.0f; color_blue = 0;
  141.             }
  142.             if (i1 == 21) {
  143.                 color_red = 0; color_green = 0; color_blue = 1.0f;
  144.             }
  145.             if (i1 == 45) {
  146.                 color_red = 0; color_green = 1.0f; color_blue = 1.0f;
  147.             }
  148.             glVertex2f(blockx,blocky - 0.05f);
  149.         }
  150.         blockx += 0.2f;
  151.         i1++;
  152.         if (i1%10 == 0) {
  153.             blockx = 0;
  154.             blocky -= 0.05f;
  155.         }
  156.        
  157.     }
  158.     glEnd();
  159.    
  160.     glBindTexture(GL_TEXTURE_2D,texture[0]);
  161.     glBegin(GL_QUADS);
  162.     GetCursorPos(&cursor_koor);
  163.     GetWindowRect(hWnd,&window_koor);
  164.     if (cursor_koor.x > window_koor.left && cursor_koor.x < window_koor.right && cursor_koor.y > window_koor.top && cursor_koor.y < window_koor.bottom) {
  165.         cursor_x = (cursor_koor.x - window_koor.left)/250.0;
  166.     }
  167.    
  168.     glColor3f(1,1,1);
  169.     glTexCoord2f(1,0);
  170.     glVertex2f(cursor_x - 0.2f,-1.875);
  171.     glTexCoord2f(1,1);
  172.     glVertex2f(cursor_x + 0.2f,-1.875);
  173.     glTexCoord2f(0,1);
  174.     glVertex2f(cursor_x + 0.2f,-1.9f);
  175.     glTexCoord2f(0,0);
  176.     glVertex2f(cursor_x - 0.2f,-1.9f);
  177.     glEnd();
  178.    
  179.     glBegin(GL_QUADS);
  180.     glColor3f(0.8f,0,1);
  181.    
  182.     if (speed_x > 0) {
  183.             param1 = 1;
  184.         }
  185.     else {
  186.         param1 = 0;
  187.     }
  188.    
  189.     if (param1) {
  190.         glVertex2f(ball_x - 0.025f + 0.01f,ball_y - 0.006f);
  191.         glVertex2f(ball_x + 0.025f,ball_y);
  192.         glVertex2f(ball_x + 0.025f - 0.01f,ball_y - 0.03f + 0.006f);
  193.         glVertex2f(ball_x - 0.025f,ball_y - 0.03f);
  194.     }
  195.     else {
  196.         glVertex2f(ball_x - 0.025f,ball_y);
  197.         glVertex2f(ball_x + 0.025f - 0.01f,ball_y - 0.006f);
  198.         glVertex2f(ball_x + 0.025f,ball_y - 0.03f);
  199.         glVertex2f(ball_x - 0.025f + 0.01f,ball_y - 0.03f + 0.006f);
  200.     }
  201.    
  202.     ball_x += speed_x;
  203.     ball_y += speed_y;
  204.     if (ball_x >= 1.975 || ball_x <= 0.025) {
  205.         speed_x = -speed_x;
  206.     }
  207.     if (ball_y >= 0) {
  208.         speed_y = -speed_y;
  209.     }
  210.     if (ball_y <= -1.845 && ball_y >= -1.87f && ball_x >= (cursor_x - 0.2f) && ball_x <= (cursor_x + 0.2f)) {
  211.         speed_x = (ball_x - cursor_x)/100.0;
  212.         speed_y = -speed_y;
  213.     }
  214.    
  215.     if (ball_y <= -2) {
  216.         bQuit = 1;
  217.     }
  218.    
  219.     if (ball_y >= -0.25f) {
  220.         i1 = -ball_y/0.05f;
  221.         i1 = i1*10;
  222.         i1 += (ball_x/0.2);
  223.         if (blockb[i1]) {
  224.             blockb[i1] = 0;
  225.             speed_y = -speed_y;
  226.         }
  227.     }
  228.     glEnd();
  229.     i1 = 0;
  230.     while (i1 < 50) {
  231.         if (blockb[i1]) {
  232.             goto net;
  233.         }
  234.         i1++;
  235.     }
  236.     bQuit = 1;
  237. net:
  238.     i1 = 0;
  239. }
  240.  
  241. GLvoid load_texture() {
  242.     glGenTextures(1,&texture[0]);
  243.     glBindTexture(GL_TEXTURE_2D,texture[0]);
  244.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  245.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  246.     //glTexImage2D(GL_TEXTURE_2D,0,3,50,5,0,GL_RGB,GL_UNSIGNED_BYTE,&texture1);
  247. }
  248.  
  249. int WINAPI WinMain (HINSTANCE hInstance,
  250.                     HINSTANCE hPrevInstance,
  251.                     LPSTR lpCmdLine,
  252.                     int iCmdShow)
  253. {
  254.     WNDCLASS wc;
  255.     HDC hDC;
  256.     HGLRC hRC;        
  257.     MSG msg;
  258.  
  259.     wc.style = CS_OWNDC;
  260.     wc.lpfnWndProc = WndProc;
  261.     wc.cbClsExtra = 0;
  262.     wc.cbWndExtra = 0;
  263.     wc.hInstance = hInstance;
  264.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  265.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  266.     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
  267.     wc.lpszMenuName = NULL;
  268.     wc.lpszClassName = "GLSample";
  269.     RegisterClass (&wc);
  270.  
  271.     hWnd = CreateWindow (
  272.       "GLSample", "Breakout",
  273.       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  274.       550, 100, 500, 800,
  275.       NULL, NULL, hInstance, NULL);
  276.  
  277.     EnableOpenGL (hWnd, &hDC, &hRC);
  278.     glTranslatef(-1.0f,1.0f,0.0f);
  279.  
  280.     while (!bQuit)
  281.     {
  282.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  283.         {
  284.             if (msg.message == WM_QUIT)
  285.             {
  286.                 bQuit = TRUE;
  287.             }
  288.             else
  289.             {
  290.                 TranslateMessage (&msg);
  291.                 DispatchMessage (&msg);
  292.             }
  293.         }
  294.         else
  295.         {
  296.             risovat();
  297.             SwapBuffers (hDC);
  298.         }
  299.     }
  300.  
  301.     DisableOpenGL (hWnd, hDC, hRC);
  302.  
  303.     DestroyWindow (hWnd);
  304.  
  305.     return msg.wParam;
  306. }
  307.  
  308. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  309.                           WPARAM wParam, LPARAM lParam)
  310. {
  311.  
  312.     switch (message)
  313.     {
  314.     case WM_CREATE:
  315.         return 0;
  316.     case WM_CLOSE:
  317.         PostQuitMessage (0);
  318.         return 0;
  319.  
  320.     case WM_DESTROY:
  321.         return 0;
  322.        
  323.  
  324.     default:
  325.         return DefWindowProc (hWnd, message, wParam, lParam);
  326.     }
  327. }
  328.  
  329.  
  330.  
  331. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
  332. {
  333.     PIXELFORMATDESCRIPTOR pfd;
  334.     int iFormat;
  335.     *hDC = GetDC (hWnd);
  336.     ZeroMemory (&pfd, sizeof (pfd));
  337.     pfd.nSize = sizeof (pfd);
  338.     pfd.nVersion = 1;
  339.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  340.       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  341.     pfd.iPixelType = PFD_TYPE_RGBA;
  342.     pfd.cColorBits = 24;
  343.     pfd.cDepthBits = 16;
  344.     pfd.iLayerType = PFD_MAIN_PLANE;
  345.     iFormat = ChoosePixelFormat (*hDC, &pfd);
  346.     SetPixelFormat (*hDC, iFormat, &pfd);
  347.     *hRC = wglCreateContext( *hDC );
  348.     wglMakeCurrent( *hDC, *hRC );
  349.    
  350.     glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  351.     load_texture();
  352.     glEnable(GL_TEXTURE_2D);
  353. }
  354.  
  355.  
  356.  
  357. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
  358. {
  359.     wglMakeCurrent (NULL, NULL);
  360.     wglDeleteContext (hRC);
  361.     ReleaseDC (hWnd, hDC);
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement