File: shapes.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (308 lines) | stat: -rw-r--r-- 9,608 bytes parent folder | download | duplicates (3)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2010,2011,2012,2016 Adrien Boussicault, Marc Zipstein
 *
 *
 *    This Library is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this Library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MLV_shape.h"

#include "platform.h"
#include "image.h"

#ifndef MEMORY_DEBUG
#if defined( OS_WINDOWS )
#	include <SDL/SDL_gfxPrimitives.h>
#elif defined( OS_APPLE )
#	include <SDL/SDL_gfxPrimitives.h>
#else
#	include <SDL/SDL_gfxPrimitives.h>
#endif
#else
#include "memory_debug.h"
#endif

#include "data_structure.h"
#include "warning_error.h"

#include "memory_management.h"

#include <stdarg.h>

#include "data_structure.h"

extern DataMLV *MLV_data;


///////////////////////////////////////////////////////////////////////////////
// Drawing operation on images                                               //
///////////////////////////////////////////////////////////////////////////////


void MLV_draw_circle_on_image(int x, int y, int radius, MLV_Color color, MLV_Image* image){
	circleColor( image->surface,	x, y, radius, color );
}

void MLV_draw_filled_circle_on_image(int x, int y, int radius, MLV_Color color, MLV_Image* image){
	filledCircleColor( image->surface, x, y, radius,  color);
}

void MLV_draw_ellipse_on_image(int x, int y, int radius_x, int radius_y, MLV_Color color, MLV_Image* image){
	ellipseColor(image->surface,x,y,radius_x,radius_y,color);      
}

void MLV_draw_filled_ellipse_on_image(int x, int y, int radius_x, int radius_y, MLV_Color color, MLV_Image* image){
	filledEllipseColor(image->surface,x,y,radius_x,radius_y,color);      
}

void MLV_draw_polygon_on_image( const int* vx, const int* vy, int nb_points, MLV_Color color, MLV_Image* image){
	Sint16* tmp_vx = MLV_MALLOC( nb_points, Sint16 );
	Sint16* tmp_vy = MLV_MALLOC( nb_points, Sint16 );
	int i;
	for( i=0; i< nb_points; i++){
		tmp_vx[i] = vx[i];
		tmp_vy[i] = vy[i];
	}
	polygonColor( image->surface, tmp_vx, tmp_vy, nb_points, color);
	MLV_FREE( tmp_vx, Sint16 );
	MLV_FREE( tmp_vy, Sint16 );
}

void MLV_draw_triangle_on_image(
	int ax, int ay, int bx, int by, int cx, int cy, MLV_Color color,
	MLV_Image* image
){
	int coordonne_x[3] = { ax, bx, cx };
	int coordonne_y[3] = { ay, by, cy };
	MLV_draw_polygon_on_image( coordonne_x, coordonne_y, 3, color, image );
}

void MLV_draw_filled_polygon_on_image(
	const int* vx, const int* vy, int nb_points, MLV_Color color, 
	MLV_Image* image
){
	Sint16* tmp_vx = MLV_MALLOC( nb_points, Sint16 );
	Sint16* tmp_vy = MLV_MALLOC( nb_points, Sint16 );
	int i;
	for( i=0; i< nb_points; i++){
		tmp_vx[i] = vx[i];
		tmp_vy[i] = vy[i];
	}
	filledPolygonColor( image->surface, tmp_vx, tmp_vy, nb_points, color);
	MLV_FREE( tmp_vx, Sint16 );
	MLV_FREE( tmp_vy, Sint16 );
}

void MLV_draw_filled_triangle_on_image(
	int ax, int ay, int bx, int by, int cx, int cy, MLV_Color color,
	MLV_Image* image
){
	int coordonne_x[3] = { ax, bx, cx };
	int coordonne_y[3] = { ay, by, cy };
	MLV_draw_filled_polygon_on_image(
		coordonne_x, coordonne_y, 3, color, image
	);
}

