Added .gitignore
[gfxprim.git] / targets / sdl / tests / trianglefps.c
blob8204521cee4c49e8d54a5313681e0ea2525fde4d
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 <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <SDL/SDL.h>
31 #include "GP.h"
32 #include "GP_SDL.h"
34 /* Draw filled triangles? */
35 int filled = 0;
37 /* The surface used as a display (in fact it is a software surface). */
38 SDL_Surface *display = NULL;
39 GP_Context context;
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.
52 SDL_TimerID timer;
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);
61 return 1000;
65 void draw_frame(void)
67 int x0 = 0;
68 int y0 = random() % display->h;
69 int x1 = display->w;
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);
75 GP_Pixel pixel;
76 GP_ColorToPixel(&context, color, &pixel);
78 if (filled) {
79 GP_FillTriangle(&context, x0, y0, x1, y1, x2, y2, pixel);
80 } else {
81 GP_Triangle(&context, x0, y0, x1, y1, x2, y2, pixel);
85 void event_loop(void)
87 SDL_Event event;
89 for (;;) {
90 while (SDL_PollEvent(&event) > 0) {
91 switch (event.type) {
92 case SDL_USEREVENT:
93 SDL_Flip(display);
94 fprintf(stdout, "%d triangles/second, min = %d, max = %d\r", fps, fps_min, fps_max);
95 fflush(stdout);
97 /* Update frames per second */
98 if (fps < fps_min)
99 fps_min = fps;
100 if (fps > fps_max)
101 fps_max = fps;
102 fps = 0;
103 break;
104 case SDL_KEYDOWN:
105 case SDL_QUIT:
106 return;
109 draw_frame();
110 fps++;
114 int main(int argc, char ** argv)
116 int bit_depth = 0;
118 int i;
119 for (i = 1; i < argc; i++) {
120 if (strcmp(argv[i], "-f") == 0) {
121 filled = 1;
123 else if (strcmp(argv[i], "-16") == 0) {
124 bit_depth = 16;
126 else if (strcmp(argv[i], "-24") == 0) {
127 bit_depth = 24;
129 else if (strcmp(argv[i], "-32") == 0) {
130 bit_depth = 32;
134 /* Initialize SDL */
135 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
136 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
137 return 1;
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());
144 goto fail;
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);
168 if (timer == 0) {
169 fprintf(stderr, "Could not set up timer: %s\n", SDL_GetError());
170 goto fail;
173 /* Enter the event loop */
174 event_loop();
176 /* Preserve the last result by a newline */
177 fprintf(stdout, "\n");
179 /* We're done */
180 SDL_Quit();
181 return 0;
183 fail:
184 SDL_Quit();
185 return 1;