Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / particle / particle_demo.c
blobe4226c27114be1d6c8f8fc70aacebb185d6bc050
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 <GP.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_BackendExit(backend);
47 fprintf(stderr, "Got signal %i\n", signo);
49 exit(1);
52 static void init_backend(const char *backend_opts)
54 backend = GP_BackendInit(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 // GP_SetDebugLevel(10);
84 signal(SIGINT, sighandler);
85 signal(SIGSEGV, sighandler);
86 signal(SIGBUS, sighandler);
87 signal(SIGABRT, sighandler);
89 init_backend(backend_opts);
91 pixmap = backend->pixmap;
93 black_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, pixmap);
94 white_pixel = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, pixmap);
96 GP_Fill(pixmap, black_pixel);
97 GP_BackendFlip(backend);
99 struct space *space;
100 space = space_create(particles, 10<<8, 10<<8, (pixmap->w - 10)<<8, (pixmap->h - 10)<<8);
102 for (;;) {
103 if (backend->Poll)
104 GP_BackendPoll(backend);
106 usleep(1000);
108 /* Read and parse events */
109 GP_Event ev;
111 while (GP_BackendGetEvent(backend, &ev)) {
113 GP_EventDump(&ev);
115 switch (ev.type) {
116 case GP_EV_KEY:
117 if (ev.code != GP_EV_KEY_DOWN)
118 continue;
120 switch (ev.val.key.key) {
121 case GP_KEY_ESC:
122 case GP_KEY_ENTER:
123 case GP_KEY_Q:
124 GP_BackendExit(backend);
125 return 0;
126 break;
127 case GP_KEY_P:
128 pause_flag = !pause_flag;
129 break;
130 case GP_KEY_G:
131 space->gay = 1;
132 break;
133 case GP_KEY_T:
134 space->gay = 0;
135 break;
137 break;
138 case GP_EV_SYS:
139 switch(ev.code) {
140 case GP_EV_SYS_QUIT:
141 GP_BackendExit(backend);
142 exit(0);
143 break;
144 case GP_EV_SYS_RESIZE:
145 GP_BackendResizeAck(backend);
146 space_destroy(space);
147 space = space_create(particles,
148 10<<8, 10<<8,
149 (pixmap->w - 10)<<8,
150 (pixmap->h - 10)<<8);
151 break;
153 break;
157 if (!pause_flag) {
158 space_time_tick(space, 1);
159 space_draw_particles(pixmap, space);
160 GP_BackendFlip(backend);
164 GP_BackendExit(backend);
166 return 0;