#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "SDL.h"
#include "SDL_draw.h"
#include "SDL_ttf.h"
/* Константные значения */
const int width = 1060, height = 660, depth = 16, ybeg = 0, yend = 1;
const double beg_f = -2*3.1415, end_f = -3.1415;
/* График функции */
void draw_func(SDL_Surface *screen, int diff){
int x0 = 30 ,y0, yt = height-60;
double x = beg_f, res;
for(int i=0; i<(width-60); i++){
res = sqrt(sin(x)/2);
y0 = fabs(yend - res)*(height-60);
printf("%d \n", y0);
x += 0.0031415;
Draw_Line(screen,x0+i-1, yt+30, x0+i, y0+30,SDL_MapRGB(screen->format, 20,20,20));
yt = y0;
}
}
/* Функция Отрисовки графика */
void draw(SDL_Surface *screen, TTF_Font *fnt){
int n, diffx, diffy, x0 = 30, y0 = 30, x ;
SDL_Surface *text_surface = NULL;
char s[5];
SDL_Color text_color{20, 20, 20};
SDL_Rect dest{30,5, 1,1};
/* Рисуем вериткальные линии */
for(int i=0; i<=10; i++){
Draw_VLine(screen,x0,y0, height-y0, SDL_MapRGB(screen->format, 200,200,200));
x0 += 100;
}
/* Рисуем горизонтальные цифры */
text_surface = TTF_RenderText_Solid(fnt,"-2Pi", text_color);
SDL_BlitSurface(text_surface, NULL, screen,&dest);
dest.x += 1000;
text_surface = TTF_RenderText_Solid(fnt,"-Pi", text_color);
SDL_BlitSurface(text_surface, NULL, screen,&dest);
/* Рисуем горизонтальные линии */
n = yend - ybeg;
diffy = (height - 60)/n;
x0 = 30;
for(int i=0; i<=10; i++){
Draw_HLine(screen,x0,y0, width-x0, SDL_MapRGB(screen->format, 200,200,200));
y0 += 60;
}
/* Рисуем вертикальные цифры */
x = ybeg;
dest.x = 15;
dest.y = height-40;
for(int i=0; i<=n; i++){
itoa(x,s,10); // Преобразование числа в строку
text_surface = TTF_RenderText_Solid(fnt,s, text_color);
SDL_BlitSurface(text_surface, NULL, screen,&dest);
dest.y -= diffy;
x++;
}
/* Рисуем график функции */
draw_func(screen, diffy);
}
int main ( int argc, char** argv )
{
int done = 0;
SDL_Event event;
SDL_Rect rect = {0,0,width, height};
TTF_Font *fnt = NULL;
/* Инициализируем видеорежим SDL */
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
if (TTF_Init()){
printf( "Unable to init SDL_Font: %s\n", SDL_GetError() );
return 1;
}
/* Создаем новое окно */
SDL_Surface* screen = SDL_SetVideoMode(width, height, depth,SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen ){
printf("Unable to set SDL video: %s\n", SDL_GetError());
TTF_Quit();
SDL_Quit();
return 1;
}
/* Подключение шрифта */
if(!(fnt = TTF_OpenFont("CharisSILR.ttf", 15))){
printf("Unable to set Font: %s\n", SDL_GetError());
TTF_Quit();
SDL_Quit();
return 3;
}
while (!done){
while (SDL_PollEvent(&event)){
// check for messages
switch (event.type){
case SDL_QUIT:
done = 1;
break;
case SDL_KEYDOWN:{
if (event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
break;
}
}
}
/* Рисуем график */
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 255,255,255));
draw(screen, fnt);
/* Обновляем экран*/
SDL_Flip(screen);
}
SDL_FreeSurface(screen);
TTF_CloseFont(fnt);
TTF_Quit();
SDL_Quit();
return 0;
}