Added .gitignore
[gfxprim.git] / old_tests / linetest.c
blob962f577093f2048d9f47aa967b8869a882559bfa
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 /* Timer used for refreshing the display */
37 SDL_TimerID timer;
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);
50 return 30;
53 double start_angle = 0.0;
55 void redraw_screen(void)
57 double angle;
58 int x, y;
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);
83 /* axes */
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);
90 void event_loop(void)
92 SDL_Event event;
94 while (SDL_WaitEvent(&event) > 0) {
95 switch (event.type) {
96 case SDL_USEREVENT:
97 redraw_screen();
98 SDL_Flip(display);
99 start_angle += 0.01;
100 if (start_angle > 2*M_PI) {
101 start_angle = 0.0;
103 break;
104 case SDL_KEYDOWN:
105 case SDL_QUIT:
106 return;
111 int main(void)
113 /* Initialize SDL */
114 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
115 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
116 return 1;
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());
123 goto fail;
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);
133 /* Get colors */
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);
142 if (timer == 0) {
143 fprintf(stderr, "Could not set up timer: %s\n", SDL_GetError());
144 goto fail;
147 /* Enter the event loop */
148 event_loop();
150 /* We're done */
151 SDL_Quit();
152 return 0;
154 fail:
155 SDL_Quit();
156 return 1;