diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/all.c | 57 | ||||
| -rw-r--r-- | src/types.c | 18 | 
2 files changed, 72 insertions, 3 deletions
| @@ -89,18 +89,23 @@ static const Color colors[N_COLORS][4] = {  	},  }; +static const Button buttons[N_BUTTONS] = { +	[BUTTON_RETRY] = {.x = 16, .y = 16, .w = 128, .h = 32}, +	[BUTTON_BACK] = {.x = 16, .y = 48, .w = 128, .h = 32}, +}; +  static DrawList *render(State *state, UI *ui, Arena *a) {  	(void) ui;  	DrawList *drawList = new(a, 1, DrawList); -	int cellWidth = ui->width / GRIDWIDTH; +	int cellWidth = (ui->width - GRID_OFFSET_X) / 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, +				.x = cellWidth * x + GRID_OFFSET_X,  				.y = cellHeight * y,  				.w = cellWidth,  				.h = cellHeight, @@ -131,16 +136,59 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  		.border = {255, 0, 0, 255},  	}; +	drawList->els[drawList->len++] = (DrawElement) { +		.x = 0, +		.y = 0, +		.w = GRID_OFFSET_X, +		.h = ui->height, +		.fill = {255, 255, 0, 255}, +		.border = {0, 0, 0, 255}, +	}; +	 +	for (int i = 0; i < N_BUTTONS; i++) { +		Color colour = {0, 0, 0, 255}; +		switch (state->buttonStates[i]) { +			case BUTTON_STATE_IDLE: +				colour = (Color) {0, 0, 0, 255}; +				break; +			case BUTTON_STATE_HOVERED: +				colour = (Color) {255, 255, 0, 255}; +				break; +			case BUTTON_STATE_PRESSED: +				colour = (Color) {255, 0, 0, 255}; +				break; +		} +		drawList->els[drawList->len++] = (DrawElement) { +			.x = buttons[i].x, +			.y = buttons[i].y, +			.w = buttons[i].w, +			.h = buttons[i].h, +			.fill = colour, +			.border = {0, 0, 0, 255} +		}; +	} +  	return drawList;  }  static void update(Game *game, uint64_t now, Arena a) { +	const int offset_width = game->ui.width - GRID_OFFSET_X; +	  	switch (game->input) {  		int x, y;  		case INPUT_CLICK: -			x = game->mousex * GRIDWIDTH / game->ui.width; +			x = (game->mousex - GRID_OFFSET_X) * GRIDWIDTH / offset_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])); +			for (int i = 0; i < N_BUTTONS; i++) { +				if ( +					game->mousex > buttons[i].x && game->mousex < buttons[i].x + buttons[i].w && +					game->mousey > buttons[i].y && game->mousey < buttons[i].y + buttons[i].h +				) { +					// TODO - CLICK THINGS +					//game->state.buttonStates[i] = BUTTON_STATE_PRESSED; +				} +			}  			break;  		case INPUT_RCLICK:  			x = game->mousex * GRIDWIDTH / game->ui.width; @@ -184,6 +232,9 @@ int main(int argc, char **argv) {  		.width = 640,  		.height = 480,  	}; +	for (int i = 0; i < N_BUTTONS; i++) { +		game->state.buttonStates[i] = BUTTON_STATE_IDLE; +	}  	SDL_Init(SDL_INIT_VIDEO);  	SDL_Window *w = SDL_CreateWindow( diff --git a/src/types.c b/src/types.c index fe72e13..cbd9315 100644 --- a/src/types.c +++ b/src/types.c @@ -4,6 +4,7 @@  #define GRIDWIDTH 20  #define GRIDHEIGHT 16  #define TICK_LENGTH 200 +#define GRID_OFFSET_X 256  typedef struct {  	unsigned char r, g, b, a; @@ -42,15 +43,32 @@ enum {  	N_COLORS,  }; +enum { +	BUTTON_RETRY, +	BUTTON_BACK, +	N_BUTTONS, +}; +  typedef struct {  	int width, height;  } UI; +typedef enum { +	BUTTON_STATE_IDLE, +	BUTTON_STATE_HOVERED, +	BUTTON_STATE_PRESSED +} ButtonState; + +typedef struct { +	int x, y, w, h; +} Button; +  typedef struct {  	uint64_t lastTick;  	char playing;  	int grid[GRIDWIDTH * GRIDHEIGHT];  	int goalx, goaly; +	ButtonState buttonStates[N_BUTTONS];  } State;  // Mirror these in src/index.html.in | 
