made the join game screen to get ready for attaching to host and also made a protocol doc
This commit is contained in:
158
join.c
158
join.c
@@ -2,19 +2,169 @@
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <mydebug.h>
|
||||
#include <game.h>
|
||||
#include <host.h>
|
||||
#include <input.h>
|
||||
#include <deck.h>
|
||||
#include <main.h>
|
||||
|
||||
void join_game(){
|
||||
running = 0;
|
||||
#define BOARDER_THICKNESS 20
|
||||
|
||||
struct join_ctx
|
||||
{
|
||||
int selected;
|
||||
|
||||
int over_object;
|
||||
|
||||
char player_name[MAX_NAMESZ];
|
||||
char lobby_name[MAX_NAMESZ];
|
||||
|
||||
TTF_Font *font;
|
||||
|
||||
SDL_Rect lobby_name_rect, player_name_rect, join_game_rect;
|
||||
SDL_Rect lobby_name_box, player_name_box, join_game_box;
|
||||
|
||||
SDL_Surface *player_name_surf, *lobby_prompt_surf, *name_promt_surf,
|
||||
*join_surf, *lobby_name_surf;
|
||||
SDL_Texture *player_name_tex, *lobby_prompt_tex, *name_prompt_tex,
|
||||
*lobby_name_tex, *join_tex;
|
||||
|
||||
};
|
||||
|
||||
struct join_ctx join;
|
||||
|
||||
void join_set_selected(int obj)
|
||||
{
|
||||
join.selected = obj;
|
||||
return;
|
||||
}
|
||||
|
||||
int join_over_object()
|
||||
{
|
||||
return join.over_object;
|
||||
}
|
||||
|
||||
char *join_get_name_ptr()
|
||||
{
|
||||
if(join.selected == PLAYER_NAME_SEL){
|
||||
return join.player_name;
|
||||
}
|
||||
else if(join.selected == LOBBY_NAME_SEL){
|
||||
return join.lobby_name;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void init_join()
|
||||
{
|
||||
|
||||
join.over_object = NO_OBJECT;
|
||||
join.font = TTF_OpenFont("assets/font.ttf", 24);
|
||||
|
||||
strcpy(join.player_name, "PLAYER");
|
||||
strcpy(join.lobby_name, "lobby_name");
|
||||
|
||||
join.player_name_box.x = DEF_WINDOW_WIDTH >> 3;
|
||||
join.player_name_box.y = DEF_WINDOW_HEIGHT >> 3;
|
||||
join.player_name_box.w = (DEF_WINDOW_WIDTH >> 1) + (DEF_WINDOW_WIDTH >> 2);
|
||||
join.player_name_box.h = DEF_WINDOW_HEIGHT >> 3;
|
||||
|
||||
join.player_name_rect.x = join.player_name_box.x + BOARDER_THICKNESS;
|
||||
join.player_name_rect.y = join.player_name_box.y + BOARDER_THICKNESS;
|
||||
join.player_name_rect.w = join.player_name_box.w - (BOARDER_THICKNESS<<1);
|
||||
join.player_name_rect.h = join.player_name_box.h - (BOARDER_THICKNESS<<1);
|
||||
|
||||
join.lobby_name_box.x = join.player_name_box.x;
|
||||
join.lobby_name_box.y = join.player_name_box.y + join.player_name_box.h + BOARDER_THICKNESS;
|
||||
join.lobby_name_box.w = join.player_name_box.w;
|
||||
join.lobby_name_box.h = join.player_name_box.h;
|
||||
|
||||
join.lobby_name_rect.x = join.player_name_rect.x;
|
||||
join.lobby_name_rect.y = join.lobby_name_box.y + BOARDER_THICKNESS;
|
||||
join.lobby_name_rect.w = join.player_name_rect.w;
|
||||
join.lobby_name_rect.h = join.player_name_rect.h;
|
||||
|
||||
join.join_game_box.w = join.lobby_name_box.w >> 1;
|
||||
join.join_game_box.x = (DEF_WINDOW_WIDTH >> 1) - (join.join_game_box.w >> 1);
|
||||
join.join_game_box.y = join.lobby_name_box.y + join.lobby_name_box.h + BOARDER_THICKNESS;
|
||||
join.join_game_box.h = join.lobby_name_box.h;
|
||||
|
||||
join.join_game_rect.x = join.join_game_box.x + BOARDER_THICKNESS;
|
||||
join.join_game_rect.y = join.join_game_box.y + BOARDER_THICKNESS;
|
||||
join.join_game_rect.w = join.join_game_box.w - (BOARDER_THICKNESS << 1);
|
||||
join.join_game_rect.h = join.join_game_box.h - (BOARDER_THICKNESS << 1);
|
||||
|
||||
join.join_surf = TTF_RenderText_Solid(join.font, "JOIN GAME", white);
|
||||
join.join_tex = SDL_CreateTextureFromSurface(renderer, join.join_surf);
|
||||
|
||||
|
||||
PRINT_M(INIT JOIN)
|
||||
}
|
||||
|
||||
void join_update_hover()
|
||||
{
|
||||
if(SDL_PointInRect(&mouse_pos, &join.join_game_box)){ join.over_object = JOIN_GAME_BUTTON; }
|
||||
else if(SDL_PointInRect(&mouse_pos, &join.lobby_name_box)){ join.over_object = LOBBY_NAME_BOX; }
|
||||
else if(SDL_PointInRect(&mouse_pos, &join.player_name_box)){ join.over_object = PLAYER_NAME_BOX; }
|
||||
else { join.over_object = NO_OBJECT; }
|
||||
return;
|
||||
}
|
||||
|
||||
void render_join_game()
|
||||
{
|
||||
join_update_hover();
|
||||
|
||||
join.player_name_surf = TTF_RenderText_Solid(join.font, join.player_name, white);
|
||||
join.player_name_tex = SDL_CreateTextureFromSurface(renderer, join.player_name_surf);
|
||||
join.lobby_name_surf = TTF_RenderText_Solid(join.font, join.lobby_name, white);
|
||||
join.lobby_name_tex = SDL_CreateTextureFromSurface(renderer, join.lobby_name_surf);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0,0,0,255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
if(join.selected == PLAYER_NAME_SEL ){
|
||||
//player name rect
|
||||
SDL_SetRenderDrawColor(renderer, 32, 64, 32, 255);
|
||||
SDL_RenderFillRect(renderer, &join.player_name_box);
|
||||
//lobby name rect
|
||||
SDL_SetRenderDrawColor(renderer, 64,64,64,255);
|
||||
SDL_RenderFillRect(renderer, &join.lobby_name_box);
|
||||
}
|
||||
else{
|
||||
//lobby name rect
|
||||
SDL_SetRenderDrawColor(renderer, 32, 64, 32, 255);
|
||||
SDL_RenderFillRect(renderer, &join.lobby_name_box);
|
||||
//player name rect
|
||||
SDL_SetRenderDrawColor(renderer, 64,64,64,255);
|
||||
SDL_RenderFillRect(renderer, &join.player_name_box);
|
||||
}
|
||||
|
||||
//player name text
|
||||
join.player_name_rect.w = (join.player_name_box.w / MAX_NAMESZ) * strlen(join.player_name);
|
||||
SDL_RenderCopy(renderer, join.player_name_tex, NULL, &join.player_name_rect);
|
||||
|
||||
//lobby name text
|
||||
join.lobby_name_rect.w = (join.lobby_name_box.w / MAX_NAMESZ) * strlen(join.lobby_name);
|
||||
SDL_RenderCopy(renderer, join.lobby_name_tex, NULL, &join.lobby_name_rect);
|
||||
|
||||
|
||||
SDL_RenderFillRect(renderer, &join.join_game_box);
|
||||
SDL_RenderCopy(renderer, join.join_tex, NULL, &join.join_game_rect);
|
||||
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
SDL_FreeSurface(join.player_name_surf);
|
||||
SDL_DestroyTexture(join.player_name_tex);
|
||||
SDL_FreeSurface(join.lobby_name_surf);
|
||||
SDL_DestroyTexture(join.lobby_name_tex);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user