Make Makefile similar to the android one (for easier copy-pasting).
[runemen.git] / src / mainmenu.c
blob02fad373899343fa6e3e3b3024aae9a9b8a87cee
1 #include <SDL.h>
3 #include "libs/lazyass/lazyass.h"
4 #include "draw.h"
5 #include "rune.h"
7 SDL_Texture *bg;
8 SDL_Texture *sf;
10 #define BUTTON_PADDING 4
12 #ifndef SDL_InBounds
13 #define SDL_InBounds(X, Y, RECT) \
14 ((X) >= (RECT)->x && (X) <= (RECT)->x + (RECT)->w && \
15 (Y) >= (RECT)->y && (Y) <= (RECT)->y + (RECT)->h)
16 #endif
18 void draw_scale9x(SDL_Renderer *renderer, SDL_Rect *full, SDL_Rect *center, SDL_Rect *dst, SDL_bool stretch) {
20 SDL_Rect top_left = { full->x, full->y, center->x, center->y };
21 SDL_Rect bottom_left = { full->x, full->y + center->y + center->h, center->x, full->h - center->y - center->h };
22 SDL_Rect top_right = { full->x + center->x + center->w, full->y, full->w - center->x - center->w, center->y };
23 SDL_Rect bottom_right = { full->x + center->x + center->w, full->y + center->y + center->h, full->w - center->x - center->w, full->h - center->y - center->h };
25 SDL_Rect top_left_dst = { dst->x, dst->y, top_left.w, top_left.h };
26 SDL_Rect bottom_left_dst = { dst->x, dst->y + dst->h - bottom_left.h, bottom_left.w, bottom_left.h };
27 SDL_Rect top_right_dst = { dst->x + dst->w - top_right.w, dst->y, top_right.w, top_right.h };
28 SDL_Rect bottom_right_dst = { dst->x + dst->w - bottom_right.w, dst->y + dst->h - bottom_right.h, bottom_right.w, bottom_right.h };
30 SDL_Rect left = { full->x, full->y + center->y, center->x, center->h };
31 SDL_Rect left_dst = { dst->x, dst->y + center->y, left.w, left.h };
33 SDL_Rect right = { full->x + center->x + center->w, full->y + center->y, center->x, center->h };
34 SDL_Rect right_dst = { dst->x + dst->w - right.w, dst->y + center->y, right.w, right.h };
36 SDL_Rect top = { full->x + center->x, full->y, center->w, center->y };
37 SDL_Rect top_dst = { dst->x + center->x, dst->y, top.w, top.h };
39 SDL_Rect bottom = { full->x + center->x, full->y + center->y + center->h, center->w, center->y };
40 SDL_Rect bottom_dst = { dst->x + center->x, dst->y + dst->h - bottom.h, bottom.w, bottom.h };
42 SDL_Rect center_src = { full->x + center->x, full->y + center->y, center->w, center->h };
43 SDL_Rect center_dst = { dst->x + center->x, dst->y + center->y, center->w, center->h };
45 if (stretch == SDL_TRUE) {
47 top_dst.w = dst->w - center->w;
48 bottom_dst.w = dst->w - center->w;
50 center_dst.w = dst->w - center->w;
51 center_dst.h = dst->h - center->h;
53 left_dst.h = dst->h - center->h;
54 right_dst.h = dst->h - center->h;
56 SDL_RenderCopy(renderer, tiles, &center_src, &center_dst);
58 SDL_RenderCopy(renderer, tiles, &top, &top_dst);
59 SDL_RenderCopy(renderer, tiles, &left, &left_dst);
60 SDL_RenderCopy(renderer, tiles, &right, &right_dst);
61 SDL_RenderCopy(renderer, tiles, &bottom, &bottom_dst);
63 } else {
65 for (center_dst.y = dst->y + center->y ; center_dst.y < dst->y + dst->h - center_dst.h; center_dst.y += center_dst.h) {
66 for (center_dst.x = dst->x + center->x ; center_dst.x < dst->x + dst->w - center_dst.w; center_dst.x += center_dst.w) {
67 SDL_RenderCopy(renderer, tiles, &center_src, &center_dst);
68 } }
70 for (top_dst.x = dst->x + center->x ; top_dst.x < dst->x + dst->w - top_dst.w; top_dst.x += top_dst.w) {
71 SDL_RenderCopy(renderer, tiles, &top, &top_dst);
72 SDL_RenderCopy(renderer, tiles, &bottom, &bottom_dst);
73 bottom_dst.x += bottom_dst.w;
76 for (left_dst.y = dst->y + center->y ; left_dst.y < dst->y + dst->h - left_dst.h; left_dst.y += left_dst.h) {
77 SDL_RenderCopy(renderer, tiles, &left, &left_dst);
78 SDL_RenderCopy(renderer, tiles, &right, &right_dst);
79 right_dst.y += right_dst.h;
84 SDL_RenderCopy(renderer, tiles, &top_left, &top_left_dst);
85 SDL_RenderCopy(renderer, tiles, &bottom_left, &bottom_left_dst);
86 SDL_RenderCopy(renderer, tiles, &top_right, &top_right_dst);
87 SDL_RenderCopy(renderer, tiles, &bottom_right, &bottom_right_dst);
90 void draw_pushbutton(SDL_Renderer *renderer, Uint32 x, Uint32 y, Uint8 tw, Uint8 th, int state) {
92 SDL_Rect src = { TILE_BUTTON_X * TILE_W + state * TILE_W, TILE_BUTTON_Y * TILE_H, TILE_W, TILE_H };
94 SDL_Rect dst = { x, y, tw * TILE_W, th * TILE_H };
96 SDL_Rect center = { TILE_W/4, TILE_H/4, TILE_W/2, TILE_H/2 };
98 draw_scale9x(renderer, &src, &center, &dst, SDL_FALSE);
101 Uint32 mouse_x = 0;
102 Uint32 mouse_y = 0;
103 Uint8 mouse_push = 0;
104 Sint16 mouse_hover = -1;
106 SDL_Rect buttons[16] = {
107 { 240, 180 + (TILE_H+BUTTON_PADDING) * 0, 8, 1 },
108 { 240, 180 + (TILE_H+BUTTON_PADDING) * 1, 8, 1 },
109 { 240, 180 + (TILE_H+BUTTON_PADDING) * 2, 8, 1 },
110 { 240, 180 + (TILE_H+BUTTON_PADDING) * 3, 8, 1 },
111 { 240, 180 + (TILE_H+BUTTON_PADDING) * 4, 8, 1 },
113 const char *button_names[16] = {
114 "Debug Mode",
115 "Single Player",
116 "Multi Player",
117 "Options",
118 "Quit Game",
120 int button_ok[16] = {
128 int max_buttons = 5;
130 void draw_mainmenu(SDL_Renderer *renderer) {
132 //SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 255);
133 //SDL_RenderClear(renderer);
135 Uint8 push_offset_x[5] = { 0, 0, 0, 0, 2 };
136 Uint8 push_offset_y[5] = { 0, 0, 0, 0, 1 };
137 SDL_Color color_offset[5] = {
138 { 0x00, 0x00, 0x00, 0x00 },
139 { 0x33, 0x33, 0x33, 0x33 },
140 { 0xCC, 0xCC, 0xCC, 0xCC },
141 { 0x33, 0x33, 0x33, 0x33 },
142 { 0xEE, 0xEE, 0xEE, 0xEE },
145 SDL_RenderCopy(renderer, bg, NULL, NULL);
147 int i;
148 for (i = 0; i < max_buttons; i++) {
150 int hover = 0;
152 if (mouse_hover == i) hover = 1 + mouse_push;
154 if (button_ok[i]) hover += 2;
155 else hover = 1;
157 draw_pushbutton(renderer, buttons[i].x, buttons[i].y, buttons[i].w, buttons[i].h, hover);
158 incolor1(&color_offset[hover]);
159 inprint(renderer, button_names[i],
160 buttons[i].x + BUTTON_PADDING + push_offset_x[hover],
161 buttons[i].y + BUTTON_PADDING + push_offset_y[hover]);
165 /* Draw cursor */
166 draw_tile(renderer, 22, 4 + (mouse_hover != -1 ? 1 : 0) + mouse_push, mouse_x - 4, mouse_y - 4);
169 void track_mouse_mainmenu() {
171 int i;
172 mouse_hover = -1;
173 for (i = 0; i < max_buttons; i++) {
175 SDL_Rect test = { buttons[i].x, buttons[i].y, buttons[i].w * TILE_W, buttons[i].h * TILE_H };
177 if (SDL_InBounds(mouse_x, mouse_y, &test)) {
179 mouse_hover = i;
180 break;
183 if (mouse_hover == -1) mouse_push = 0;
186 int do_mainmenu_event(SDL_Event *e) {
187 if (e->type == SDL_MOUSEMOTION) {
188 mouse_x = e->motion.x;
189 mouse_y = e->motion.y;
191 if (e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP) {
192 mouse_x = e->button.x;
193 mouse_y = e->button.y;
195 if (e->type == SDL_MOUSEBUTTONDOWN) {
196 mouse_push = 1;
198 if (e->type == SDL_MOUSEBUTTONUP) {
199 mouse_push = 0;
201 if (mouse_hover != -1) {
203 switch (mouse_hover) {
205 case 0:
206 return 1;
207 break;
208 case 4:
209 return -1;
210 break;
211 default: break;
218 track_mouse_mainmenu();
219 return 0;
222 /* Returns -1 to quit game, 1 to continue somewhere... */
223 int mainmenu_loop(SDL_Renderer *renderer) {
225 SDL_ShowCursor(SDL_FALSE);
227 SDL_Color white = { 0xff, 0xff, 0xff };
229 bg = ASS_LoadTexture("data/endowment.bmp", NULL);
230 tiles = ASS_LoadTexture("data/gfx/runelord.bmp", &white);
232 printf("Tiles: %p -- %s\n", tiles, SDL_GetError());
234 int done = 0;
236 while (!done)
238 SDL_Event event = { 0 };
239 while (SDL_PollEvent(&event)) {
240 done = do_mainmenu_event(&event);
241 if (event.type == SDL_QUIT) done = -1;
242 if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) done = -1;
245 draw_mainmenu(renderer);
247 SDL_RenderPresent(renderer);
248 SDL_Delay(10);
251 ASS_FreeTexture(bg);
253 SDL_ShowCursor(SDL_TRUE);
255 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 255);
256 SDL_RenderClear(renderer);
257 SDL_RenderPresent(renderer);
259 return done;