Advertisement
Kostiggig

Untitled

Mar 25th, 2023
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.89 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cmath>
  5.  
  6. const double PI = 3.14;
  7.  
  8. const int SCREEN_WIDTH = 640;
  9. const int SCREEN_HEIGHT = 640;
  10. const int RADIUS = SCREEN_WIDTH / 2 - 50;
  11. const int DELAY_BEFORE_EXIT = 7000;
  12.  
  13. void draw_circle_for_clock(SDL_Renderer* renderer, int centerX, int centerY)
  14. {
  15.     SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  16.  
  17.     double step = 2 * PI / 1000;
  18.     for (float theta = 0; theta < 2 * PI; theta += step)
  19.     {
  20.         int x = (int)(centerX + RADIUS * cos(theta));
  21.         int y = (int)(centerY + RADIUS * sin(theta));
  22.         SDL_RenderDrawPoint(renderer, x, y);
  23.     }
  24.  
  25.  
  26. }
  27.  
  28. void draw_dividers_on_clock(SDL_Renderer* renderer) {
  29.     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  30.     for (int i = 0; i < 24; i++) {
  31.         int angle = i * 15;
  32.         int dividerLength = 20;
  33.  
  34.         if (i % 3 == 0 && i % 6 != 0) dividerLength = dividerLength + 15;
  35.         if (i % 6 == 0) dividerLength = dividerLength + 30;
  36.        
  37.  
  38.         int dividerX = SCREEN_WIDTH / 2 + (RADIUS - 20 - dividerLength) * std::sin(angle * 2 * PI / 360);
  39.         int dividerY = SCREEN_HEIGHT / 2 - (RADIUS - 20 - dividerLength) * std::cos(angle * 2 * PI / 360);
  40.  
  41.        
  42.         SDL_RenderDrawLine(renderer, (SCREEN_WIDTH / 2) + (RADIUS - 20) * std::sin(angle *2 * PI / 360), (SCREEN_HEIGHT / 2) - (RADIUS - 20) * std::cos(angle * 2 * PI / 360), dividerX, dividerY);
  43.     }
  44. }
  45.  
  46. void animate_second_hand_arrow(SDL_Renderer* renderer) {
  47.     double seconds = 0;
  48.     int centerX = SCREEN_WIDTH / 2;
  49.     int centerY = SCREEN_HEIGHT / 2;
  50.     int secondX = 0;
  51.     int secondY = 0;
  52.     while (true) {
  53.  
  54.         SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  55.         SDL_RenderClear(renderer);
  56.         SDL_RenderDrawLine(renderer, centerX, centerY, secondX, secondY);
  57.         draw_circle_for_clock(renderer, centerX, centerY);
  58.         draw_dividers_on_clock(renderer);
  59.  
  60.         secondX = centerX + (RADIUS - 20) * std::sin(seconds * (2 * PI) / 60);
  61.         secondY = centerY - (RADIUS - 20) * std::cos(seconds * (2 * PI) / 60);
  62.         SDL_RenderDrawLine(renderer, centerX, centerY, secondX, secondY);
  63.  
  64.         SDL_RenderPresent(renderer);
  65.  
  66.         SDL_Delay(20);
  67.         seconds+= 0.02;
  68.     }
  69. }
  70.  
  71. void call_clock() {
  72.     SDL_Init(SDL_INIT_VIDEO);
  73.  
  74.     SDL_Window* window = SDL_CreateWindow("Clock", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  75.     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  76.  
  77.  
  78.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, NULL);
  79.     SDL_RenderClear(renderer);
  80.  
  81.     SDL_RenderPresent(renderer);
  82.     animate_second_hand_arrow(renderer);
  83. }
  84.  
  85. void draw_parabol(SDL_Renderer* renderer) {
  86.     int x = 0;
  87.    
  88.         for (int i = 0; i < SCREEN_WIDTH; i++) {
  89.             int y = (SCREEN_WIDTH / 2) - pow((i - SCREEN_WIDTH / 2) / 20.0, 2);
  90.             SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  91.             SDL_RenderDrawLine(renderer, i, y, i + 1, (SCREEN_WIDTH / 2) - pow((i + 1 - SCREEN_WIDTH / 2) / 20.0, 2));
  92.         }
  93.  
  94.         SDL_RenderPresent(renderer);
  95.  
  96.    
  97.  
  98. }
  99.  
  100. int call_graphics() {  
  101.  
  102.  
  103.  
  104.    
  105.         int a = 0;
  106.         SDL_Window* window = NULL;
  107.         SDL_Renderer* renderer = NULL;
  108.         int num;
  109.         if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
  110.         {
  111.             printf_s("SDL не смог запуститься! SDL_Error: %s\n", SDL_GetError());
  112.             return 1;
  113.         }
  114.         else
  115.         {
  116.             window = SDL_CreateWindow(u8"График", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  117.             if (window == NULL)
  118.             {
  119.                 printf_s("Окно не может быть создано! SDL_Error: %s\n", SDL_GetError());
  120.             }
  121.             else
  122.             {
  123.                 renderer = SDL_CreateRenderer(window, -1, 0);
  124.                 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  125.                 SDL_RenderClear(renderer);
  126.                 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  127.                 SDL_RenderDrawLine(renderer, SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT);
  128.                 SDL_RenderDrawLine(renderer, 0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2);
  129.                 for (a = 0; a <= 360; a++)
  130.                 {
  131.                     SDL_RenderDrawPoint(renderer, int(SCREEN_WIDTH / 2 + 100 * cos(a * (3.14 / 180))), int(SCREEN_HEIGHT / 2 + 100 * sin(a * (3.14 / 180))));
  132.                 }
  133.  
  134.                 draw_parabol(renderer);
  135.  
  136.                 SDL_RenderPresent(renderer);
  137.                 SDL_Delay(3000);
  138.             }
  139.         }
  140.  
  141.         SDL_DestroyRenderer(renderer);
  142.         SDL_DestroyWindow(window);
  143.  
  144.  
  145.         SDL_Quit();
  146.  
  147.         return 0;
  148.    
  149. }
  150.  
  151.  
  152.  
  153. int main(int argc, char** argv)
  154. {
  155.     //call_clock();
  156.     call_graphics();
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement