Added simple counters (+tests)
[gfxprim.git] / old_tests / pixeltest.c
blobb4bea6ec02f999651066d1b19ab260c57372fa79
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-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <SDL/SDL.h>
30 #include "GP.h"
32 /* The surface used as a display (in fact it is a software surface). */
33 SDL_Surface *display = NULL;
35 /* Timer used for refreshing the display */
36 SDL_TimerID timer;
38 /* An event used for signaling that the timer was triggered. */
39 SDL_UserEvent timer_event;
41 /* Values for color pixels in display format. */
42 long red;
43 long green;
44 long blue;
46 Uint32 timer_callback(__attribute__((unused)) Uint32 interval,
47 __attribute__((unused)) void *param)
49 timer_event.type = SDL_USEREVENT;
50 SDL_PushEvent((SDL_Event *) &timer_event);
51 return 30;
54 void draw_pixels(void)
56 long pixel;
57 int x = random() % 320;
58 int y = random() % 240;
60 SDL_LockSurface(display);
61 pixel = GP_GetPixel(display, x, y);
63 if (pixel == green)
64 GP_SetPixel(display, blue, x, y);
65 else
66 GP_SetPixel(display, green, x, y);
68 SDL_UnlockSurface(display);
71 void event_loop(void)
73 SDL_Event event;
75 while (SDL_WaitEvent(&event) > 0) {
77 switch (event.type) {
78 case SDL_USEREVENT:
79 draw_pixels();
80 SDL_Flip(display);
81 break;
82 case SDL_KEYDOWN:
83 case SDL_QUIT:
84 return;
89 int main(void)
91 /* Initialize SDL */
92 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
93 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
94 return 1;
97 /* Create a window with a software back surface */
98 display = SDL_SetVideoMode(320, 240, 0, SDL_SWSURFACE);
99 if (display == NULL) {
100 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
101 goto fail;
104 /* Print basic information about the surface */
105 printf("Display surface properties:\n");
106 printf(" width: %4d, height: %4d, pitch: %4d\n",
107 display->w, display->h, display->pitch);
108 printf(" bits per pixel: %2d, bytes per pixel: %2d\n",
109 display->format->BitsPerPixel, display->format->BytesPerPixel);
111 /* Get colors */
112 red = SDL_MapRGB(display->format, 255, 0, 0);
113 green = SDL_MapRGB(display->format, 0, 255, 0);
114 blue = SDL_MapRGB(display->format, 0, 0, 255);
116 /* Set up a clipping rectangle to test proper clipping of pixels */
117 SDL_Rect clip_rect = {10, 10, 300, 220};
118 SDL_SetClipRect(display, &clip_rect);
120 /* Set up the refresh timer */
121 timer = SDL_AddTimer(30, timer_callback, NULL);
122 if (timer == 0) {
123 fprintf(stderr, "Could not set up timer: %s\n", SDL_GetError());
124 goto fail;
127 /* Enter the event loop */
128 event_loop();
130 /* We're done */
131 SDL_Quit();
132 return 0;
134 fail:
135 SDL_Quit();
136 return 1;