diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/all.c | 126 | ||||
| -rw-r--r-- | src/index.html.in | 11 | 
2 files changed, 71 insertions, 66 deletions
| @@ -2,16 +2,23 @@  #include <stdint.h>  typedef struct { -	int x, y, w, h;  	unsigned char r, g, b, a; +} Color; + +typedef struct { +	int x, y, w, h; +	Color fill; +	Color border;  } DrawElement;  typedef struct {  	int len; -	DrawElement els[256]; +	DrawElement els[512];  } DrawList;  #define MEM_SIZE (1<<16) +#define GRIDWIDTH 16 +#define GRIDHEIGHT 16  typedef struct {  	char *start; @@ -38,56 +45,58 @@ typedef struct {  } UI;  typedef struct { -	int x, y; +	int grid[GRIDWIDTH * GRIDHEIGHT];  } State;  // Mirror these in src/index.html.in  enum {  	INPUT_NONE, -	INPUT_UP, -	INPUT_DOWN, -	INPUT_LEFT, -	INPUT_RIGHT, +	INPUT_CLICK,  };  typedef struct {  	State state;  	UI ui;  	int input; +	int mousex, mousey;  } Game; -static DrawList *render(State state, UI *ui, Arena *a) { +static const Color colors[] = { +	{255, 255, 255, 255}, +	{0, 0, 0, 255}, +}; + +static DrawList *render(State *state, UI *ui, Arena *a) {  	(void) ui;  	DrawList *drawList = new(a, 1, DrawList); -	drawList->len = 1; -	drawList->els[0] = (DrawElement) { -		.x = state.x, -		.y = state.y, -		.w = 100, -		.h = 100, -		.r = 255, -		.g = 0, -		.b = 0, -		.a = 255, -	}; + +	int cellWidth = ui->width / GRIDWIDTH; +	int cellHeight = ui->height / GRIDHEIGHT; + +	for (int x = 0; x < GRIDWIDTH; x++) { +		for (int y = 0; y < GRIDHEIGHT; y++) { +			drawList->els[drawList->len++] = (DrawElement) { +				.x = cellWidth * x, +				.y = cellHeight * y, +				.w = cellWidth, +				.h = cellHeight, +				.fill = colors[state->grid[x + GRIDWIDTH * y]], +				.border = {0, 0, 0, 255}, +			}; +		} +	}  	return drawList;  }  static void update(Game *game, Arena a) {  	switch (game->input) { -		case INPUT_UP: -			game->state.y -= 20; -			break; -		case INPUT_DOWN: -			game->state.y += 20; -			break; -		case INPUT_LEFT: -			game->state.x -= 20; -			break; -		case INPUT_RIGHT: -			game->state.x += 20; +		int x, y; +		case INPUT_CLICK: +			x = game->mousex * GRIDWIDTH / game->ui.width; +			y = game->mousey * GRIDHEIGHT / game->ui.height; +			game->state.grid[x + y * GRIDWIDTH] = (game->state.grid[x + y * GRIDWIDTH] + 1) % (sizeof(colors) / sizeof(colors[0]));  			break;  		default:  			break; @@ -109,10 +118,7 @@ int main(int argc, char **argv) {  	};  	Game *game = new(&a, 1, Game); -	game->state = (State) { -		.x = 100, -		.y = 100, -	}; +	game->state.grid[0] = 1;  	game->ui = (UI) {  		.width = 640,  		.height = 480, @@ -138,44 +144,43 @@ int main(int argc, char **argv) {  			switch (e.type) {  				case SDL_EVENT_QUIT:  					return 0; -				case SDL_EVENT_KEY_DOWN: -					switch (e.key.key) { -						case 'w': -							game->input = INPUT_UP; -							break; -						case 'a': -							game->input = INPUT_LEFT; -							break; -						case 's': -							game->input = INPUT_DOWN; -							break; -						case 'd': -							game->input = INPUT_RIGHT; -							break; -					} +				case SDL_EVENT_MOUSE_BUTTON_DOWN: +					game->input = INPUT_CLICK; +					game->mousex = (int) e.button.x; +					game->mousey = (int) e.button.y;  					break;  			}  		}  		Arena scratch = a; -		DrawList *drawList = render(game->state, &game->ui, &scratch); +		DrawList *drawList = render(&game->state, &game->ui, &scratch);  		SDL_SetRenderDrawColor(r, 0, 0, 0, 255);  		SDL_RenderFillRect(r, NULL);  		for (int i = 0; i < drawList->len; i++) { -			SDL_SetRenderDrawColor( -				r, -				drawList->els[i].r, -				drawList->els[i].g, -				drawList->els[i].b, -				drawList->els[i].a -			);  			SDL_FRect rect = {  				.x = (float) drawList->els[i].x,  				.y = (float) drawList->els[i].y,  				.w = (float) drawList->els[i].w,  				.h = (float) drawList->els[i].h  			}; + +			SDL_SetRenderDrawColor( +				r, +				drawList->els[i].fill.r, +				drawList->els[i].fill.g, +				drawList->els[i].fill.b, +				drawList->els[i].fill.a +			);  			SDL_RenderFillRect(r, &rect); + +			SDL_SetRenderDrawColor( +				r, +				drawList->els[i].border.r, +				drawList->els[i].border.g, +				drawList->els[i].border.b, +				drawList->els[i].border.a +			); +			SDL_RenderRect(r, &rect);  		}  		update(game, a); @@ -198,10 +203,7 @@ void game_init(void) {  	perm.end = heap + MEM_SIZE;  	game = new(&perm, 1, Game); -	game->state = (State) { -		.x = 100, -		.y = 100, -	}; +	game->state.grid[0] = 1;  }  __attribute((export_name("game_render"))) @@ -209,7 +211,7 @@ DrawList *game_render(int width, int height) {  	game->ui.width = width;  	game->ui.height = height;  	Arena scratch = perm; -	return render(game->state, &game->ui, &scratch); +	return render(&game->state, &game->ui, &scratch);  }  __attribute((export_name("game_update"))) diff --git a/src/index.html.in b/src/index.html.in index fcfb8ef..bd4343d 100644 --- a/src/index.html.in +++ b/src/index.html.in @@ -65,12 +65,15 @@ async function main() {          let len    = dl[0];          let ops    = dl.subarray(1); +			console.log(new Uint32Array(ops.subarray(6, 12)));          for (let i = 0; i < len; i++) { -            let op    = ops.subarray(5*i, 5*i+5); -					const color = new Uint8Array(new Uint32Array(ops.subarray(4, 5)).buffer); -					let style = `#${color[0].toString(16).padStart(2, "0")}${color[1].toString(16).padStart(2, "0")}${color[2].toString(16).padStart(2, "0")}`; -						ctx.fillStyle = style; +            let op    = ops.subarray(6*i, 6*i+6); +					const color = new Uint8Array(new Uint32Array(ops.subarray(4, 6)).buffer); +					ctx.fillStyle = `#${color[0].toString(16).padStart(2, "0")}${color[1].toString(16).padStart(2, "0")}${color[2].toString(16).padStart(2, "0")}`; +					// console.log(color);  						ctx.fillRect(op[0], op[1], op[2], op[3]); +					ctx.strokeStyle = `#${color[4].toString(16).padStart(2, "0")}${color[5].toString(16).padStart(2, "0")}${color[6].toString(16).padStart(2, "0")}`; +						ctx.strokeRect(op[0], op[1], op[2], op[3]);          }      } | 
