loaders: Add meta-data clear method and double type.
[gfxprim.git] / tests / SDL / linetest.c
blob5a2e08239d56fc627179aca0947668089272f2ee
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-2011 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 /* The surface used as a display (in fact it is a software surface). */
35 SDL_Surface *display = NULL;
36 GP_Context context;
38 /* Timer used for refreshing the display */
39 SDL_TimerID timer;
41 /* An event used for signaling that the timer was triggered. */
42 SDL_UserEvent timer_event;
44 /* Values for color pixels in display format. */
45 GP_Pixel black, white;
47 Uint32 timer_callback(__attribute__((unused)) Uint32 interval,
48 __attribute__((unused)) void * param)
50 timer_event.type = SDL_USEREVENT;
51 SDL_PushEvent((SDL_Event *) &timer_event);
52 return 30;
55 double start_angle = 0.0;
57 static int aa_flag = 0;
58 static int pause_flag = 0;
60 void redraw_screen(void)
62 double angle;
63 int x, y;
64 int xcenter = display->w/2;
65 int ycenter = display->h/2;
67 SDL_LockSurface(display);
68 GP_Fill(&context, black);
70 for (angle = 0.0; angle < 2*M_PI; angle += 0.1) {
71 x = (int) (display->w/2 * cos(start_angle + angle));
72 y = (int) (display->h/2 * sin(start_angle + angle));
74 Uint8 r = 127.0 + 127.0 * cos(start_angle + angle);
75 Uint8 b = 127.0 + 127.0 * sin(start_angle + angle);
77 GP_Pixel pixel;
78 pixel = GP_RGBToPixel(r, 0, b, context.pixel_type);
80 if (aa_flag) {
81 GP_LineAA_Raw(&context, GP_FP_FROM_INT(xcenter), GP_FP_FROM_INT(ycenter),
82 GP_FP_FROM_INT(xcenter + x), GP_FP_FROM_INT(ycenter + y), pixel);
83 } else {
84 GP_Line(&context, xcenter + x, ycenter + y, xcenter, ycenter, pixel);
85 GP_Line(&context, xcenter, ycenter, xcenter + x, ycenter + y, pixel);
89 /* axes */
90 // GP_HLineXYW(&context, 0, ycenter, display->w, white);
91 // GP_VLineXYH(&context, xcenter, 0, display->h, white);
93 SDL_UnlockSurface(display);
96 void event_loop(void)
98 SDL_Event event;
100 while (SDL_WaitEvent(&event) > 0) {
101 switch (event.type) {
102 case SDL_USEREVENT:
103 redraw_screen();
104 SDL_Flip(display);
106 if (pause_flag)
107 continue;
109 start_angle += 0.01;
110 if (start_angle > 2*M_PI) {
111 start_angle = 0.0;
113 break;
114 case SDL_KEYDOWN:
115 switch (event.key.keysym.sym) {
116 case SDLK_a:
117 aa_flag = !aa_flag;
118 break;
119 case SDLK_p:
120 pause_flag = !pause_flag;
121 break;
122 default:
123 return;
125 break;
126 case SDL_QUIT:
127 return;
132 int main(int argc, char **argv)
134 GP_RetCode retcode;
135 retcode = GP_SDL_VideoInit(&context, 640, 480, argc, argv);
136 if (retcode != GP_ESUCCESS) {
137 fprintf(stderr, "Video initialization failed: %s\n",
138 GP_RetCodeName(retcode));
139 return 1;
142 display = SDL_GetVideoSurface();
143 GP_SDL_ContextFromSurface(&context, display);
145 /* Load colors in display format */
146 black = GP_ColorToContextPixel(GP_COL_BLACK, &context);
147 white = GP_ColorToContextPixel(GP_COL_WHITE, &context);
149 /* Set up the refresh timer */
150 timer = SDL_AddTimer(30, timer_callback, NULL);
151 if (timer == 0) {
152 fprintf(stderr, "Could not set up timer: %s\n", SDL_GetError());
153 goto fail;
156 /* Enter the event loop */
157 event_loop();
159 /* We're done */
160 SDL_Quit();
161 return 0;
163 fail:
164 SDL_Quit();
165 return 1;