summaryrefslogtreecommitdiff
path: root/src/graphic.c
blob: 05a12cde5d7e0ae1e403c881f8b8cfb4222a179a (plain)
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
#include "SDL/SDL_stdinc.h"
#include "SDL/SDL_image.h"
#include "graphic.h"
#include "utils.h"

#define ccc_nooverride 0x00cc0000
#define ccc_terrain 0x0000cc00
#define ccc_nothing 0x00000000

Uint32 getPixel(SDL_Surface *s, int x, int y) {
/* 24 bits retournés 
 	Uint32 res=0;
	//FIXME : Big Endian
	res |= ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+0] << 0;
	res |= ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+1] << 8;
	res |= ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+2] << 16;
	return res;
*/
	return ((Uint32 *)s->pixels)[y*s->w + x];
}

Uint32 getPixel8BitPalette(SDL_Surface *s, int x, int y) {
	Uint8 index;
	SDL_Color c;
	Uint32 res=0;

	index=((Uint8 *)s->pixels)[y*s->pitch + x];
	c=s->format->palette->colors[index];

	//FIXME : Big Endian
	res |= c.r << 0;
	res |= c.g << 8;
	res |= c.b << 16;
	return res;
}

int isTransparent(SDL_Surface *s, int x, int y) {
	Uint8 index;

	index=((Uint8 *)s->pixels)[y*s->pitch + x];
	return (index == s->format->colorkey);
}

void putPixel(SDL_Surface *s, int x, int y, Uint32 p) {
	((Uint32 *)s->pixels)[y*s->w + x]=p;
}

