aboutsummaryrefslogtreecommitdiffstats
path: root/src/game.c
blob: 956c5d24901c09d2a6cda0f6d4fd819ffbf5f65f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "game.h"
#include "utils.h"

void resetScreenScale(Game* game)
{
  Texture texture = game->screen.render.texture;

  // Get screen size on window.
  float renderWidth = GetRenderWidth();
  float renderHeight = GetRenderHeight();
  float scale;

  if (renderWidth > renderHeight)
  {
    scale = (float)renderHeight / texture.height;
  }
  else
  {
    scale = (float)renderWidth / texture.width;
  }

  game->screen.scale = scale;

  float width = texture.width * scale;
  float height = texture.height * scale;

  game->screen.destination = (Rectangle){
    renderWidth / 2.0 - width / 2.0,
    renderHeight / 2.0 - height / 2.0,
    width,
    height
  };
}

void initGame(Game* game)
{
  game->sceneId = GAME_SCENE;
  
  // Settings.
  game->settings = defaultSettings();


  // Window.
  InitWindow(game->settings.windowWidth, game->settings.windowHeight,
             "Find Things");
  SetWindowState(FLAG_WINDOW_RESIZABLE);

  // Screen.
  game->screen.render = LoadRenderTexture(game->settings.screenWidth,
                                          game->settings.screenHeight);
  resetScreenScale(game);

  // Assets.
  initAssets(&game->assets);

  // Skybox.
  game->skybox = LoadModelFromMesh(GenMeshCube(1.0, 1.0, 1.0));
  game->skybox.materials[0].shader = game->assets.shaders[SKYBOX_SHADER];
  game->skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture =
    LoadTextureCubemap(game->assets.images[SKYBOX_IMAGE],
                       CUBEMAP_LAYOUT_AUTO_DETECT);

  // World.
  game->world = createWorld(345893, &game->assets);

  // Player.
  game->player = createPlayer();
  game->player.position = Vector3Scale(game->world.size, 0.5);

  // DisableCursor();
}

void updateMainMenuScene(Game* game)
{
  ClearBackground(BLACK);
}

void drawGameScreen(Game* game)
{
  Texture texture = game->screen.render.texture;

  DrawTexturePro(
    texture,
    (Rectangle){0.0, 0.0, texture.width, -texture.height},
    game->screen.destination,
    (Vector2){0.0, 0.0},
    0.0,
    WHITE);
}

void updateGameScene(Game* game)
{
  ClearBackground(BLACK);

  BeginTextureMode(game->screen.render);
  ClearBackground(BLACK);
  BeginMode3D(game->player.camera);

  // Render skybox.
  rlDisableBackfaceCulling();
  rlDisableDepthMask();
  DrawModel(game->skybox, Vector3Zero(), 1.0, WHITE);
  rlEnableBackfaceCulling();
  rlEnableDepthMask();

  updatePlayer(&game->player, game);

  updateWorld(&game->world, game);

  EndMode3D();
  EndTextureMode();

  drawGameScreen(game);
}

void handleGameResize(Game* game)
{
  resetScreenScale(game);
}

void updateGame(Game* game)
{
  BeginDrawing();

  if (IsWindowResized())
  {
    handleGameResize(game);
  }

  switch (game->sceneId)
  {
  case MAIN_MENU_SCENE:
    updateMainMenuScene(game);
    break;
  case GAME_SCENE:
    updateGameScene(game);
    break;
  default:
    break;
  }

  DrawFPS(0, 0);
  
  EndDrawing();
}

void closeGame(Game* game)
{
  closeAssets(&game->assets);
  UnloadRenderTexture(game->screen.render);
  UnloadTexture(game->skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
  UnloadModel(game->skybox);
  freeWorld(game->world);
  CloseWindow();
}