include stdlib include math include string include SDL include SDL_dra

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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;
}