1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
33 /* The surface used as a display (in fact it is a software surface). */
34 SDL_Surface
*display
= NULL
;
36 /* Timer used for refreshing the display */
39 /* An event used for signaling that the timer was triggered. */
40 SDL_UserEvent timer_event
;
42 /* Values for color pixels in display format. */
43 static long colors
[GP_BASIC_COLOR_COUNT
];
45 Uint32
timer_callback(__attribute__((unused
)) Uint32 interval
,
46 __attribute__((unused
)) void * param
)
48 timer_event
.type
= SDL_USEREVENT
;
49 SDL_PushEvent((SDL_Event
*) &timer_event
);
53 double start_angle
= 0.0;
55 void redraw_screen(void)
59 int xcenter
= display
->w
/2;
60 int ycenter
= display
->h
/2;
62 SDL_LockSurface(display
);
64 GP_Clear(display
, colors
[GP_BLACK
]);
66 for (angle
= 0.0; angle
< 2*M_PI
; angle
+= 0.1) {
67 x
= (int) (display
->w
/2 * cos(start_angle
+ angle
));
68 y
= (int) (display
->h
/2 * sin(start_angle
+ angle
));
70 Uint8 r
= 127.0 + 127.0 * cos(start_angle
+ angle
);
71 Uint8 g
= 127.0 + 127.0 * sin(start_angle
+ angle
);
73 Uint32 color
= SDL_MapRGB(display
->format
, r
, 0, g
);
76 * Draw the line forth and back to detect any pixel change
77 * between one direction and the other.
79 GP_Line(display
, color
, xcenter
, ycenter
, xcenter
+ x
, ycenter
+ y
);
80 GP_Line(display
, color
, xcenter
+ x
, ycenter
+ y
, xcenter
, ycenter
);
84 GP_HLine(display
, colors
[GP_WHITE
], 0, display
->w
, ycenter
);
85 GP_VLine(display
, colors
[GP_WHITE
], xcenter
, 0, display
->h
);
87 SDL_UnlockSurface(display
);
94 while (SDL_WaitEvent(&event
) > 0) {
100 if (start_angle
> 2*M_PI
) {
114 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0) {
115 fprintf(stderr
, "Could not initialize SDL: %s\n", SDL_GetError());
119 /* Create a window with a software back surface */
120 display
= SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE
);
121 if (display
== NULL
) {
122 fprintf(stderr
, "Could not open display: %s\n", SDL_GetError());
126 /* Print basic information about the surface */
127 printf("Display surface properties:\n");
128 printf(" width: %4d, height: %4d, pitch: %4d\n",
129 display
->w
, display
->h
, display
->pitch
);
130 printf(" bits per pixel: %2d, bytes per pixel: %2d\n",
131 display
->format
->BitsPerPixel
, display
->format
->BytesPerPixel
);
134 GP_LoadBasicColors(display
, colors
);
136 /* Set up a clipping rectangle to test proper clipping of pixels */
137 SDL_Rect clip_rect
= { 10, 10, 620, 460 };
138 SDL_SetClipRect(display
, &clip_rect
);
140 /* Set up the refresh timer */
141 timer
= SDL_AddTimer(30, timer_callback
, NULL
);
143 fprintf(stderr
, "Could not set up timer: %s\n", SDL_GetError());
147 /* Enter the event loop */