-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo1.c
203 lines (153 loc) · 4.69 KB
/
demo1.c
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
/**
* Copyright 2021 Jesús Abelardo Saldívar Aguilar
*
*
* Demonstration of multiple sprites bouncing and rotating on screen.
*
*/
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <VG/openvg.h>
#include <VG/vgu.h>
#include "vc.h"
#include "sprites.h"
#include "spritedata1.h"
#define PI 3.1416
VGfloat stroke_desp = 0.0f;
VGfloat rota = 0.0f;
typedef struct {
JS_VG_SPRITE *sprite;
float dr;
float dx;
float dy;
void *sig
} SPRITE_REBOTA;
SPRITE_REBOTA *list_bouncers = NULL;
void addBouncer(int w, int h, float radius){
float x, y, ang, vel, dr, dx, dy;
float rangox, rangoy;
JS_VG_SPRITE *sprite;
sprite = malloc(sizeof(JS_VG_SPRITE));
rangox = w - (2 * radius);
rangoy = h - (2 * radius);
x = ( ( (float)rand() / (float)RAND_MAX ) * rangox ) + radius;
y = ( ( (float)rand() / (float)RAND_MAX ) * rangoy ) + radius;
ang = ( ( (float)rand() / (float)RAND_MAX ) * PI * 2);
vel = ( ( (float)rand() / (float)RAND_MAX ) * 8);
dr = ( ( (float)rand() / (float)RAND_MAX ) * 4) - 2;
//dx = ( ( (float)rand() / (float)RAND_MAX ) * 4 ) - 2;
//dy = ( ( (float)rand() / (float)RAND_MAX ) * 4 ) - 2;
dx = sin(ang) * vel;
dy = cos(ang) * vel;
//x =
sprite->translate[0] = x;
sprite->translate[1] = y;
sprite->scale[0] = 1;
sprite->scale[1] = 1;
sprite->rotate = 0;
sprite->data = &sprite_group_1;
sprite->init_sprite = init_sprite_group_array;
sprite->draw_func = draw_sprite_group_array;
init_sprite(sprite);
SPRITE_REBOTA *rebota = malloc(sizeof(SPRITE_REBOTA));
rebota->sprite = sprite;
rebota->dr = dr;
rebota->dx = dx;
rebota->dy = dy;
rebota->sig = list_bouncers;
list_bouncers = rebota;
}
void procesaRebota(SPRITE_REBOTA *rebota, int scenewidth, int sceneheight){
rebota->sprite->rotate += rebota->dr;
rebota->sprite->translate[0] += rebota->dx;
rebota->sprite->translate[1] += rebota->dy;
if (rebota->sprite->translate[0] + 100 >= scenewidth){
rebota->sprite->translate[0] = scenewidth - 100;
if (rebota->dx > 0){
rebota->dx *= -1;
rebota->dr += 0 - rebota->dy;
rebota->dr /= 2;
}
}
if (rebota->sprite->translate[0] - 100 <= 0){
rebota->sprite->translate[0] = 100;
if (rebota->dx < 0){
rebota->dx *= -1;
rebota->dr += rebota->dy;
rebota->dr /= 2;
}
}
if (rebota->sprite->translate[1] + 100 >= sceneheight){
rebota->sprite->translate[1] = sceneheight - 100;
if (rebota->dy > 0){
rebota->dy *= -1;
rebota->dr += rebota->dx;
rebota->dr /= 2;
}
}
if (rebota->sprite->translate[1] - 100 <= 0){
rebota->sprite->translate[1] = 100;
if (rebota->dy < 0){
rebota->dy *= -1;
rebota->dr += 0 - rebota->dx;
rebota->dr /= 2;
}
}
};
void draw(EGL_DISPMANX_WINDOW_T *nativewindow){
float clearColor[4] = {1, 1, 1, 0};
float dx = 1;
float dy = 1;
vgSetfv(VG_CLEAR_COLOR, 4, clearColor);
vgClear(0, 0, nativewindow->width, nativewindow->height);
vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
while (1){
// ¿Por qué ellipse.c no necesita llamar a esta función durante ciclo?
vgSetfv(VG_CLEAR_COLOR, 4, clearColor);
vgClear(0, 0, nativewindow->width, nativewindow->height);
//draw_sprite(sprite_escena);
for (SPRITE_REBOTA *rebota = list_bouncers; rebota != NULL; rebota = rebota->sig){
draw_sprite(rebota->sprite);
}
eglSwapBuffers(egldisplay, eglsurface);
for (SPRITE_REBOTA *rebota = list_bouncers; rebota != NULL; rebota = rebota->sig){
procesaRebota(rebota, nativewindow->width, nativewindow->height);
}
//vgFlush();
}
vgFlush();
}
void sig_handler(int sig) {
printf("Abortando proceso...\n");
deinit();
exit(1);
}
int
main(int argc, char *argv[])
{
EGL_DISPMANX_WINDOW_T nativewindow;
JS_VG_SPRITE_LIST lista_sprites = {
.sprite = &sprite_grupo,
.sig = NULL
};
srand((unsigned int) time(NULL));
signal(SIGINT, sig_handler);
inicia_vc(&nativewindow);
//init_sprite(&sprite_escena);
addBouncer(nativewindow.width, nativewindow.height, 100);
addBouncer(nativewindow.width, nativewindow.height, 100);
addBouncer(nativewindow.width, nativewindow.height, 100);
addBouncer(nativewindow.width, nativewindow.height, 100);
addBouncer(nativewindow.width, nativewindow.height, 100);
addBouncer(nativewindow.width, nativewindow.height, 100);
addBouncer(nativewindow.width, nativewindow.height, 100);
addBouncer(nativewindow.width, nativewindow.height, 100);
draw(&nativewindow);
deinit();
exit(0);
}