gfx: Working on Anti Aliased rectangles
[gfxprim.git] / tests / SDL / aatest.c
blob247c5535a19d5a30e8bfacab8daa5d56b1a5b3e6
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-2012 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <SDL/SDL.h>
30 #include "GP.h"
31 #include "GP_SDL.h"
33 SDL_Surface *display = NULL;
34 GP_Context context;
36 static GP_Coord x = 10<<8;
37 static GP_Coord y = 10<<8;
39 SDL_UserEvent timer_event;
41 GP_Pixel red_pixel, green_pixel, blue_pixel, white_pixel;
43 static void draw(void)
45 GP_Context *ctx = &context;
47 GP_Fill(ctx, white_pixel);
49 GP_Coord i;
51 for (i = 0; i < 24; i++) {
52 GP_FillRect_AA(ctx, x, y + ((10*i)<<8),
53 x + (60<<8), y + ((10*i)<<8) + 64*i + 64, 0);
55 GP_FillRect_AA(ctx, x + (80<<8), y + ((10*i)<<8) + 64,
56 x + (140<<8), y + ((10*i)<<8) + 64*i + 128, 0);
58 GP_FillRect_AA(ctx, x + (160<<8), y + ((10*i)<<8) + 128,
59 x + (220<<8), y + ((10*i)<<8) + 64*i + 192, 0);
61 GP_FillRect_AA(ctx, x + (240<<8), y + ((10*i)<<8) + 192,
62 x + (300<<8), y + ((10*i)<<8) + 64*i + 256, 0);
63 printf("--------------------------------------------------------\n");
66 SDL_Flip(display);
69 void event_loop(void)
71 SDL_Event event;
73 while (SDL_WaitEvent(&event) > 0) {
74 switch (event.type) {
75 case SDL_KEYDOWN:
76 switch (event.key.keysym.sym) {
77 case SDLK_DOWN:
78 y += 64;
79 draw();
80 break;
81 case SDLK_UP:
82 y -= 64;
83 draw();
84 break;
85 case SDLK_LEFT:
86 x -= 64;
87 draw();
88 break;
89 case SDLK_RIGHT:
90 x += 64;
91 draw();
92 break;
93 case SDLK_x:
94 context.x_swap = !context.x_swap;
95 draw();
96 break;
97 case SDLK_y:
98 context.y_swap = !context.y_swap;
99 draw();
100 break;
101 case SDLK_r:
102 context.axes_swap = !context.axes_swap;
103 draw();
104 break;
105 case SDLK_ESCAPE:
106 case SDLK_q:
107 return;
108 default:
109 break;
111 break;
112 case SDL_QUIT:
113 return;
118 int main(int argc, char **argv)
120 int display_bpp = 0;
122 int i;
123 for (i = 1; i < argc; i++) {
124 if (strcmp(argv[i], "-16") == 0) {
125 display_bpp = 16;
126 } else if (strcmp(argv[i], "-24") == 0) {
127 display_bpp = 24;
131 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
132 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
133 return 1;
136 display = SDL_SetVideoMode(320, 240, display_bpp, SDL_SWSURFACE);
137 if (display == NULL) {
138 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
139 goto fail;
143 printf("Display surface properties:\n");
144 printf(" width: %4d, height: %4d, pitch: %4d\n",
145 display->w, display->h, display->pitch);
146 printf(" bits per pixel: %2d, bytes per pixel: %2d\n",
147 display->format->BitsPerPixel, display->format->BytesPerPixel);
149 GP_SDL_ContextFromSurface(&context, display);
151 red_pixel = GP_ColorToContextPixel(GP_COL_RED, &context);
152 green_pixel = GP_ColorToContextPixel(GP_COL_GREEN, &context);
153 blue_pixel = GP_ColorToContextPixel(GP_COL_BLUE, &context);
154 white_pixel = GP_ColorToContextPixel(GP_COL_WHITE, &context);
156 draw();
158 event_loop();
159 SDL_Quit();
160 return 0;
162 fail:
163 SDL_Quit();
164 return 1;