Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/src/all.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/all.c')
-rw-r--r--src/all.c109
1 files changed, 95 insertions, 14 deletions
diff --git a/src/all.c b/src/all.c
index 82f1360..3a249c9 100644
--- a/src/all.c
+++ b/src/all.c
@@ -99,9 +99,32 @@ static const Button buttons[N_BUTTONS] = {
[BUTTON_BACK] = {.x = BUTTON_SPACING, .y = BUTTON_SPACER(BUTTON_BACK), .w = BUTTON_SIZE, .h = BUTTON_SIZE},
};
-static DrawList *render(State *state, UI *ui, Arena *a) {
- (void) ui;
+static int getPlaceAction(int currentColor, int cellx, int celly, float cellWidth, float cellHeight) {
+ int hoverColor = EMPTY;
+
+ switch (currentColor) {
+ case BLACK:
+ if (cellx < cellWidth / 4 && cellx < celly && cellx < cellHeight - celly) {
+ hoverColor = RED_LEFT;
+ } else if (celly < cellHeight / 4 && celly < cellx && celly < cellWidth - cellx) {
+ hoverColor = RED_UP;
+ } else if (cellx > cellWidth / 4 * 3 && cellWidth - cellx < celly && cellWidth - cellx < cellHeight - celly) {
+ hoverColor = RED_RIGHT;
+ } else if (celly > cellHeight / 4 * 3 && cellHeight - celly < cellx && cellHeight - celly < cellWidth - cellx) {
+ hoverColor = RED_DOWN;
+ } else {
+ hoverColor = RED;
+ }
+ break;
+ case EMPTY:
+ hoverColor = BLACK;
+ break;
+ }
+ return hoverColor;
+}
+
+static DrawList *render(State *state, UI *ui, Arena *a) {
DrawList *drawList = new(a, 1, DrawList);
float cellWidth = (float) (ui->width - GRID_OFFSET_X) / GRIDWIDTH;
@@ -145,6 +168,7 @@ static DrawList *render(State *state, UI *ui, Arena *a) {
};
}
+ // Goal
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * state->goalx + GRID_OFFSET_X,
.y = cellHeight * state->goaly,
@@ -154,6 +178,42 @@ static DrawList *render(State *state, UI *ui, Arena *a) {
.border = {255, 0, 0, 255},
};
+ // Hover
+ if (ui->mousex >= GRID_OFFSET_X) {
+ int hoverx = (ui->mousex - GRID_OFFSET_X) * GRIDWIDTH / (ui->width - GRID_OFFSET_X);
+ int hovery = ui->mousey * GRIDHEIGHT / ui->height;
+
+ int cellx = ui->mousex - GRID_OFFSET_X - hoverx * cellWidth;
+ int celly = ui->mousey - hovery * cellHeight;
+
+ int hoverColor = getPlaceAction(
+ state->grid[hoverx + hovery * GRIDWIDTH],
+ cellx,
+ celly,
+ cellWidth,
+ cellHeight
+ );
+
+ if (hoverColor != EMPTY) {
+ int subCellWidth = cellWidth / 2;
+ int subCellHeight = cellHeight / 2;
+ for (int subx = 0; subx < 2; subx++) {
+ for (int suby = 0; suby < 2; suby++) {
+ Color fill = colors[hoverColor][subx + 2 * suby];
+ fill.a = 127;
+ drawList->els[drawList->len++] = (DrawElement) {
+ .x = cellWidth * hoverx + GRID_OFFSET_X + (subx * subCellWidth),
+ .y = cellHeight * hovery + (suby * subCellHeight),
+ .w = subCellWidth,
+ .h = subCellHeight,
+ .fill = fill,
+ };
+ }
+ }
+ }
+ }
+
+ // Side panel
drawList->els[drawList->len++] = (DrawElement) {
.x = 0,
.y = 0,
@@ -195,13 +255,25 @@ static void update(Game *game, uint64_t now, Arena a) {
switch (game->input) {
int x, y;
case INPUT_CLICK:
- 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]));
+ x = (game->ui.mousex - GRID_OFFSET_X) * GRIDWIDTH / offset_width;
+ y = game->ui.mousey * GRIDHEIGHT / game->ui.height;
+ float cellWidth = (float) (game->ui.width - GRID_OFFSET_X) / GRIDWIDTH;
+ float cellHeight = (float) game->ui.height / GRIDHEIGHT;
+ int cellx = game->ui.mousex - GRID_OFFSET_X - x * cellWidth;
+ int celly = game->ui.mousey - y * cellHeight;
+ // Keeping this around for testing purposes
+ // game->state.grid[x + y * GRIDWIDTH] = (game->state.grid[x + y * GRIDWIDTH] + 1) % (sizeof(colors) / sizeof(colors[0]));
+ game->state.grid[x + y * GRIDWIDTH] = getPlaceAction(
+ game->state.grid[x + y * GRIDWIDTH],
+ cellx,
+ celly,
+ cellWidth,
+ cellHeight
+ );
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
+ game->ui.mousex > buttons[i].x && game->ui.mousex < buttons[i].x + buttons[i].w &&
+ game->ui.mousey > buttons[i].y && game->ui.mousey < buttons[i].y + buttons[i].h
) {
// TODO - CLICK THINGS
//game->state.buttonStates[i] = BUTTON_STATE_PRESSED;
@@ -209,8 +281,8 @@ static void update(Game *game, uint64_t now, Arena a) {
}
break;
case INPUT_RCLICK:
- x = game->mousex * GRIDWIDTH / game->ui.width;
- y = game->mousey * GRIDHEIGHT / game->ui.height;
+ x = (game->ui.mousex - GRID_OFFSET_X) * GRIDWIDTH / (game->ui.width - GRID_OFFSET_X);
+ y = game->ui.mousey * GRIDHEIGHT / game->ui.height;
game->state.grid[x + y * GRIDWIDTH] = EMPTY;
break;
case INPUT_PAUSE_PLAY:
@@ -263,6 +335,8 @@ int main(int argc, char **argv) {
game->ui = (UI) {
.width = 640,
.height = 480,
+ .mousex = 0,
+ .mousey = 0,
};
for (int i = 0; i < N_BUTTONS; i++) {
game->state.buttonStates[i] = BUTTON_STATE_IDLE;
@@ -317,14 +391,19 @@ int main(int argc, char **argv) {
case SDL_EVENT_QUIT:
return 0;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
- game->mousex = (int) e.button.x;
- game->mousey = (int) e.button.y;
+ game->ui.mousex = (int) e.button.x;
+ game->ui.mousey = (int) e.button.y;
if (e.button.button == 1) {
game->input = INPUT_CLICK;
} else if (e.button.button == 3) {
game->input = INPUT_RCLICK;
}
break;
+ case SDL_EVENT_MOUSE_MOTION:
+ game->ui.mousex = (int) e.motion.x;
+ game->ui.mousey = (int) e.motion.y;
+ game->input = INPUT_MOVE;
+ break;
case SDL_EVENT_KEY_DOWN:
switch (e.key.key) {
case SDLK_SPACE:
@@ -405,9 +484,11 @@ void game_init(void) {
}
__attribute((export_name("game_render")))
-DrawList *game_render(int width, int height) {
+DrawList *game_render(int width, int height, int mousex, int mousey) {
game->ui.width = width;
game->ui.height = height;
+ game->ui.mousex = mousex;
+ game->ui.mousey = mousey;
Arena scratch = perm;
return render(&game->state, &game->ui, &scratch);
}
@@ -415,8 +496,8 @@ DrawList *game_render(int width, int height) {
__attribute((export_name("game_update")))
void game_update(int input, int mousex, int mousey, int now) {
game->input = input;
- game->mousex = mousex;
- game->mousey = mousey;
+ game->ui.mousex = mousex;
+ game->ui.mousey = mousey;
update(game, now, perm);
}