This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffect.h
295 lines (253 loc) · 8.92 KB
/
effect.h
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef EFFECT_H_INCLUDED
#define EFFECT_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <math.h>
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
#include <SDL2/SDL.h>
#define MUS_PATH "nero.wav"
// prototype for our audio callback
// see the implementation for more information
// variable declarations
static Uint8 *audio_pos; // global pointer to the audio buffer to be played
static Uint32 audio_len; // remaining length of the sample we have to play
void my_audio_callback(void *userdata, Uint8 *stream, unsigned int len) {
if (audio_len ==0)
return;
len = ( len > audio_len ? audio_len : len );
SDL_memcpy (stream, audio_pos, len); // simply copy from one buffer into the other
//SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);// mix from one buffer into another
audio_pos += len;
audio_len -= len;
}
/*
** PLAYING A SOUND IS MUCH MORE COMPLICATED THAN IT SHOULD BE
*/
Uint8* play(){
// Initialize SDL.
if (SDL_Init(SDL_INIT_AUDIO) < 0)
return 1;
// local variables
static Uint32 wav_length; // length of our sample
static Uint8 *wav_buffer; // buffer containing our audio file
static SDL_AudioSpec wav_spec; // the specs of our piece of music
printf("hi");
/* Load the WAV */
// the specs, length and buffer of our wav are filled
if( SDL_LoadWAV(MUS_PATH, &wav_spec, &wav_buffer, &wav_length) == NULL ){
return 1;
}
// set the callback function
wav_spec.callback = my_audio_callback;
wav_spec.userdata = NULL;
// set our global static variables
audio_pos = wav_buffer; // copy sound buffer
audio_len = wav_length; // copy file length
/* Open the audio device */
printf("%d\n",wav_spec.freq);
if ( SDL_OpenAudio(&wav_spec, NULL) < 0 ){
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
exit(-1);
}
printf("%d\n",wav_spec.freq);
/* Start playing */
SDL_PauseAudio(0);
// wait until we're don't playing
while ( audio_len > 0 ) {
SDL_Delay(10);
}
// shut everything down
atexit(abort_play(wav_buffer));
}
void abort_play(Uint8* wav_buf){
SDL_CloseAudio();
SDL_FreeWAV(wav_buf);
}
int WIDTH_GLOBAL,HEIGHT_GLOBAL,FPS_CAP;
/**
* Gibt zu einem Paar mathematischer x- und y-Koordinaten
* die BMP X- und Y-Koordinaten aus.
*
* x - Wert zwischen x_MIN und x_MAX.
* y - Wert zwischen y_MIN und y_MAX.
* X - Berechneter BMP Wert zwischen 0 und WIDTH_GLOBAL.
* Y - Berechneter BMP Wert zwischen 0 und HEIGHT_GLOBAL.
*/
struct function_start_time{
char functionid;
unsigned long long starttime;
}function_start_time;
struct function_start_time speicher[100];
void toBMP_alt(double x, double y, int* X, int* Y,int x_MIN,int x_MAX, int y_MIN, int y_MAX) { //in Benutzung
*X = (int) ((x - x_MIN) * WIDTH_GLOBAL / (x_MAX - x_MIN));
*Y = HEIGHT_GLOBAL - (int) ((y - y_MIN) * HEIGHT_GLOBAL / (y_MAX - y_MIN));
}
/**
* Gibt zu einem Paar BMP X- und Y-Koordinaten
* die mathematischen x- und y-Koordinaten aus.
*
* X - Wert zwischen 0 und WIDTH_GLOBAL.
* Y - Wert zwischen 0 und HEIGHT_GLOBAL.
* x - Berechnete mathematische Koordinate zwischen x_MIN und x_MAX.
* y - Berechnete mathematische Koordinate zwischen y_MIN und y_MAX.
*/
void toMath(int X, int Y, double* x, double* y,int x_MIN,int x_MAX, int y_MIN, int y_MAX) {
*x = x_MIN + ((double) X * (x_MAX - x_MIN)) / WIDTH_GLOBAL;
*y = y_MIN + ((double) (HEIGHT_GLOBAL - Y) * (y_MAX - y_MIN)) / HEIGHT_GLOBAL;
}
struct SDL_Point vektorwinder(int x_alt,int y_alt, float angle,int x, int y ){
struct SDL_Point new_point;
new_point.x=x+cos(angle)*(x_alt-x)-sin(angle)*(y_alt-y);
new_point.y=y+sin(angle)*(x_alt-x)+cos(angle)*(y_alt-y);
return new_point;
}
double length_points(int x1,int y1, int x2, int y2){
return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
void effect_wandernder_balkenY(SDL_Renderer* renderer, int position,float angle) {
struct SDL_Point x_neu;
struct SDL_Point y_neu;
for (int i = 0;i < 10;i++) {
x_neu = vektorwinder(position +i, 0, angle, WIDTH_GLOBAL/ 2, HEIGHT_GLOBAL/2);
y_neu = vektorwinder(position +i, HEIGHT_GLOBAL, angle,WIDTH_GLOBAL/2, HEIGHT_GLOBAL/2);
SDL_RenderDrawLine(renderer, x_neu.x, x_neu.y, y_neu.x, y_neu.y);
}
}
void effect_wandernder_balkenX(SDL_Renderer* renderer,int position) {
for (int i = 0;i < 10;i++) {
SDL_RenderDrawLine(renderer, 0, position + i, WIDTH_GLOBAL, position + i);
}
}
void effect_balken_kreis(SDL_Renderer* renderer, int position,float angle){
struct SDL_Point x_neu;
struct SDL_Point y_neu;
angle *= -1;
for (int i = 0;i < 10;i++) {
x_neu = vektorwinder(position +i, 0, angle, WIDTH_GLOBAL/ 2, HEIGHT_GLOBAL/2);
y_neu = vektorwinder(position +i, HEIGHT_GLOBAL, angle,WIDTH_GLOBAL/2, HEIGHT_GLOBAL/2);
SDL_RenderDrawLine(renderer, x_neu.x, x_neu.y, y_neu.x, y_neu.y);
}
}
void effect_array(SDL_Renderer* renderer, char* pixel_array ) {
for(int y=0;y<HEIGHT_GLOBAL;y++){
for(int x=0;x<WIDTH_GLOBAL;x++){
if (pixel_array[y*WIDTH_GLOBAL+x]==1){
SDL_RenderDrawPoint(renderer,x,y);
}
}
}
}
void effect_rand_points(SDL_Renderer* renderer, int input_signal,int num_points) {// random Punkte (ziemlich cool)
int i;
int width;
int height;
srand(input_signal);
for( i = 0 ; i < num_points ; i++ ) {
width=rand() % WIDTH_GLOBAL;
height=rand() % HEIGHT_GLOBAL;
for(int y = -10; y < 10; y++){
for(int x = -10; x < 10; x++){
SDL_RenderDrawPoint(renderer,width + x,height +y);
}
}
}
}
void effect_func_quad_alt(SDL_Renderer* renderer, float radius, float angle, int o_x, int o_y){// quadratische Funktion
/* Issues: Strich am oberen rand
Issues: Keine durchgehende Linie
*/
SDL_Point* screen=calloc(WIDTH_GLOBAL,sizeof(SDL_Point));
double x,y;
int YY,XX;
for(int i=0;i<WIDTH_GLOBAL;i++){
toMath(i,0,&x,&y,-16,16,-9,9);
toBMP_alt(x,sin(x)*radius,&XX,&YY,-16,16,-9,9);
struct SDL_Point new_point=vektorwinder(XX,YY,angle,o_x,o_y);
XX=new_point.x;
YY=new_point.y;
screen[i].x=XX;
screen[i].y=YY;
}
for(int j=1;j<WIDTH_GLOBAL;j++){
SDL_RenderDrawLine(renderer,screen[j-1].x,screen[j-1].y,screen[j].x,screen[j].y);
//printf("From(%d,%d) to(%d,%d)\n",screen[j-1].x,screen[j-1].y,screen[j].x,screen[j].y);
}
free(screen);
}
void effect_func_kreis_unten(SDL_Renderer* renderer, float radius, int o_x, int o_y){// quadratische Funktion
/* Issues: Strich am oberen rand
Issues: Keine durchgehende Linie
*/
SDL_Point* screen=calloc(WIDTH_GLOBAL,sizeof(SDL_Point));
double x,y;
int YY,XX;
for(int i=0;i<WIDTH_GLOBAL;i++){
toMath(i,0,&x,&y,-16,16,-9,9);
float ergebnis=-sqrt((-(x*x)+radius*radius));
if (ergebnis==ergebnis){
toBMP_alt(x,ergebnis,&XX,&YY,-16,16,-9,9);
screen[i].x=XX;
screen[i].y=YY;}
else{
screen[i].x=0;
screen[i].y=0;
}
}
for(int j=1;j<WIDTH_GLOBAL;j++){
if((screen[j].x!=0)&&(screen[j].y!=0)&&(screen[j-1].x!=0)&&(screen[j-1].y!=0)){
SDL_RenderDrawLine(renderer,screen[j-1].x,screen[j-1].y,screen[j].x,screen[j].y);
//printf("From(%d,%d) to(%d,%d)\n",screen[j-1].x,screen[j-1].y,screen[j].x,screen[j].y);
}
}
free(screen);
}
void effect_func_kreis_oben(SDL_Renderer* renderer, float radius, int o_x, int o_y){// quadratische Funktion
/* Issues: Strich am oberen rand
Issues: Keine durchgehende Linie
*/
SDL_Point* screen=calloc(WIDTH_GLOBAL,sizeof(SDL_Point));
double x,y;
int YY,XX;
for(int i=0;i<WIDTH_GLOBAL;i++){
toMath(i,0,&x,&y,-16,16,-9,9);
float ergebnis=sqrt((-(x*x)+radius*radius));
if (ergebnis==ergebnis){
toBMP_alt(x,ergebnis,&XX,&YY,-16,16,-9,9);
screen[i].x=XX;
screen[i].y=YY;}
else{
screen[i].x=0;
screen[i].y=0;
}
}
for(int j=1;j<WIDTH_GLOBAL;j++){
if((screen[j].x!=0)&&(screen[j].y!=0)&&(screen[j-1].x!=0)&&(screen[j-1].y!=0)){
SDL_RenderDrawLine(renderer,screen[j-1].x,screen[j-1].y,screen[j].x,screen[j].y);
//printf("From(%d,%d) to(%d,%d)\n",screen[j-1].x,screen[j-1].y,screen[j].x,screen[j].y);
}
}
free(screen);
}
void effect_coord(SDL_Renderer* renderer){//Koordinatensystem printen
char *screen;
int X,Y;
screen=calloc(HEIGHT_GLOBAL*WIDTH_GLOBAL,sizeof(char));
int XX, YY;
toBMP_alt(0.0, 0.0, &XX, &YY,-16,16,-9,9);
for (int X = 0; X < WIDTH_GLOBAL; X++) {
screen[YY * WIDTH_GLOBAL + X] = 1;
}
for (int Y = 0; Y < HEIGHT_GLOBAL; Y++) {
screen[Y * WIDTH_GLOBAL + XX] = 1;
}
effect_array(renderer,screen);
free(screen);
}
void effect_3D_sinus(SDL_Renderer* renderer,int radius){
SDL_SetRenderDrawColor(renderer,255,0,0,255);
for(int i = -10; i < 10; i++){
effect_func_quad_alt(renderer,radius*0.02,1.57082144*2,WIDTH_GLOBAL/2+i,HEIGHT_GLOBAL/2+i);
}
}
#endif // EFFECT_H_INCLUDED