1 /* Pongix - clone of the "Pong" video game with net support
2 * Copyright (C) 2005 - Hugo Ruscitti (see AUTHORS file)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "dirty.h"
20
dirty_iniciar(void)21 Dirty * dirty_iniciar (void)
22 {
23 Dirty * tmp;
24
25 tmp = (Dirty *) malloc (sizeof (Dirty));
26
27 if (! tmp)
28 {
29 printf ("error al iniciar Dirty Rects\n");
30 return NULL;
31 }
32
33 tmp->limite_actuales = 0;
34
35 return tmp;
36 }
37
dirty_agregar_sin_duplicados(SDL_Rect * dst,int * ndst,SDL_Rect * src)38 void dirty_agregar_sin_duplicados (SDL_Rect * dst, int * ndst, SDL_Rect * src)
39 {
40 int i;
41
42 /* busca un duplicado */
43 for (i = 0; i < * ndst; i ++)
44 {
45 if (dst [i].x == (* src).x \
46 && dst [i].y == (* src).y \
47 && dst [i].w == (* src).w \
48 && dst [i].h == (* src).h)
49 return;
50 }
51
52 /* si no encuentra duplicados lo agrega */
53 dst [* ndst] = * src;
54 (* ndst) ++;
55 }
56
dirty_agregar(Dirty * data,SDL_Rect rect)57 void dirty_agregar (Dirty * data, SDL_Rect rect)
58 {
59 dirty_agregar_sin_duplicados (data->actuales, \
60 &(data->limite_actuales), & rect);
61 }
62
dirty_agregar_pantalla_completa(Dirty * data)63 void dirty_agregar_pantalla_completa (Dirty * data)
64 {
65 data->actuales [0].x = 0;
66 data->actuales [0].y = 0;
67 data->actuales [0].w = 640;
68 data->actuales [0].h = 480;
69
70 data->limite_actuales = 1;
71 }
72
dirty_terminar(Dirty * data)73 void dirty_terminar (Dirty * data)
74 {
75 free (data);
76 }
77
78 #define iguales(A, B) A.x == B.x && A.y == B.y && A.w == B.w && A.h == B.h
79
dirty_copiar(SDL_Rect * dst,SDL_Rect * src,int * lim_dst,int lim_src)80 void dirty_copiar (SDL_Rect * dst, SDL_Rect * src, int * lim_dst, int lim_src)
81 {
82 int i;
83
84 for (i = 0; i < lim_src; i ++)
85 dirty_agregar_sin_duplicados (dst, lim_dst, src + i);
86 }
87
dirty_restaurar(SDL_Surface * dst,SDL_Surface * src,SDL_Rect * rect,int n)88 void dirty_restaurar (SDL_Surface * dst, SDL_Surface * src, SDL_Rect * rect, \
89 int n)
90 {
91 int i;
92
93 for (i = 0; i < n; i ++)
94 SDL_BlitSurface (src, rect + i, dst, rect + i);
95
96 }
97
dirty_actualizar(Dirty * data,SDL_Surface * screen,SDL_Surface * fondo)98 void dirty_actualizar (Dirty * data, SDL_Surface * screen, SDL_Surface * fondo)
99 {
100 static SDL_Rect todos [400];
101 static int n = 0;
102
103 dirty_copiar (todos, data->actuales, &n, data->limite_actuales);
104
105 SDL_UpdateRects (screen, n, todos);
106
107 /* prepara el pr�ximo cuadro */
108 n = 0;
109 dirty_restaurar (screen, fondo, data->actuales, data->limite_actuales);
110 dirty_copiar (todos, data->actuales, &n, data->limite_actuales);
111 data->limite_actuales = 0;
112 }
113