Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <iostream>
- #include <ctime>
- #include <cmath>
- const double PI = 3.14;
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 640;
- const int RADIUS = SCREEN_WIDTH / 2 - 50;
- const int DELAY_BEFORE_EXIT = 7000;
- void draw_circle_for_clock(SDL_Renderer* renderer, int centerX, int centerY)
- {
- SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
- double step = 2 * PI / 1000;
- for (float theta = 0; theta < 2 * PI; theta += step)
- {
- int x = (int)(centerX + RADIUS * cos(theta));
- int y = (int)(centerY + RADIUS * sin(theta));
- SDL_RenderDrawPoint(renderer, x, y);
- }
- }
- void draw_dividers_on_clock(SDL_Renderer* renderer) {
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- for (int i = 0; i < 24; i++) {
- int angle = i * 15;
- int dividerLength = 20;
- if (i % 3 == 0 && i % 6 != 0) dividerLength = dividerLength + 15;
- if (i % 6 == 0) dividerLength = dividerLength + 30;
- int dividerX = SCREEN_WIDTH / 2 + (RADIUS - 20 - dividerLength) * std::sin(angle * 2 * PI / 360);
- int dividerY = SCREEN_HEIGHT / 2 - (RADIUS - 20 - dividerLength) * std::cos(angle * 2 * PI / 360);
- 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);
- }
- }
- void animate_second_hand_arrow(SDL_Renderer* renderer) {
- double seconds = 0;
- int centerX = SCREEN_WIDTH / 2;
- int centerY = SCREEN_HEIGHT / 2;
- int secondX = 0;
- int secondY = 0;
- while (true) {
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
- SDL_RenderClear(renderer);
- SDL_RenderDrawLine(renderer, centerX, centerY, secondX, secondY);
- draw_circle_for_clock(renderer, centerX, centerY);
- draw_dividers_on_clock(renderer);
- secondX = centerX + (RADIUS - 20) * std::sin(seconds * (2 * PI) / 60);
- secondY = centerY - (RADIUS - 20) * std::cos(seconds * (2 * PI) / 60);
- SDL_RenderDrawLine(renderer, centerX, centerY, secondX, secondY);
- SDL_RenderPresent(renderer);
- SDL_Delay(20);
- seconds+= 0.02;
- }
- }
- void call_clock() {
- SDL_Init(SDL_INIT_VIDEO);
- SDL_Window* window = SDL_CreateWindow("Clock", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, NULL);
- SDL_RenderClear(renderer);
- SDL_RenderPresent(renderer);
- animate_second_hand_arrow(renderer);
- }
- void draw_parabol(SDL_Renderer* renderer) {
- int x = 0;
- for (int i = 0; i < SCREEN_WIDTH; i++) {
- int y = (SCREEN_WIDTH / 2) - pow((i - SCREEN_WIDTH / 2) / 20.0, 2);
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderDrawLine(renderer, i, y, i + 1, (SCREEN_WIDTH / 2) - pow((i + 1 - SCREEN_WIDTH / 2) / 20.0, 2));
- }
- SDL_RenderPresent(renderer);
- }
- int call_graphics() {
- int a = 0;
- SDL_Window* window = NULL;
- SDL_Renderer* renderer = NULL;
- int num;
- if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
- {
- printf_s("SDL не смог запуститься! SDL_Error: %s\n", SDL_GetError());
- return 1;
- }
- else
- {
- window = SDL_CreateWindow(u8"График", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (window == NULL)
- {
- printf_s("Окно не может быть создано! SDL_Error: %s\n", SDL_GetError());
- }
- else
- {
- renderer = SDL_CreateRenderer(window, -1, 0);
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
- SDL_RenderClear(renderer);
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
- SDL_RenderDrawLine(renderer, SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT);
- SDL_RenderDrawLine(renderer, 0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2);
- for (a = 0; a <= 360; a++)
- {
- SDL_RenderDrawPoint(renderer, int(SCREEN_WIDTH / 2 + 100 * cos(a * (3.14 / 180))), int(SCREEN_HEIGHT / 2 + 100 * sin(a * (3.14 / 180))));
- }
- draw_parabol(renderer);
- SDL_RenderPresent(renderer);
- SDL_Delay(3000);
- }
- }
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }
- int main(int argc, char** argv)
- {
- //call_clock();
- call_graphics();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement