summaryrefslogtreecommitdiff
path: root/src/graphic.c
blob: d68c28997c6362e72e8efb5cf3bfeabd949267fd (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
#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
#define SDLSURF_OPTS SDL_HWSURFACE|SDL_HWACCEL|/*SDL_ASYNCBLIT|*/SDL_RLEACCEL

Uint32 getPixel(SDL_Surface *s, int x, int y) {
	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;
}

void putPixel(SDL_Surface *s, int x, int y, Uint32 p) {
	//FIXME : Big Endian
	((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+0)] = (p & 0x000000ff) >> 0;
	((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+1)] = (p & 0x0000ff00) >> 8;
	((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+2)] = (p & 0x00ff0000) >> 16;
}

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(SDL_HWSURFACE|SDL_HWACCEL|/*SDL_ASYNCBLIT|*/SDL_RLEACCEL, width, height, 24, rmask, gmask, bmask, amask);

}

SDL_Surface * makeTerrain(gameIni_t *gIni, gameRess_t *gRess) {
	int res, i;
	int x,y,xmin,xmax,ymin,ymax,y2,xdst, ydst;
	Uint32 srcPixel, dstPixel, srcStencil, dstStencil;
	SDL_Surface *terrain, *stencil, *tile;

	terrain=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT);
	if (terrain==NULL) {
		logs(LOG_ERROR, "makeTerrain(), SDL_CreateRGBSurface() returns NULL");
		return NULL;
	}
	stencil=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT);
	if (stencil==NULL) {
		logs(LOG_ERROR, "makeTerrain(), SDL_CreateRGBSurface() returns NULL");
		return NULL;
	}

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

	res=SDL_FillRect(stencil, &(stencil->clip_rect), ccc_nothing);
	if (res!=0) {
		logs(LOG_WARN, "makeTerrain(), SDL_FillRect() failed");
		return NULL;
	}

	SDL_LockSurface(terrain);
	SDL_LockSurface(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, "makeTerrain(), tile==NULL");
			return NULL;
		}

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

		// 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, 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, terrain->clip_rect.w - gIni->level.terrains[i].xpos);
		SDL_LockSurface(tile);
		for (y=ymin; y<ymax; y++) {
			for (x=0; x<xmax; x++) {
				// If we have Upside Down modifier, count lines in reverse order fot dest
				if ( gIni->level.terrains[i].modifier % 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;

				// Grab current pixel and stencil state (from previous blits)
				srcPixel=getPixel(tile, x, y2);
				srcStencil=getPixel(stencil, xdst, ydst);

				// Act only if srcPixel is not transparent and srcStentil doesn't says NO_OVERRIDE
				if ( (srcPixel != tile->format->colorkey) && ( srcStencil != ccc_nooverride) ) {
					// If we have REMOVE modifier, dstPixel will be rolled back to bgColor, else, it will be identical to the source pixel
					if ( gIni->level.terrains[i].modifier % 2 ) {
						dstPixel=gIni->style.bgColor;
					} else {
						dstPixel=srcPixel;
					}
					putPixel(terrain, xdst, ydst, dstPixel);

					// If we have NO_OVERRIDE modifier, put that info on stencil, else, save that there is a (normal) terrain here
					if ( gIni->level.terrains[i].modifier % 8 ) {
						dstStencil=ccc_nooverride;
					} else {
						dstStencil=ccc_terrain;
					}
					putPixel(stencil, xdst, ydst, dstStencil);
				}

			}
		}
		SDL_UnlockSurface(tile);
/*
		res=SDL_BlitSurface(tile, NULL, terrain, &dstRect);
		if (res!=0) {
			logs2(LOG_WARN, "makeTerrain(), SDL_BlitSurface()", SDL_GetError());
			return NULL;
		}
*/
	}
	SDL_UnlockSurface(stencil);
	SDL_UnlockSurface(terrain);

	return terrain;
}