VgaGames4 - tutorial

Tutorial 1 

screenshot.gif

In this tutorial-part we create the pure body of the game, creating a window, loading audio files and setting keys for exiting and pausing the game. Then the game-loop is executed.


At first an overview about the file pingpong.c.

pingpong.c: Overview

/* global declarations */
[CODEBOX]

/* show help-text */
static VG_BOOL show_help(void) { ... (see pingpong.c) ... }

/* main function */
int main(int argc, char **argv) {
  /* variable declaration */
  [CODEBOX]

  /* initializing */
  [CODEBOX]  /* opening */
  [CODEBOX]  /* set keys */
  [CODEBOX]  /* show help, start background music */

  /* game loop */
  [CODEBOX]  /* receive input-events and check the local key-strokes */
  [CODEBOX]  /* draw out */
  [CODEBOX]  /* flush and wait */

  /* end game */
  [CODEBOX]  /* destroy and exit */
}

An overview about the used variables in function main().

[To Position]
pingpong.c: the variables in function main()

const int border_size = 10;  /* size of borders in pixels */

int winw, winh;  /* window's width and height */
int audc_bgmusic;  /* audio descriptors */

/* helper variables */
struct VG_Rect rect;

We open the window with 320x200 pixels and scale it to the maximal integer scaling, deactivate mouse grabbing which does not catch the mouse in the window, but may use it while it is in the window, open the audio system and load the audio-files, here just the background music.

[To Position]
pingpong.c: continuing the function main()

/* initialize and open window */
if (!VG_init("VgaGames4 tutorial 1")) { exit(1); }
if (!vg4->window->open(VG_WINDOW_SIZE_LOW, VG_WINDOW_SCALE_BEST)) { VG_dest(); exit(1); }
vg4->window->getsize(&winw, &winh);  /* get window-size */

/* set mouse grabbing to off */
vg4->input->mouse_grabbing(VG_FALSE);

/* open audio system */
if (!vg4->audio->open(VG_AUDIO_FREQ_MEDIUM, VG_FALSE)) { VG_dest(); exit(1); }

/* load audio files */
audc_bgmusic = vg4->audio->load("files/bgmusic.wav", 50, VG_AUDIO_VOLUME_MUSIC);

At the beginning of the file pingpong.c we declare the keys we want to use. We quit the game with ALT+Q. We want to pause the game with P and can continue with Space, Return or Escape.

[To Position]
pingpong.c: global declarations

/* keys */
struct {
  int k_quit_lalt;  /* part of quit: Left-ALT */
  int k_quit_q;     /* part of quit: Q */
  int k_pause;      /* pause */
} kref;

Now we set the keys: - quit the game with - keyboard: [ALT] + [Q] it shall not be changeable in a key-change menu - pause the game with - keyboard: [P] it shall be changeable in a key-change menu

[To Position]
pingpong.c: continuing the function main()

/* set keys */

/* quit with ALT+Q, not changeable */
if ((kref.k_quit_lalt = vg4->input->key_insert("Quit-LALT", VG_FALSE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
vg4->input->key_setkbd(kref.k_quit_lalt, VG_INPUT_KBDCODE_LALT);
if ((kref.k_quit_q = vg4->input->key_insert("Quit-Q", VG_FALSE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
vg4->input->key_setkbd(kref.k_quit_q, VG_INPUT_KBDCODE_Q);

/* pause with P, changeable */
if ((kref.k_pause = vg4->input->key_insert("Pause", VG_TRUE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
vg4->input->key_setkbd(kref.k_pause, VG_INPUT_KBDCODE_P);

At last we show a help-screen with the key-strokes and start the background music crescending and looping.

[To Position]
pingpong.c: continuing the function main()

/* show help (for the code see pingpong.c) */
if (!show_help()) { VG_dest(); exit(1); }

/* play background music looping */
vg4->audio->play(audc_bgmusic, VG_TRUE, VG_TRUE);

Now the game-loop can begin. We receive input-events and check the key-strokes for quit and pause.

[To Position]
pingpong.c: entering into game-loop of the function main()

/* game loop */
for (;;) {
  /* retrieve input-events */
  if (!vg4->input->update(VG_TRUE)) { goto endgame; }

  /* quit? */
  if (vg4->input->key_newpressed(kref.k_quit_q) && vg4->input->key_pressed(kref.k_quit_lalt)) { goto endgame; }

  /* pause? */
  if (vg4->input->key_newpressed(kref.k_pause)) {
    if (!vg4->misc->pause()) { goto endgame; }
  }

We draw out all to the window: - at first the window is cleared - as background we set the window to a dimmed blue color - we draw the borders with a height respectively width of border_size pixels

[To Position]
pingpong.c: continuing the game-loop of the function main()

  /* +++ draw out +++ */

  /* clear window */
  vg4->window->clear();

  /* draw background and borders */

  vg4->window->fill(vg4->misc->colorbrightness(VG_COLOR_BLUE, 50));

  /* top border */
  rect.x = rect.y = 0; rect.w = winw; rect.h = border_size;
  vg4->window->draw_rect(&rect, VG_COLOR_RGB(0xb1, 0, 0), VG_TRUE);

  /* bottom border */
  rect.x = 0; rect.y = winh - border_size; rect.w = winw; rect.h = border_size;
  vg4->window->draw_rect(&rect, VG_COLOR_RGB(0xb1, 0, 0), VG_TRUE);

  /* right border */
  rect.x = winw - border_size; rect.y = 0; rect.w = border_size; rect.h = winh;
  vg4->window->draw_rect(&rect, VG_COLOR_RGB(0xb1, 0, 0), VG_TRUE);

Now the drawn content is flushed to the window to make it visible. Then we wait up to 40 milliseconds, the time that every game-loop shall take. This closes the game-loop.

[To Position]
pingpong.c: continuing and closing the game-loop of the function main()

  /* flush contents to window and wait */
  vg4->window->flush();
  vg4->misc->wait_time(40);
}

The rest in pingpong.c is cleaning up.

[To Position]
pingpong.c: continuing and closing the function main()

endgame:
  /* destroy and exit */
  VG_dest();
  exit(0);



<<Previous Download Next>>