made the join game screen to get ready for attaching to host and also made a protocol doc
This commit is contained in:
310
main.c
310
main.c
@@ -4,6 +4,7 @@
|
||||
#include <game.h>
|
||||
#include <input.h>
|
||||
#include <overworld.h>
|
||||
#include <host.h>
|
||||
|
||||
#include <mydebug.h>
|
||||
#include <SDL2/SDL.h>
|
||||
@@ -12,170 +13,73 @@
|
||||
|
||||
#define SPACING 20
|
||||
|
||||
void render_mainmenu();
|
||||
void init_mainmenu();
|
||||
|
||||
SDL_Point mouse_pos;
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
SDL_Event event;
|
||||
SDL_Color white = {255,255,255,0};
|
||||
|
||||
struct mainmenu_ctx {
|
||||
TTF_Font* font;
|
||||
SDL_Texture* background;
|
||||
SDL_Surface *start_surface, *host_surface, *join_surface, *exit_surface, *options_surface;
|
||||
SDL_Texture *start_text, *host_text, *join_text, *exit_text, *options_text;
|
||||
SDL_Rect start_rect, host_rect, join_rect, exit_rect, options_rect;
|
||||
SDL_Rect mainmenu_rects[MAIN_SELECTIONS];
|
||||
};
|
||||
|
||||
struct mainmenu_ctx mainmenu;
|
||||
int state = TITLE;
|
||||
int running;
|
||||
|
||||
|
||||
int selected_index = START_GAME;
|
||||
|
||||
int main(void) {
|
||||
int x1, x2, y1, y2;
|
||||
SDL_Point mouse_pos;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
TTF_Init();
|
||||
window = SDL_CreateWindow("Shufflers", SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, DEF_WINDOW_WIDTH,DEF_WINDOW_HEIGHT, 0);
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
IMG_Init(IMG_INIT_PNG);
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
|
||||
SDL_CaptureMouse(SDL_TRUE);
|
||||
|
||||
init_mainmenu();
|
||||
init_start_game();
|
||||
init_join();
|
||||
init_host();
|
||||
init_overworld();
|
||||
|
||||
SDL_Texture* background = IMG_LoadTexture(renderer, "assets/mainscreen.png");
|
||||
|
||||
if (!background) {
|
||||
printf("IMG_LoadTexture error: %s\n", IMG_GetError());
|
||||
}
|
||||
|
||||
|
||||
|
||||
TTF_Font* font = TTF_OpenFont("assets/font.ttf", 24);
|
||||
|
||||
SDL_Color white = {255, 255, 255, 0};
|
||||
SDL_Surface* start_surface = TTF_RenderText_Solid(font, "START", white);
|
||||
SDL_Texture* start_text = SDL_CreateTextureFromSurface(renderer,
|
||||
start_surface);
|
||||
SDL_Surface* host_surface = TTF_RenderText_Solid(font, "HOST", white);
|
||||
SDL_Texture* host_text = SDL_CreateTextureFromSurface(renderer,
|
||||
host_surface);
|
||||
SDL_Surface* join_surface = TTF_RenderText_Solid(font, "JOIN", white);
|
||||
SDL_Texture* join_text = SDL_CreateTextureFromSurface(renderer,
|
||||
join_surface);
|
||||
SDL_Surface* exit_surface = TTF_RenderText_Solid(font, "EXIT", white);
|
||||
SDL_Texture* exit_text = SDL_CreateTextureFromSurface(renderer,
|
||||
exit_surface);
|
||||
SDL_Surface* options_surface = TTF_RenderText_Solid(font, "OPTIONS", white);
|
||||
SDL_Texture* options_text = SDL_CreateTextureFromSurface(renderer,
|
||||
options_surface);
|
||||
|
||||
SDL_Rect start_rect, host_rect, join_rect, exit_rect, options_rect;
|
||||
|
||||
start_rect.w = (DEF_WINDOW_WIDTH >> 2) - (DEF_WINDOW_WIDTH >> 3);
|
||||
start_rect.h = (DEF_WINDOW_HEIGHT >> 3) - (DEF_WINDOW_HEIGHT >> 4);
|
||||
start_rect.x = (DEF_WINDOW_WIDTH - start_rect.w) >> 1;
|
||||
start_rect.y = (DEF_WINDOW_HEIGHT - start_rect.h) >> 1;
|
||||
|
||||
host_rect.w = join_rect.w = exit_rect.w = options_rect.w = start_rect.w;
|
||||
options_rect.w <<= 1;
|
||||
host_rect.h = join_rect.h = exit_rect.h = options_rect.h = start_rect.h;
|
||||
host_rect.x = join_rect.x = exit_rect.x = options_rect.x = start_rect.x;
|
||||
options_rect.x -= join_rect.w >> 1;
|
||||
|
||||
host_rect.y = start_rect.y + start_rect.h + SPACING;
|
||||
join_rect.y = host_rect.y + host_rect.h + SPACING;
|
||||
options_rect.y = join_rect.y + join_rect.h + SPACING;
|
||||
exit_rect.y = options_rect.y + options_rect.h + SPACING;
|
||||
|
||||
SDL_Rect mainmenu_rects[MAIN_SELECTIONS];
|
||||
|
||||
mainmenu_rects[START_GAME] = start_rect;
|
||||
mainmenu_rects[HOST_GAME] = host_rect;
|
||||
mainmenu_rects[JOIN_GAME] = join_rect;
|
||||
mainmenu_rects[OPTIONS_MENU] = options_rect;
|
||||
mainmenu_rects[EXIT_GAME] = exit_rect;
|
||||
|
||||
running = 1;
|
||||
|
||||
|
||||
while (running)
|
||||
{
|
||||
//main game loop
|
||||
while (running)
|
||||
{
|
||||
handle_event(&event);
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0,196,0,255);
|
||||
|
||||
SDL_RenderCopy(renderer, background, NULL, NULL);
|
||||
|
||||
SDL_RenderCopy(renderer, start_text, NULL, &start_rect);
|
||||
SDL_RenderCopy(renderer, host_text, NULL, &host_rect);
|
||||
SDL_RenderCopy(renderer, join_text, NULL, &join_rect);
|
||||
SDL_RenderCopy(renderer, options_text, NULL, &options_rect);
|
||||
SDL_RenderCopy(renderer, exit_text, NULL, &exit_rect);
|
||||
|
||||
SDL_GetMouseState(&(mouse_pos.x), &(mouse_pos.y));
|
||||
for(int i = 0; i<MAIN_SELECTIONS; i++)
|
||||
SDL_GetMouseState(&mouse_pos.x, &mouse_pos.y);
|
||||
|
||||
switch(state)
|
||||
{
|
||||
if(SDL_PointInRect(&mouse_pos, &mainmenu_rects[i]))
|
||||
{
|
||||
selected_index = i;
|
||||
case START:
|
||||
render_start_game();
|
||||
break;
|
||||
case OVERWORLD:
|
||||
render_overworld();
|
||||
break;
|
||||
case HOST:
|
||||
render_host_game();
|
||||
break;
|
||||
case JOIN:
|
||||
render_join_game();
|
||||
break;
|
||||
case OPTIONS:
|
||||
show_options();
|
||||
break;
|
||||
case TITLE:
|
||||
render_mainmenu();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
switch(selected_index)
|
||||
{
|
||||
case START_GAME:
|
||||
x1 = start_rect.x;
|
||||
x2 = x1 + start_rect.w;
|
||||
y1 = y2 = start_rect.y + start_rect.h;
|
||||
break;
|
||||
case HOST_GAME:
|
||||
x1 = host_rect.x;
|
||||
x2 = x1 + host_rect.w;
|
||||
y1 = y2 = host_rect.y + host_rect.h;
|
||||
break;
|
||||
case JOIN_GAME:
|
||||
x1 = join_rect.x;
|
||||
x2 = x1 + join_rect.w;
|
||||
y1 = y2 = join_rect.y + join_rect.h;
|
||||
break;
|
||||
case OPTIONS_MENU:
|
||||
x1 = options_rect.x;
|
||||
x2 = x1 + options_rect.w;
|
||||
y1 = y2 = options_rect.y + options_rect.h;
|
||||
break;
|
||||
case EXIT_GAME:
|
||||
x1 = exit_rect.x;
|
||||
x2 = x1 + exit_rect.w;
|
||||
y1 = y2 = exit_rect.y + exit_rect.h;
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
if(state)
|
||||
{
|
||||
switch(state)
|
||||
{
|
||||
case START:
|
||||
start_game();
|
||||
break;
|
||||
case HOST:
|
||||
PRINT_M(hosting state)
|
||||
host_game();
|
||||
break;
|
||||
case JOIN:
|
||||
PRINT_M(joining state)
|
||||
join_game();
|
||||
break;
|
||||
case OPTIONS:
|
||||
PRINT_M(options menu state)
|
||||
show_options();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
IMG_Quit();
|
||||
@@ -183,3 +87,119 @@ int main(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void render_mainmenu_selected()
|
||||
{
|
||||
int x1, x2, y1, y2;
|
||||
switch(selected_index)
|
||||
{
|
||||
case START_GAME:
|
||||
x1 = mainmenu.start_rect.x;
|
||||
x2 = x1 + mainmenu.start_rect.w;
|
||||
y1 = y2 = mainmenu.start_rect.y + mainmenu.start_rect.h;
|
||||
break;
|
||||
case HOST_GAME:
|
||||
x1 = mainmenu.host_rect.x;
|
||||
x2 = x1 + mainmenu.host_rect.w;
|
||||
y1 = y2 = mainmenu.host_rect.y + mainmenu.host_rect.h;
|
||||
break;
|
||||
case JOIN_GAME:
|
||||
x1 = mainmenu.join_rect.x;
|
||||
x2 = x1 + mainmenu.join_rect.w;
|
||||
y1 = y2 = mainmenu.join_rect.y + mainmenu.join_rect.h;
|
||||
break;
|
||||
case OPTIONS_MENU:
|
||||
x1 = mainmenu.options_rect.x;
|
||||
x2 = x1 + mainmenu.options_rect.w;
|
||||
y1 = y2 = mainmenu.options_rect.y + mainmenu.options_rect.h;
|
||||
break;
|
||||
case EXIT_GAME:
|
||||
x1 = mainmenu.exit_rect.x;
|
||||
x2 = x1 + mainmenu.exit_rect.w;
|
||||
y1 = y2 = mainmenu.exit_rect.y + mainmenu.exit_rect.h;
|
||||
break;
|
||||
}
|
||||
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
void init_mainmenu()
|
||||
{
|
||||
window = SDL_CreateWindow("Shufflers", SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, DEF_WINDOW_WIDTH,DEF_WINDOW_HEIGHT, 0);
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
IMG_Init(IMG_INIT_PNG);
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
SDL_CaptureMouse(SDL_TRUE);
|
||||
|
||||
SDL_StartTextInput();
|
||||
|
||||
mainmenu.background = IMG_LoadTexture(renderer, "assets/mainscreen.png");
|
||||
|
||||
mainmenu.font = TTF_OpenFont("assets/font.ttf", 24);
|
||||
|
||||
mainmenu.start_surface = TTF_RenderText_Solid(mainmenu.font, "START", white);
|
||||
mainmenu.start_text = SDL_CreateTextureFromSurface(renderer, mainmenu.start_surface);
|
||||
mainmenu.host_surface = TTF_RenderText_Solid(mainmenu.font, "HOST", white);
|
||||
mainmenu.host_text = SDL_CreateTextureFromSurface(renderer, mainmenu.host_surface);
|
||||
mainmenu.join_surface = TTF_RenderText_Solid(mainmenu.font, "JOIN", white);
|
||||
mainmenu.join_text = SDL_CreateTextureFromSurface(renderer, mainmenu.join_surface);
|
||||
mainmenu.exit_surface = TTF_RenderText_Solid(mainmenu.font, "EXIT", white);
|
||||
mainmenu.exit_text = SDL_CreateTextureFromSurface(renderer, mainmenu.exit_surface);
|
||||
mainmenu.options_surface = TTF_RenderText_Solid(mainmenu.font, "OPTIONS", white);
|
||||
mainmenu.options_text = SDL_CreateTextureFromSurface(renderer, mainmenu.options_surface);
|
||||
|
||||
mainmenu.start_rect.w = (DEF_WINDOW_WIDTH >> 2) - (DEF_WINDOW_WIDTH >> 3);
|
||||
mainmenu.start_rect.h = (DEF_WINDOW_HEIGHT >> 3) - (DEF_WINDOW_HEIGHT >> 4);
|
||||
mainmenu.start_rect.x = (DEF_WINDOW_WIDTH - mainmenu.start_rect.w) >> 1;
|
||||
mainmenu.start_rect.y = (DEF_WINDOW_HEIGHT - mainmenu.start_rect.h) >> 1;
|
||||
|
||||
mainmenu.host_rect.w = mainmenu.join_rect.w = mainmenu.exit_rect.w =
|
||||
mainmenu.options_rect.w = mainmenu.start_rect.w;
|
||||
mainmenu.options_rect.w <<= 1;
|
||||
mainmenu.host_rect.h = mainmenu.join_rect.h = mainmenu.exit_rect.h =
|
||||
mainmenu.options_rect.h = mainmenu.start_rect.h;
|
||||
mainmenu.host_rect.x = mainmenu.join_rect.x = mainmenu.exit_rect.x =
|
||||
mainmenu.options_rect.x = mainmenu.start_rect.x;
|
||||
mainmenu.options_rect.x -= mainmenu.join_rect.w >> 1;
|
||||
|
||||
mainmenu.host_rect.y = mainmenu.start_rect.y + mainmenu.start_rect.h + SPACING;
|
||||
mainmenu.join_rect.y = mainmenu.host_rect.y + mainmenu.host_rect.h + SPACING;
|
||||
mainmenu.options_rect.y = mainmenu.join_rect.y + mainmenu.join_rect.h + SPACING;
|
||||
mainmenu.exit_rect.y = mainmenu.options_rect.y + mainmenu.options_rect.h + SPACING;
|
||||
|
||||
mainmenu.mainmenu_rects[START_GAME] = mainmenu.start_rect;
|
||||
mainmenu.mainmenu_rects[HOST_GAME] = mainmenu.host_rect;
|
||||
mainmenu.mainmenu_rects[JOIN_GAME] = mainmenu.join_rect;
|
||||
mainmenu.mainmenu_rects[OPTIONS_MENU] = mainmenu.options_rect;
|
||||
mainmenu.mainmenu_rects[EXIT_GAME] = mainmenu.exit_rect;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void render_mainmenu()
|
||||
{
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0,196,0,255);
|
||||
SDL_RenderCopy(renderer, mainmenu.background, NULL, NULL);
|
||||
|
||||
SDL_RenderCopy(renderer, mainmenu.start_text, NULL, &(mainmenu.start_rect));
|
||||
SDL_RenderCopy(renderer, mainmenu.host_text, NULL, &mainmenu.host_rect);
|
||||
SDL_RenderCopy(renderer, mainmenu.join_text, NULL, &mainmenu.join_rect);
|
||||
SDL_RenderCopy(renderer, mainmenu.options_text, NULL, &mainmenu.options_rect);
|
||||
SDL_RenderCopy(renderer, mainmenu.exit_text, NULL, &mainmenu.exit_rect);
|
||||
|
||||
for(int i = 0; i<MAIN_SELECTIONS; i++)
|
||||
{
|
||||
if(SDL_PointInRect(&mouse_pos, &mainmenu.mainmenu_rects[i]))
|
||||
{
|
||||
selected_index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
render_mainmenu_selected();
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user