Advertisement
Echo89

Sine Wave Generator Win32/SFML

Jun 18th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <windows.h>
  3. #include <cmath>
  4.  
  5. int w = 800;
  6. int h = 600;
  7.  
  8. sf::RenderWindow window;
  9.  
  10. LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  11. {
  12.     switch(message)
  13.     {
  14.         case WM_DESTROY:
  15.         {
  16.             PostQuitMessage(0);
  17.             break;
  18.         }
  19.         case WM_CLOSE:
  20.         {
  21.             DestroyWindow(hwnd);
  22.             break;
  23.         }
  24.         case WM_PAINT:
  25.         {
  26.             int b, c, nw, nh;
  27.             double i, m, x1, y1, x2, y2, l, a, z;
  28.             sf::Vertex vertices[2];
  29.            
  30.             RECT ClientRect;
  31.             GetClientRect(hwnd, &ClientRect);
  32.             nw = ClientRect.right - ClientRect.left;
  33.             nh = ClientRect.bottom - ClientRect.top;
  34.            
  35.             sf::View NewView;
  36.             NewView.setSize(nw, nh);
  37.             NewView.setCenter(nw / 2, nh / 2);
  38.             window.setView(NewView);
  39.             window.clear(sf::Color(255, 255, 255));
  40.            
  41.             z = 5.0;
  42.             a = (nh / 2) - 20;
  43.             l = nw / z;
  44.            
  45.             i = l / 360.0;
  46.             m = nh / 2;
  47.            
  48.             x1 = 0;
  49.             y1 = 0;
  50.            
  51.             for(b = 0; b < z; b++)
  52.             {
  53.                 for(c = 0; c < 360; c++)
  54.                 {
  55.                     x2 = x1 + i;
  56.                     y2 = std::sin((x2 / i) * (M_PI / 180.0)) * a;
  57.                     vertices[0] = sf::Vertex(sf::Vector2f(x1, y1 + m), sf::Color(0, 0, 0));
  58.                     vertices[1] = sf::Vertex(sf::Vector2f(x2, y2 + m), sf::Color(0, 0, 0));
  59.                     window.draw(vertices, 2, sf::Lines);
  60.                     x1 = x2;
  61.                     y1 = y2;
  62.                 }
  63.             }
  64.            
  65.             window.display();
  66.             break;
  67.         }
  68.         default:
  69.         {
  70.             return DefWindowProc(hwnd, message, wParam, lParam);
  71.         }
  72.     }
  73.    
  74.     return 0;
  75. }
  76.  
  77. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  78. {
  79.     MSG message;
  80.     WNDCLASS w;
  81.     HWND port, wnd;
  82.    
  83.     w.style = 0;
  84.     w.lpfnWndProc = &WinProc;
  85.     w.cbClsExtra = 0;
  86.     w.cbWndExtra = 0;
  87.     w.hInstance = hInstance;
  88.     w.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  89.     w.hCursor = LoadCursor(NULL, IDC_ARROW);
  90.     w.hbrBackground = NULL;
  91.     w.lpszMenuName = NULL;
  92.     w.lpszClassName = "Sine Wave";
  93.     if(!RegisterClass(&w))
  94.     {
  95.         return 1;
  96.     }
  97.    
  98.     wnd = CreateWindow(
  99.         "Sine Wave",
  100.         "Sine Wave Generator (Caelan Stewart)",
  101.         WS_OVERLAPPEDWINDOW,
  102.         50, 50,
  103.         800, 600,
  104.         NULL,
  105.         NULL,
  106.         hInstance,
  107.         NULL
  108.     );
  109.    
  110.     if(wnd == NULL)
  111.     {
  112.         return 1;
  113.     }
  114.    
  115.     ShowWindow(wnd, nCmdShow);
  116.     UpdateWindow(wnd);
  117.    
  118.     window.create(wnd);
  119.    
  120.     while(GetMessage(&message, NULL, 0, 0) > 0)
  121.     {
  122.         TranslateMessage(&message);
  123.         DispatchMessage(&message);
  124.     }
  125.    
  126.     return message.wParam;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement