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 *****************************************************************************/
34 /* Draw filled triangles? */
37 /* The surface used as a display (in fact it is a software surface). */
38 SDL_Surface
*display
= NULL
;
41 /* Frames per second. */
42 int fps
= 0, fps_min
= 1000000, fps_max
= 0;
44 /* Color pixel values in display format. */
45 GP_Pixel black
, white
;
48 * Timer used for FPS measurement and key reactions.
49 * SDL_USEREVENT is triggered each second.
54 SDL_UserEvent timer_event
;
56 Uint32
timer_callback(__attribute__((unused
)) Uint32 interval
,
57 __attribute__((unused
)) void * param
)
59 timer_event
.type
= SDL_USEREVENT
;
60 SDL_PushEvent((SDL_Event
*) &timer_event
);
68 int y0
= random() % display
->h
;
70 int y1
= random() % display
->h
;
71 int x2
= display
->w
/2;
72 int y2
= random() % display
->h
;
73 GP_Color color
= GP_RGB888_PACK(random() % 255, random() % 255, random() % 255);
76 GP_ColorToPixel(&context
, color
, &pixel
);
79 GP_FillTriangle(&context
, x0
, y0
, x1
, y1
, x2
, y2
, pixel
);
81 GP_Triangle(&context
, x0
, y0
, x1
, y1
, x2
, y2
, pixel
);
90 while (SDL_PollEvent(&event
) > 0) {
94 fprintf(stdout
, "%d triangles/second, min = %d, max = %d\r", fps
, fps_min
, fps_max
);
97 /* Update frames per second */
114 int main(int argc
, char ** argv
)
119 for (i
= 1; i
< argc
; i
++) {
120 if (strcmp(argv
[i
], "-f") == 0) {
123 else if (strcmp(argv
[i
], "-16") == 0) {
126 else if (strcmp(argv
[i
], "-24") == 0) {
129 else if (strcmp(argv
[i
], "-32") == 0) {
135 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0) {
136 fprintf(stderr
, "Could not initialize SDL: %s\n", SDL_GetError());
140 /* Create a window with a software back surface */
141 display
= SDL_SetVideoMode(640, 480, bit_depth
, SDL_SWSURFACE
);
142 if (display
== NULL
) {
143 fprintf(stderr
, "Could not open display: %s\n", SDL_GetError());
147 /* Print basic information about the surface */
148 printf("Display surface properties:\n");
149 printf(" width: %4d, height: %4d, pitch: %4d\n",
150 display
->w
, display
->h
, display
->pitch
);
151 printf(" bits per pixel: %2d, bytes per pixel: %2d\n",
152 display
->format
->BitsPerPixel
, display
->format
->BytesPerPixel
);
153 printf("Machine properties:\n");
154 printf(" sizeof(int) = %u, sizeof(long) = %u\n",
155 (unsigned int)sizeof(int), (unsigned int)sizeof(long));
157 /* Set up a clipping rectangle to test proper clipping of pixels */
158 SDL_Rect clip_rect
= { 10, 10, 620, 460 };
159 SDL_SetClipRect(display
, &clip_rect
);
161 GP_SDL_ContextFromSurface(&context
, display
);
163 GP_ColorNameToPixel(&context
, GP_COL_WHITE
, &white
);
164 GP_ColorNameToPixel(&context
, GP_COL_BLACK
, &black
);
166 /* Set up the timer */
167 timer
= SDL_AddTimer(1000, timer_callback
, NULL
);
169 fprintf(stderr
, "Could not set up timer: %s\n", SDL_GetError());
173 /* Enter the event loop */
176 /* Preserve the last result by a newline */
177 fprintf(stdout
, "\n");