Module: RedBird::Engine

Defined in:
ext/red_bird/engine.c,
lib/red_bird/engine.rb,
ext/red_bird/engine.c

Overview

This module is responsible for the initialization, control of general behavior, and finalization of a game.

Author:

  • Frederico Linhares

Constant Summary collapse

@@max_fps =

Maximum frames per second. The Engine will try to maintain the game running at the frame rate defined by this variable.

30

Class Method Summary collapse

Class Method Details

.debug=(debug) ⇒ Object

By default, debug is false. If you set this to true, the engine displays some information useful for debugging your project.

Parameters:

  • debug (Boolean)

    true turn bebug on.

Author:

  • Frederico Linhares



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'ext/red_bird/engine.c', line 257

static VALUE
bird_mEngine_set_debug(VALUE self, VALUE debug)
{
  switch (TYPE(debug))
  {
    case T_TRUE:
      bird_core.debug = SDL_TRUE;
      break;
    case T_FALSE:
      bird_core.debug = SDL_FALSE;
      break;
    default:
      rb_raise(rb_eTypeError, "debug must be true or false");
      break;
  }

  return self;
}

.fpsInteger

Get frames per second.

Returns:

  • (Integer)

    frames per second.



19
20
21
# File 'lib/red_bird/engine.rb', line 19

def self.fps
  return @@max_fps
end

.fps=(fps) ⇒ Object

Set frames per second.

Parameters:

  • fps (Integer)

Author:

  • Frederico Linhares



12
13
14
# File 'lib/red_bird/engine.rb', line 12

def self.fps=(fps)
  @@max_fps = fps
end

.loadObject

It is only necessary to use this method in a game to change the default behavior of the Engine. The run alredy uses it in a proper way, so calling that method is enough for most games.

This method loads internal subsystems; most functionalities in this Engine depend on those subsystems, so it is necessary to call this method before instantiating any class from RedBird. This method must receive a block. After the block finishes executing, it unloads all subsystems.

Author:

  • Frederico Linhares



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'ext/red_bird/engine.c', line 288

VALUE
bird_mEngine_load(VALUE self)
{
  VALUE result;

  rb_need_block();

  bird_core.loader = malloc(sizeof(LoaderStack));

  LoaderStack_constructor(bird_core.loader, NULL);

  LoaderStack_add(bird_core.loader, &load_variables, &unload_variables);
  LoaderStack_add(bird_core.loader, &load_sdl, &unload_sdl);
  LoaderStack_add(bird_core.loader, &load_window, &unload_window);
  LoaderStack_add(bird_core.loader, &load_sdl_renderer, &unload_sdl_renderer);
  LoaderStack_add(bird_core.loader, &load_pre_screen, &unload_pre_screen);
  LoaderStack_add(bird_core.loader, &load_sdl_ttf, &unload_sdl_ttf);

  // Load Engine
  if(!LoaderStack_load(bird_core.loader))
  {
    rb_raise(rb_eRuntimeError, "%s", bird_core.loader->error_message);
    LoaderStack_destructor(bird_core.loader);

    free(bird_core.loader);

    return self;
  }

  engine_initialized = SDL_TRUE;

  // Execute block
  result = rb_yield(Qundef);

  // Ensure that objects using SDL are destroyed in case of errors.
  rb_gc_mark(result);
  rb_gc();
  SDL_Delay(1000); // Wait one second to give garbage collector some time.

  // Unload Engine
  LoaderStack_destructor(bird_core.loader);

  free(bird_core.loader);

  engine_initialized = SDL_FALSE;
  return self;
}

.quit_gameObject

Exit the current stage and end the run loop.

Author:

  • Frederico Linhares



26
27
28
29
# File 'lib/red_bird/engine.rb', line 26

def self.quit_game
  @@quit_game = true
  @@quit_stage = true
end

.quit_stageObject

Exit the current stage. The code block sent to run will be called again to get the next stage.

Author:

  • Frederico Linhares



35
36
37
# File 'lib/red_bird/engine.rb', line 35

def self.quit_stage
  @@quit_stage = true
end

.run(global_data) {|global_data| ... } ⇒ Object

This method initializes the engine, executes the main loop, and returns when the game is over. This function calls the code block to get the first stage of the game and call it again every time a stage end to get the next one.

Parameters:

  • global_data (Object)

    any object that you want to share between the game. This method sends this object back to the code block received.

Yields:

  • (global_data)

    give global_data to the block and expect it to return the next stage.



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
# File 'lib/red_bird/engine.rb', line 48

def self.run(global_data, &stage_manager)
  @@quit_game = false

  self.load do
    while not @@quit_game do
      @@quit_stage = false

      timer = Timer.new(@@max_fps)
      clear_color = Color.new(0, 0, 0, 0)
      current_stage = stage_manager.call(global_data)

      # The best moment to cleanup memory is before the engine loads a new
      # stage.
      GC.start

      while not @@quit_stage do
        timer.tick

        current_stage.input_device.exec_events(current_stage.entities)

        current_stage.pre_tick
        current_stage.entities.each { |e| e.tick }
        current_stage.post_tick

        Render.set_color(clear_color)
        Render.clear_screen
        current_stage.entities.each { |e| e.render }
        Render.update_screen
      end
    end
  end
end

.set_pixel_quantity(width, height) ⇒ Object

Defines the virtual number of pixels used by the game. When the real screen resolution is higher than this, the frames are scaled.

Parameters:

  • width (Integer)

    number of columns.

  • height (Integer)

    number of rows.

Author:

  • Frederico Linhares



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/red_bird/engine.c', line 198

static VALUE
bird_mEngine_set_pixel_quantity(VALUE self, VALUE width, VALUE height)
{
  RB_INTEGER_TYPE_P(width);
  RB_INTEGER_TYPE_P(height);

  bird_core.game_width = FIX2INT(width);
  bird_core.game_height = FIX2INT(height);

  calculate_full_scale();

  return self;
}

.set_screen_resolution(width, height) ⇒ Object

Use this method to define the game screen resolution. By default the resolution is set to 480x320.

Parameters:

  • width (Integer)

    screen width.

  • height (Integer)

    screen height.

Author:

  • Frederico Linhares



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/red_bird/engine.c', line 220

static VALUE
bird_mEngine_set_screen_resolution(VALUE self, VALUE width, VALUE height)
{
  // Variables converted to C.
  int c_width, c_height;

  RB_INTEGER_TYPE_P(width);
  RB_INTEGER_TYPE_P(height);

  c_width = FIX2INT(width);
  c_height = FIX2INT(height);

  if (c_width < bird_core.game_width)
    rb_raise(rb_eArgError, "width must be bigger or equal to pixels width");
  if (c_height < bird_core.game_height)
    rb_raise(rb_eArgError, "height must be bigger or equal to pixels height");

  bird_core.screen_width = c_width;
  bird_core.screen_height = c_height;

  calculate_full_scale();

  // If window was already created, this is a resize.
  if(engine_initialized)
    SDL_SetWindowSize(
        bird_core.window, bird_core.screen_width, bird_core.screen_height);

  return self;
}