diff options
| -rw-r--r-- | src/all.c | 53 | ||||
| -rw-r--r-- | src/index.html.in | 9 | ||||
| -rw-r--r-- | src/tick.c | 10 | ||||
| -rw-r--r-- | src/types.c | 16 | 
4 files changed, 50 insertions, 38 deletions
| @@ -101,6 +101,7 @@ static const Button buttons[N_BUTTONS] = {  	[BUTTON_CONTINUE] = {.x = BUTTON_SPACING, .y = BUTTON_SPACER(BUTTON_CONTINUE), .w = BUTTON_SIZE, .h = BUTTON_SIZE},  }; +// This returns a positive color index or a negated image index  static int getPlaceAction(int currentColor, int cellx, int celly, float cellWidth, float cellHeight) {  	int hoverColor = EMPTY; @@ -134,35 +135,16 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  	for (int x = 0; x < GRIDWIDTH; x++) {  		for (int y = 0; y < GRIDHEIGHT; y++) { -			int image; -			switch (state->grid[x + GRIDWIDTH * y]) { -				case RED: -					image = IMAGE_NELSON; -					break; -				case RED_LEFT: -					image = IMAGE_NELSON_LEFT; -					break; -				case RED_UP: -					image = IMAGE_NELSON_UP; -					break; -				case RED_RIGHT: -					image = IMAGE_NELSON_RIGHT; -					break; -				case RED_DOWN: -					image = IMAGE_NELSON_DOWN; -					break; -				default: -					image = 0; -					break; -			} - -			if (image) { +			if (colorImages[state->grid[x + GRIDWIDTH * y]]) {  				drawList->els[drawList->len++] = (DrawElement) {  					.x = cellWidth * x + GRID_OFFSET_X,  					.y = cellHeight * y,  					.w = cellWidth,  					.h = cellHeight, -					.image = image, +					.image = (ImageRef) { +						.index = colorImages[state->grid[x + GRIDWIDTH * y]], +						.opacity = 255, +					},  				};  			} else {  				for (int subx = 0; subx < 2; subx++) { @@ -228,7 +210,18 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  			cellHeight  		); -		if (hoverColor != EMPTY) { +		if (colorImages[hoverColor]) { +			drawList->els[drawList->len++] = (DrawElement) { +				.x = cellWidth * hoverx + GRID_OFFSET_X, +				.y = cellHeight * hovery, +				.w = cellWidth, +				.h = cellHeight, +				.image = { +					.index = colorImages[hoverColor], +					.opacity = 127, +				}, +			}; +		} else if (hoverColor != EMPTY) {  			int subCellWidth = cellWidth / 2;  			int subCellHeight = cellHeight / 2;  			for (int subx = 0; subx < 2; subx++) { @@ -291,7 +284,10 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  			.y = ui->height - buttons[i].y,  			.w = buttons[i].w,  			.h = buttons[i].h, -			.image = image, +			.image = (ImageRef) { +				.index = image, +				.opacity = 255, +			},  		};  	} @@ -566,8 +562,9 @@ int main(int argc, char **argv) {  			);  			SDL_RenderRect(r, &rect); -			if (drawList->els[i].image != 0) { -				SDL_RenderTexture(r, textures[drawList->els[i].image], NULL, &rect); +			if (drawList->els[i].image.index != 0) { +				SDL_SetTextureAlphaMod(textures[(int) drawList->els[i].image.index], drawList->els[i].image.opacity); +				SDL_RenderTexture(r, textures[(int) drawList->els[i].image.index], NULL, &rect);  			}  		} diff --git a/src/index.html.in b/src/index.html.in index 119d646..46d39e4 100644 --- a/src/index.html.in +++ b/src/index.html.in @@ -118,7 +118,6 @@ async function main() {  					ctx.fillStyle = "#000000";  					ctx.fillRect(0, 0, width, height); -			console.log("frame");          for (let i = 0; i < len; i++) {              let op    = ops.subarray(7*i, 7*i+7);  					const color = new Uint8Array(new Uint32Array(op.subarray(4, 6)).buffer); @@ -128,9 +127,11 @@ async function main() {  					ctx.strokeStyle = `#${color[4].toString(16).padStart(2, "0")}${color[5].toString(16).padStart(2, "0")}${color[6].toString(16).padStart(2, "0")}`;  					ctx.globalAlpha = color[7] / 255;  						ctx.strokeRect(op[0], op[1], op[2], op[3]); -					if (op[6] !== 0) { -						ctx.globalAlpha = 1; -						ctx.drawImage(images[op[6]], op[0], op[1], op[2], op[3]); +					const image = new Uint8Array(new Uint32Array(op.subarray(6, 7)).buffer); +					if (image[0] !== 0) { +						console.log(image); +						ctx.globalAlpha = image[1] / 255; +						ctx.drawImage(images[image[0]], op[0], op[1], op[2], op[3]);  					}          }      } @@ -236,7 +236,7 @@ static void tick(Game *game, Arena a) {  				case BLUE_LEFT:  					if (  						(x > 0 && isRed(lastState->grid[x - 1 + y * GRIDWIDTH])) || -						(x < GRIDWIDTH - 1 && isRed(lastState->grid[x + 1 + y * GRIDWIDTH])) || +						// (x < GRIDWIDTH - 1 && isRed(lastState->grid[x + 1 + y * GRIDWIDTH])) ||  						(y > 0 && isRed(lastState->grid[x + (y - 1) * GRIDWIDTH])) ||  						(y < GRIDHEIGHT - 1 && isRed(lastState->grid[x + (y + 1) * GRIDWIDTH]))  					) { @@ -252,7 +252,7 @@ static void tick(Game *game, Arena a) {  					break;  				case BLUE_RIGHT:  					if ( -						(x > 0 && isRed(lastState->grid[x - 1 + y * GRIDWIDTH])) || +						// (x > 0 && isRed(lastState->grid[x - 1 + y * GRIDWIDTH])) ||  						(x < GRIDWIDTH - 1 && isRed(lastState->grid[x + 1 + y * GRIDWIDTH])) ||  						(y > 0 && isRed(lastState->grid[x + (y - 1) * GRIDWIDTH])) ||  						(y < GRIDHEIGHT - 1 && isRed(lastState->grid[x + (y + 1) * GRIDWIDTH])) @@ -271,8 +271,8 @@ static void tick(Game *game, Arena a) {  					if (  						(x > 0 && isRed(lastState->grid[x - 1 + y * GRIDWIDTH])) ||  						(x < GRIDWIDTH - 1 && isRed(lastState->grid[x + 1 + y * GRIDWIDTH])) || -						(y > 0 && isRed(lastState->grid[x + (y - 1) * GRIDWIDTH])) || -						(y < GRIDHEIGHT - 1 && isRed(lastState->grid[x + (y + 1) * GRIDWIDTH])) +						(y > 0 && isRed(lastState->grid[x + (y - 1) * GRIDWIDTH]))// || +						// (y < GRIDHEIGHT - 1 && isRed(lastState->grid[x + (y + 1) * GRIDWIDTH]))  					) {  						game->state.grid[x + y * GRIDWIDTH] = BLUE;  					} else if ( @@ -288,7 +288,7 @@ static void tick(Game *game, Arena a) {  					if (  						(x > 0 && isRed(lastState->grid[x - 1 + y * GRIDWIDTH])) ||  						(x < GRIDWIDTH - 1 && isRed(lastState->grid[x + 1 + y * GRIDWIDTH])) || -						(y > 0 && isRed(lastState->grid[x + (y - 1) * GRIDWIDTH])) || +						// (y > 0 && isRed(lastState->grid[x + (y - 1) * GRIDWIDTH])) ||  						(y < GRIDHEIGHT - 1 && isRed(lastState->grid[x + (y + 1) * GRIDWIDTH]))  					) {  						game->state.grid[x + y * GRIDWIDTH] = BLUE; diff --git a/src/types.c b/src/types.c index d2a7575..fdb9aa0 100644 --- a/src/types.c +++ b/src/types.c @@ -15,10 +15,16 @@ typedef struct {  } Color;  typedef struct { +	char index; +	unsigned char opacity; +	char padding[2]; +} ImageRef; + +typedef struct {  	int x, y, w, h;  	Color fill;  	Color border; -	int image; +	ImageRef image;  } DrawElement;  typedef struct { @@ -63,6 +69,14 @@ enum {  	N_IMAGES,  }; +static const char colorImages[N_COLORS] = { +	[RED] = IMAGE_NELSON, +	[RED_LEFT] = IMAGE_NELSON_LEFT, +	[RED_UP] = IMAGE_NELSON_UP, +	[RED_RIGHT] = IMAGE_NELSON_RIGHT, +	[RED_DOWN] = IMAGE_NELSON_DOWN, +}; +  enum {  	BUTTON_BACK,  	BUTTON_RETRY, | 
