forked from batiste/sdl2-opengl-es
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_app.c
222 lines (164 loc) · 5.85 KB
/
my_app.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "common.c"
#include "intersection.c"
int main(int argc, char** argv)
{
init();
GLuint lineProgram;
lineProgram = initProgram("vertex-shader-1.vert", "line-shader-1.frag");
CHECK_GL();
GLuint pointProgram;
pointProgram = initProgram("vertex-shader-1.vert", "point-shader-1.frag");
CHECK_GL();
GLuint textureProgram;
textureProgram = initProgram("vertex-shader-1.vert", "texture-shader-1.frag");
CHECK_GL();
// load texture
ImageData png;
png.filename = "bamboo-2.png";
loadPNG(&png);
TextureInfos texture;
loadTexture(&texture, &png, 1.0, 1.0);
transformTexture(&texture, 0.0, 0.0, 0.0);
// load texture
ImageData background;
background.filename = "bamboo-b2.png";
loadPNG(&background);
TextureInfos back;
float scalex = screen.w / (float)background.width;
loadTexture(&back, &background, scalex, scalex);
transformTexture(&back, 0, -back.height / 2.0 + screen.h / 2.0, 0);
CHECK_GL();
CHECK_SDL();
glClearColor(110. / 255., 127. / 255., 91. / 255., 0.0f);
float x, y;
// Main loop variables
SDL_Event event;
float theta = 0;
then = SDL_GetTicks();
next_time = SDL_GetTicks() + 16;
int done = 0;
float vx = 0;
float vy = 0;
int i = 0;
int mouse_x = 0;
int mouse_y = 0;
int mouse_x_prev = 0;
int mouse_y_prev = 0;
int lastCut = 0;
GenericList pieces = { 0, NULL, NULL };
transformTexture(&texture, 0, screen.h / 4.0, 0.00);
addToList(&pieces, &texture);
// Main loop
while (!done) {
++frames;
theta = theta + 0.1;
while (SDL_PollEvent(&event)) {
if(event.type == SDL_WINDOWEVENT_CLOSE || event.type == SDL_QUIT) {
done = 1;
}
}
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
CHECK_GL();
useProgram(textureProgram);
CHECK_GL();
float background_bottom_position = (back.height - screen.h) / 2.0;
float floor_position = -((screen.h / 2.0) -30 + 2 * (background_bottom_position - back.y));
if(back.y < background_bottom_position) {
transformTexture(&back, 0, 4, 0);
transformTexture(&texture, 0, 0, 0.01);
}
/*else if(texture.y > floor_position) {
transformTexture(&texture, 0, -4, 0);
transformTexture(&texture, 0, 0, 0.01);
}
drawTexture(&texture, 0, 0, 0);*/
drawTexture(&back, 0, 0, 0);
CHECK_GL();
useProgram(lineProgram);
CHECK_GL();
// Load the vertex position
GLfloat vertices[6];
getMouse(&mouse_x, &mouse_y);
ListElement *el;
MousePosition *pos1 = 0;
MousePosition *pos2 = 0;
for(el = mouse_buffer.first; el != NULL; el=el->next) {
pos2 = pos1;
pos1 = (MousePosition *) el->data;
if(pos1 && pos2) {
vertices[0] = pos1->x;
vertices[1] = pos1->y;
vertices[2] = pos2->x;
vertices[3] = pos2->y;
drawLines(vertices, 2);
}
}
vx = (vx / 1.5) + ((mouse_x - mouse_x_prev) / 30.0);
vy = (vy / 1.5) + ((mouse_y - mouse_y_prev) / 30.0);
mouse_y_prev = mouse_y_prev + vy;
mouse_x_prev = mouse_x_prev + vx;
vertices[0] = mouse_x_prev;
vertices[1] = mouse_y_prev;
vertices[2] = mouse_x;
vertices[3] = mouse_y;
//drawLines(vertices, 2);
MousePosition *first = 0;
MousePosition *last = 0;
first = (MousePosition *)mouse_buffer.first->data;
last = (MousePosition *)mouse_buffer.last->data;
struct Line line;
GLfloat points[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
if(first != last) {
line.ax = first->x;
line.ay = first->y;
line.bx = last->x;
line.by = last->y;
}
int nbPoints = 0;
TextureInfos * myTexture;
for(el = pieces.first; el != NULL; el=el->next) {
myTexture = (TextureInfos *) el->data;
// TODO: free and cleanup
if(myTexture->y < -screen.h) {
removeFromList(&pieces, el);
free(myTexture);
continue;
}
if(nbPoints = find_intersect_points(myTexture, &line, points)) {
useProgram(pointProgram);
drawLines(points, nbPoints);
if(nbPoints > 1 && lastCut < frames) {
lastCut = frames + 5;
TextureInfos * texture1 = malloc(sizeof(TextureInfos));
TextureInfos * texture2 = malloc(sizeof(TextureInfos));
split_vertex(myTexture, &line, texture1, texture2);
addToList(&pieces, texture1);
addToList(&pieces, texture2);
texture1->vx = 2.0;
texture2->vx = -2.0;
texture1->vy = -2.0;
texture2->vy = -2.0;
texture1->vr = 0.02;
texture2->vr = -0.02;
removeFromList(&pieces, el);
}
}
if(myTexture->y < floor_position) {
myTexture->vy = -myTexture->vy / 1.5;
myTexture->vx = myTexture->vx / 1.5;
myTexture->vr = myTexture->vr / 1.5;
}
// gravity
myTexture->vy = myTexture->vy - 0.15;
transformTexture(myTexture, myTexture->vx, myTexture->vy, myTexture->vr);
useProgram(textureProgram);
drawTexture(myTexture, 0, 0, 0);
}
//transformTexture(&texture, 0, -200.0, 0.00);
SDL_GL_SwapWindow(mainwindow);
SDL_Delay(time_left());
next_time += 16;
}
/* Clean up */
return cleanup(0);
}