#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=600, depth=16, beg_f=5 , end_f=15, ybeg=0, yend=3;
//график
void draw_func(SDL_Surface *screen, int diff){
int x0=30 ,y0=400;
double x=beg_f, res;
for(int i=0; i<(width-60); i++){
res=cos(x/12)/sin(x/12);
y0=fabs(yend-res)*diff;
x+=0.01;
Draw_Pixel(screen,x0+i, y0+30,SDL_MapRGB(screen->format, 20,20,20));
}
}
//рисуем график
void draw(SDL_Surface *screen, TTF_Font *fnt){
int n, diffx, diffy, x0=30, y0=30, x=beg_f;
SDL_Surface *text_surface=NULL;
char s[5];
SDL_Color text_color{20, 20, 20};
SDL_Rect dest{30,5, 1,1};
n=end_f-beg_f;
diffx=(width-60)/n;
for(int i=0; i<=n; i++){
Draw_VLine(screen,x0,y0, height-y0, SDL_MapRGB(screen->format, 200,200,200));
x0+=diffx;
}
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.x+=diffx;
x++;
}
n=yend-ybeg;
diffy=(height - 60)/n;
x0=30;
for(int i=0; i<=n; i++){
Draw_HLine(screen,x0,y0, width-x0, SDL_MapRGB(screen->format, 200,200,200));
y0+=diffy;
}
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;
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)){
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;
}