void MLV_draw_bezier_curve_on_image( const int* vx, const int* vy, int nb_points, MLV_Color color, MLV_Image* image){
	Sint16* tmp_vx = MLV_MALLOC( nb_points, Sint16 );
	Sint16* tmp_vy = MLV_MALLOC( nb_points, Sint16 );
	int i;
	for( i=0; i< nb_points; i++){
		tmp_vx[i] = vx[i];
		tmp_vy[i] = vy[i];
	}
	bezierColor( image->surface, tmp_vx, tmp_vy, nb_points, 6 , color);
	MLV_FREE( tmp_vx, Sint16 );
	MLV_FREE( tmp_vy, Sint16 );
}


void MLV_draw_rectangle_on_image(int x, int y, int width, int height, MLV_Color color, MLV_Image* image){
	rectangleColor(image->surface, x, y, x+width-1, y+height-1, color);
}

void MLV_draw_filled_rectangle_on_image(int x, int y, int width, int height, MLV_Color color, MLV_Image* image){
	boxColor(image->surface, x, y, x+width-1, y+height-1, color);
}

void MLV_draw_line_on_image(int x1, int y1, int x2, int y2, MLV_Color color, MLV_Image* image){
	lineColor( image->surface, x1, y1, x2, y2, color);
}

void draw_pixel_on_sdl_surface( SDL_Surface* surface, int x, int y, MLV_Color color ){
#if 0  // Je ne me souvient plus pourquoi pexelColor ne fonctionnait pas. Je crois que c'est la composante alpha qui pose problème.
	pixelColor(MLV_data->screen, x, y, color);
#else
	if( !( 0<= x && x<surface->w && 0<=y && y<surface->h ) ){
		return;
	}
	SDL_LockSurface(surface);

	SDL_PixelFormat *fmt;
	Uint32 *pixel;

	fmt = surface->format;
	pixel = ((Uint32*) surface->pixels)+ y*surface->pitch/4 + x;

	Uint8 red, blue, green, alpha;
	MLV_convert_color_to_rgba( color, &red, &green, &blue, &alpha );

	int red_image, green_image, blue_image, alpha_image;

	get_pixel_on_image_unsafe( 
		surface, x, y,
		&red_image, &green_image, &blue_image, &alpha_image 
	);

	int inv_alpha = 255 - alpha;
	Uint32 red32 = ( alpha * red + inv_alpha * red_image )/255;
	Uint32 blue32 = ( alpha * blue + inv_alpha * blue_image )/255;
	Uint32 green32 = ( alpha * green + inv_alpha * green_image )/255;
	Uint32 alpha32 = alpha_image;

	/* set the Red component */
	red32 = red32 >> fmt->Rloss;
	red32 = red32 << fmt->Rshift;

	/* set the Green component */
	green32 = green32 >> fmt->Gloss;
	green32 = green32 << fmt->Gshift;

	/* set the Blue component */
	blue32 = blue32 >> fmt->Bloss;
	blue32 = blue32 << fmt->Bshift;

	/* set the Alpha component */
	alpha32 = alpha32 >> fmt->Aloss;
	alpha32 = alpha32 << fmt->Ashift;

	*pixel = red32 | green32 | blue32 | alpha32;

	SDL_UnlockSurface(surface);
#endif
}

void draw_pixel( MLV_Image* image, int x, int y, MLV_Color color ){
	SDL_Surface* surface = image->surface;
	draw_pixel_on_sdl_surface( surface, x, y, color );
}

void MLV_draw_pixel_on_image(int x, int y, MLV_Color color, MLV_Image* image){
	draw_pixel( image, x, y, color);
}

void MLV_draw_point_on_image(int x, int y, MLV_Color color, MLV_Image* image){
	MLV_draw_pixel_on_image( x, y, color, image);
}



///////////////////////////////////////////////////////////////////////////////
// Drawing operation on iscreen                                              //
///////////////////////////////////////////////////////////////////////////////

void MLV_draw_circle(int x, int y, int radius, MLV_Color color){
	circleColor( MLV_data->screen,	x, y, radius, color );
}

