13 SDL_Surface* screen; // Screen surface
21 GL_API int GL_APIENTRY _dgles_load_library(void *, void *(*)(void *, const char *));
23 static void *proc_loader(void *h, const char *name)
26 return SDL_GL_GetProcAddress(name);
31 glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
32 glMatrixMode(GL_PROJECTION);
34 glOrthof(0.0f, (float)screen->w, (float)screen->h, 0.0f, -1.0f, 1.0f);
35 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
37 // Initialize vertices
38 vertices = new GLfloat[NPIXELS * 6];
39 memset(vertices, 0, sizeof(GLfloat) * NPIXELS * 6);
40 glEnableClientState(GL_VERTEX_ARRAY);
41 glVertexPointer(3, GL_FLOAT, 0, vertices);
47 // Initialize pixel objects
48 pixels = new Pixel*[NPIXELS];
49 for (int x = 0; x < NPIXELS; x++) {
50 pixels[x] = new Pixel(screen);
55 void pushPixels(int pushX, int pushY) {
56 // Apply force to each pixel outwards from the mouse
57 for (int i = 0; i < NPIXELS; i++) {
59 float dx = pixels[i]->x - pushX;
60 float dy = pixels[i]->y - pushY;
61 float mag = sqrtf(dx*dx + dy*dy);
66 pixels[i]->accel(dx * 10.0, dy * 10.0);
72 glClear(GL_COLOR_BUFFER_BIT);
82 vertices[10] = my + 5;
83 glDrawArrays(GL_LINE_LOOP, 0, 4);
85 // Update all pixel objects
86 for (int i = 0; i < NPIXELS; i++) {
88 float dx = mx - pixels[i]->x;
89 float dy = my - pixels[i]->y;
90 float d = sqrtf(dx*dx + dy*dy);
92 // Apply acceleration to pixels towards the mouse cursor
96 // Apply a drag proportional to the distance from the cursor
98 ax += -pixels[i]->vx * (d - 25.0) / 1000.0;
99 ay += -pixels[i]->vy * (d - 25.0) / 1000.0;
102 // And a "chaotic" acceleration inversely proportional
103 // to the distance from the cursor
104 ax += ((rand() / (float)RAND_MAX) * 40.0 - 20.0) / d;
105 ay += ((rand() / (float)RAND_MAX) * 40.0 - 20.0) / d;
107 pixels[i]->accel(ax,ay);
109 pixels[i]->draw(&vertices[i*6]);
111 glDrawArrays(GL_LINES, 0, NPIXELS * 2);
114 int main(int argc, char** argv)
116 // Initialize the SDL library with the Video subsystem
117 SDL_Init(SDL_INIT_VIDEO);
119 // start the PDL library
122 // Tell it to use OpenGL ES version 1.1
123 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
125 // Set the video mode to full screen with OpenGL-ES support
126 screen = SDL_SetVideoMode(SCREENW, SCREENH, 0, SDL_OPENGL);
128 printf("Could not initialize video!\n");
131 printf("width: %d, height, %d\n", screen->w, screen->h);
132 printf("Starting up with %d pixels\n", NPIXELS);
135 // Load the desktop OpenGL-ES emulation library
136 _dgles_load_library(NULL, proc_loader);
143 int app_is_active = 1;
144 Uint32 last = SDL_GetTicks();
148 // Process the events
151 while (SDL_PollEvent(&Event)) {
152 switch (Event.type) {
153 case SDL_MOUSEMOTION:
157 case SDL_MOUSEBUTTONDOWN:
158 pushPixels(Event.button.x, Event.button.y);
159 case SDL_ACTIVEEVENT:
160 if (Event.active.state == SDL_APPACTIVE)
161 app_is_active = Event.active.gain;
170 SDL_GL_SwapBuffers();
174 Uint32 now = SDL_GetTicks();
175 if (now - last > 1000) {
176 printf("%d FPS\n", fps_counter);
180 } while (Event.type != SDL_QUIT);
181 // We exit anytime we get a request to quit the app