summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 34cbcb4e5f16eb48f27b29b77ab6c5aca1a60f00 (plain)
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
/*
 * Copyright 2022 Frederico de Oliveira Linhares
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "camera.hpp"
#include "candy_gear.hpp"
#include "core.hpp"
#include "key.hpp"
#include "model.hpp"
#include "model/instance.hpp"
#include "sound.hpp"
#include "texture.hpp"

cg_sCore cg_core;

static void handle_error(mrb_state *mrb)
{
    mrb_print_error(mrb);
    cg_core.quit_game = true;
}

int main(int argc, char *argv[])
{
  SDL_Event event;
  Uint32 frame_start;

  // Random numbers
  random_number_generator.seed(random_seed());

  mrb_value main_obj;
  mrb_sym sym_init, sym_key_down, sym_key_up, sym_quit, sym_tick;
  FILE *fp;

  cg_core.config_file = argv[1];

  try{ cg_sCore::loader.execute(nullptr); }
  catch(const CommandError &error)
  {
    cg_core.log.message(Log::Level::Fatal, error.what());
    return 1;
  }

  mrb_define_module(cg_core.mrb, "CandyGear");
  cg_camera_init(cg_core.mrb);
  cg_candy_gear_init(cg_core.mrb);
  cg_key_init(cg_core.mrb);
  cg_model_init(cg_core.mrb);
  cg_model_instance_init(cg_core.mrb);
  cg_sound_init(cg_core.mrb);
  cg_texture_init(cg_core.mrb);

  main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self);
  sym_init = mrb_intern_cstr(cg_core.mrb, "init");
  sym_key_down = mrb_intern_cstr(cg_core.mrb, "key_down");
  sym_key_up = mrb_intern_cstr(cg_core.mrb, "key_up");
  sym_quit = mrb_intern_cstr(cg_core.mrb, "quit");
  sym_tick = mrb_intern_cstr(cg_core.mrb, "tick");

  fp = fopen(argv[2], "r");
  mrb_load_irep_file(cg_core.mrb, fp);
  fclose(fp);
  if (cg_core.mrb->exc)
    handle_error(cg_core.mrb);
  else
  {
    mrb_funcall_id(cg_core.mrb, main_obj, sym_init, 0);
    if (cg_core.mrb->exc) handle_error(cg_core.mrb);
  }

  frame_start = SDL_GetTicks();

  // Game main loop.
  while(!cg_core.quit_game)
  {
    // Get input.
    while(SDL_PollEvent(&event) != 0)
    {
      switch(event.type)
      {
      case SDL_KEYDOWN:
	mrb_funcall_id(
	  cg_core.mrb, main_obj, sym_key_down, 1,
	  mrb_int_value(cg_core.mrb, event.key.keysym.sym));
	break;
      case SDL_KEYUP:
	mrb_funcall_id(
	  cg_core.mrb, main_obj, sym_key_up, 1,
	  mrb_int_value(cg_core.mrb, event.key.keysym.sym));
	break;
      case SDL_MOUSEMOTION:
	break;
      case SDL_MOUSEBUTTONDOWN:
	break;
      case SDL_MOUSEBUTTONUP:
	break;
      case SDL_QUIT:
	mrb_funcall_id(cg_core.mrb, main_obj, sym_quit, 0);
	break;
      }
    }

    mrb_funcall_id(cg_core.mrb, main_obj, sym_tick, 0);
    if (cg_core.mrb->exc) handle_error(cg_core.mrb);

    cg_core.vk_graphics_pipeline->draw();

    // Timer
    {
      Uint32 frame_stop, frame_duration;

      frame_stop = SDL_GetTicks();
      frame_duration = frame_stop - frame_start;

      // If frame take less time than maximum allowed.
      if(cg_core.max_frame_duration_ms > frame_duration)
	SDL_Delay(cg_core.max_frame_duration_ms - frame_duration);

      frame_start = frame_stop;
    }
  }

  cg_sCore::loader.revert(nullptr);

  return 0;
}