SDL_Surface * createSurface(int width, int height) {
	Uint32 rmask, gmask, bmask, amask;

	/* SDL interprets each pixel as a 32-bit number, so our masks must depend
	on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	rmask = 0xff000000;
	gmask = 0x00ff0000;
	bmask = 0x0000ff00;
	amask = 0x00000000;
#else
	rmask = 0x000000ff;
	gmask = 0x0000ff00;
	bmask = 0x00ff0000;
	amask = 0x00000000;
#endif

	return SDL_CreateRGBSurface(MY_SDLSURFACE_OPTS, width, height, SCREEN_BPP, rmask, gmask, bmask, amask);

}

SDL_Surface * loadGif(char *filePath) {
	return IMG_Load(filePath);
}

int pxAreaColor (SDL_Surface* s, Uint32 p , int x , int y , int mX , int mY ){
	int i,j;

	// FIXME : make some test to prevent out of bound pixel

	for (i=x;i<x+mX;++i){
		for (j=y;j<y+mY;++j){
			putPixel(s, i , j , p);
		}
	}

	return 0;
}

int paintTerrain(gameIni_t *gIni, gameRess_t *gRess, gameGraphics_t *gGraph) {
	int res, i, modifier;
	int l,k;
	int x,y,xmin,xmax,ymin,ymax,y2,xdst,ydst;
	Uint32 dstPixel, dstStencil;
	SDL_Surface *tile;

	gGraph->surfaces.terrain=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT);
	if (gGraph->surfaces.terrain==NULL) {
		logs(LOG_ERROR, "paintTerrain(), SDL_CreateRGBSurface() returns NULL");
		return 1;
	}
	gGraph->surfaces.stencil=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT);
	if (gGraph->surfaces.stencil==NULL) {
		logs(LOG_ERROR, "paintTerrain(), SDL_CreateRGBSurface() returns NULL");
		return 2;
	}

	res=SDL_FillRect(gGraph->surfaces.terrain, &(gGraph->surfaces.terrain->clip_rect), gIni->style.bgColor);
	if (res!=0) {
		logs(LOG_WARN, "paintTerrain(), SDL_FillRect() failed");
		return 3;
	}

	res=SDL_FillRect(gGraph->surfaces.stencil, &(gGraph->surfaces.stencil->clip_rect), ccc_nothing);
	if (res!=0) {
		logs(LOG_WARN, "paintTerrain(), SDL_FillRect() failed");
		return 4;
	}

	SDL_LockSurface(gGraph->surfaces.terrain);
	SDL_LockSurface(gGraph->surfaces.stencil);
	for(i=0 ; i < gIni->level.terrainCount ; i++) {
		//FIXME : check sanity for id value
		tile=gRess->style.tiles[gIni->level.terrains[i].id];
		if (tile==NULL) {
			logs(LOG_ERROR, "paintTerrain(), tile==NULL");
			return 5;
		}

		
		// Special modifier values :
		// 15 : Hidden : Lemini hack. Seems to be the same as NO_OVERRIDE

		// Combinable modifier :
		// 8 : NO_OVERRIDE : marquer les pixels comme indestructibles pour les prochains plaquages
		// 4 : Upside Down
		// 2 : REMOVE : oublier (rendre transparent) tous les pixels qu'on a déjà plaqué

		modifier=gIni->level.terrains[i].modifier;
		// If we match the Lemini hack, change the value to NO_OVERRIDE
		if (modifier == 15) {
			modifier=8;
		}

		// If both REMOVE and NO_OVERRIDE is enabled, prefer NO_OVERRIDE (turn off REMOVE flag)
		if ( (modifier & 10) == 10) {
			modifier &= ~2;
		}

		// For each tile pixel, without going outside of the terrain
		ymin=(gIni->level.terrains[i].ypos>=0)?0:-gIni->level.terrains[i].ypos;
		ymax=min(tile->clip_rect.h, gGraph->surfaces.terrain->clip_rect.h - gIni->level.terrains[i].ypos);
		xmin=(gIni->level.terrains[i].xpos>=0)?0:-gIni->level.terrains[i].xpos;
		xmax=min(tile->clip_rect.w, gGraph->surfaces.terrain->clip_rect.w - gIni->level.terrains[i].xpos);

		SDL_LockSurface(tile);
		for (y=ymin; y<ymax; y+=2) {
			for (x=xmin; x<xmax; x+=2) {
				// If we have Upside Down modifier, count lines in reverse order
				if (( modifier & 4) == 4 ) {
					y2=tile->clip_rect.h-1-y;
				} else {
					y2=y;
				}
				ydst=gIni->level.terrains[i].ypos+y;
				xdst=gIni->level.terrains[i].xpos+x;

				// Act only if current pixel in tile is not transparent
				if ( ! isTransparent(tile, x, y2) ) {
					// Always paint pixel, except in one case:
					// If we are in NO_OVERRIDE mode and there is already a terrain on the current (source) pixel
					if ( !( (modifier & 8) == 8 && 
						getPixel(gGraph->surfaces.stencil, xdst, ydst) == ccc_terrain ) ) {
						// If we have REMOVE modifier, dstPixel will be rolled back to bgColor, else, it will be identical to the source pixel. We have to update stencil consistenly.
						if ( (modifier & 2) == 2 ) {
							dstPixel=gIni->style.bgColor;
							dstStencil=ccc_nothing;
						} else {
							dstPixel=getPixel8BitPalette(tile, x, y2);
							dstStencil=ccc_terrain;
						}

						pxAreaColor (gGraph->surfaces.terrain,dstPixel,xdst,ydst,2,2);
						pxAreaColor (gGraph->surfaces.stencil,dstStencil,xdst,ydst,2,2);
						/*
						//FIXME : optimiser le nombre d'appels ici !
						putPixel(gGraph->surfaces.terrain, xdst, ydst, dstPixel);
						putPixel(gGraph->surfaces.terrain, xdst+1, ydst, dstPixel);
						putPixel(gGraph->surfaces.terrain, xdst, ydst+1, dstPixel);
						putPixel(gGraph->surfaces.terrain, xdst+1, ydst+1, dstPixel);
				
						putPixel(gGraph->surfaces.stencil, xdst, ydst, dstStencil);
						putPixel(gGraph->surfaces.stencil, xdst+1, ydst, dstStencil);
						putPixel(gGraph->surfaces.stencil, xdst, ydst+1, dstStencil);
						putPixel(gGraph->surfaces.stencil, xdst+1, ydst+1, dstStencil);
						*/
						
					}
				}
			}
		}
		SDL_UnlockSurface(tile);
	}
	SDL_UnlockSurface(gGraph->surfaces.stencil);
	SDL_UnlockSurface(gGraph->surfaces.terrain);

	return 0;
}