Tutorial 1
We create a very simple (non-)game, just showing a centered text ("Press space to exit")
and when pressing the space-key the program exits.
- Step 1
We open a window, using a program parameter for the scaling of the window, then close it again.#include <vgagames3.h> int main(int argc, char **argv) { struct vg3_window *wstruct; int i1, scale, audioparam; /* read command parameters */ scale = VGAG3_WINSCALE_NOSCALE; opterr = opterr ? opterr : 1; while ((i1 = getopt(argc, argv, "+s:h")) != -1) { switch(i1) { case 's': if (strcmp(optarg, "no") == 0) { scale = VGAG3_WINSCALE_NOSCALE; } else if (strcmp(optarg, "best") == 0) { scale = VGAG3_WINSCALE_BESTSCALE; } else if (strcmp(optarg, "max") == 0) { scale = VGAG3_WINSCALE_MAXIMIZE; } else if (strcmp(optarg, "full") == 0) { scale = VGAG3_WINSCALE_FULLSCREEN; } break; case 'h': default: fprintf(stderr, "Usage: %s [<options>]\n", argv[0]); fprintf(stderr, "options:\n"); fprintf(stderr, " -s <scaling>: scale window according to <scaling>:\n"); fprintf(stderr, " - no: no scaling\n"); fprintf(stderr, " - best: best scaling\n"); fprintf(stderr, " - max: maximize window\n"); fprintf(stderr, " - full: fullscreen\n"); exit(1); } } /* set audio parameters, may be 0 for default */ audioparam = (VGAG3_AUDIOPARAM_FREQ_MEDIUM | VGAG3_AUDIOPARAM_CHANNEL_STEREO); /* open window */ wstruct = VG3_window_new(argv[0], VGAG3_VGAVERSION_LOW, scale | audioparam); if (wstruct == NULL) { fprintf(stderr, "%s\n", VG3_error()); exit(1); } /* close window and exit */ VG3_window_free(wstruct); exit(0); }
- Step 2
We create a text, simulate drawing it onto the window to get the width and height of it and then draw it onto the windowint winw, winh; struct vg3_text stxt; struct vg3_rect rect; /* get the size of the window */ VG3_window_getsize(wstruct, &winw, &winh); /* create text with standard font */ VGAG3_TEXT_ATTRIBUTES_SET(&stxt, NULL, '\n', 0, "Press space to exit"); /* simulate drawing text to get real size in width and height */ rect.x = 0; rect.w = winw; rect.y = 0; rect.h = winh; rect = VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 1); /* now we got in rect.w and rect.h the size of the text */ /* correct x/y-position to draw text horizontal and vertical centered */ rect.x = (winw - rect.w) / 2; rect.y = (winh - rect.h) / 2; /* draw text onto the window */ VG3_draw_clear(wstruct, NULL, VGAG3_COLOR_BLACK); VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 0);
- Step 3
Now we create the game-loop.
As we drawn the text just onto the window and don't want to change the window contents anymore, it is not needed to clear and redraw it again, just to update the shown contents./* update window contents and wait for pressing space-key */ VG3_discard_input(wstruct); for (;;) { if (VG3_inputevent_update(wstruct) > 0) { break; } if (VG3_key_ispressed(wstruct, VGAG3_KEY_SPACE, VGAG3_IS_NEW_PRESSED)) { break; } VG3_window_update(wstruct, 0, 0); VG3_wait_time(50); } VG3_discard_input(wstruct);
- Putting together
Finally we got the following file (main.c):#include <vgagames3.h> int main(int argc, char **argv) { struct vg3_window *wstruct; int i1, scale, audioparam; int winw, winh; struct vg3_text stxt; struct vg3_rect rect; /* read command parameters */ scale = VGAG3_WINSCALE_NOSCALE; opterr = opterr ? opterr : 1; while ((i1 = getopt(argc, argv, "+s:h")) != -1) { switch(i1) { case 's': if (strcmp(optarg, "no") == 0) { scale = VGAG3_WINSCALE_NOSCALE; } else if (strcmp(optarg, "best") == 0) { scale = VGAG3_WINSCALE_BESTSCALE; } else if (strcmp(optarg, "max") == 0) { scale = VGAG3_WINSCALE_MAXIMIZE; } else if (strcmp(optarg, "full") == 0) { scale = VGAG3_WINSCALE_FULLSCREEN; } break; case 'h': default: fprintf(stderr, "Usage: %s [<options>]\n", argv[0]); fprintf(stderr, "options:\n"); fprintf(stderr, " -s <scaling>: scale window according to <scaling>:\n"); fprintf(stderr, " - no: no scaling\n"); fprintf(stderr, " - best: best scaling\n"); fprintf(stderr, " - max: maximize window\n"); fprintf(stderr, " - full: fullscreen\n"); exit(1); } } /* set audio parameters, may be 0 for default */ audioparam = (VGAG3_AUDIOPARAM_FREQ_MEDIUM | VGAG3_AUDIOPARAM_CHANNEL_STEREO); /* open window */ wstruct = VG3_window_new(argv[0], VGAG3_VGAVERSION_LOW, scale | audioparam); if (wstruct == NULL) { fprintf(stderr, "%s\n", VG3_error()); exit(1); } /* get the size of the window */ VG3_window_getsize(wstruct, &winw, &winh); /* create text with standard font */ VGAG3_TEXT_ATTRIBUTES_SET(&stxt, NULL, '\n', 0, "Press space to exit"); /* simulate drawing text to get real size in width and height */ rect.x = 0; rect.w = winw; rect.y = 0; rect.h = winh; rect = VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 1); /* now we got in rect.w and rect.h the size of the text */ /* correct x/y-position to draw text horizontal and vertical centered */ rect.x = (winw - rect.w) / 2; rect.y = (winh - rect.h) / 2; /* draw text onto the window */ VG3_draw_clear(wstruct, NULL, VGAG3_COLOR_BLACK); VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 0); /* update window contents and wait for pressing space-key */ VG3_discard_input(wstruct); for (;;) { if (VG3_inputevent_update(wstruct) > 0) { break; } if (VG3_key_ispressed(wstruct, VGAG3_KEY_SPACE, VGAG3_IS_NEW_PRESSED)) { break; } VG3_window_update(wstruct, 0, 0); VG3_wait_time(50); } VG3_discard_input(wstruct); /* close window and exit */ VG3_window_free(wstruct); exit(0); }
- Makefile
For compiling the gamePREFIX = /usr/local CFLAGS = -W -Wall -O2 VGAG_CFLAGS = `vgagames3-config --cflags` VGAG_LIBS = `vgagames3-config --libs` # +++ compile +++ all: game game: main.o $(CC) main.o $(VGAG_LIBS) -o game # +++ install +++ install: mkdir -p $(PREFIX)/bin cp game $(PREFIX)/bin/ && chmod 755 $(PREFIX)/bin/game # +++ uninstall +++ uninstall: rm -f $(PREFIX)/bin/game # +++ clean +++ clean: rm -f game main.o # +++ object files +++ main.o: main.c $(CC) $(CFLAGS) $(VGAG_CFLAGS) -c main.c
- Running
Running the game
<<Prev | Top | Next>> |