Remove tests for color and palette.
[gfxprim.git] / old_tests / linefps.c
blobd88d427c8de68a721382468d1fb5379709c1fe4d
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"
33 /* The surface used as a display (in fact it is a software surface). */
34 SDL_Surface *display = NULL;
36 /* Frames per second. */
37 int fps = 0, fps_min = 1000000, fps_max = 0;
40 * Timer used for FPS measurement and key reactions.
41 * SDL_USEREVENT is triggered each second.
44 SDL_TimerID timer;
46 SDL_UserEvent timer_event;
48 Uint32 timer_callback(__attribute__((unused)) Uint32 interval,
49 __attribute__((unused)) void * param)
51 timer_event.type = SDL_USEREVENT;
52 SDL_PushEvent((SDL_Event *) &timer_event);
53 return 1000;
56 /* Values for color pixels in display format. */
57 static long colors[GP_BASIC_COLOR_COUNT];
59 void draw_frame(void)
61 int x0 = 0;
62 int y0 = random() % display->h;
63 int x1 = display->w;
64 int y1 = random() % display->h;
65 long color = SDL_MapRGB(display->format, random() % 255, random() % 255, random() % 255);
67 GP_Line(display, color, x0, y0, x1, y1);
70 void event_loop(void)
72 SDL_Event event;
74 for (;;) {
75 while (SDL_PollEvent(&event) > 0) {
76 switch (event.type) {
77 case SDL_USEREVENT:
78 SDL_Flip(display);
79 fprintf(stdout, "%d FPS, min = %d, max = %d\r", fps, fps_min, fps_max);
80 fflush(stdout);
82 /* Update frames per second */
83 if (fps < fps_min)
84 fps_min = fps;
85 if (fps > fps_max)
86 fps_max = fps;
87 fps = 0;
88 break;
89 case SDL_KEYDOWN:
90 case SDL_QUIT:
91 return;
94 draw_frame();
95 fps++;
99 int main(void)
101 /* Initialize SDL */
102 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
103 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
104 return 1;
107 /* Create a window with a software back surface */
108 display = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
109 if (display == NULL) {
110 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
111 goto fail;
114 /* Print basic information about the surface */
115 printf("Display surface properties:\n");
116 printf(" width: %4d, height: %4d, pitch: %4d\n",
117 display->w, display->h, display->pitch);
118 printf(" bits per pixel: %2d, bytes per pixel: %2d\n",
119 display->format->BitsPerPixel, display->format->BytesPerPixel);
121 /* Get colors */
122 GP_LoadBasicColors(display, colors);
124 /* Set up a clipping rectangle to test proper clipping of pixels */
125 SDL_Rect clip_rect = { 10, 10, 620, 460 };
126 SDL_SetClipRect(display, &clip_rect);
128 /* Set up the timer */
129 timer = SDL_AddTimer(1000, timer_callback, NULL);
130 if (timer == 0) {
131 fprintf(stderr, "Could not set up timer: %s\n", SDL_GetError());
132 goto fail;
135 /* Enter the event loop */
136 event_loop();
138 /* Preserve the last result by a newline */
139 fprintf(stdout, "\n");
141 /* We're done */
142 SDL_Quit();
143 return 0;
145 fail:
146 SDL_Quit();
147 return 1;