File: TestGfxTexture.c

package info (click to toggle)
sdlgfx 2.0.25-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 9,484 kB
  • sloc: ansic: 18,523; sh: 13,444; makefile: 83; javascript: 37; perl: 5
file content (254 lines) | stat: -rw-r--r-- 5,707 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* 

TestGfxTexture.c: test program for textured polygon routine
                  (Contributed by Kees Jongenburger)

(C) A. Schiffler, December 2006, zlib license

*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "SDL.h"

#ifdef WIN32
#include <windows.h>
#include "SDL_gfxPrimitives.h"
#include "SDL_framerate.h"
#else
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_framerate.h"
#endif

void HandleEvent()
{
	SDL_Event event; 

	/* Check for events */
	while ( SDL_PollEvent(&event) ) {
		switch (event.type) {
			 case SDL_KEYDOWN:
			 case SDL_QUIT:
				 exit(0);
				 break;
		}
	}
}

void ClearScreen(SDL_Surface *screen)
{
	int i;
	/* Set the screen to black */
	if ( SDL_LockSurface(screen) == 0 ) {
		Uint32 black;
		Uint8 *pixels;
		black = SDL_MapRGB(screen->format, 0, 0, 0);
		pixels = (Uint8 *)screen->pixels;
		for ( i=0; i<screen->h; ++i ) {
			memset(pixels, black,
				screen->w*screen->format->BytesPerPixel);
			pixels += screen->pitch;
		}
		SDL_UnlockSurface(screen);
	}
}

#define NUM_POINTS 150

void Draw(SDL_Surface *screen)
{
	int i,rate,x,y,dx,dy;
	int psize = NUM_POINTS; 
	double sin_start = 0;
	double sin_amp = 100;
	Sint16 polygon_x[NUM_POINTS], polygon_y[NUM_POINTS];
	Sint16 polygon_alpha_x[4], polygon_alpha_y[4];
	SDL_Surface *texture;
	SDL_Surface *texture_alpha;
	FPSmanager fpsm;
	int width_half = screen->w/2;
	int height_half = screen->h/2;

	/* Load texture surfaces */
	texture = SDL_LoadBMP("texture.bmp");
	texture_alpha = SDL_LoadBMP("texture_alpha.bmp");
	SDL_SetAlpha(texture_alpha, SDL_SRCALPHA, 128);

	/* Initialize variables */
	srand((int)time(NULL));
	i=0;
	x=width_half;
	y=height_half;
	dx=7;
	dy=5;

	/* Initialize Framerate manager */  
	SDL_initFramerate(&fpsm);

	/* Polygon for blended texture */
	polygon_alpha_x[0]= 0;
	polygon_alpha_y[0]= 0;
	polygon_alpha_x[1]= width_half;
	polygon_alpha_y[1]= 0;
	polygon_alpha_x[2]= screen->w*2 / 3;
	polygon_alpha_y[2]= screen->h;
	polygon_alpha_x[3]= 0;
	polygon_alpha_y[3]= screen->h;

	/* Set/switch framerate */
	rate=25;
	SDL_setFramerate(&fpsm,rate);

	/* Drawing loop */
	while (1) {

		/* Generate wave polygon */
		sin_start++;
		polygon_x[0]= 0;
		polygon_y[0]= screen->h;
		polygon_x[1]= 0;
		polygon_y[1]= height_half;    
		for (i=2; i < psize -2 ; i++){
			polygon_x[i]= (screen->w  * (i-2)) / (psize -5) ;
			polygon_y[i]= (Sint16)(sin(sin_start/100) * 200) + height_half - (Sint16)(sin((i + sin_start) / 20) * sin_amp);
		}

		polygon_x[psize-2]= screen->w;
		polygon_y[psize-2]= height_half;
		polygon_x[psize-1]= screen->w;
		polygon_y[psize-1]= screen->h;

		/* Event handler */
		HandleEvent();

		/* Black screen */
		ClearScreen(screen);

		/* Move */
		x += dx;
		y += dy;

		/* Reflect */
		if ((x<0) || (x>screen->w)) { dx=-dx; }
		if ((y<0) || (y>screen->h)) { dy=-dy; }

		/* Draw */
		texturedPolygon(screen,polygon_x,polygon_y,psize,texture, -(screen->w  * (Sint16)(sin_start-2)) / (psize - 5), -(Sint16)(sin(sin_start/100) * 200));
		texturedPolygon(screen,polygon_alpha_x,polygon_alpha_y,4,texture_alpha,(Sint16)sin_start,-(Sint16)sin_start);

		/* Display by flipping screens */
		SDL_Flip(screen);

		/* Delay to fix rate */                   
		SDL_framerateDelay(&fpsm);  
	}
}

/* ======== */

int main ( int argc, char *argv[] )
{
	SDL_Surface *screen;
	int w, h;
	int desired_bpp;
	Uint32 video_flags;

	/* Title */
	fprintf (stderr,"texturedPolygon test\n");

	/* Set default options and check command-line */
	w = 640;
	h = 480;
	desired_bpp = 0;
	video_flags = 0;
	while ( argc > 1 ) {
		if ( strcmp(argv[1], "-width") == 0 ) {
			if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
				argv += 2;
				argc -= 2;
			} else {
				fprintf(stderr,
					"The -width option requires an argument\n");
				exit(1);
			}
		} else
			if ( strcmp(argv[1], "-height") == 0 ) {
				if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
					argv += 2;
					argc -= 2;
				} else {
					fprintf(stderr,
						"The -height option requires an argument\n");
					exit(1);
				}
			} else
				if ( strcmp(argv[1], "-bpp") == 0 ) {
					if ( argv[2] ) {
						desired_bpp = atoi(argv[2]);
						argv += 2;
						argc -= 2;
					} else {
						fprintf(stderr,
							"The -bpp option requires an argument\n");
						exit(1);
					}
				} else
					if ( strcmp(argv[1], "-warp") == 0 ) {
						video_flags |= SDL_HWPALETTE;
						argv += 1;
						argc -= 1;
					} else
						if ( strcmp(argv[1], "-hw") == 0 ) {
							video_flags |= SDL_HWSURFACE;
							argv += 1;
							argc -= 1;
						} else
							if ( strcmp(argv[1], "-fullscreen") == 0 ) {
								video_flags |= SDL_FULLSCREEN;
								argv += 1;
								argc -= 1;
							} else
								break;
	}

	/* Force double buffering */
	video_flags |= SDL_DOUBLEBUF;

	/* Initialize SDL */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr,
			"Couldn't initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);			/* Clean up on exit */

	/* Initialize the display */
	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
			w, h, desired_bpp, SDL_GetError());
		exit(1);
	}

	/* Show some info */
	printf("Set %dx%dx%d mode\n",
		screen->w, screen->h, screen->format->BitsPerPixel);
	printf("Video surface located in %s memory.\n",
		(screen->flags&SDL_HWSURFACE) ? "video" : "system");

	/* Check for double buffering */
	if ( screen->flags & SDL_DOUBLEBUF ) {
		printf("Double-buffering enabled - good!\n");
	}

	/* Set the window manager title bar */
	SDL_WM_SetCaption("texturedPolygon test", "texturedPolygon");

	/* Do all the drawing work */
	Draw (screen);

	return(0);
}