Skip to content

Commit

Permalink
Simple start menu
Browse files Browse the repository at this point in the history
  • Loading branch information
necromyhan committed Nov 16, 2023
1 parent fbbd5e1 commit 82c7c9a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ typedef struct _FONT
{
TTF_Font* TtfFont;
int Size;
SDL_Color Color;
} FONT;

FONT*
Expand Down Expand Up @@ -41,7 +40,6 @@ CreateFont(
goto exit;
}

font->Color = Color;
font->Size = Size;

exit:
Expand All @@ -64,6 +62,7 @@ PrintFontToRenderer(
const FONT* Font,
SDL_Renderer* Renderer,
const char* Text,
SDL_Color Color,
SDL_Point Position)
{
int status = 0;
Expand All @@ -74,7 +73,7 @@ PrintFontToRenderer(
goto exit;
}

SDL_Surface* surface = TTF_RenderText_Solid(Font->TtfFont, Text, Font->Color);
SDL_Surface* surface = TTF_RenderText_Solid(Font->TtfFont, Text, Color);
if (NULL == surface)
{
SDL_Log("TTF_RenderText_Solid error = %s", SDL_GetError());
Expand Down
1 change: 1 addition & 0 deletions src/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ PrintFontToRenderer(
const FONT* Font,
SDL_Renderer* Renderer,
const char* Text,
SDL_Color Color,
SDL_Point Position);

#endif //__SNAKE_GAME_FONT_H__
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ bool gInputHandled = false;

static SCENE* gScenes[] =
{
&gMenuScene,
&gGameplayScene,
&gGameOverScene
};
static GAME_STATE gCurrentScene = StateGameplay;
GAME_STATE gCurrentScene = StateMenu;

static
int
Expand Down
37 changes: 37 additions & 0 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ DestroyMenu(MENU* Menu)
}
}

extern GAME_STATE gCurrentScene;

int
MenuHandleEvents(
GAME* Game,
Expand Down Expand Up @@ -80,6 +82,14 @@ MenuHandleEvents(
}
}
break;
case SDLK_RETURN:
{
SDL_Log("Enter");
if (Game->Menu->CurrentType == MenuItemStart)
{
gCurrentScene = StateGameplay;
}
}
}
}

Expand All @@ -106,6 +116,33 @@ MenuRender(GAME* Game)



status = SDL_SetRenderDrawColor(Game->Renderer, 0, 0, 0, 0);
if (status) { goto exit; }

status = SDL_RenderFillRect(Game->Renderer, NULL);
if (status) { goto exit; }

int posX = Game->Field.CellSize;
int posY = Game->Field.CellSize;
for (int i = 0; i < Game->Menu->Count; ++i)
{
status = PrintFontToRenderer(
Game->Font,
Game->Renderer,
Game->Menu->Items[i].Text,
(Game->Menu->Items[i].State) ? (SDL_Color){.b = 200, .g = 0, .r = 0, .a = 0} : (SDL_Color){.b = 255, .g = 255, .r = 255, .a = 0},
(SDL_Point){.x = posX, .y = posY});
if (status)
{
goto exit;
}

// TODO: get screen width from window
posY += Game->Field.CellSize;
}

status = SDL_RenderPresent(Game->Renderer);

exit:
return status;
}
Expand Down

0 comments on commit 82c7c9a

Please sign in to comment.