Files
shitgame/main.c

206 lines
6.1 KiB
C

/*Troy Rosin*/
#include <main.h>
#include <game.h>
#include <input.h>
#include <overworld.h>
#include <host.h>
#include <mydebug.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_ttf.h>
#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)
{
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
init_mainmenu();
init_start_game();
init_join();
init_host();
init_overworld();
running = 1;
//main game loop
while (running)
{
handle_event(&event);
SDL_GetMouseState(&mouse_pos.x, &mouse_pos.y);
switch(state)
{
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;
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
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;
}