Rename API to snake_case.
[gfxprim.git] / demos / particle / particle_demo.c
blob9a3422f2418170c33ed6f9203f32e5b68115e30a
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Particle demo.
29 #include <signal.h>
30 #include <string.h>
31 #include <gfxprim.h>
32 #include <backends/GP_Backends.h>
34 #include "space.h"
36 static gp_pixel black_pixel;
37 static gp_pixel white_pixel;
39 static gp_backend *backend = NULL;
40 static gp_pixmap *pixmap = NULL;
42 static void sighandler(int signo)
44 if (backend != NULL)
45 gp_backend_exit(backend);
47 fprintf(stderr, "Got signal %i\n", signo);
49 exit(1);
52 static void init_backend(const char *backend_opts)
54 backend = gp_backend_init(backend_opts, "Particles");
56 if (backend == NULL) {
57 fprintf(stderr, "Failed to initalize backend '%s'\n", backend_opts);
58 exit(1);
62 int main(int argc, char *argv[])
64 const char *backend_opts = "X11";
65 int opt;
66 int pause_flag = 0;
67 int particles = 160;
69 while ((opt = getopt(argc, argv, "b:n:")) != -1) {
70 switch (opt) {
71 case 'b':
72 backend_opts = optarg;
73 break;
74 case 'n':
75 particles = atoi(optarg);
76 break;
77 default:
78 fprintf(stderr, "Invalid paramter '%c'\n", opt);
82 signal(SIGINT, sighandler);
83 signal(SIGSEGV, sighandler);
84 signal(SIGBUS, sighandler);
85 signal(SIGABRT, sighandler);
87 init_backend(backend_opts);
89 pixmap = backend->pixmap;
91 black_pixel = gp_rgb_to_pixmap_pixel(0x00, 0x00, 0x00, pixmap);
92 white_pixel = gp_rgb_to_pixmap_pixel(0xff, 0xff, 0xff, pixmap);
94 gp_fill(pixmap, black_pixel);
95 gp_backend_flip(backend);
97 struct space *space;
98 space = space_create(particles, 10<<8, 10<<8, (pixmap->w - 10)<<8, (pixmap->h - 10)<<8);
100 for (;;) {
101 if (backend->poll)
102 gp_backend_poll(backend);
104 usleep(1000);
106 /* Read and parse events */
107 gp_event ev;
109 while (gp_backend_get_event(backend, &ev)) {
111 gp_event_dump(&ev);
113 switch (ev.type) {
114 case GP_EV_KEY:
115 if (ev.code != GP_EV_KEY_DOWN)
116 continue;
118 switch (ev.val.key.key) {
119 case GP_KEY_ESC:
120 case GP_KEY_ENTER:
121 case GP_KEY_Q:
122 gp_backend_exit(backend);
123 return 0;
124 break;
125 case GP_KEY_P:
126 pause_flag = !pause_flag;
127 break;
128 case GP_KEY_G:
129 space->gay = 1;
130 break;
131 case GP_KEY_T:
132 space->gay = 0;
133 break;
135 break;
136 case GP_EV_SYS:
137 switch(ev.code) {
138 case GP_EV_SYS_QUIT:
139 gp_backend_exit(backend);
140 exit(0);
141 break;
142 case GP_EV_SYS_RESIZE:
143 gp_backend_resize_ack(backend);
144 space_destroy(space);
145 space = space_create(particles,
146 10<<8, 10<<8,
147 (pixmap->w - 10)<<8,
148 (pixmap->h - 10)<<8);
149 break;
151 break;
155 if (!pause_flag) {
156 space_time_tick(space, 1);
157 space_draw_particles(pixmap, space);
158 gp_backend_flip(backend);
162 gp_backend_exit(backend);
164 return 0;