void MLV_draw_filled_circle(int x, int y, int radius, MLV_Color color){
	filledCircleColor( MLV_data->screen, x, y, radius,  color);
}

void MLV_draw_ellipse(int x, int y, int radius_x, int radius_y, MLV_Color color){
	ellipseColor(MLV_data->screen,x,y,radius_x,radius_y,color);      
}

void MLV_draw_filled_ellipse(int x, int y, int radius_x, int radius_y, MLV_Color color){
	filledEllipseColor(MLV_data->screen,x,y,radius_x,radius_y,color);      
}

void MLV_draw_polygon( const int* vx, const int* vy, int nb_points, MLV_Color color){
	Sint16* tmp_vx = MLV_MALLOC( nb_points, Sint16 );
	Sint16* tmp_vy = MLV_MALLOC( nb_points, Sint16 );
	int i;
	for( i=0; i< nb_points; i++){
		tmp_vx[i] = vx[i];
		tmp_vy[i] = vy[i];
	}
	polygonColor( MLV_data->screen, tmp_vx, tmp_vy, nb_points, color);
	MLV_FREE( tmp_vx, Sint16 );
	MLV_FREE( tmp_vy, Sint16 );
}

void MLV_draw_triangle(
	int ax, int ay, int bx, int by, int cx, int cy, MLV_Color color
){
	int coordonne_x[3] = { ax, bx, cx };
	int coordonne_y[3] = { ay, by, cy };
	MLV_draw_polygon( coordonne_x, coordonne_y, 3, color );
}

void MLV_draw_filled_polygon( const int* vx, const int* vy, int nb_points, MLV_Color color){
	Sint16* tmp_vx = MLV_MALLOC( nb_points, Sint16 );
	Sint16* tmp_vy = MLV_MALLOC( nb_points, Sint16 );
	int i;
	for( i=0; i< nb_points; i++){
		tmp_vx[i] = vx[i];
		tmp_vy[i] = vy[i];
	}
	filledPolygonColor( MLV_data->screen, tmp_vx, tmp_vy, nb_points, color);
	MLV_FREE( tmp_vx, Sint16 );
	MLV_FREE( tmp_vy, Sint16 );
}

void MLV_draw_filled_triangle(
	int ax, int ay, int bx, int by, int cx, int cy, MLV_Color color
){
	int coordonne_x[3] = { ax, bx, cx };
	int coordonne_y[3] = { ay, by, cy };
	MLV_draw_filled_polygon( coordonne_x, coordonne_y, 3, color );
}

void MLV_draw_bezier_curve( const int* vx, const int* vy, int nb_points, MLV_Color color){
	Sint16* tmp_vx = MLV_MALLOC( nb_points, Sint16 );
	Sint16* tmp_vy = MLV_MALLOC( nb_points, Sint16 );
	int i;
	for( i=0; i< nb_points; i++){
		tmp_vx[i] = vx[i];
		tmp_vy[i] = vy[i];
	}
	bezierColor( MLV_data->screen, tmp_vx, tmp_vy, nb_points, 6 , color);
	MLV_FREE( tmp_vx, Sint16 );
	MLV_FREE( tmp_vy, Sint16 );
}

void MLV_draw_rectangle(int x, int y, int width, int height, MLV_Color color){
	rectangleColor(MLV_data->screen, x, y, x+width-1, y+height-1, color);
}

void MLV_draw_filled_rectangle(int x, int y, int width, int height, MLV_Color color){
	boxColor(MLV_data->screen, x, y, x+width-1, y+height-1, color);
}

void MLV_draw_line(int x1, int y1, int x2, int y2, MLV_Color color){
	lineColor( MLV_data->screen, x1, y1, x2, y2, color);
}

void MLV_draw_pixel(int x, int y, MLV_Color color){
	draw_pixel_on_sdl_surface( MLV_data->screen, x, y, color );
}

void MLV_draw_point(int x, int y, MLV_Color color){
	MLV_draw_pixel( x, y, color);
}