10 SDL_Surface* screen; // Screen surface
18 GL_API int GL_APIENTRY _dgles_load_library(void *, void *(*)(void *, const char *));
20 static void *proc_loader(void *h, const char *name)
23 return SDL_GL_GetProcAddress(name);
28 glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
29 glMatrixMode(GL_PROJECTION);
31 glOrthof(0.0f, 320.0f, 400.0f, 0.0f, -1.0f, 1.0f);
32 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
34 // Initialize vertices
35 vertices = new GLfloat[NPIXELS * 6];
36 memset(vertices, 0, sizeof(GLfloat) * NPIXELS * 6);
37 glEnableClientState(GL_VERTEX_ARRAY);
38 glVertexPointer(3, GL_FLOAT, 0, vertices);
44 // Initialize pixel objects
45 pixels = new Pixel*[NPIXELS];
46 for (int x = 0; x < NPIXELS; x++) {
47 pixels[x] = new Pixel();
52 void pushPixels(int pushX, int pushY) {
53 // Apply force to each pixel outwards from the mouse
54 for (int i = 0; i < NPIXELS; i++) {
56 float dx = pixels[i]->x - pushX;
57 float dy = pixels[i]->y - pushY;
58 float mag = sqrtf(dx*dx + dy*dy);
63 pixels[i]->accel(dx * 10.0, dy * 10.0);
69 glClear(GL_COLOR_BUFFER_BIT);
79 vertices[10] = my + 5;
80 glDrawArrays(GL_LINE_LOOP, 0, 4);
82 // Update all pixel objects
83 for (int i = 0; i < NPIXELS; i++) {
85 float dx = mx - pixels[i]->x;
86 float dy = my - pixels[i]->y;
87 float d = sqrtf(dx*dx + dy*dy);
89 // Apply acceleration to pixels towards the mouse cursor
93 // Apply a drag proportional to the distance from the cursor
95 ax += -pixels[i]->vx * (d - 25.0) / 1000.0;
96 ay += -pixels[i]->vy * (d - 25.0) / 1000.0;
99 // And a "chaotic" acceleration inversely proportional
100 // to the distance from the cursor
101 ax += ((rand() / (float)RAND_MAX) * 40.0 - 20.0) / d;
102 ay += ((rand() / (float)RAND_MAX) * 40.0 - 20.0) / d;
104 pixels[i]->accel(ax,ay);
106 pixels[i]->draw(&vertices[i*6]);
108 glDrawArrays(GL_LINES, 0, NPIXELS * 2);
111 int main(int argc, char** argv)
113 // Initialize the SDL library with the Video subsystem
114 SDL_Init(SDL_INIT_VIDEO);
116 // start the PDL library
119 // Tell it to use OpenGL ES version 1.1
120 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
122 // Set the video mode to full screen with OpenGL-ES support
123 screen = SDL_SetVideoMode(320, 400, 0, SDL_OPENGL);
125 printf("Could not initialize video!\n");
128 printf("width: %d, height, %d\n", screen->w, screen->h);
131 // Load the desktop OpenGL-ES emulation library
132 _dgles_load_library(NULL, proc_loader);
139 int app_is_active = 1;
140 Uint32 last = SDL_GetTicks();
144 // Process the events
147 while (SDL_PollEvent(&Event)) {
148 switch (Event.type) {
149 // List of keys that have been pressed
151 switch (Event.key.keysym.sym) {
152 // Escape forces us to quit the app
153 // this is also sent when the user makes a back gesture
155 Event.type = SDL_QUIT;
161 case SDL_MOUSEMOTION:
165 case SDL_MOUSEBUTTONDOWN:
166 pushPixels(Event.button.x, Event.button.y);
167 case SDL_ACTIVEEVENT:
168 if (Event.active.state == SDL_APPACTIVE)
169 app_is_active = Event.active.gain;
178 SDL_GL_SwapBuffers();
182 Uint32 now = SDL_GetTicks();
183 if (now - last > 1000) {
184 printf("%d FPS\n", fps_counter);
188 } while (Event.type != SDL_QUIT);
189 // We exit anytime we get a request to quit the app